mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-29 18:52:22 +01:00
show coupon details in invoice
This commit is contained in:
parent
6a87467f7c
commit
6e1dfda8f5
@ -152,8 +152,7 @@ class Reservation < ActiveRecord::Base
|
||||
self.stp_invoice_id = invoice_items.first.refresh.invoice
|
||||
self.invoice.stp_invoice_id = invoice_items.first.refresh.invoice
|
||||
self.invoice.invoice_items.push InvoiceItem.new(amount: subscription.plan.amount, stp_invoice_item_id: subscription.stp_subscription_id, description: subscription.plan.name, subscription_id: subscription.id)
|
||||
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
||||
self.invoice.total = total
|
||||
set_total_and_coupon(coupon_code)
|
||||
save!
|
||||
#
|
||||
# IMPORTANT NOTE: here, we don't have to create a stripe::invoice and pay it
|
||||
@ -188,7 +187,7 @@ class Reservation < ActiveRecord::Base
|
||||
card.delete if card
|
||||
self.stp_invoice_id = stp_invoice.id
|
||||
self.invoice.stp_invoice_id = stp_invoice.id
|
||||
self.invoice.total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
||||
set_total_and_coupon(coupon_code)
|
||||
save!
|
||||
rescue Stripe::CardError => card_error
|
||||
clear_payment_info(card, stp_invoice, invoice_items)
|
||||
@ -285,16 +284,14 @@ class Reservation < ActiveRecord::Base
|
||||
self.subscription.attributes = {plan_id: plan_id, user_id: user.id, expired_at: nil}
|
||||
if subscription.save_with_local_payment(false)
|
||||
self.invoice.invoice_items.push InvoiceItem.new(amount: subscription.plan.amount, description: subscription.plan.name, subscription_id: subscription.id)
|
||||
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
||||
self.invoice.total = total
|
||||
set_total_and_coupon(coupon_code)
|
||||
save!
|
||||
else
|
||||
errors[:card] << subscription.errors[:card].join
|
||||
return false
|
||||
end
|
||||
else
|
||||
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
||||
self.invoice.total = total
|
||||
set_total_and_coupon(coupon_code)
|
||||
save!
|
||||
end
|
||||
|
||||
@ -303,6 +300,7 @@ class Reservation < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def machine_not_already_reserved
|
||||
already_reserved = false
|
||||
self.slots.each do |slot|
|
||||
@ -372,4 +370,21 @@ class Reservation < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
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
|
||||
# @param [coupon_code] {String} optional coupon code to apply to the invoice
|
||||
##
|
||||
def set_total_and_coupon(coupon_code = nil)
|
||||
total = invoice.invoice_items.map(&:amount).map(&:to_i).reduce(:+)
|
||||
|
||||
unless coupon_code.nil?
|
||||
cp = Coupon.find_by_code(coupon_code)
|
||||
total = total - (total * cp.percent_off / 100)
|
||||
self.invoice.coupon_id = cp.id
|
||||
end
|
||||
|
||||
self.invoice.total = total
|
||||
end
|
||||
end
|
||||
|
@ -83,6 +83,8 @@ module PDF
|
||||
object = subscription_verbose(invoice.invoiced, invoice.user)
|
||||
when 'OfferDay'
|
||||
object = offer_day_verbose(invoice.invoiced, invoice.user)
|
||||
else
|
||||
puts "ERROR : specified invoiced type (#{invoice.invoiced_type}) is unknown"
|
||||
end
|
||||
end
|
||||
text I18n.t('invoices.object')+' '+object
|
||||
@ -136,6 +138,16 @@ module PDF
|
||||
total_calc += price
|
||||
end
|
||||
|
||||
# subtract the coupon, if any
|
||||
unless invoice.coupon_id.nil?
|
||||
cp = invoice.coupon
|
||||
discount = total_calc * cp.percent_off / 100
|
||||
total_calc = total_calc - discount
|
||||
|
||||
# add a row for the coupon
|
||||
data += [ [I18n.t('invoices.coupon_CODE_discount_of_PERCENT', CODE: cp.code, PERCENT: cp.percent_off), number_to_currency(-discount)] ]
|
||||
end
|
||||
|
||||
# total verification
|
||||
total = invoice.total / 100.0
|
||||
if total_calc != total
|
||||
@ -215,16 +227,20 @@ module PDF
|
||||
payment_verbose += ' '+I18n.t('invoices.for_an_amount_of_AMOUNT', AMOUNT: number_to_currency(total))
|
||||
|
||||
else
|
||||
# subtract the wallet amount for this invoice from the total
|
||||
if invoice.wallet_amount
|
||||
wallet_amount = invoice.wallet_amount / 100.0
|
||||
total = total - wallet_amount
|
||||
end
|
||||
|
||||
# payment method
|
||||
if invoice.stp_invoice_id
|
||||
payment_verbose = I18n.t('invoices.settlement_by_debit_card')
|
||||
else
|
||||
payment_verbose = I18n.t('invoices.settlement_done_at_the_reception')
|
||||
end
|
||||
|
||||
# if the invoice was 100% payed with the wallet ...
|
||||
if total == 0 and wallet_amount
|
||||
payment_verbose = I18n.t('invoices.settlement_by_wallet')
|
||||
end
|
||||
|
@ -86,6 +86,7 @@ en:
|
||||
one: "One reduced rate ticket"
|
||||
other: "%{count} reduced rate tickets"
|
||||
reservation_other: "Reservation (other)"
|
||||
coupon_CODE_discount_of_PERCENT: "Coupon %{CODE}: discount of %{PERCENT}%"
|
||||
total_including_all_taxes: "Total incl. all taxes"
|
||||
including_VAT_RATE: "Including VAT %{RATE}%"
|
||||
including_total_excluding_taxes: "Including Total excl. taxes"
|
||||
|
@ -86,6 +86,7 @@ fr:
|
||||
one: "Une place à tarif réduit"
|
||||
other: "%{count} places à tarif réduit"
|
||||
reservation_other: "Réservation (autre)"
|
||||
coupon_CODE_discount_of_PERCENT: "Code %{CODE} : remise de %{PERCENT} %"
|
||||
total_including_all_taxes: "Total TTC"
|
||||
including_VAT_RATE: "Dont TVA %{RATE}%"
|
||||
including_total_excluding_taxes: "Dont total HT"
|
||||
|
Loading…
x
Reference in New Issue
Block a user