1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00

Merge branch 'product-store-invoice' into product-store

This commit is contained in:
Du Peng 2022-09-09 16:37:33 +02:00
commit 9bfeaf3ef3
20 changed files with 138 additions and 29 deletions

View File

@ -98,6 +98,7 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I
rateTraining: '',
rateEvent: '',
rateSubscription: '',
rateProduct: '',
editTemplateUrl: '/admin/invoices/settings/editMultiVAT.html',
historyTemplateUrl: '/admin/invoices/settings/multiVATHistory.html'
},
@ -203,6 +204,14 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I
name: 'accounting_Pack_label',
value: settings.accounting_Pack_label
},
productCode: {
name: 'accounting_Product_code',
value: settings.accounting_Product_code
},
productLabel: {
name: 'accounting_Product_label',
value: settings.accounting_Product_label
},
errorCode: {
name: 'accounting_Error_code',
value: settings.accounting_Error_code
@ -550,7 +559,7 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I
}]
});
return editMultiVATModalInstance.result.then(function (result) {
['Machine', 'Space', 'Training', 'Event', 'Subscription'].forEach(rateType => {
['Machine', 'Space', 'Training', 'Event', 'Subscription', 'Product'].forEach(rateType => {
const value = _.isFinite(result.multiVAT[`rate${rateType}`]) ? result.multiVAT[`rate${rateType}`] + '' : '';
Setting.update({ name: `invoice_VAT-rate_${rateType}` }, { value }, function (data) {
return growl.success(_t('app.admin.invoices.VAT_rate_successfully_saved'));
@ -1054,6 +1063,7 @@ Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'I
$scope.invoice.multiVAT.rateTraining = settings['invoice_VAT-rate_Training'] ? parseFloat(settings['invoice_VAT-rate_Training']) : '';
$scope.invoice.multiVAT.rateEvent = settings['invoice_VAT-rate_Event'] ? parseFloat(settings['invoice_VAT-rate_Event']) : '';
$scope.invoice.multiVAT.rateSubscription = settings['invoice_VAT-rate_Subscription'] ? parseFloat(settings['invoice_VAT-rate_Subscription']) : '';
$scope.invoice.multiVAT.rateProduct = settings['invoice_VAT-rate_Product'] ? parseFloat(settings['invoice_VAT-rate_Product']) : '';
$scope.invoice.number.model = settings['invoice_order-nb'];
$scope.invoice.code.model = settings['invoice_code-value'];
$scope.invoice.code.active = (settings['invoice_code-active'] === 'true');

View File

@ -60,6 +60,7 @@ export const invoicesSettings = [
'invoice_VAT-rate_Space',
'invoice_VAT-rate_Event',
'invoice_VAT-rate_Subscription',
'invoice_VAT-rate_Product',
'invoice_text',
'invoice_legals',
'invoice_prefix',
@ -114,7 +115,9 @@ export const accountingSettings = [
'accounting_Event_code',
'accounting_Event_label',
'accounting_Space_code',
'accounting_Space_label'
'accounting_Space_label',
'accounting_Product_code',
'accounting_Product_label'
];
export const modulesSettings = [

View File

@ -948,13 +948,13 @@ angular.module('application.router', ['ui.router'])
settings: ['Setting', function (Setting) {
return Setting.query({
names: "['invoice_legals', 'invoice_text', 'invoice_VAT-rate', 'invoice_VAT-rate_Machine', 'invoice_VAT-rate_Training', 'invoice_VAT-rate_Space', " +
"'invoice_VAT-rate_Event', 'invoice_VAT-rate_Subscription', 'invoice_VAT-active', 'invoice_order-nb', 'invoice_code-value', " +
"'invoice_VAT-rate_Event', 'invoice_VAT-rate_Subscription', 'invoice_VAT-rate_Product', 'invoice_VAT-active', 'invoice_order-nb', 'invoice_code-value', " +
"'invoice_code-active', 'invoice_reference', 'invoice_logo', 'accounting_journal_code', 'accounting_card_client_code', " +
"'accounting_card_client_label', 'accounting_wallet_client_code', 'accounting_wallet_client_label', 'invoicing_module', " +
"'accounting_other_client_code', 'accounting_other_client_label', 'accounting_wallet_code', 'accounting_wallet_label', " +
"'accounting_VAT_code', 'accounting_VAT_label', 'accounting_subscription_code', 'accounting_subscription_label', " +
"'accounting_Machine_code', 'accounting_Machine_label', 'accounting_Training_code', 'accounting_Training_label', " +
"'accounting_Event_code', 'accounting_Event_label', 'accounting_Space_code', 'accounting_Space_label', " +
"'accounting_Event_code', 'accounting_Event_label', 'accounting_Space_code', 'accounting_Space_label', 'accounting_Product_code', 'accounting_Product_label', " +
"'payment_gateway', 'accounting_Error_code', 'accounting_Error_label', 'payment_schedule_prefix', " +
"'feature_tour_display', 'online_payment_module', 'stripe_public_key', 'stripe_currency', 'invoice_prefix', " +
"'accounting_Pack_code', 'accounting_Pack_label']"

View File

@ -116,6 +116,16 @@
<input type="text" id="packLabel" ng-model="settings.packLabel.value" class="form-control" placeholder="{{ 'app.admin.invoices.general_pack_label' | translate }}"/>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="productCode" translate>{{ 'app.admin.invoices.accounting_Product_code' }}</label>
<input type="text" id="productCode" ng-model="settings.productCode.value" class="form-control" placeholder="{{ 'app.admin.invoices.general_product_code' | translate }}" />
</div>
<div class="col-md-6">
<label for="productLabel" translate>{{ 'app.admin.invoices.accounting_Product_label' }}</label>
<input type="text" id="productLabel" ng-model="settings.productLabel.value" class="form-control" placeholder="{{ 'app.admin.invoices.general_product_label' | translate }}"/>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="spaceCode" translate>{{ 'app.admin.invoices.accounting_Error_code' }}</label>

View File

@ -49,6 +49,14 @@
<button class="btn pull-right" ng-click="showMultiRateHistory('Subscription')"><i class="fa fa-history"></i></button>
</div>
</div>
<div class="form-group">
<label for="vatRateProduct" class="control-label" translate>{{ 'app.admin.invoices.VAT_rate_product' }}</label>
<div class="input-group">
<span class="input-group-addon">% </span>
<input id="vatRateSubscription" type="number" ng-model="multiVAT.rateProduct" class="form-control multi-vat-rate-input" min="0" max="100"/>
<button class="btn pull-right" ng-click="showMultiRateHistory('Product')"><i class="fa fa-history"></i></button>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-warning" ng-click="ok()" translate>{{ 'app.shared.buttons.confirm' }}</button>

View File

@ -83,7 +83,7 @@ class Coupon < ApplicationRecord
end
def users
invoices.map(&:user).concat(orders.map(&:user)).uniq(&:id)
invoices.map(&:user).uniq(&:id)
end
def users_ids

View File

@ -23,7 +23,7 @@ class InvoiceItem < Footprintable
# deduct VAT
vat_service = VatHistoryService.new
vat_rate = vat_service.invoice_item_vat(self)
Rational(amount_after_coupon / (vat_rate / 100.00 + 1)).round.to_f
Rational(amount_after_coupon / ((vat_rate / 100.00) + 1)).round.to_f
end
# return the VAT amount for this item
@ -31,7 +31,7 @@ class InvoiceItem < Footprintable
amount_after_coupon - net_amount
end
# return invoice item type (Machine/Training/Space/Event/Subscription) used to determine the VAT rate
# return invoice item type (Machine/Training/Space/Event/Subscription/OrderItem) used to determine the VAT rate
def invoice_item_type
if object_type == Reservation.name
object.try(:reservable_type) || ''
@ -39,6 +39,8 @@ class InvoiceItem < Footprintable
Subscription.name
elsif object_type == StatisticProfilePrepaidPack.name
object.prepaid_pack.priceable_type
elsif object_type == OrderItem.name
Product.name
else
''
end

View File

@ -5,6 +5,7 @@ class Order < PaymentDocument
belongs_to :statistic_profile
belongs_to :operator_profile, class_name: 'InvoicingProfile'
belongs_to :coupon
belongs_to :invoice
has_many :order_items, dependent: :destroy
has_one :payment_gateway_object, as: :item
@ -21,6 +22,10 @@ class Order < PaymentDocument
delegate :user, to: :statistic_profile
def generate_reference(_date = DateTime.current)
self.reference = PaymentDocumentService.generate_order_number(self)
end
def footprint_children
order_items
end

View File

@ -4,8 +4,8 @@
class PaymentDocument < Footprintable
self.abstract_class = true
def generate_reference
self.reference = PaymentDocumentService.generate_order_number(self)
def generate_reference(date = DateTime.current)
self.reference = PaymentDocumentService.generate_reference(self, date: date)
end
def update_reference

View File

@ -33,6 +33,7 @@ class Setting < ApplicationRecord
invoice_VAT-rate_Space
invoice_VAT-rate_Event
invoice_VAT-rate_Subscription
invoice_VAT-rate_Product
invoice_text
invoice_legals
booking_window_start
@ -74,6 +75,8 @@ class Setting < ApplicationRecord
accounting_Event_label
accounting_Space_code
accounting_Space_label
accounting_Product_code
accounting_Product_label
hub_last_version
hub_public_key
fab_analytics

View File

@ -41,14 +41,13 @@ class PDF::Invoice < Prawn::Document
else
text I18n.t('invoices.invoice_reference', REF: invoice.reference), leading: 3
end
if Setting.get('invoice_code-active')
text I18n.t('invoices.code', CODE: Setting.get('invoice_code-value')), leading: 3
end
text I18n.t('invoices.code', CODE: Setting.get('invoice_code-value')), leading: 3 if Setting.get('invoice_code-active')
if invoice.main_item.object_type != WalletTransaction.name
if invoice.is_a?(Avoir)
text I18n.t('invoices.order_number', NUMBER: invoice.invoice.order_number), leading: 3
else
text I18n.t('invoices.order_number', NUMBER: invoice.order_number), leading: 3
order_number = invoice.main_item.object_type == OrderItem.name ? invoice.main_item.object.order.reference : invoice.order_number
text I18n.t('invoices.order_number', NUMBER: order_number), leading: 3
end
end
if invoice.is_a?(Avoir)
@ -119,6 +118,8 @@ class PDF::Invoice < Prawn::Document
object = I18n.t('invoices.error_invoice')
when 'StatisticProfilePrepaidPack'
object = I18n.t('invoices.prepaid_pack')
when 'OrderItem'
object = I18n.t('invoices.order')
else
Rails.logger.error "specified main_item.object_type type (#{invoice.main_item.object_type}) is unknown"
end
@ -136,7 +137,6 @@ class PDF::Invoice < Prawn::Document
total_vat = 0
# going through invoice_items
invoice.invoice_items.each do |item|
price = item.amount.to_i / 100.00
details = invoice.is_a?(Avoir) ? I18n.t('invoices.cancellation') + ' - ' : ''
@ -162,7 +162,6 @@ class PDF::Invoice < Prawn::Document
END: I18n.l(subscription_end_at.to_date))
end
elsif item.object_type == Reservation.name
case invoice.main_item.object.try(:reservable_type)
### Machine reservation
@ -243,7 +242,8 @@ class PDF::Invoice < Prawn::Document
else
data += [[I18n.t('invoices.total_including_all_taxes'), number_to_currency(total)]]
vat_rate_group.each do |_type, rate|
data += [[I18n.t('invoices.including_VAT_RATE', RATE: rate[:vat_rate], AMOUNT: number_to_currency(rate[:amount] / 100.00)), number_to_currency(rate[:total_vat] / 100.00)]]
data += [[I18n.t('invoices.including_VAT_RATE', RATE: rate[:vat_rate], AMOUNT: number_to_currency(rate[:amount] / 100.00)),
number_to_currency(rate[:total_vat] / 100.00)]]
end
data += [[I18n.t('invoices.including_total_excluding_taxes'), number_to_currency(total_ht / 100.00)]]
data += [[I18n.t('invoices.including_amount_payed_on_ordering'), number_to_currency(total)]]
@ -290,7 +290,7 @@ class PDF::Invoice < Prawn::Document
# payment details
move_down 20
if invoice.is_a?(Avoir)
payment_verbose = I18n.t('invoices.refund_on_DATE', DATE:I18n.l(invoice.avoir_date.to_date)) + ' '
payment_verbose = I18n.t('invoices.refund_on_DATE', DATE: I18n.l(invoice.avoir_date.to_date)) + ' '
case invoice.payment_method
when 'stripe'
payment_verbose += I18n.t('invoices.by_card_online_payment')
@ -351,7 +351,6 @@ class PDF::Invoice < Prawn::Document
text line, style: :bold, inline_format: true
end
# address and legals information
move_down 40
txt = parse_html(Setting.get('invoice_legals'))

View File

@ -72,6 +72,8 @@ class AccountingExportService
rows << "#{wallet_row(invoice)}\n"
when 'StatisticProfilePrepaidPack'
rows << "#{pack_row(invoice)}\n"
when 'OrderItem'
rows << "#{product_row(invoice)}\n"
when 'Error'
items = invoice.invoice_items.reject { |ii| ii.object_type == 'Subscription' }
items.each do |item|
@ -148,6 +150,16 @@ class AccountingExportService
)
end
def product_row(invoice)
row(
invoice,
account(invoice, :product),
account(invoice, :product, type: :label),
invoice.invoice_items.first.net_amount / 100.00,
line_label: label(invoice)
)
end
# Generate the "VAT" row, which contains the credit to the VAT account, with VAT amount only
def vat_row(invoice)
total = invoice.invoice_items.map(&:net_amount).sum
@ -235,6 +247,12 @@ class AccountingExportService
else
Rails.logger.debug { "WARN: Invoice #{invoice.id} has no prepaid-pack" }
end
when :product
if invoice.main_item.object_type == 'OrderItem'
Setting.get("accounting_Product_#{type}")
else
Rails.logger.debug { "WARN: Invoice #{invoice.id} has no prepaid-pack" }
end
when :error
Setting.get("accounting_Error_#{type}")
else
@ -275,7 +293,9 @@ class AccountingExportService
reference = invoice.reference
items = invoice.subscription_invoice? ? [I18n.t('accounting_export.subscription')] : []
items.push I18n.t("accounting_export.#{invoice.main_item.object.reservable_type}_reservation") if invoice.main_item.object_type == 'Reservation'
if invoice.main_item.object_type == 'Reservation'
items.push I18n.t("accounting_export.#{invoice.main_item.object.reservable_type}_reservation")
end
items.push I18n.t('accounting_export.wallet') if invoice.main_item.object_type == 'WalletTransaction'
summary = items.join(' + ')

View File

@ -15,7 +15,6 @@ class InvoicesService
.page(page)
.per(size)
if filters[:number].size.positive?
invoices = invoices.where(
'invoices.reference LIKE :search',
@ -96,7 +95,7 @@ class InvoicesService
# Generate an array of {InvoiceItem} with the provided elements, price included.
# @param invoice {Invoice} the parent invoice
# @param payment_details {Hash} as generated by ShoppingCart.total
# @param objects {Array<Reservation|Subscription|StatisticProfilePrepaidPack>}
# @param objects {Array<Reservation|Subscription|StatisticProfilePrepaidPack|OrderItem>}
##
def self.generate_invoice_items(invoice, payment_details, objects)
objects.each_with_index do |object, index|
@ -108,6 +107,8 @@ class InvoicesService
InvoicesService.generate_reservation_item(invoice, object, payment_details, index.zero?)
elsif object.is_a?(StatisticProfilePrepaidPack)
InvoicesService.generate_prepaid_pack_item(invoice, object, payment_details, index.zero?)
elsif object.is_a?(OrderItem)
InvoicesService.generate_order_item(invoice, object, payment_details, index.zero?)
else
InvoicesService.generate_generic_item(invoice, object, payment_details, index.zero?)
end
@ -123,16 +124,16 @@ class InvoicesService
reservation.slots_reservations.map(&:slot).each do |slot|
description = "#{reservation.reservable.name}\n"
description += if slot.start_at.to_date != slot.end_at.to_date
description += if slot.start_at.to_date == slot.end_at.to_date
"#{I18n.l slot.start_at.to_date, format: :long} #{I18n.l slot.start_at, format: :hour_minute}" \
" - #{I18n.l slot.end_at, format: :hour_minute}"
else
I18n.t('events.from_STARTDATE_to_ENDDATE',
STARTDATE: I18n.l(slot.start_at.to_date, format: :long),
ENDDATE: I18n.l(slot.end_at.to_date, format: :long)) + ' ' +
I18n.t('events.from_STARTTIME_to_ENDTIME',
STARTTIME: I18n.l(slot.start_at, format: :hour_minute),
ENDTIME: I18n.l(slot.end_at, format: :hour_minute))
else
"#{I18n.l slot.start_at.to_date, format: :long} #{I18n.l slot.start_at, format: :hour_minute}" \
" - #{I18n.l slot.end_at, format: :hour_minute}"
end
price_slot = payment_details[:elements][:slots].detect { |p_slot| p_slot[:start_at].to_time.in_time_zone == slot[:start_at] }
@ -196,6 +197,21 @@ class InvoicesService
)
end
##
# Generate an InvoiceItem for given OrderItem and sva it in invoice.invoice_items
# This method must be called whith an order
##
def self.generate_order_item(invoice, item, _payment_details, main = false)
raise TypeError unless item
invoice.invoice_items.push InvoiceItem.new(
amount: item.is_offered ? 0 : item.amount * item.quantity,
description: "#{item.orderable.name} x #{item.quantity}",
object: item,
main: main
)
end
def self.generate_generic_item(invoice, item, payment_details, main = false)
invoice.invoice_items.push InvoiceItem.new(
amount: payment_details[:elements][item.class.name.to_sym],
@ -205,7 +221,6 @@ class InvoicesService
)
end
##
# Set the total price to the reservation's invoice, summing its whole items.
# Additionally a coupon may be applied to this invoice to make a discount on the total price

View File

@ -35,7 +35,21 @@ module Payments::PaymentConcern
order.order_items.each do |item|
ProductService.update_stock(item.orderable, 'external', 'sold', -item.quantity, item.id)
end
order.save
if order.save
invoice = InvoicesService.create(
{ total: order.total, coupon: coupon },
order.operator_profile_id,
order.order_items,
order.statistic_profile.user,
payment_id: payment_id,
payment_type: payment_type,
payment_method: order.payment_method
)
invoice.wallet_amount = order.wallet_amount
invoice.wallet_transaction_id = order.wallet_transaction_id
invoice.save
order.update_attribute(:invoice_id, invoice.id)
end
order.reload
end
end

View File

@ -599,6 +599,7 @@ en:
VAT_rate_training: "Training reservation"
VAT_rate_event: "Event reservation"
VAT_rate_subscription: "Subscription"
VAT_rate_product: "Products (store)"
changed_at: "Changed at"
changed_by: "By"
deleted_user: "Deleted user"
@ -714,6 +715,10 @@ en:
general_pack_code: "Accounting code for prepaid-packs"
accounting_Pack_label: "Prepaid-pack label"
general_pack_label: "Account label for prepaid-packs"
accounting_Product_code: "Product code (Store)"
general_product_code: "Accounting code for products (Store)"
accounting_Product_label: "Product label (Store)"
general_product_label: "Account label for products (Store)"
accounting_Error_code: "Errors code"
general_error_code: "Accounting code for erroneous invoices"
accounting_Error_label: "Errors label"
@ -2054,4 +2059,4 @@ en:
title: 'Settings'
withdrawal_instructions: 'Product withdrawal instructions'
withdrawal_info: "This text is displayed on the checkout page to inform the client about the products withdrawal method"
save: "Save"
save: "Save"

View File

@ -599,6 +599,7 @@ fr:
VAT_rate_training: "Réservation de formations"
VAT_rate_event: "Réservation d'événements"
VAT_rate_subscription: "Abonnements"
VAT_rate_product: "Achat de produits (boutique)"
changed_at: "Changé le"
changed_by: "Par"
deleted_user: "Utilisateur supprimé"
@ -714,6 +715,10 @@ fr:
general_pack_code: "Code comptable pour tous les packs prépayés"
accounting_Pack_label: "Libellé pack prépayé"
general_pack_label: "Libellé du compte pour tous les packs prépayés"
accounting_Product_code: "Code produit (Boutique)"
general_product_code: "Code comptable pour tous les produits (Boutique)"
accounting_Product_label: "Libellé produit (Boutique)"
general_product_label: "Libellé du compte pour tous les produits (Boutique)"
accounting_Error_code: "Code erreurs"
general_error_code: "Code comptable pour les factures en erreur"
accounting_Error_label: "Libellé erreurs"

View File

@ -121,6 +121,7 @@ en:
error_invoice: "Erroneous invoice. The items below ware not booked. Please contact the FabLab for a refund."
prepaid_pack: "Prepaid pack of hours"
pack_item: "Pack of %{COUNT} hours for the %{ITEM}"
order: "Order of products"
#PDF payment schedule generation
payment_schedules:
schedule_reference: "Payment schedule reference: %{REF}"

View File

@ -121,6 +121,7 @@ fr:
error_invoice: "Facture en erreur. Les éléments ci-dessous n'ont pas été réservés. Veuillez contacter le Fablab pour un remboursement."
prepaid_pack: "Paquet d'heures prépayé"
pack_item: "Pack de %{COUNT} heures pour la %{ITEM}"
order: "La commande des produits"
#PDF payment schedule generation
payment_schedules:
schedule_reference: "Référence de l'échéancier : %{REF}"

View File

@ -0,0 +1,5 @@
class AddInvoiceIdToOrder < ActiveRecord::Migration[5.2]
def change
add_reference :orders, :invoice, index: true, foreign_key: true
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_08_26_175129) do
ActiveRecord::Schema.define(version: 2022_09_09_131300) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
@ -475,7 +475,9 @@ ActiveRecord::Schema.define(version: 2022_08_26_175129) do
t.string "environment"
t.bigint "coupon_id"
t.integer "paid_total"
t.bigint "invoice_id"
t.index ["coupon_id"], name: "index_orders_on_coupon_id"
t.index ["invoice_id"], name: "index_orders_on_invoice_id"
t.index ["operator_profile_id"], name: "index_orders_on_operator_profile_id"
t.index ["statistic_profile_id"], name: "index_orders_on_statistic_profile_id"
end
@ -1171,6 +1173,7 @@ ActiveRecord::Schema.define(version: 2022_08_26_175129) do
add_foreign_key "invoicing_profiles", "users"
add_foreign_key "order_items", "orders"
add_foreign_key "orders", "coupons"
add_foreign_key "orders", "invoices"
add_foreign_key "orders", "invoicing_profiles", column: "operator_profile_id"
add_foreign_key "orders", "statistic_profiles"
add_foreign_key "organizations", "invoicing_profiles"