From 644b94764e1547eea74cdca7924136c6fd721ab4 Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Tue, 8 Aug 2017 18:03:02 +0630 Subject: [PATCH] update closing receipt --- app/controllers/origami/shifts_controller.rb | 20 ++- app/models/printer/cashier_station_printer.rb | 4 +- app/models/sale.rb | 11 +- app/models/shift_sale.rb | 29 +++++ app/pdf/close_cashier_pdf.rb | 118 ++++++++++++++++-- 5 files changed, 156 insertions(+), 26 deletions(-) diff --git a/app/controllers/origami/shifts_controller.rb b/app/controllers/origami/shifts_controller.rb index e03dd0fc..f7690fef 100644 --- a/app/controllers/origami/shifts_controller.rb +++ b/app/controllers/origami/shifts_controller.rb @@ -31,15 +31,27 @@ class Origami::ShiftsController < BaseOrigamiController unique_code = "CloseCashierPdf" shop_details = Shop.find(1) #get tax - shift_obj = ShiftSale.where('id =?',@shift.id) - @sale_taxes = Sale.get_separate_tax(shift_obj,from=nil,to=nil,type='') - + shift_obj = ShiftSale.where('id =?',@shift.id) + @sale_taxes = Sale.get_separate_tax(shift_obj,from=nil,to=nil,type='') + #other payment details for mpu or visa like card + @other_payment = ShiftSale.get_by_shift_other_payment(@shift) + #t details for mpu or visa like card + @other_payment = ShiftSale.get_by_shift_other_payment(@shift) + + # Calculate price_by_accounts + @total_amount_by_account = ShiftSale.calculate_total_price_by_accounts(@shift,'amount') + @total_discount_by_account = ShiftSale.calculate_total_price_by_accounts(@shift,'discount') + + + puts "sssssssssss" + puts @total_amount_by_account.to_json + # get printer info print_settings=PrintSetting.find_by_unique_code(unique_code) printer = Printer::CashierStationPrinter.new(print_settings) - printer.print_close_cashier(print_settings,@shift,shop_details,@sale_taxes) + printer.print_close_cashier(print_settings,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account) end diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index 55bf8c39..b7bc34ae 100644 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -22,14 +22,14 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker # end #Bill Receipt Print - def print_close_cashier(printer_settings,shift_sale,shop_details,sale_taxes) + def print_close_cashier(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount) #Use CUPS service #Generate PDF #Print cashier = shift_sale.employee.name shift_name = shift_sale.shift_started_at.utc.getlocal.strftime("%d-%m-%Y %I:%M %p") + "_" + shift_sale.shift_closed_at.utc.getlocal.strftime("%d-%m-%Y %I:%M %p") - pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes) + pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount) filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" pdf.render_file filename self.print(filename) diff --git a/app/models/sale.rb b/app/models/sale.rb index 4a4c451f..57794048 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -569,7 +569,7 @@ def self.get_item_query() query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") query = query.group('i.product_code ').order("mi.account_id, mi.menu_category_id") - end +end def self.get_by_shift_items(shift_sale_range, shift, from, to, status) @@ -746,15 +746,6 @@ def self.get_separate_tax(shift_sale_range=nil,shift,from,to,payment_type) end end -# def self.get_separate_tax(from,to,payment_method=nil) - -# query = SaleTax.select("SUM(tax_payable_amount) AS st_amount,tax_name") -# .joins("INNER JOIN sales ON sales.sale_id = sale_taxes.sale_id") -# .group("sale_taxes.tax_name") - -# return query = query.where("sale_status=? and receipt_date between ? and ?","completed",from,to) -# end - def grand_total_after_rounding return self.old_grand_total.to_f + self.rounding_adjustment.to_f end diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index b7ce7299..af9d014d 100644 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -88,4 +88,33 @@ class ShiftSale < ApplicationRecord closing_balance = shiftobj.grand_total + shiftobj.cash_in - shiftobj.cash_out + shiftobj.total_cash return closing_balance end + + def self.get_by_shift_other_payment(shift) + + other_payment = Sale.select("sale_payments.payment_method as name, + SUM(case when (sale_payments.payment_method='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_amount, + SUM(case when (sale_payments.payment_method='visa') then (sale_payments.payment_amount) else 0 end) as visa_amount, + SUM(case when (sale_payments.payment_method='master') then (sale_payments.payment_amount) else 0 end) as master_amount, + SUM(case when (sale_payments.payment_method='jcb') then (sale_payments.payment_amount) else 0 end) as jcb_amount, + SUM(case when (sale_payments.payment_method='paypar') then (sale_payments.payment_amount) else 0 end) as paypar_amount") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .where("sales.shift_sale_id =? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.id) + end + + def self.calculate_total_price_by_accounts(shift,type) + query = Sale.select("acc.title as account_name," + + "SUM(case when (acc.id=i.account_id) then (i.price) else 0 end) as total_price") + + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id "+ + "JOIN accounts acc ON acc.id = i.account_id" + + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id") + if type == 'discount' + query = query.where("sales.shift_sale_id =? and sale_status = 'completed' and i.is_taxable = false and i.remark = 'Discount'", shift.id) + .group("acc.title").order("acc.id") + else + query = query.where("sales.shift_sale_id =? and sale_status = 'completed'", shift.id) + .group("acc.title").order("acc.id") + end + end + end diff --git a/app/pdf/close_cashier_pdf.rb b/app/pdf/close_cashier_pdf.rb index 4eadf007..b5325aac 100644 --- a/app/pdf/close_cashier_pdf.rb +++ b/app/pdf/close_cashier_pdf.rb @@ -1,6 +1,6 @@ class CloseCashierPdf < Prawn::Document 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,:text_width - def initialize(printer_settings, shift_sale,shop_details,sale_taxes) + def initialize(printer_settings, shift_sale,shop_details,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account) self.page_width = 210 self.page_height = 7000 self.margin = 5 @@ -32,7 +32,7 @@ class CloseCashierPdf < Prawn::Document stroke_horizontal_rule - shift_detail(shift_sale,sale_taxes) + shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account) @@ -51,7 +51,7 @@ class CloseCashierPdf < Prawn::Document stroke_horizontal_rule end - def shift_detail(shift_sale,sale_taxes) + def shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account) move_down 7 y_position = cursor bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do @@ -151,6 +151,10 @@ class CloseCashierPdf < Prawn::Document text "#{shift_sale.total_taxes}", :size => self.item_font_size, :align => :right end + move_down -5 + stroke_horizontal_rule + move_down 7 + y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Cash Payment :", :size => self.item_font_size, :align => :right @@ -166,14 +170,88 @@ class CloseCashierPdf < Prawn::Document bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "#{shift_sale.credit_sales}", :size => self.item_font_size, :align => :right end +#start other payment details + if shift_sale.other_sales > 0 + + other_payment.each do |other| + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Other Payment Details", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "", :size => self.item_font_size, :align => :right + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "MPU Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{other.mpu_amount.round(2)}", :size => self.item_font_size, :align => :right + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "VISA Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{other.visa_amount.round(2)}", :size => self.item_font_size, :align => :right + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "JCB Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{other.master_amount.round(2)}", :size => self.item_font_size, :align => :right + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Master Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{other.jcb_amount.round(2)}", :size => self.item_font_size, :align => :right + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Reedem Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{other.paypar_amount.round(2)}", :size => self.item_font_size, :align => :right + end + + end + + else + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Other Payment :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{shift_sale.other_sales}", :size => self.item_font_size, :align => :right + end - y_position = cursor - bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do - text "Other Payment :", :size => self.item_font_size, :align => :right end - bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - text "#{shift_sale.other_sales}", :size => self.item_font_size, :align => :right + # end other payment details + move_down -5 + stroke_horizontal_rule + move_down 7 + #start total amount by Account Like Food / Beverage /.. + total_amount_by_account.each do |amount| + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total #{amount.account_name} Amount:", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{amount.total_price.round(2)} ", :size => self.item_font_size, :align => :right + end end + #end total amount by Account y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do @@ -183,14 +261,34 @@ class CloseCashierPdf < Prawn::Document text "#{shift_sale.total_revenue}", :size => self.item_font_size, :align => :right end + move_down -5 + stroke_horizontal_rule + move_down 7 + #start total amount by Account Like Food / Beverage /.. + total_discount_by_account.each do |amount| + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total #{amount.account_name} Discount:", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{amount.total_price.round(2)} ", :size => self.item_font_size, :align => :right + end + end + #end total amount by Account + y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do - text "Discount Amount :", :size => self.item_font_size, :align => :right + text "Overall Discount Amount :", :size => self.item_font_size, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "#{shift_sale.total_discounts}", :size => self.item_font_size, :align => :right end - + + move_down -5 + stroke_horizontal_rule + move_down 7 + sale_taxes.each do |tax| y_position = cursor