merge automatic group order invoice generation
see https://github.com/foodcoops/foodsoft/pull/907 for reference and original work by viehlieb Co-authored-by: viehlieb <pf@pragma-shift.net> fix PDF Pdf make explicit deposit in invoices work add ordergroupname to invoice file name mark bold sum for vat exempt foodcoops download multiple group order invoice as zip
This commit is contained in:
parent
6abf998b56
commit
93143c28f2
37 changed files with 988 additions and 69 deletions
|
|
@ -7,11 +7,27 @@ module PriceCalculation
|
|||
add_percent(price + deposit, tax)
|
||||
end
|
||||
|
||||
def gross_price_without_deposit
|
||||
add_percent(price, tax)
|
||||
end
|
||||
|
||||
def gross_deposit_price
|
||||
add_percent(deposit, tax)
|
||||
end
|
||||
|
||||
# @return [Number] Price for the foodcoop-member.
|
||||
def fc_price
|
||||
add_percent(gross_price, FoodsoftConfig[:price_markup].to_i)
|
||||
end
|
||||
|
||||
def fc_price_without_deposit
|
||||
add_percent(gross_price_without_deposit, FoodsoftConfig[:price_markup].to_i)
|
||||
end
|
||||
|
||||
def fc_deposit_price
|
||||
add_percent(gross_deposit_price, FoodsoftConfig[:price_markup].to_i)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def add_percent(value, percent)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ class GroupOrder < ApplicationRecord
|
|||
has_many :group_order_articles, dependent: :destroy
|
||||
has_many :order_articles, through: :group_order_articles
|
||||
has_one :financial_transaction
|
||||
has_one :group_order_invoice
|
||||
belongs_to :updated_by, optional: true, class_name: 'User', foreign_key: 'updated_by_user_id'
|
||||
|
||||
validates :order_id, presence: true
|
||||
|
|
|
|||
|
|
@ -208,6 +208,18 @@ class GroupOrderArticle < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def total_price_without_deposit(order_article = self.order_article)
|
||||
if order_article.order.open?
|
||||
if FoodsoftConfig[:tolerance_is_costly]
|
||||
order_article.price.fc_price_without_deposit * (quantity + tolerance)
|
||||
else
|
||||
order_article.price.fc_price_without_deposit * quantity
|
||||
end
|
||||
else
|
||||
order_article.price.fc_price_without_deposit * result
|
||||
end
|
||||
end
|
||||
|
||||
# Check if the result deviates from the result_computed
|
||||
def result_manually_changed?
|
||||
result != result_computed unless result.nil?
|
||||
|
|
|
|||
58
app/models/group_order_invoice.rb
Normal file
58
app/models/group_order_invoice.rb
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
class GroupOrderInvoice < ApplicationRecord
|
||||
belongs_to :group_order
|
||||
validates_presence_of :group_order
|
||||
validates_uniqueness_of :invoice_number
|
||||
validate :tax_number_set
|
||||
after_initialize :init, unless: :persisted?
|
||||
|
||||
def generate_invoice_number(count)
|
||||
trailing_number = count.to_s.rjust(4, '0')
|
||||
if GroupOrderInvoice.find_by(invoice_number: self.invoice_date.strftime("%Y%m%d") + trailing_number)
|
||||
generate_invoice_number(count.to_i + 1)
|
||||
else
|
||||
self.invoice_date.strftime("%Y%m%d") + trailing_number
|
||||
end
|
||||
end
|
||||
|
||||
def tax_number_set
|
||||
if FoodsoftConfig[:contact][:tax_number].blank?
|
||||
errors.add(:group_order_invoice, "Keine Steuernummer in FoodsoftConfig :contact gesetzt")
|
||||
end
|
||||
end
|
||||
|
||||
def init
|
||||
self.invoice_date = Time.now unless invoice_date
|
||||
self.invoice_number = generate_invoice_number(1) unless self.invoice_number
|
||||
self.payment_method = FoodsoftConfig[:group_order_invoices]&.[](:payment_method) || I18n.t('activerecord.attributes.group_order_invoice.payment_method') unless self.payment_method
|
||||
end
|
||||
|
||||
def name
|
||||
I18n.t('activerecord.attributes.group_order_invoice.name') + "_#{invoice_number}"
|
||||
end
|
||||
|
||||
def load_data_for_invoice
|
||||
invoice_data = {}
|
||||
order = group_order.order
|
||||
invoice_data[:supplier] = order.supplier.name
|
||||
invoice_data[:ordergroup] = group_order.ordergroup
|
||||
invoice_data[:group_order] = group_order
|
||||
invoice_data[:invoice_number] = invoice_number
|
||||
invoice_data[:invoice_date] = invoice_date
|
||||
invoice_data[:tax_number] = FoodsoftConfig[:contact][:tax_number]
|
||||
invoice_data[:payment_method] = payment_method
|
||||
invoice_data[:order_articles] = {}
|
||||
group_order.order_articles.each do |order_article|
|
||||
# Get the result of last time ordering, if possible
|
||||
goa = group_order.group_order_articles.detect { |tmp_goa| tmp_goa.order_article_id == order_article.id }
|
||||
|
||||
# Build hash with relevant data
|
||||
invoice_data[:order_articles][order_article.id] = {
|
||||
:price => order_article.article.fc_price,
|
||||
:quantity => (goa ? goa.quantity : 0),
|
||||
:total_price => (goa ? goa.total_price : 0),
|
||||
:tax => order_article.article.tax
|
||||
}
|
||||
end
|
||||
invoice_data
|
||||
end
|
||||
end
|
||||
|
|
@ -207,7 +207,7 @@ class Order < ApplicationRecord
|
|||
# :fc, guess what...
|
||||
def sum(type = :gross)
|
||||
total = 0
|
||||
if %i[net gross fc].include?(type)
|
||||
if %i[net gross gross_deposit fc_deposit deposit fc].include?(type)
|
||||
for oa in order_articles.ordered.includes(:article, :article_price)
|
||||
quantity = oa.units * oa.price.unit_quantity
|
||||
case type
|
||||
|
|
@ -217,6 +217,12 @@ class Order < ApplicationRecord
|
|||
total += quantity * oa.price.gross_price
|
||||
when :fc
|
||||
total += quantity * oa.price.fc_price
|
||||
when :gross_deposit
|
||||
total += quantity * oa.price.gross_deposit_price
|
||||
when :fc_deposit
|
||||
total += quantity * oa.price.fc_deposit_price
|
||||
when :deposit
|
||||
total += quantity * oa.price.deposit
|
||||
end
|
||||
end
|
||||
elsif %i[groups groups_without_markup].include?(type)
|
||||
|
|
@ -224,7 +230,11 @@ class Order < ApplicationRecord
|
|||
for goa in go.group_order_articles
|
||||
case type
|
||||
when :groups
|
||||
total += goa.result * goa.order_article.price.fc_price
|
||||
total += if FoodsoftConfig[:group_order_invoices]&.[](:separate_deposits)
|
||||
goa.result * (goa.order_article.price.fc_price + goa.order_article.price.fc_deposit_price)
|
||||
else
|
||||
goa.result * goa.order_article.price.fc_price
|
||||
end
|
||||
when :groups_without_markup
|
||||
total += goa.result * goa.order_article.price.gross_price
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue