improve receipt/details reports and implement number formatting

1) eager load reports for receipt/details
2) introduce number_format lookups to replace print_settings for number formatting
3) implement NumberFormattable concern, reference number_format lookups or print_settings if not exist, to get number format settings and number formatting
4) replace rails NumberHelper.number_with_precision with NumberFormattable.number_format hopefully to reduce overhead, formatting numbers for huge lists of data
This commit is contained in:
Thein Lin Kyaw
2019-11-25 23:17:53 +06:30
parent a36e170d94
commit 3c1cc737b5
71 changed files with 1338 additions and 1898 deletions

View File

@@ -1,5 +1,5 @@
class ReceiptBillA5Pdf < Prawn::Document
include ActionView::Helpers::NumberHelper
include NumberFormattable
attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width, :description_width, :price_num_width, :line_move
def initialize(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info = nil,rebate_amount = nil,shop_details, printed_status,current_balance,card_data,other_charges_amount,latest_order_no,card_balance_amount)
@@ -26,11 +26,6 @@ class ReceiptBillA5Pdf < Prawn::Document
#setting page margin and width
super(:margin => [printer_settings.heading_space, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height])
#precision checked
if printer_settings.precision.to_i > 2
printer_settings.precision = 2
end
# db font setup
if printer_settings.font != ""
font_families.update("#{printer_settings.font}" => {
@@ -46,23 +41,17 @@ class ReceiptBillA5Pdf < Prawn::Document
# font "public/fonts/Zawgyi-One.ttf"
# font "public/fonts/padauk.ttf"
if printer_settings.delimiter
delimiter = ","
else
delimiter = ""
end
header(shop_details)
stroke_horizontal_rule
cashier_info(sale_data, customer_name, latest_order_no)
line_items(sale_items,printer_settings.precision,delimiter)
all_total(sale_data,printer_settings.precision,delimiter)
line_items(sale_items,precision,delimiter)
all_total(sale_data,precision,delimiter)
if member_info != nil
member_info(member_info,customer_name,rebate_amount,sale_data,printer_settings.precision,delimiter,current_balance)
member_info(member_info,customer_name,rebate_amount,sale_data,precision,delimiter,current_balance)
end
customer(customer_name)
@@ -77,21 +66,21 @@ class ReceiptBillA5Pdf < Prawn::Document
card_balance_data(card_balance_amount)
end
#end card blanace amount
if discount_price_by_accounts.length > 0 && shop_details.show_account_info
discount_account(discount_price_by_accounts,printer_settings.precision,delimiter)
discount_account(discount_price_by_accounts,precision,delimiter)
end
if shop_details.show_account_info
items_account(item_price_by_accounts,printer_settings.precision,delimiter)
items_account(item_price_by_accounts,precision,delimiter)
if other_charges_amount
show_other_charges_amount(other_charges_amount,printer_settings.precision,delimiter)
show_other_charges_amount(other_charges_amount,precision,delimiter)
end
end
#start for individual payment
if !sale_data.equal_persons.nil?
individual_payment(sale_data, printer_settings.precision, delimiter)
individual_payment(sale_data, precision, delimiter)
end
#end for individual payment
@@ -100,7 +89,7 @@ class ReceiptBillA5Pdf < Prawn::Document
if shop_details.note && !shop_details.note.nil?
shop_note(printed_status)
end
footer(printed_status)
end
@@ -130,7 +119,7 @@ class ReceiptBillA5Pdf < Prawn::Document
text "OrderNo : #{ latest_order_no }", :size => self.header_font_size+2,:align => :left
end
move_down line_move
# move_down 2
y_position = cursor
bounding_box([0,y_position], :width =>self.item_description_width, :height => self.item_height) do
@@ -154,7 +143,7 @@ class ReceiptBillA5Pdf < Prawn::Document
bounding_box([self.item_description_width, y_position], :width =>self.item_description_width, :height => self.item_height) do
text "W: #{sale_data.requested_by}" , :size => self.item_font_size, :align => :left
end
end
bounding_box([self.item_description_width - 2,y_position], :width =>self.item_description_width, :height => self.item_height) do
text "C: #{sale_data.cashier_name}", :size => self.item_font_size,:align => :right
end
@@ -172,7 +161,7 @@ class ReceiptBillA5Pdf < Prawn::Document
# bounding_box([self.item_description_width,y_position], :width =>self.label_width+5) do
# text "(#{ sale_data.bookings[0].checkin_at.utc.getlocal.strftime('%I:%M %p') }
# text "(#{ sale_data.bookings[0].checkin_at.utc.getlocal.strftime('%I:%M %p') }
# - #{ sale_data.bookings[0].checkin_at.utc.getlocal.strftime('%I:%M %p') })" ,
# :size => self.item_font_size,:align => :right
# end
@@ -209,7 +198,7 @@ class ReceiptBillA5Pdf < Prawn::Document
end
def add_line_item_row(sale_items,precision,delimiter)
if precision.to_i > 0
item_name_width = (self.item_width+self.price_width)
item_qty_front_width = (self.item_width+self.price_width) + 5
@@ -229,17 +218,17 @@ class ReceiptBillA5Pdf < Prawn::Document
sub_total = 0.0
total_qty = 0.0
sale_items.each do |item|
# check for item not to show
# check for item not to show
show_price = Lookup.find_by_lookup_type("show_price")
sub_total += item.price #(item.qty*item.unit_price) - comment for room charges
if item.status != 'Discount' && item.qty > 0
if !show_price.nil? && show_price.value.to_i > 0 && item.price == 0
total_qty += item.qty
total_qty += item.qty
else
if item.price != 0
total_qty += item.qty
total_qty += item.qty
end
end
end
@@ -265,14 +254,14 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Sub Total", :size => self.item_font_size,:align => :left
end
# bounding_box([self.item_width + self.price_width + 11,y_position], :width =>self.qty_width, :height => self.item_height) do
# text "#{number_with_precision(total_qty, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :center
# text "#{number_format(total_qty, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :center
# end
# bounding_box([self.item_width + self.price_width + 8,y_position], :width =>self.total_width, :height => self.item_height) do
# text "#{number_with_precision(sub_total, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
# text "#{number_format(sub_total, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
# end
text_box "#{number_with_precision(total_qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
text_box "#{number_with_precision(sub_total, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
text_box "#{number_format(total_qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
text_box "#{number_format(sub_total, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
end
def item_row(item,precision,delimiter,product_name,price,qty ,total_price)
@@ -297,10 +286,10 @@ class ReceiptBillA5Pdf < Prawn::Document
bounding_box([0,y_position], :width =>self.item_width) do
text "#{product_name}", :size => self.item_font_size,:align => :left
end
# text_box "#{product_name}", :at =>[0,y_position], :width => self.item_width, :size => self.item_font_size
text_box "#{number_with_precision(price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[self.item_width,y_position], :width => self.price_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
text_box "#{number_with_precision(qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
text_box "#{number_with_precision(total_price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
# text_box "#{product_name}", :at =>[0,y_position], :width => self.item_width, :size => self.item_font_size
text_box "#{number_format(price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[self.item_width,y_position], :width => self.price_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
text_box "#{number_format(qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
text_box "#{number_format(total_price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
if show_alt_name
if !(item.product_alt_name).empty?
@@ -328,7 +317,7 @@ class ReceiptBillA5Pdf < Prawn::Document
text "#{ dis_type }", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "( #{number_with_precision(sale_data.total_discount, :precision => precision.to_i, :delimiter => delimiter)} )" , :size => self.item_font_size,:align => :right
text "( #{number_format(sale_data.total_discount, :precision => precision.to_i, :delimiter => delimiter)} )" , :size => self.item_font_size,:align => :right
end
if sale_data.sale_taxes.length > 0
@@ -344,7 +333,7 @@ class ReceiptBillA5Pdf < Prawn::Document
text "#{ st.tax_name } (#{incl_tax} #{ st.tax_rate.to_i }%)", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(st.tax_payable_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(st.tax_payable_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
end
else
@@ -370,7 +359,7 @@ class ReceiptBillA5Pdf < Prawn::Document
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{sale_data.rounding_adjustment}", :size => self.item_font_size,:align => :right
end
end
end
move_down line_move
@@ -380,15 +369,15 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Grand Total",:style => :bold, :size => self.header_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(sale_data.grand_total, :precision => precision.to_i, :delimiter => delimiter)}" , :style => :bold, :size => self.header_font_size,:align => :right
text "#{number_format(sale_data.grand_total, :precision => precision.to_i, :delimiter => delimiter)}" , :style => :bold, :size => self.header_font_size,:align => :right
end
move_down line_move
sale_payment(sale_data,precision,delimiter)
end
def sale_payment(sale_data,precision,delimiter)
def sale_payment(sale_data,precision,delimiter)
stroke_horizontal_rule
# move_down 10
sql = "SELECT SUM(payment_amount)
@@ -419,28 +408,28 @@ class ReceiptBillA5Pdf < Prawn::Document
text "#{payment.payment_method.capitalize} Payment", :size => self.item_font_size,:align => :left
end
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(payment.payment_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(payment.payment_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
move_down line_move
end
if sale_data.amount_received > 0
if sale_data.amount_received > 0
y_position = cursor
move_down line_move
bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do
text "Change Amount", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(sale_data.amount_changed, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(sale_data.amount_changed, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
#move_down line_move
end
end
end
# show member information
def member_info(member_info,customer_name,rebate_amount,sale_data,precision,delimiter,current_balance)
if rebate_amount != nil
if rebate_amount != nil
if rebate_amount["status"] == true
stroke_horizontal_rule
total = 0
@@ -467,10 +456,10 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Rebate Earn", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(res["deposit"], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(res["deposit"], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
end
end
# Total Rebate Amount if birthday
if res["receipt_no"]== sale_data.receipt_no && res["account_status"]== "RebatebonusAccount" && res["status"]== "Rebate"
rebate_balance = rebate_balance + res["deposit"]
@@ -480,9 +469,9 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Rebate Earn Bonus", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(res["deposit"], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(res["deposit"], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
end
end
#end Total rebate if birthday
end
@@ -492,7 +481,7 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Redeem Amount", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(redeem, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
text "#{number_format(redeem, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
end
if current_balance != nil
@@ -502,10 +491,10 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Old Balance", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(current_balance, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
text "#{number_format(current_balance, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
end
end
end
end
@@ -515,7 +504,7 @@ class ReceiptBillA5Pdf < Prawn::Document
if res["accountable_type"] == "RebateAccount" || res["accountable_type"] == "RebatebonusAccount"
total_balance = total_balance + res["balance"]
end
end
move_down line_move
@@ -524,10 +513,10 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Total Balance", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "#{number_with_precision(total_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(total_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
end
end
def customer(customer_name)
@@ -554,8 +543,8 @@ class ReceiptBillA5Pdf < Prawn::Document
text "#{ 'Total ' + ipa[:name] + ' Discounts' }", :size => self.item_font_size,:align => :left
end
bounding_box([self.description_width,y_position], :width =>self.label_width) do
text "(" + "#{ number_with_precision(ipa[:price], :precision => precision.to_i, :delimiter => delimiter) }" + ")" , :size => self.item_font_size,:align => :right
end
text "(" + "#{ number_format(ipa[:price], :precision => precision.to_i, :delimiter => delimiter) }" + ")" , :size => self.item_font_size,:align => :right
end
move_down line_move
end
end
@@ -570,8 +559,8 @@ class ReceiptBillA5Pdf < Prawn::Document
text "#{ ipa[:name] }", :size => self.item_font_size,:align => :left
end
bounding_box([self.label_width,y_position], :width =>self.description_width) do
text "#{number_with_precision(ipa[:price], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
text "#{number_format(ipa[:price], :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
move_down line_move
end
end
@@ -582,9 +571,9 @@ class ReceiptBillA5Pdf < Prawn::Document
text "Other Charges", :size => self.item_font_size,:align => :left
end
bounding_box([self.label_width,y_position], :width =>self.item_description_width) do
text "#{number_with_precision(other_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
text "#{number_format(other_amount, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :right
end
move_down line_move
move_down line_move
end
#individual payment per person
@@ -604,7 +593,7 @@ class ReceiptBillA5Pdf < Prawn::Document
bounding_box([self.label_width,y_position], :width =>self.item_description_width) do
move_down 15
text "#{number_with_precision(per_person, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
text "#{number_format(per_person, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
end
end
@@ -623,17 +612,17 @@ class ReceiptBillA5Pdf < Prawn::Document
JOIN sale_audits sa
ON SUBSTRING_INDEX(sa.remark,'||',1)=sale_payment_id
where sa.sale_id='#{sale_data.sale_id}')) = 0
THEN payment_method!='creditnote' ELSE 1 END) AND sale_id = ?", sale_data.sale_id).each do |payment|
THEN payment_method!='creditnote' ELSE 1 END) AND sale_id = ?", sale_data.sale_id).each do |payment|
if payment.payment_method == "creditnote"
y_position = cursor
stroke_horizontal_rule
bounding_box([self.label_width,y_position], :width =>self.description_width) do
move_down 70
stroke_horizontal_rule
end
bounding_box([self.label_width,y_position], :width =>self.description_width) do
move_down 73
text "Approved By" , :size => self.item_font_size,:align => :center
@@ -650,8 +639,8 @@ class ReceiptBillA5Pdf < Prawn::Document
move_down 70
stroke_horizontal_rule
end
if sale_data.payment_status == "foc"
if sale_data.payment_status == "foc"
bounding_box([self.label_width,y_position], :width =>self.description_width) do
move_down 73
text "Acknowledged By" , :size => self.item_font_size,:align => :center
@@ -661,10 +650,10 @@ class ReceiptBillA5Pdf < Prawn::Document
move_down 73
text "Approved By" , :size => self.item_font_size,:align => :center
end
end
end
end
end
end
def shop_note(shop)
@@ -674,8 +663,8 @@ class ReceiptBillA5Pdf < Prawn::Document
move_down line_move
y_position = cursor
text "#{shop.note}", :size => self.item_font_size,:align => :left
text "#{shop.note}", :size => self.item_font_size,:align => :left
move_down line_move
end
@@ -688,10 +677,10 @@ class ReceiptBillA5Pdf < Prawn::Document
y_position = cursor
bounding_box([0, y_position], :width =>self.item_description_width) do
text "#{printed_status}",:style => :bold, :size => header_font_size,:align => :left
end
end
bounding_box([self.item_description_width,y_position], :width =>self.item_description_width, :height => self.item_height) do
text "Thank You! See you Again", :left_margin => -5, :size => self.item_font_size,:align => :right
end
end
move_down line_move
end
@@ -729,7 +718,7 @@ class ReceiptBillA5Pdf < Prawn::Document
y_position = cursor
bounding_box([0, y_position], :width =>self.label_width) do
text "Card Balance: ",:style => :bold, :size => header_font_size,:align => :left
end
end
bounding_box([self.item_description_width,y_position], :width =>self.label_width) do
text "#{card_balance_amount}" , :size => self.item_font_size,:align => :right
end
@@ -753,4 +742,4 @@ class ReceiptBillA5Pdf < Prawn::Document
end
return status
end
end
end