make explicit deposit in invoices work

This commit is contained in:
viehlieb 2023-08-11 15:01:22 +02:00
parent 85bdf28f91
commit 6f7c057a47
14 changed files with 242 additions and 84 deletions

View file

@ -21,13 +21,11 @@ class GroupOrderInvoicePdf < RenderPdf
move_down 5
text "#{contact[:zip_code]} #{contact[:city]}", size: fontsize(9), align: :left
move_down 5
unless contact[:phone].blank?
if contact[:phone].present?
text "#{Supplier.human_attribute_name :phone}: #{contact[:phone]}", size: fontsize(9), align: :left
move_down 5
end
unless contact[:email].blank?
text "#{Supplier.human_attribute_name :email}: #{contact[:email]}", size: fontsize(9), align: :left
end
text "#{Supplier.human_attribute_name :email}: #{contact[:email]}", size: fontsize(9), align: :left if contact[:email].present?
move_down 5
text I18n.t('documents.group_order_invoice_pdf.tax_number', :number => @options[:tax_number]), size: fontsize(9), align: :left
end
@ -38,14 +36,18 @@ class GroupOrderInvoicePdf < RenderPdf
move_down 7
text I18n.t('documents.group_order_invoice_pdf.ordergroup.name', ordergroup: ordergroup.name.to_s), size: fontsize(9)
move_down 5
if ordergroup.contact_address
if ordergroup.contact_address.present?
text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_address', contact_address: ordergroup.contact_address.to_s), size: fontsize(9)
move_down 5
end
if ordergroup.contact_phone
if ordergroup.contact_phone.present?
text I18n.t('documents.group_order_invoice_pdf.ordergroup.contact_phone', contact_phone: ordergroup.contact_phone.to_s), size: fontsize(9)
move_down 5
end
if ordergroup.customer_number.present?
text I18n.t('documents.group_order_invoice_pdf.ordergroup.customer_number', customer_number: ordergroup.customer_number.to_s), size: fontsize(9)
move_down 5
end
end
# invoice Date and nnvoice number
@ -66,6 +68,7 @@ class GroupOrderInvoicePdf < RenderPdf
#------------- Table Data -----------------------
@group_order = GroupOrder.find(@options[:group_order].id)
if FoodsoftConfig[:group_order_invoices][:vat_exempt]
body_for_vat_exempt
else
@ -78,15 +81,25 @@ class GroupOrderInvoicePdf < RenderPdf
data = [I18n.t('documents.group_order_invoice_pdf.vat_exempt_rows')]
move_down 10
group_order_articles = GroupOrderArticle.where(group_order_id: @group_order.id)
separate_deposits = FoodsoftConfig[:group_order_invoices]&.[](:separate_deposits)
group_order_articles.each do |goa|
# if no unit is received, nothing is to be charged
next if goa.result.to_i == 0
goa_total_gross = goa.result * goa.order_article.price.gross_price
goa_total_price = separate_deposits ? goa.total_price_without_deposit : goa.total_price
data << [goa.order_article.article.name,
goa.result.to_i,
number_to_currency(goa.order_article.price.gross_price),
number_to_currency(goa.total_price)]
total_gross += goa_total_gross
number_to_currency(goa.order_article.price.fc_price_without_deposit),
number_to_currency(goa_total_price)]
total_gross += goa_total_price
next unless separate_deposits && goa.order_article.price.deposit > 0.0
goa_total_deposit = goa.result * goa.order_article.price.fc_deposit_price
data << ["zzgl. Pfand",
goa.result.to_i,
number_to_currency(goa.order_article.article.fc_deposit_price),
number_to_currency(goa_total_deposit)]
total_gross += goa_total_deposit
end
table data, position: :left, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table|
@ -122,6 +135,7 @@ class GroupOrderInvoicePdf < RenderPdf
end
def body_with_vat
separate_deposits = FoodsoftConfig[:group_order_invoices]&.[](:separate_deposits)
total_gross = 0
total_net = 0
# Articles
@ -129,6 +143,14 @@ class GroupOrderInvoicePdf < RenderPdf
tax_hash_net = Hash.new(0) # for summing up article net prices grouped into vat percentage
tax_hash_gross = Hash.new(0) # same here with gross prices
if separate_deposits
total_deposit = 0
total_deposit_gross = 0
tax_hash_deposit_gross = Hash.new(0) # for summing up deposit gross prices grouped into vat percentage
tax_hash_deposit_net = Hash.new(0) # same here with gross prices
end
marge = FoodsoftConfig[:price_markup]
# data table looks different when price_markup > 0
@ -145,15 +167,37 @@ class GroupOrderInvoicePdf < RenderPdf
order_article = goa.order_article
goa_total_net = goa.result * order_article.price.price
goa_total_gross = goa.result * order_article.price.gross_price
goa_total_gross = separate_deposits ? goa.total_price_without_deposit : goa.total_price
data << [order_article.article.name,
goa.result.to_i,
number_to_currency(order_article.price.price),
number_to_currency(goa_total_net),
tax.to_s + '%',
number_to_currency(goa.total_price)]
number_to_currency(goa_total_gross)]
if separate_deposits && order_article.price.deposit > 0.0
goa_deposit = goa.result * order_article.price.deposit
goa_total_deposit = goa.result * order_article.price.fc_deposit_price
data << ["zzgl. Pfand",
goa.result.to_i,
number_to_currency(order_article.price.deposit),
number_to_currency(goa_deposit),
tax.to_s + '%',
number_to_currency(goa_total_deposit)]
total_deposit += goa_deposit
total_deposit_gross += goa_total_deposit
tax_hash_deposit_net[tax.to_i] += goa_deposit
tax_hash_deposit_gross[tax.to_i] += goa_total_deposit
end
tax_hash_net[tax.to_i] += goa_total_net
tax_hash_gross[tax.to_i] += goa_total_gross
total_net += goa_total_net
total_gross += goa_total_gross
end
@ -166,34 +210,66 @@ class GroupOrderInvoicePdf < RenderPdf
table.position = :center
table.cells.border_width = 1
table.cells.border_color = '666666'
table.row(0).column(1).width = 40
table.row(0).columns(0..6).style(background_color: 'cccccc', font_style: :bold)
table.rows(0..-1).columns(0..6).width = 80
table.row(0).border_bottom_width = 2
table.columns(1).align = :right
table.columns(1..6).align = :right
end
sum = []
sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_net'), number_to_currency(total_net)]
tax_hash_net.each_key.each do |tax|
sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.tax_included', tax: tax), number_to_currency(tax_hash_gross[tax] - tax_hash_net[tax])]
# 4 spalten Netto, MwSt, Brutto
# Produkte mit 7% MwSt
# Pfand mit 7% MwSt
# Produkte mit 19% MwSt
# Pfand mit 19% MwSt
#
# x,x,GESAMT; SUMME
sum = [[nil, nil, nil, "Netto", "MwSt", "Brutto"]]
[7, 19].each do |key|
sum << [nil, nil, "Produkte mit #{key}%", number_to_currency(tax_hash_net[key]), number_to_currency(tax_hash_gross[key] - tax_hash_net[key]), number_to_currency(tax_hash_gross[key])]
sum << [nil, nil, "Pfand mit #{key}%", number_to_currency(tax_hash_deposit_net[key]), number_to_currency(tax_hash_deposit_gross[key] - tax_hash_deposit_net[key]), number_to_currency(tax_hash_deposit_gross[key])] if separate_deposits
end
unless marge == 0
sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.markup_included', marge: marge), number_to_currency(total_gross * marge / 100.0)]
end
end_sum = total_gross * (1 + marge / 100.0)
sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_gross'), number_to_currency(end_sum)]
total_deposit_gross ||= 0
sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_gross'), number_to_currency(total_gross + total_deposit_gross)]
# tax_hash_net.each_key.each do |tax|
# sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.tax_included', tax: tax), number_to_currency(tax_hash_gross[tax] - tax_hash_net[tax])]
# end
# unless marge == 0
# sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.markup_included', marge: marge), number_to_currency(total_gross * marge / 100.0)]
# end
# end_sum = total_gross * (1 + marge / 100.0)
# sum << [nil, nil, nil, nil, I18n.t('documents.group_order_invoice_pdf.sum_to_pay_gross'), number_to_currency(end_sum)]
# table for sum
table sum, position: :right, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table|
sum.length.times do |count|
table.row(count).columns(0..5).borders = []
end
table.row(sum.length - 1).columns(0..4).borders = []
table.row(sum.length - 1).border_bottom_width = 2
table.row(sum.length - 1).columns(5).borders = [:bottom]
# table sum, position: :right, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table|
# sum.length.times do |count|
# table.row(count).columns(0..5).borders = []
# end
# table.row(sum.length - 1).columns(0..4).borders = []
# table.row(sum.length - 1).border_bottom_width = 2
# table.row(sum.length - 1).columns(5).borders = [:bottom]
# end
move_down 10
table sum, cell_style: { size: fontsize(8), overflow: :shrink_to_fit } do |table|
table.header = true
table.position = :center
table.cells.border_width = 1
table.cells.border_color = '666666'
table.row(0).columns(2..6).style(align: :bottom)
table.row(0).border_bottom_width = 2
table.row(0..-1).columns(0..1).border_width = 0
table.rows(0..-1).columns(0..6).width = 80
table.row(-1).column(-1).style(font_style: :bold)
table.row(-1).column(-2).style(font_style: :bold)
table.row(-1).column(-1).size = fontsize(10)
table.row(-1).column(-2).size = fontsize(10)
table.columns(1).align = :right
table.columns(1..6).align = :right
end
if(FoodsoftConfig[:group_order_invoices][:vat_exempt])
if FoodsoftConfig[:group_order_invoices][:vat_exempt]
move_down 15
text I18n.t('documents.group_order_invoice_pdf.small_business_regulation')
end

View file

@ -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)

View file

@ -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?

View file

@ -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

View file

@ -16,6 +16,7 @@
%h4= t '.group_order_invoices'
= form.fields_for :group_order_invoices do |field|
= config_input field, :use_automatic_invoices, as: :boolean
= config_input field, :separate_deposits, as: :boolean
= config_input field, :vat_exempt, as: :boolean
= config_input field, :payment_method, as: :string, input_html: {class: 'input-medium'}

View file

@ -2,6 +2,7 @@
%p= t('.first_paragraph', url: link_to(t('.here'), new_invite_path(id: @ordergroup.id), remote: true)).html_safe
= simple_form_for [:admin, @ordergroup] do |f|
- captured = capture do
= f.input :customer_number
= f.input :contact_person
= f.input :contact_phone
= f.input :contact_address

View file

@ -12,6 +12,16 @@
%tr
%td= t('.fc_amount')
%td.numeric= number_to_currency(order.sum(:fc))
- if FoodsoftConfig[:group_order_invoices]&.[](:separate_deposits)
%tr
%td= t('.deposit')
%td.numeric= number_to_currency(order.sum(:deposit))
%tr
%td= t('.gross_deposit')
%td.numeric= number_to_currency(order.sum(:gross_deposit))
%tr
%td= t('.fc_deposit')
%td.numeric= number_to_currency(order.sum(:fc_deposit))
%tr
%td= t('.groups_amount')
%td.numeric= number_to_currency(order.sum(:groups))

View file

@ -57,6 +57,10 @@
= f.label :contact_person
%br/
= f.text_field :contact_person
%p
= f.label :customer_number
%br/
= f.text_field :customer_number
%p
= f.label :contact_phone
%br/

View file

@ -6,6 +6,8 @@
%dd=h group.contact
%dt= heading_helper(Ordergroup, :contact_address) + ':'
%dd= link_to_gmaps group.contact_address
%dt= heading_helper(Ordergroup, :customer_number) + ':'
%dd=h group.customer_number
- if group.break_start? or group.break_end?
%dt= heading_helper(Ordergroup, :break) + ':'
%dd= raw t '.break', start: format_date(group.break_start), end: format_date(group.break_end)