diff --git a/app/pdf/receipt_bill_a5_pdf.rb b/app/pdf/receipt_bill_a5_pdf.rb new file mode 100644 index 00000000..4346759c --- /dev/null +++ b/app/pdf/receipt_bill_a5_pdf.rb @@ -0,0 +1,507 @@ +class ReceiptBillA5Pdf < Prawn::Document + include ActionView::Helpers::NumberHelper + 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 + 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) + self.page_width = printer_settings.page_width + self.page_height = printer_settings.page_height + self.margin = 20 + self.price_width = 120 + self.qty_width = 50 + self.total_width = 120 + self.item_width = self.page_width - ((self.qty_width + self.price_width + self.total_width)) + self.item_height = 25 + self.item_description_width = (self.page_width-20) / 2 + self.label_width = 150 + + self.description_width = 500 + self.price_num_width = 100 + # @item_width = self.page_width.to_i / 2 + # @qty_width = @item_width.to_i / 3 + # @double = @qty_width * 1.3 + # @half_qty = @qty_width / 2 + #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}" => { + :normal => "public/fonts/#{printer_settings.font}.ttf", + :italic => "public/fonts/#{printer_settings.font}.ttf", + :bold => "public/fonts/#{printer_settings.font}.ttf", + :bold_italic => "public/fonts/#{printer_settings.font}.ttf" + }) + + font "#{printer_settings.font}" + fallback_fonts ["Courier", "Helvetica", "Times-Roman"] + end + # font "public/fonts/Zawgyi-One.ttf" + # font "public/fonts/padauk.ttf" + self.header_font_size = 16 + self.item_font_size = 14 + + if printer_settings.delimiter + delimiter = "," + else + delimiter = "" + end + + header(shop_details) + + stroke_horizontal_rule + + cashier_info(sale_data, customer_name) + line_items(sale_items,printer_settings.precision,delimiter) + all_total(sale_data,printer_settings.precision,delimiter) + + + if member_info != nil + member_info(member_info,customer_name,rebate_amount,sale_data,printer_settings.precision,delimiter,current_balance) + end + + customer(customer_name) + + if discount_price_by_accounts.length > 0 && shop_details.show_account_info + discount_account(discount_price_by_accounts,printer_settings.precision,delimiter) + end + + if shop_details.show_account_info + items_account(item_price_by_accounts,printer_settings.precision,delimiter) + end + + sign(sale_data) + + footer(printed_status) + end + + def header (shop_details) + move_down 10 + text "#{shop_details.name}", :size => self.header_font_size,:align => :center + move_down 10 + text "#{shop_details.address}", :size => self.item_font_size,:align => :center + # move_down self.item_height + move_down 10 + text "#{shop_details.phone_no}", :size => self.item_font_size,:align => :center + move_down 10 + + stroke_horizontal_rule + end + + def cashier_info(sale_data, customer_name) + move_down 12 + # move_down 2 + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => self.item_height) do + text "Receipt No: #{sale_data.receipt_no}", :size => self.item_font_size,:align => :left + end + bounding_box([self.item_description_width, y_position], :width => self.item_description_width, :height => self.item_height) do + text "#{ sale_data.bookings[0].dining_facility.type } - #{ sale_data.bookings[0].dining_facility.name }" , :size => self.item_font_size,:align => :right + end + move_down 10 + + y_position = cursor + bounding_box([0, 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 + bounding_box([self.item_description_width,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 + move_down 10 + + y_position = cursor + bounding_box([0,y_position], :width =>self.page_width - 20, :height => self.item_height) do + text "Date : #{ sale_data.receipt_date.strftime('%d-%m-%Y') } ( #{ sale_data.bookings[0].checkin_at.utc.getlocal.strftime('%I:%M %p') } - #{ sale_data.bookings[0].checkout_at.utc.getlocal.strftime('%I:%M %p') } )",:size => self.item_font_size,:align => :left + end + + + # 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') } + # - #{ sale_data.bookings[0].checkin_at.utc.getlocal.strftime('%I:%M %p') })" , + # :size => self.item_font_size,:align => :right + # end + + move_down 10 + stroke_horizontal_rule + end + + def line_items(sale_items,precision,delimiter) + if precision.to_i > 0 + item_label_qty_front_width = (self.item_width+self.price_width) + 5 + item_label_qty_end_width = self.qty_width + 4 + item_label_total_front_width = (self.item_width+self.price_width) + 10 + item_label_total_end_width = self.total_width + 9 + else + self.item_width = self.item_width.to_i + 8 + item_label_qty_front_width = (self.item_width+self.price_width) + 8 + item_label_qty_end_width = self.qty_width + 7 + item_label_total_front_width = (self.item_width+self.price_width) + 5 + item_label_total_end_width = self.total_width + 4 + end + move_down 10 + y_position = cursor + move_down 10 + pad_top(15) { + # @item_width.to_i + @half_qty.to_i + text_box "Items", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix + text_box "Price", :at =>[(self.item_width),y_position], :width => self.price_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix + text_box "Qty", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix + text_box "Total", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix + } + move_down -5 + stroke_horizontal_rule + add_line_item_row(sale_items,precision,delimiter) + 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 + item_qty_end_width = self.qty_width + 4 + item_total_front_width = item_name_width + 10 + item_total_end_width = self.total_width + 9 + else + item_name_width = (self.item_width+self.price_width) + item_qty_front_width = item_name_width + 8 + item_qty_end_width = self.qty_width + 7 + item_total_front_width = item_name_width + 5 + item_total_end_width = self.total_width + 4 + end + y_position = cursor + move_down 10 + sub_total = 0.0 + sale_items.each do |item| + # check for item not to show + if item.price != 0 + sub_total += item.price #(item.qty*item.unit_price) - comment for room charges + qty = item.qty + total_price = item.price #item.qty*item.unit_price - comment for room charges + price = item.unit_price + product_name = item.product_name + + + y_position = cursor + + pad_top(15) { + 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 + } + move_down 10 + end + end + + stroke_horizontal_rule + + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + text "Sub Total", :size => self.item_font_size,:align => :left + end + bounding_box([self.description_width,y_position], :width =>self.label_width) do + text "#{number_with_precision(sub_total, :precision => precision.to_i, :delimiter => delimiter)}" ,:delimiter => ',', :size => self.item_font_size,:align => :right + end + move_down 10 + end + + def all_total(sale_data,precision,delimiter) + item_name_width = self.item_width + y_position = cursor + if sale_data.discount_type == 'member_discount' + dis_type = "Member Discount:" + else + dis_type = "Overall Discount:" + end + + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + + if sale_data.sale_taxes.length > 0 + sale_data.sale_taxes.each do |st| + move_down 10 + y_position = cursor + + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + text "#{ st.tax_name } ( #{ 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 + end + end + else + + end + + # move_down 10 + # y_position = cursor + + # bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + # text "Total Tax", :size => self.item_font_size,:align => :left + # end + # bounding_box([self.description_width,y_position], :width =>self.label_width) do + # text "( " +"#{sale_data.total_tax}" +" )" , :size => self.item_font_size,:align => :right + # end + + if sale_data.rounding_adjustment != 0.0 + move_down 10 + y_position = cursor + + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + text "Rounding Adjustment", :size => self.item_font_size,:align => :left + 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 + + move_down 10 + y_position = cursor + move_down 10 + bounding_box([0,y_position], :width =>self.description_width) do + 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 + end + move_down 10 + + sale_payment(sale_data,precision,delimiter) + + end + + def sale_payment(sale_data,precision,delimiter) + stroke_horizontal_rule + move_down 10 + sale_payments = SalePayment.select("SUM(sale_payments.payment_amount) as payment_amount,sale_payments.payment_method").where('sale_id = ?', sale_data.sale_id).group("payment_method") + sale_payments.each do |payment| + y_position = cursor + if payment.payment_method == "paypar" + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + text "Redeem Payment", :size => self.item_font_size,:align => :left + end + else + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + move_down 10 + end + if sale_data.amount_received > 0 + y_position = cursor + move_down 10 + 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 + end + move_down 10 + 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["status"] == true + stroke_horizontal_rule + total = 0 + balance = 0 + rebate_balance =0 + redeem = 0 + redeem_count = 0 + rebate_amount["data"].each do |res| + total = total + res["balance"] + #total redeem amount + if res["receipt_no"]== sale_data.receipt_no && res["status"]== "Redeem" + redeem = redeem + res["withdraw"] + balance = balance + res["balance"] + end + #end Total redem + #total Rebate Earn + if res["receipt_no"]== sale_data.receipt_no && res["account_status"]== "RebateAccount" && res["status"]== "Rebate" + + rebate_balance = rebate_balance + res["deposit"] + + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + 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"] + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + end + #end Total rebate if birthday + end + + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + + if current_balance != nil + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + end + + end + end + + if member_info["status"] == true && member_info["data"].present? + total_balance = 0 + member_info["data"].each do |res| + + if res["accountable_type"] == "RebateAccount" || res["accountable_type"] == "RebatebonusAccount" + total_balance = total_balance + res["balance"] + + end + end + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + end + + end + + def customer(customer_name) + move_down 10 + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + text "Customer Name", :size => self.item_font_size,:align => :left + end + bounding_box([self.description_width,y_position], :width =>self.label_width) do + text "#{ customer_name }" , :size => self.item_font_size,:align => :right + end + end + + + def discount_account(discount_price_by_accounts,precision,delimiter) + move_down 10 + stroke_horizontal_rule + move_down 10 + y_position = cursor + discount_price_by_accounts.each do |ipa| + y_position = cursor + bounding_box([0,y_position], :width =>self.description_width, :height => self.item_height) do + 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 + end + end + + def items_account(item_price_by_accounts,precision,delimiter) + move_down 10 + stroke_horizontal_rule + move_down 10 + y_position = cursor + item_price_by_accounts.each do |ipa| + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width) do + 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 + end + end + + def sign(sale_data) + SalePayment.where('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 + end + break; + end + end + if sale_data.payment_status == "foc" + + 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 "Acknowledged By" , :size => self.item_font_size,:align => :center + end + end + + + end + + def footer(printed_status) + move_down 10 + stroke_horizontal_rule + move_down 10 + + y_position = cursor + bounding_box([0, y_position], :width =>self.label_width) do + text "#{printed_status}",:style => :bold, :size => header_font_size,:align => :left + end + bounding_box([self.description_width,y_position], :width =>self.description_width, :height => self.item_height) do + text "Thank You! See you Again", :left_margin => -5, :size => self.item_font_size,:align => :left + end + + move_down 10 + end +end +