From 4a0b88cfb92625e81f21427ce2dae5a195381ada Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Thu, 15 Feb 2018 10:42:02 +0630 Subject: [PATCH 01/31] update application for license --- app/controllers/application_controller.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e66d7700..0966a573 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -38,11 +38,11 @@ class ApplicationController < ActionController::Base end else # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end + if check_license + current_license(ENV["SX_PROVISION_URL"]) + else + redirect_to activate_path + end end end From dff0a4798e56cfffcb162875f708a61103fc1c41 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 15 Feb 2018 12:11:54 +0630 Subject: [PATCH 02/31] add new line --- app/models/printer/cashier_station_printer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index e62ab4d1..fd1dc0cd 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -30,6 +30,7 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker 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,other_payment,amount,discount,member_discount) + filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" pdf.render_file filename self.print(filename, cashier_terminal.printer_name) From 35721cd9177b7244e0197e07b19df144a5781d9e Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 15 Feb 2018 12:15:39 +0630 Subject: [PATCH 03/31] remove space line --- app/models/printer/cashier_station_printer.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index e62ab4d1..3c6dac88 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -34,6 +34,4 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker pdf.render_file filename self.print(filename, cashier_terminal.printer_name) end - - end From d8d119edd6c7610664a6c7d8150c2cb4a50c017f Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 15 Feb 2018 13:27:45 +0630 Subject: [PATCH 04/31] release_c1 --- app/models/printer/cashier_station_printer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index 2577e8c1..fc79934d 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -35,4 +35,5 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker pdf.render_file filename self.print(filename, cashier_terminal.printer_name) end + #release_c1 end From 016d3f4187c91c5a9aaf94f8eda777f2dc2aa6f5 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 15 Feb 2018 13:29:52 +0630 Subject: [PATCH 05/31] sqa --- app/models/printer/cashier_station_printer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index 3c6dac88..dc0d79d1 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -34,4 +34,5 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker pdf.render_file filename self.print(filename, cashier_terminal.printer_name) end + #sqa end From e50915aa3b38087da30b4f740b9461fbf75960a8 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 15 Feb 2018 18:35:33 +0630 Subject: [PATCH 06/31] Add Dinein, Takeway, Change Total to Gross Sale,Grand Total --- app/controllers/application_controller.rb | 14 +- app/controllers/origami/shifts_controller.rb | 8 +- app/models/printer/cashier_station_printer.rb | 8 +- app/models/shift_sale.rb | 14 + app/pdf/close_cashier_customerize_pdf.rb | 433 ++++++++++++++++++ app/pdf/close_cashier_pdf.rb | 6 +- 6 files changed, 467 insertions(+), 16 deletions(-) create mode 100644 app/pdf/close_cashier_customerize_pdf.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0966a573..505a38b3 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,13 +36,13 @@ class ApplicationController < ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - else - # check for license file - if check_license - current_license(ENV["SX_PROVISION_URL"]) - else - redirect_to activate_path - end + # else + # # check for license file + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end end end diff --git a/app/controllers/origami/shifts_controller.rb b/app/controllers/origami/shifts_controller.rb index cb2cc830..8bb684e9 100755 --- a/app/controllers/origami/shifts_controller.rb +++ b/app/controllers/origami/shifts_controller.rb @@ -73,12 +73,15 @@ class Origami::ShiftsController < BaseOrigamiController @total_amount_by_account = ShiftSale.calculate_total_price_by_accounts(@shift,'amount') @total_discount_by_account = ShiftSale.calculate_total_price_by_accounts(@shift,'discount') @total_member_discount = ShiftSale.get_total_member_discount(@shift) + @total_dinein = ShiftSale.get_total_dinein(@shift).total_dinein_amount + @total_takeway = ShiftSale.get_total_takeway(@shift).total_takeway_amount + # get printer info print_settings = PrintSetting.find_by_unique_code(unique_code) printer = Printer::CashierStationPrinter.new(print_settings) - printer.print_close_cashier(print_settings,cashier_terminal,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount) + printer.print_close_cashier(print_settings,cashier_terminal,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount,@total_dinein,@total_takeway) end end Employee.logout(session[:session_token]) @@ -102,8 +105,7 @@ class Origami::ShiftsController < BaseOrigamiController # 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') - @total_member_discount = ShiftSale.get_total_member_discount(@shift) - + @total_member_discount = ShiftSale.get_total_member_discount(@shift) end end diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index fc79934d..400a0934 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -22,18 +22,18 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker # end #Bill Receipt Print - def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount) + def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway) #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,other_payment,amount,discount,member_discount) - + pdf = CloseCashierCustomerizePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway) filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" pdf.render_file filename self.print(filename, cashier_terminal.printer_name) end - #release_c1 + + end diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index 57816948..0127bd5c 100755 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -135,6 +135,20 @@ class ShiftSale < ApplicationRecord end + def self.get_total_dinein(shift) + query = Sale.select("sum(sales.grand_total) as total_dinein_amount") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Dinein" and c.membership_id is null',shift.id) + .first() + end + + def self.get_total_takeway(shift) + query = Sale.select("sum(sales.grand_total) as total_takeway_amount") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Takeaway" and c.membership_id is null',shift.id) + .first() + end + def self.search(filter,from,to) if filter.blank? keyword = '' diff --git a/app/pdf/close_cashier_customerize_pdf.rb b/app/pdf/close_cashier_customerize_pdf.rb new file mode 100644 index 00000000..45834489 --- /dev/null +++ b/app/pdf/close_cashier_customerize_pdf.rb @@ -0,0 +1,433 @@ +class CloseCashierCustomerizePdf < 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,:text_width + + def initialize(printer_settings, shift_sale,shop_details,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,total_dinein,total_takeway) + self.page_width = printer_settings.page_width #PrintSetting.where("name = ?","Close Cashier").first.page_width + self.page_height = printer_settings.page_height + self.margin = 5 + self.price_width = 60 + self.qty_width = 20 + self.total_width = 40 + self.item_width = self.page_width - ((self.price_width + self.qty_width + self.total_width)) + self.item_height = 15 + self.item_description_width = (self.page_width-20) / 2 + self.label_width = 100 + + self.text_width = (self.page_width - 80) - self.price_width / 3 + # @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]) + + # 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 = 10 + self.item_font_size = 8 + + #precision checked + if printer_settings.precision.to_i > 2 + printer_settings.precision = 2 + end + #check delimiter + if printer_settings.delimiter + delimiter = "," + else + delimiter = "" + end + + header( shop_details) + + stroke_horizontal_rule + + shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,printer_settings.precision,delimiter,total_dinein,total_takeway) + end + + def header (shop_details) + move_down 7 + text "#{shop_details.name}", :left_margin => -10, :size => self.header_font_size,:align => :center + move_down 5 + text "#{shop_details.address}", :size => self.item_font_size,:align => :center + # move_down self.item_height + move_down 5 + text "#{shop_details.phone_no}", :size => self.item_font_size,:align => :center + move_down 5 + + stroke_horizontal_rule + end + + def shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,precision,delimiter,total_dinein,total_takeway) + move_down 7 + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Cashier : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ shift_sale.employee.name}" , :size => self.item_font_size,:align => :left + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Cashier Station : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ shift_sale.cashier_terminal.name}" , :size => self.item_font_size,:align => :left + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Opening Date : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ shift_sale.shift_started_at.utc.getlocal.strftime('%d-%m-%Y %I:%M %p') }" , :size => self.item_font_size,:align => :left + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Closing Date : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ shift_sale.shift_closed_at.utc.getlocal.strftime('%d-%m-%Y %I:%M %p') }" , :size => self.item_font_size,:align => :left + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Opening Float : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ number_with_precision(shift_sale.opening_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :left + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do + text "Closing Float : ", :size => self.item_font_size,:align => :left + end + bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do + text "#{ number_with_precision(shift_sale.closing_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :left + # text_box "#{number_with_precision(total_price, :precision => precision.to_i, :delimiter => delimiter)}" + end + + + move_down 10 + + y_position = cursor + bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do + text "Shift Sale Summary", :size => self.header_font_size, :align => :center + end + move_down 10 + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Received 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 "#{number_with_precision(shift_sale.closing_balance, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Cash In :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{number_with_precision(shift_sale.cash_in, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Cash Out :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{number_with_precision(shift_sale.cash_out, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + + move_down -5 + stroke_horizontal_rule + move_down 7 + + @total_grand_total = shift_sale.grand_total + @overall + @total_foc + shift_sale.total_void + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Grand Total :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{number_with_precision(shift_sale.cash_out, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total 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 "#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total FOC :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "(#{ number_with_precision(@total_foc, :precision => precision.to_i, :delimiter => delimiter)})", :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 "Total Void :", :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_void})", :size => self.item_font_size, :align => :right + end + + move_down -5 + stroke_horizontal_rule + move_down 7 + + @total_foc = 0 + 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 + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(shift_sale.cash_sales, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Credit 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 "#{number_with_precision(shift_sale.credit_sales, :precision => precision.to_i, :delimiter => delimiter) }", :size => self.item_font_size, :align => :right + end + #start other payment details + if shift_sale.other_sales > 0 + other_payment.each do |other| + @total_foc = other.foc_amount.round(2) + 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 "#{ number_with_precision(other.mpu_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{number_with_precision(other.visa_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(other.master_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(other.jcb_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(other.paypar_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(shift_sale.other_sales, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Rounding Adjustments :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Gross Sale :", :style => :bold, :size => self.header_font_size - 1, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(shift_sale.grand_total, :precision => precision.to_i, :delimiter => delimiter)}", :style => :bold, :size => self.header_font_size - 1, :align => :right + end + + # end other payment details + move_down -5 + stroke_horizontal_rule + move_down 7 + + # start Dinein and Takeaway + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Dinein :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(total_dinein, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total Takeaway :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(total_takeway, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + + end + # stop Dinein and Takeaway + move_down -5 + stroke_horizontal_rule + move_down 7 + + #start service charges and commercial tax + sale_taxes.each do |tax| + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "#{tax.tax_name} :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(tax.st_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + end + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Taxes :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(shift_sale.total_taxes, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Net Sales :", :style => :bold, :size => self.header_font_size - 1, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{number_with_precision(shift_sale.nett_sales, :precision => precision.to_i, :delimiter => delimiter) }", :style => :bold , :size => self.header_font_size - 1, :align => :right + end + #end for service charges and commercial tax +#COMMENTED FOR NO NEED AND NOT CORRECT WHEN OTHER CHARGES + # y_position = cursor + # bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + # text "Total Sale :", :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_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 "#{ number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right + end + end + #end total amount by Account + + if total_member_discount[0].member_discount.present? + @member_discount = total_member_discount[0].member_discount rescue 0.0 + @overall = shift_sale.total_discounts - @member_discount + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Member 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 "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + else + @overall = shift_sale.total_discounts + end + + # y_position = cursor + # bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + # text "Grand Total :", :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.grand_total}", :size => self.item_font_size, :align => :right + # end + + + #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 "#{number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :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 "Total Dine-in Count :", :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.dining_count}", :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 "Total Takeaway Count :", :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.takeaway_count}", :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 "Total Receipts :", :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_receipt}", :size => self.item_font_size, :align => :right + end + + move_down 5 + stroke_horizontal_rule + move_down 5 + move_down 5 + end +end + diff --git a/app/pdf/close_cashier_pdf.rb b/app/pdf/close_cashier_pdf.rb index 35248b97..7bee4ed3 100755 --- a/app/pdf/close_cashier_pdf.rb +++ b/app/pdf/close_cashier_pdf.rb @@ -234,12 +234,14 @@ class CloseCashierPdf < Prawn::Document text "Rounding Adjustments :", :size => self.item_font_size, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + # text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + text "#{ number_with_precision(shift_sale.total_dinein, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total :", :style => :bold, :size => self.header_font_size - 1, :align => :right + text "Gross Sale :", :style => :bold, :size => self.header_font_size - 1, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "#{ number_with_precision(shift_sale.grand_total, :precision => precision.to_i, :delimiter => delimiter)}", :style => :bold, :size => self.header_font_size - 1, :align => :right From 5b354299bbd5f84b61138c2a4380bfa3a52371ee Mon Sep 17 00:00:00 2001 From: Jueli Moon Aung Date: Fri, 16 Feb 2018 03:04:30 +0000 Subject: [PATCH 07/31] puma.rb.production edited by julian --- config/puma.rb.production | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/puma.rb.production b/config/puma.rb.production index 4953a9b0..e88031af 100755 --- a/config/puma.rb.production +++ b/config/puma.rb.production @@ -1,7 +1,6 @@ -application_path = '/home/superuser/Application/production/sxrestaurant' +application_path = '/home/superuser/Application/sxrestaurant' directory application_path -environment ENV.fetch("RAILS_ENV") { "development" } -daemonize true +environment ENV.fetch("RAILS_ENV") { "production" } pidfile "#{application_path}/tmp/puma/pid" state_path "#{application_path}/tmp/puma/state" stdout_redirect "#{application_path}/log/puma.stdout.log", "#{application_path}/log/puma.stderr.log" From cf7da9f25ce629e8517393aeda859d0bacbba6f6 Mon Sep 17 00:00:00 2001 From: Jueli Moon Aung Date: Fri, 16 Feb 2018 03:05:10 +0000 Subject: [PATCH 08/31] secrets.yml edited online by julian --- config/secrets.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/secrets.yml b/config/secrets.yml index a0220639..73e4fc37 100755 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -24,7 +24,7 @@ test: # instead read values from the environment. production: secret_key_base: c4bc81065013f9a3506d385bcbd49586c42e586488144b0de90c7da36867de9fa880f46b5c4f86f0ce9b7c783bb5a73bdb0e5605a47716567294390e726d3e22 - sx_provision_url: connect.smartsales.asia/api #192.168.1.147:3002/api + sx_provision_url: connect.smartsales.dev/api #192.168.1.147:3002/api server_mode: application cipher_type: AES-256-CBC sx_key: Wh@t1$C2L From dc5cd84f5cf6b98b4262a6e2c2e385be520a3663 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 19 Feb 2018 13:46:08 +0630 Subject: [PATCH 09/31] show cashier dashboard view --- app/controllers/application_controller.rb | 2 + app/controllers/home_controller.rb | 36 +-- app/models/sale.rb | 349 +++++++++++++++++++--- app/views/home/dashboard.html.erb | 31 +- config/locales/en.yml | 1 + config/locales/mm.yml | 1 + 6 files changed, 351 insertions(+), 69 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e66d7700..eaf89c34 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -89,6 +89,8 @@ class ApplicationController < ActionController::Base end def current_user + puts "session" + puts session[:session_token] @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 25fb0d70..db843aca 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -77,42 +77,42 @@ class HomeController < ApplicationController .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') @inventories = StockJournal.inventory_balances(today).sum(:balance) - @total_sale = Sale.total_sale(today) - @total_count = Sale.total_count(today) - @total_card = Sale.total_card_sale(today) - @total_credit = Sale.credit_payment(today) + @total_sale = Sale.total_sale(today,current_user) + @total_count = Sale.total_count(today,current_user) + @total_card = Sale.total_card_sale(today,current_user) + @total_credit = Sale.credit_payment(today,current_user) @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today) + @total_payment_methods = Sale.total_payment_methods(today,current_user) @total_payment_methods.each do |payment| if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" - pay = Sale.payment_sale('card', today) + pay = Sale.payment_sale('card', today, current_user) @sale_data.push({'card' => pay.payment_amount}) else - pay = Sale.payment_sale(payment.payment_method, today) + pay = Sale.payment_sale(payment.payment_method, today, current_user) @sale_data.push({payment.payment_method => pay.payment_amount}) end end - @summ_sale = Sale.summary_sale_receipt(today) - @total_customer = Sale.total_customer(today) - @total_dinein = Sale.total_dinein(today) - @total_takeaway = Sale.total_takeaway(today) - @total_other_customer = Sale.total_other_customer(today) - @total_membership = Sale.total_membership(today) + @summ_sale = Sale.summary_sale_receipt(today,current_user) + @total_customer = Sale.total_customer(today,current_user) + @total_dinein = Sale.total_dinein(today,current_user) + @total_takeaway = Sale.total_takeaway(today,current_user) + @total_other_customer = Sale.total_other_customer(today,current_user) + @total_membership = Sale.total_membership(today,current_user) - @total_order = Sale.total_order(today) - @total_accounts = Sale.total_account(today) + @total_order = Sale.total_order(today,current_user) + @total_accounts = Sale.total_account(today,current_user) @account_data = Array.new @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today) + acc = Sale.account_data(account.account_id, today,current_user) if !acc.nil? @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end end - @top_items = Sale.top_items(today) - @total_foc_items = Sale.total_foc_items(today) + @top_items = Sale.top_items(today,current_user) + @total_foc_items = Sale.total_foc_items(today,current_user) # get printer info @print_settings = PrintSetting.get_precision_delimiter() diff --git a/app/models/sale.rb b/app/models/sale.rb index efdc8d30..3c97dbf8 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1088,60 +1088,158 @@ end .order("e.name") end - def self.total_sale(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") - + def self.total_sale(today,current_user=nil) + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .sum("grand_total") + end + end + end end - def self.total_count(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + def self.total_count(today,current_user=nil) + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count + end + end + end end - def self.total_card_sale(today) - payment_type = " and payment_method = 'mpu' or payment_method = 'visa' or payment_method = 'master' or payment_method = 'jcb' or payment_method = 'unionpay' " - # query = Sale.select("SUM(tax_payable_amount) AS st_amount,tax_name") - # .where('sale_status = "completed" #{payment_type} and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - # .joins("join sale_payments on sale_id = sales.sale_id") - # .group("sales.sale_id") - - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + def self.total_card_sale(today,current_user=nil) + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") .sum("sp.payment_amount") - + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end end - def self.credit_payment(today) - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + def self.credit_payment(today,current_user=nil) + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end end - def self.summary_sale_receipt(today) - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + def self.summary_sale_receipt(today,current_user=nil) + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_payment_methods(today) - query = Sale.select("distinct sp.payment_method") + def self.total_payment_methods(today,current_user=nil) + if current_user.nil? + query = Sale.select("distinct sp.payment_method") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end end - def self.payment_sale(payment_method, today) - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + def self.payment_sale(payment_method, today, current_user=nil) + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) else query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) end query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',today,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end end - def self.total_customer(today) - dinein_cnt = self.total_dinein(today) - takeaway_cnt = self.total_takeaway(today) - membership_cnt = self.total_membership(today) + def self.total_customer(today,current_user=nil) + dinein_cnt = self.total_dinein(today,current_user) + takeaway_cnt = self.total_takeaway(today,current_user) + membership_cnt = self.total_membership(today,current_user) total_cus = 0 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1151,69 +1249,226 @@ end return total_cus end - def self.total_dinein(today) - query = Sale.select("count(sales.customer_id) as total_dinein_cus") + def self.total_dinein(today,current_user=nil) + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_takeaway(today) - query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) - .first() + def self.total_takeaway(today,current_user=nil) + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_membership(today) - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + def self.total_membership(today,current_user=nil) + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_other_customer(today) - query = Sale.select("count(sales.customer_id) as total_cus") + def self.total_other_customer(today,current_user=nil) + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_order(today) - query = Sale.select("count(distinct a.order_id) as total_order") + def self.total_order(today,current_user=nil) + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") .joins("JOIN orders as b ON b.order_id = a.order_id") .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end end - def self.total_account(today) - query = Sale.select("distinct b.id as account_id, b.title as title") + def self.total_account(today,current_user=nil) + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .joins("JOIN accounts as b ON b.id = a.account_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + end + end + end end - def self.account_data(account_id, today) - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + def self.account_data(account_id, today, current_user=nil) + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .first() + end + end + end end - def self.top_items(today) - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + def self.top_items(today,current_user=nil) + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .group("a.product_code") .order("SUM(a.qty) DESC") .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end end - def self.total_foc_items(today) - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + def self.total_foc_items(today,current_user=nil) + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .sum("a.qty") + end + end + end end #card sale trans data diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 104c4c67..4056e064 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -1,6 +1,7 @@
-

<%= t :dashboard %>

+ +

<%= t :date_time %> : <%= Time.zone.now.utc.getlocal.strftime("%Y-%m-%d %I:%M %p") %>

<% if @print_settings.precision.to_i > 0 precision = @print_settings.precision @@ -14,6 +15,7 @@ delimiter = "" end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
@@ -62,11 +64,12 @@
+ <% end %> - <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
+ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
@@ -122,10 +125,18 @@
-
+ <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'cashier' %> <% if !@summ_sale.nil? %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %> +
+ <% else %> +
+
+
+ <% end %>
<%= t :sale %>
@@ -184,11 +195,17 @@
+ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
<% end %> + <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
+ <% else %> +
+ <% end %>
<%= t :customer %>
@@ -227,10 +244,16 @@
+ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
+ <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
+ <% else %> +
+ <% end %>
<%= t("views.right_panel.detail.order") %>
@@ -274,6 +297,6 @@
+ <% end %>
- <% end %>
\ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index d4dc8005..6d562366 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -71,6 +71,7 @@ en: bottom: "Bottom" payment: "Payment" backend: "Backend" + date_time: "DateTime" views: btn: diff --git a/config/locales/mm.yml b/config/locales/mm.yml index 2d35b1bf..06756c1c 100755 --- a/config/locales/mm.yml +++ b/config/locales/mm.yml @@ -66,6 +66,7 @@ mm: bottom: "အရောင်းအနဲဆုံး" payment: "ငွေပေးချေမှု" backend: "Backend" + date_time: "DateTime" views: btn: From d134e3f67d8393bd4cbd90b3ca04edc300eef98a Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 19 Feb 2018 14:43:34 +0630 Subject: [PATCH 10/31] add survey for waiter --- app/models/sale.rb | 4 ++-- app/views/home/dashboard.html.erb | 10 ++++++++-- app/views/origami/home/show.html.erb | 4 +++- app/views/origami/rooms/show.html.erb | 4 ++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 3c97dbf8..af2df171 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1060,7 +1060,7 @@ end .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) DESC").limit(10) + .order("SUM(i.qty) DESC").limit(20) end def self.bottom_products(today) @@ -1070,7 +1070,7 @@ end .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) ASC").limit(10) + .order("SUM(i.qty) ASC").limit(20) end def self.hourly_sales(today) diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 4056e064..f935522b 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -15,7 +15,6 @@ delimiter = "" end %> - <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
@@ -64,10 +63,17 @@
- <% end %> + +
+
+ +
+
+ +
<% if current_user.role == 'administrator' || current_user.role == 'manager' %>
diff --git a/app/views/origami/home/show.html.erb b/app/views/origami/home/show.html.erb index 6e1f7128..386a142f 100755 --- a/app/views/origami/home/show.html.erb +++ b/app/views/origami/home/show.html.erb @@ -411,8 +411,10 @@ <%= t("views.btn.back") %> - <% if current_login_employee.role != "waiter" %> + <% if current_login_employee.role == "administrator" || current_login_employee.role == "manager" || current_login_employee.role == "supervisor" || current_login_employee.role == "cashier" || current_login_employee.role == "waiter" %> + <% end %> + <% if current_login_employee.role != "waiter" %> <% if @dining.status != "available" %> <% if @status_order == 'order' && @status_sale != 'sale' %> diff --git a/app/views/origami/rooms/show.html.erb b/app/views/origami/rooms/show.html.erb index 17674fff..de774e2b 100755 --- a/app/views/origami/rooms/show.html.erb +++ b/app/views/origami/rooms/show.html.erb @@ -382,7 +382,10 @@ + <% if current_login_employee.role == "administrator" || current_login_employee.role == "manager" || current_login_employee.role == "supervisor" || current_login_employee.role == "cashier" || current_login_employee.role == "waiter" %> + <% end %> + <% if current_login_employee.role != "waiter" %> <% if @room.bookings.length >= 1 %> <% if @status_order == 'order' && @status_sale != 'sale' %> @@ -453,6 +456,7 @@ <% end %> + <% end %>
From 9f2bab7187d4d7d854c1ebfcb886aa7d5377b08c Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 19 Feb 2018 14:49:46 +0630 Subject: [PATCH 11/31] check customer tax profiles in origami --- app/controllers/origami/home_controller.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index 93a83be2..31fea079 100755 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -88,14 +88,17 @@ class Origami::HomeController < BaseOrigamiController end @order_items.push(item) end - accounts = @customer.tax_profiles - puts accounts.to_json - puts "sssssssssss" - puts @customer.tax_profiles - @account_arr =[] - accounts.each do |acc| - account = TaxProfile.find(acc) - @account_arr.push(account) + @account_arr = Array.new + if @customer.tax_profiles + accounts = @customer.tax_profiles + puts accounts.to_json + puts "sssssssssss" + puts @customer.tax_profiles + @account_arr =[] + accounts.each do |acc| + account = TaxProfile.find(acc) + @account_arr.push(account) + end end end end From 521b4d897b82ff19f352bc3da1296f3f72c40ddd Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 19 Feb 2018 15:43:27 +0630 Subject: [PATCH 12/31] change migration file --- app/controllers/application_controller.rb | 10 +++++----- app/controllers/home_controller.rb | 19 +++++++++++++++++++ app/views/home/dashboard.html.erb | 16 +++++++++++++--- db/migrate/20170403155230_create_employees.rb | 1 + db/migrate/20170622050926_create_customers.rb | 1 + .../20170825090115_create_commissioners.rb | 1 + ...80213050551_add_image_path_to_employees.rb | 9 --------- ...80213050631_add_image_path_to_customers.rb | 9 --------- ...3052206_add_image_path_to_commissioners.rb | 9 --------- 9 files changed, 40 insertions(+), 35 deletions(-) delete mode 100644 db/migrate/20180213050551_add_image_path_to_employees.rb delete mode 100644 db/migrate/20180213050631_add_image_path_to_customers.rb delete mode 100644 db/migrate/20180213052206_add_image_path_to_commissioners.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 67f6cb46..eaf89c34 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -38,11 +38,11 @@ class ApplicationController < ActionController::Base end else # check for license file - if check_license - current_license(ENV["SX_PROVISION_URL"]) - else - redirect_to activate_path - end + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index db843aca..99c6d5f6 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -62,6 +62,9 @@ class HomeController < ApplicationController end def dashboard + # puts "date range =>" + # puts get_date_range_from_params + @shop = Shop.first today = DateTime.now.strftime('%Y-%m-%d') @@ -162,4 +165,20 @@ class HomeController < ApplicationController redirect_to reports_dailysale_index_path end end + + def get_date_range_from_params + from = params[:from] + to = params[:to] + + if from.present? && to.present? + f_date = DateTime.parse(from) + t_date = DateTime.parse(to) + f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) + t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) + from = f_time.beginning_of_day.utc.getlocal + to = t_time.end_of_day.utc.getlocal + end + + return from, to + end end diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index f935522b..d1a13775 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -67,11 +67,21 @@ -
-
- +
diff --git a/db/migrate/20170403155230_create_employees.rb b/db/migrate/20170403155230_create_employees.rb index c4bf65ac..4270b265 100755 --- a/db/migrate/20170403155230_create_employees.rb +++ b/db/migrate/20170403155230_create_employees.rb @@ -9,6 +9,7 @@ class CreateEmployees < ActiveRecord::Migration[5.1] t.datetime :session_expiry t.datetime :session_last_login t.string :created_by + t.string :image_path t.timestamps end end diff --git a/db/migrate/20170622050926_create_customers.rb b/db/migrate/20170622050926_create_customers.rb index cb370bc1..b01362fe 100755 --- a/db/migrate/20170622050926_create_customers.rb +++ b/db/migrate/20170622050926_create_customers.rb @@ -18,6 +18,7 @@ class CreateCustomers < ActiveRecord::Migration[5.1] t.string :membership_authentication_code t.string :customer_type, :default => "Dinein" t.json :tax_profiles + t.string :image_path end end end diff --git a/db/migrate/20170825090115_create_commissioners.rb b/db/migrate/20170825090115_create_commissioners.rb index 1794b5c7..a89920d5 100755 --- a/db/migrate/20170825090115_create_commissioners.rb +++ b/db/migrate/20170825090115_create_commissioners.rb @@ -9,6 +9,7 @@ class CreateCommissioners < ActiveRecord::Migration[5.1] t.datetime :joined_date t.datetime :resigned_date t.boolean :is_active, default: true + t.string :image_path t.timestamps end end diff --git a/db/migrate/20180213050551_add_image_path_to_employees.rb b/db/migrate/20180213050551_add_image_path_to_employees.rb deleted file mode 100644 index bb26e888..00000000 --- a/db/migrate/20180213050551_add_image_path_to_employees.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddImagePathToEmployees < ActiveRecord::Migration[5.1] - def change - add_column :employees, :image_path, :string - end - - def down - remove_column :image_path - end -end diff --git a/db/migrate/20180213050631_add_image_path_to_customers.rb b/db/migrate/20180213050631_add_image_path_to_customers.rb deleted file mode 100644 index 11eecdc0..00000000 --- a/db/migrate/20180213050631_add_image_path_to_customers.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddImagePathToCustomers < ActiveRecord::Migration[5.1] - def change - add_column :customers, :image_path, :string - end - - def down - remove_column :image_path - end -end diff --git a/db/migrate/20180213052206_add_image_path_to_commissioners.rb b/db/migrate/20180213052206_add_image_path_to_commissioners.rb deleted file mode 100644 index f61dce61..00000000 --- a/db/migrate/20180213052206_add_image_path_to_commissioners.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddImagePathToCommissioners < ActiveRecord::Migration[5.1] - def change - add_column :commissioners, :image_path, :string - end - - def down - remove_column :image_path - end -end From 51a243ea7cc00d50855e37dce2966929cd328b93 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Tue, 20 Feb 2018 17:35:54 +0630 Subject: [PATCH 13/31] To update close cashier --- app/controllers/application_controller.rb | 14 +- app/controllers/origami/shifts_controller.rb | 3 +- app/models/printer/cashier_station_printer.rb | 4 +- app/models/shift_sale.rb | 7 + ..._pdf.rb => close_cashier_customise_pdf.rb} | 144 ++++++++++++------ 5 files changed, 116 insertions(+), 56 deletions(-) rename app/pdf/{close_cashier_customerize_pdf.rb => close_cashier_customise_pdf.rb} (84%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 505a38b3..0966a573 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,13 +36,13 @@ class ApplicationController < ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - # else - # # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end + else + # check for license file + if check_license + current_license(ENV["SX_PROVISION_URL"]) + else + redirect_to activate_path + end end end diff --git a/app/controllers/origami/shifts_controller.rb b/app/controllers/origami/shifts_controller.rb index 8bb684e9..e05922fd 100755 --- a/app/controllers/origami/shifts_controller.rb +++ b/app/controllers/origami/shifts_controller.rb @@ -75,13 +75,14 @@ class Origami::ShiftsController < BaseOrigamiController @total_member_discount = ShiftSale.get_total_member_discount(@shift) @total_dinein = ShiftSale.get_total_dinein(@shift).total_dinein_amount @total_takeway = ShiftSale.get_total_takeway(@shift).total_takeway_amount + @total_other_charges = ShiftSale.get_total_other_charges(@shift).total_other_charges_amount # get printer info print_settings = PrintSetting.find_by_unique_code(unique_code) printer = Printer::CashierStationPrinter.new(print_settings) - printer.print_close_cashier(print_settings,cashier_terminal,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount,@total_dinein,@total_takeway) + printer.print_close_cashier(print_settings,cashier_terminal,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount,@total_dinein,@total_takeway,@total_other_charges) end end Employee.logout(session[:session_token]) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index 400a0934..b1cab253 100755 --- 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,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway) + def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) #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 = CloseCashierCustomerizePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway) + pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" pdf.render_file filename self.print(filename, cashier_terminal.printer_name) diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index 0127bd5c..10b2de38 100755 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -149,6 +149,13 @@ class ShiftSale < ApplicationRecord .first() end + def self.get_total_other_charges(shift) + query = SaleItem.select("sum(sale_items.qty * sale_items.unit_price) as total_other_charges_amount") + .joins("JOIN sales as s ON s.sale_id = sale_items.sale_id") + .where('shift_sale_id =? and s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null',shift.id) + .first() + end + def self.search(filter,from,to) if filter.blank? keyword = '' diff --git a/app/pdf/close_cashier_customerize_pdf.rb b/app/pdf/close_cashier_customise_pdf.rb similarity index 84% rename from app/pdf/close_cashier_customerize_pdf.rb rename to app/pdf/close_cashier_customise_pdf.rb index 45834489..902fede4 100644 --- a/app/pdf/close_cashier_customerize_pdf.rb +++ b/app/pdf/close_cashier_customise_pdf.rb @@ -1,8 +1,8 @@ -class CloseCashierCustomerizePdf < Prawn::Document +class CloseCashierCustomisePdf < 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,:text_width - def initialize(printer_settings, shift_sale,shop_details,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,total_dinein,total_takeway) + def initialize(printer_settings, shift_sale,shop_details,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,total_dinein,total_takeway,total_other_charges) self.page_width = printer_settings.page_width #PrintSetting.where("name = ?","Close Cashier").first.page_width self.page_height = printer_settings.page_height self.margin = 5 @@ -54,7 +54,7 @@ class CloseCashierCustomerizePdf < Prawn::Document stroke_horizontal_rule - shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,printer_settings.precision,delimiter,total_dinein,total_takeway) + shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,printer_settings.precision,delimiter,total_dinein,total_takeway,total_other_charges) end def header (shop_details) @@ -70,7 +70,7 @@ class CloseCashierCustomerizePdf < Prawn::Document stroke_horizontal_rule end - def shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,precision,delimiter,total_dinein,total_takeway) + def shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,precision,delimiter,total_dinein,total_takeway,total_other_charges) move_down 7 y_position = cursor bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do @@ -159,27 +159,76 @@ class CloseCashierCustomerizePdf < Prawn::Document stroke_horizontal_rule move_down 7 - @total_grand_total = shift_sale.grand_total + @overall + @total_foc + shift_sale.total_void + #start total amount by Account Like Food / Beverage /.. + total_discount_account = 0 + total_discount_by_account.each do |amount| + + total_discount_account = total_discount_account.to_f + amount.total_price.to_f + end + #end total amount by Account + + #start total FOC amount + @total_foc = 0 + other_payment.each do |other| + @total_foc = other.foc_amount.round(2) + end + + #end total FOC amount + total_grand_total = shift_sale.grand_total + shift_sale.total_discounts + @total_foc.to_f + shift_sale.total_void.to_f - total_discount_account.to_f + # @total_grand_total = @shift_sale.grand_total + @overall + @total_foc + @shift_sale.total_void y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Grand Total :", :size => self.item_font_size, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - text "#{number_with_precision(shift_sale.cash_out, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + text "#{number_with_precision(total_grand_total, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total Discount :", :size => self.item_font_size, :align => :right + #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 "#{ number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right + end end - bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - text "#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + #end total amount by Account + + if total_member_discount[0].member_discount.present? + @member_discount = total_member_discount[0].member_discount rescue 0.0 + @overall = shift_sale.total_discounts - @member_discount + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Member 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 "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + else + @overall = shift_sale.total_discounts + end + + if @overall > 0 + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Overall 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 "-#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end end y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Total FOC :", :size => self.item_font_size, :align => :right end + if @total_foc.nil? + @total_foc = 0 + end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "(#{ number_with_precision(@total_foc, :precision => precision.to_i, :delimiter => delimiter)})", :size => self.item_font_size, :align => :right end @@ -248,13 +297,13 @@ class CloseCashierCustomerizePdf < Prawn::Document text "#{ number_with_precision(other.jcb_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(other.paypar_amount, :precision => precision.to_i, :delimiter => delimiter)}", :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 "#{ number_with_precision(other.paypar_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + # end end else y_position = cursor @@ -287,26 +336,32 @@ class CloseCashierCustomerizePdf < Prawn::Document move_down -5 stroke_horizontal_rule move_down 7 - + # start Dinein and Takeaway y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Total Dinein :", :size => self.item_font_size, :align => :right end + if total_dinein.nil? + total_dinein = 0 + end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - text "#{ number_with_precision(total_dinein, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - + text "#{ number_with_precision(total_dinein, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Total Takeaway :", :size => self.item_font_size, :align => :right end + if total_takeway.nil? + total_takeway = 0 + end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "#{ number_with_precision(total_takeway, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - end + + # stop Dinein and Takeaway move_down -5 stroke_horizontal_rule @@ -352,33 +407,18 @@ class CloseCashierCustomerizePdf < Prawn::Document stroke_horizontal_rule move_down 7 #start total amount by Account Like Food / Beverage /.. - total_discount_by_account.each do |amount| + # 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 "#{ number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right - end - end + # 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 "#{ number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right + # end + # end #end total amount by Account - if total_member_discount[0].member_discount.present? - @member_discount = total_member_discount[0].member_discount rescue 0.0 - @overall = shift_sale.total_discounts - @member_discount - - y_position = cursor - bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do - text "Total Member 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 "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - end - else - @overall = shift_sale.total_discounts - end - # y_position = cursor # bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do # text "Grand Total :", :size => self.item_font_size, :align => :right @@ -400,6 +440,18 @@ class CloseCashierCustomerizePdf < Prawn::Document end #end total amount by Account + #start total other charges amount + if total_other_charges > 0 + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Other Charges :", :size => self.item_font_size, :align => :right + end + bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do + text "#{ number_with_precision(total_other_charges, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + end + #end total other charges amount + y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Total Dine-in Count :", :size => self.item_font_size, :align => :right From 84c0a60c0876d8cf081e07396bf898e6f5fe10db Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 21 Feb 2018 09:15:41 +0630 Subject: [PATCH 14/31] add filter for dashboard --- app/controllers/application_controller.rb | 2 - app/controllers/home_controller.rb | 51 +- app/models/sale.rb | 847 ++++++++++++++++------ app/models/stock_journal.rb | 12 +- app/views/home/dashboard.html.erb | 47 +- 5 files changed, 703 insertions(+), 256 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index eaf89c34..e66d7700 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -89,8 +89,6 @@ class ApplicationController < ActionController::Base end def current_user - puts "session" - puts session[:session_token] @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 99c6d5f6..97ac4051 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -62,60 +62,59 @@ class HomeController < ApplicationController end def dashboard - # puts "date range =>" - # puts get_date_range_from_params - + @from, @to = get_date_range_from_params + @shop = Shop.first today = DateTime.now.strftime('%Y-%m-%d') @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() - @top_products = Sale.top_products(today).sum('i.qty') - @bottom_products = Sale.bottom_products(today).sum('i.qty') - @hourly_sales = Sale.hourly_sales(today).sum(:grand_total) + @top_products = Sale.top_products(today,@from,@to).sum('i.qty') + @bottom_products = Sale.bottom_products(today,@from,@to).sum('i.qty') + @hourly_sales = Sale.hourly_sales(today,@from,@to).sum(:grand_total) # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) - @employee_sales = Sale.employee_sales(today) + @employee_sales = Sale.employee_sales(today,@from,@to) .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') - @inventories = StockJournal.inventory_balances(today).sum(:balance) + @inventories = StockJournal.inventory_balances(today,@from,@to).sum(:balance) - @total_sale = Sale.total_sale(today,current_user) - @total_count = Sale.total_count(today,current_user) - @total_card = Sale.total_card_sale(today,current_user) - @total_credit = Sale.credit_payment(today,current_user) + @total_sale = Sale.total_sale(today,current_user,@from,@to) + @total_count = Sale.total_count(today,current_user,@from,@to) + @total_card = Sale.total_card_sale(today,current_user,@from,@to) + @total_credit = Sale.credit_payment(today,current_user,@from,@to) @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today,current_user) + @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to) @total_payment_methods.each do |payment| if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" - pay = Sale.payment_sale('card', today, current_user) + pay = Sale.payment_sale('card', today, current_user,@from,@to) @sale_data.push({'card' => pay.payment_amount}) else - pay = Sale.payment_sale(payment.payment_method, today, current_user) + pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to) @sale_data.push({payment.payment_method => pay.payment_amount}) end end - @summ_sale = Sale.summary_sale_receipt(today,current_user) - @total_customer = Sale.total_customer(today,current_user) - @total_dinein = Sale.total_dinein(today,current_user) - @total_takeaway = Sale.total_takeaway(today,current_user) - @total_other_customer = Sale.total_other_customer(today,current_user) - @total_membership = Sale.total_membership(today,current_user) + @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to) + @total_customer = Sale.total_customer(today,current_user,@from,@to) + @total_dinein = Sale.total_dinein(today,current_user,@from,@to) + @total_takeaway = Sale.total_takeaway(today,current_user,@from,@to) + @total_other_customer = Sale.total_other_customer(today,current_user,@from,@to) + @total_membership = Sale.total_membership(today,current_user,@from,@to) - @total_order = Sale.total_order(today,current_user) - @total_accounts = Sale.total_account(today,current_user) + @total_order = Sale.total_order(today,current_user,@from,@to) + @total_accounts = Sale.total_account(today,current_user,@from,@to) @account_data = Array.new @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today,current_user) + acc = Sale.account_data(account.account_id, today,current_user,@from,@to) if !acc.nil? @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end end - @top_items = Sale.top_items(today,current_user) - @total_foc_items = Sale.total_foc_items(today,current_user) + @top_items = Sale.top_items(today,current_user,@from,@to) + @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to) # get printer info @print_settings = PrintSetting.get_precision_delimiter() diff --git a/app/models/sale.rb b/app/models/sale.rb index af2df171..3e9bbdaa 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1053,193 +1053,376 @@ end return tax end - def self.top_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.top_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) DESC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') .order("SUM(i.qty) DESC").limit(20) + end end - def self.bottom_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.bottom_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) ASC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') .order("SUM(i.qty) ASC").limit(20) + end end - def self.hourly_sales(today) - query= Sale.select("grand_total") + def self.hourly_sales(today,from,to) + if !from.nil? && !to.nil? + query= Sale.select("grand_total") + .where('payment_status="paid" and sale_status = "completed" and receipt_date between ? and ?',from,to) + .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + else + query= Sale.select("grand_total") .where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") .order('receipt_date') + end end - def self.employee_sales(today) - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + def self.employee_sales(today,from,to) + if !from.nil? && !to.nil? + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") + .where("sales.payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between '#{from}' and '#{to}'") + .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") + .order("e.name") + else + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") + end end - def self.total_sale(today,current_user=nil) - if current_user.nil? - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + def self.total_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and sales.receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .sum("grand_total") + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - .sum("grand_total") + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .sum("grand_total") + end end end end end - def self.total_count(today,current_user=nil) - if current_user.nil? - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + def self.total_count(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id = ?',from,to,shift.id).count + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count + end end end end end - def self.total_card_sale(today,current_user=nil) - if current_user.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + def self.total_card_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',today,shift.id) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end end end end end - def self.credit_payment(today,current_user=nil) - if current_user.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + def self.credit_payment(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ? and s.shift_sale_id=?',from,to,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end end end end end - def self.summary_sale_receipt(today,current_user=nil) - if current_user.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - .first() - else - if current_user.role == 'administrator' + def self.summary_sale_receipt(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - .first() - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_payment_methods(today,current_user=nil) - if current_user.nil? - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + def self.total_payment_methods(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.select("distinct sp.payment_method") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end end end end end - def self.payment_sale(payment_method, today, current_user=nil) - if current_user.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() - else - if current_user.role == 'administrator' + def self.payment_sale(payment_method, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',today,shift.id) + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) end query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',from,to,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end + else + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',today,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end end end end end - def self.total_customer(today,current_user=nil) - dinein_cnt = self.total_dinein(today,current_user) - takeaway_cnt = self.total_takeaway(today,current_user) - membership_cnt = self.total_membership(today,current_user) + def self.total_customer(today,current_user=nil,from,to) + dinein_cnt = self.total_dinein(today,current_user,from,to) + takeaway_cnt = self.total_takeaway(today,current_user,from,to) + membership_cnt = self.total_membership(today,current_user,from,to) total_cus = 0 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1249,187 +1432,379 @@ end return total_cus end - def self.total_dinein(today,current_user=nil) - if current_user.nil? - query = Sale.select("count(sales.customer_id) as total_dinein_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) - .first() + def self.total_dinein(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.select("count(sales.customer_id) as total_dinein_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("count(sales.customer_id) as total_dinein_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_takeaway(today,current_user=nil) - if current_user.nil? - query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) - .first() - else - if current_user.role == 'administrator' + def self.total_takeaway(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) - .first() + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) - .first() + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_membership(today,current_user=nil) - if current_user.nil? - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() - else - if current_user.role == 'administrator' + def self.total_membership(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) + .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_other_customer(today,current_user=nil) - if current_user.nil? - query = Sale.select("count(sales.customer_id) as total_cus") + def self.total_other_customer(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) .first() - else - if current_user.role == 'administrator' - query = Sale.select("count(sales.customer_id) as total_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) - .first() - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = Sale.select("count(sales.customer_id) as total_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) - .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_order(today,current_user=nil) - if current_user.nil? - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .first() - else - if current_user.role == 'administrator' + def self.total_order(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .first() + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) + .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("count(distinct a.order_id) as total_order") .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .first() + end end end end end - def self.total_account(today,current_user=nil) - if current_user.nil? - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + def self.total_account(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.select("distinct b.id as account_id, b.title as title") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .joins("JOIN accounts as b ON b.id = a.account_id") .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + end end end end end - def self.account_data(account_id, today, current_user=nil) - if current_user.nil? - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .first() + def self.account_data(account_id, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .first() + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - .first() + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .first() + end end end end end - def self.top_items(today,current_user=nil) - if current_user.nil? - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + def self.top_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) @@ -1437,35 +1812,65 @@ end .order("SUM(a.qty) DESC") .first() else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end end end end end - def self.total_foc_items(today,current_user=nil) - if current_user.nil? - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .sum("a.qty") + def self.total_foc_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .sum("a.qty") + end + end + end else - if current_user.role == 'administrator' + if current_user.nil? query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .sum("a.qty") else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? + if current_user.role == 'administrator' query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .sum("a.qty") + end end end end diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 35bebca2..c2a12f4a 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -36,12 +36,20 @@ class StockJournal < ApplicationRecord journal.save end - def self.inventory_balances(today) - query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") + def self.inventory_balances(today,from,to) + if !from.nil? && !to.nil? + query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") + .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") + .where("stock_journals.created_at between '#{from}' and '#{to}'") + .group("mii.item_instance_name") + .order("mii.item_instance_name ASC") + else + query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") .where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'") .group("mii.item_instance_name") .order("mii.item_instance_name ASC") + end end end diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index d1a13775..85fe9630 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -67,21 +67,23 @@ - +
@@ -315,4 +317,39 @@
<% end %>
-
\ No newline at end of file +
+ + \ No newline at end of file From 8c0f379a4fad6034f32797f1a111526a4b91abb0 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Wed, 21 Feb 2018 14:39:33 +0630 Subject: [PATCH 15/31] Edit Close Cashier Customise Pdf --- app/controllers/application_controller.rb | 14 ++--- app/pdf/close_cashier_customise_pdf.rb | 62 +++++++++++++---------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0966a573..505a38b3 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,13 +36,13 @@ class ApplicationController < ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - else - # check for license file - if check_license - current_license(ENV["SX_PROVISION_URL"]) - else - redirect_to activate_path - end + # else + # # check for license file + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end end end diff --git a/app/pdf/close_cashier_customise_pdf.rb b/app/pdf/close_cashier_customise_pdf.rb index 902fede4..cd9d7b3b 100644 --- a/app/pdf/close_cashier_customise_pdf.rb +++ b/app/pdf/close_cashier_customise_pdf.rb @@ -174,7 +174,7 @@ class CloseCashierCustomisePdf < Prawn::Document end #end total FOC amount - total_grand_total = shift_sale.grand_total + shift_sale.total_discounts + @total_foc.to_f + shift_sale.total_void.to_f - total_discount_account.to_f + total_grand_total = shift_sale.grand_total + @total_foc.to_f + shift_sale.total_void.to_f - total_discount_account.to_f # @total_grand_total = @shift_sale.grand_total + @overall + @total_foc + @shift_sale.total_void y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do @@ -197,31 +197,6 @@ class CloseCashierCustomisePdf < Prawn::Document end #end total amount by Account - if total_member_discount[0].member_discount.present? - @member_discount = total_member_discount[0].member_discount rescue 0.0 - @overall = shift_sale.total_discounts - @member_discount - - y_position = cursor - bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do - text "Total Member 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 "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - end - else - @overall = shift_sale.total_discounts - end - - if @overall > 0 - y_position = cursor - bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do - text "Total Overall 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 "-#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - end - end - y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Total FOC :", :size => self.item_font_size, :align => :right @@ -452,6 +427,41 @@ class CloseCashierCustomisePdf < Prawn::Document end #end total other charges amount + move_down -5 + stroke_horizontal_rule + move_down 7 + + #start total over all discount + if total_member_discount[0].member_discount.present? + @member_discount = total_member_discount[0].member_discount rescue 0.0 + @overall = shift_sale.total_discounts - @member_discount + + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Member 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 "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + else + @overall = shift_sale.total_discounts + end + + if @overall > 0 + y_position = cursor + bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do + text "Total Overall 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 "-#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right + end + end + #end total over all discount + + 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 "Total Dine-in Count :", :size => self.item_font_size, :align => :right From f93f7a0a2c9e397a8320e2a3418ea013fb712ab1 Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Wed, 21 Feb 2018 14:42:09 +0630 Subject: [PATCH 16/31] Add License --- app/controllers/application_controller.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 505a38b3..0966a573 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,13 +36,13 @@ class ApplicationController < ActionController::Base # redirect_to root_url(:host => request.domain) + "store_error" render :json => [{ status: false, message: 'Invalid Access!'}] end - # else - # # check for license file - # if check_license - # current_license(ENV["SX_PROVISION_URL"]) - # else - # redirect_to activate_path - # end + else + # check for license file + if check_license + current_license(ENV["SX_PROVISION_URL"]) + else + redirect_to activate_path + end end end From 2cb13f9a6294224a7a5b29726343be5e820e12d9 Mon Sep 17 00:00:00 2001 From: phyusin Date: Thu, 22 Feb 2018 10:10:20 +0630 Subject: [PATCH 17/31] change dashboard query --- app/models/sale.rb | 4 ++-- app/views/home/dashboard.html.erb | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 6dea5db3..2265a135 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1375,7 +1375,7 @@ end if payment_method == 'card' query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) end query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else @@ -1384,7 +1384,7 @@ end if payment_method == 'card' query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) end query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() else diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 85fe9630..91cce7e3 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -165,19 +165,19 @@ <%= t("views.right_panel.detail.total") %> <%= t :sale %> : - <%= number_with_precision( @summ_sale.total_amount, precision: precision.to_i ,delimiter: delimiter) %> + <%= number_with_precision( @summ_sale.total_amount, precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.discount") %> : - <%= number_with_precision( @summ_sale.total_discount, precision: precision.to_i ,delimiter: delimiter) %> + <%= number_with_precision( @summ_sale.total_discount, precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.tax") %> : - <%= number_with_precision( @summ_sale.total_tax , precision: precision.to_i ,delimiter: delimiter)%> + <%= number_with_precision( @summ_sale.total_tax , precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.grand_total") %> : - <%= number_with_precision( @summ_sale.grand_total , precision: precision.to_i ,delimiter: delimiter)%> + <%= number_with_precision( @summ_sale.grand_total , precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> @@ -190,7 +190,7 @@ @@ -203,7 +203,7 @@ <% end %> From 5dac52160538564728fb207ab54560b8aedb410e Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Thu, 22 Feb 2018 18:26:46 +0630 Subject: [PATCH 18/31] Correct Redeem in close_cashier_pdf.rb --- app/pdf/close_cashier_pdf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/pdf/close_cashier_pdf.rb b/app/pdf/close_cashier_pdf.rb index 7bee4ed3..412cb9a4 100755 --- a/app/pdf/close_cashier_pdf.rb +++ b/app/pdf/close_cashier_pdf.rb @@ -213,7 +213,7 @@ class CloseCashierPdf < Prawn::Document 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 + text "Redeem 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 "#{ number_with_precision(other.paypar_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right From 23548dffa63441d3dac638b311b93ea14b221654 Mon Sep 17 00:00:00 2001 From: phyusin Date: Fri, 23 Feb 2018 11:34:53 +0630 Subject: [PATCH 19/31] check report error precision --- README.md | 25 ++++++++++ .../reports/saleitem_controller.rb | 2 + .../reports/stock_check_controller.rb | 2 +- app/views/reports/dailysale/index.html.erb | 48 +++++++++---------- app/views/reports/saleitem/index.html.erb | 32 ++++++++----- app/views/reports/shiftsale/index.html.erb | 20 ++++---- app/views/reports/stock_check/index.html.erb | 16 ++++++- 7 files changed, 95 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 97f43595..8ec595bb 100755 --- a/README.md +++ b/README.md @@ -108,6 +108,31 @@ sudo cat /etc/sudoers ### cope and parse %superuser ALL=(ALL) NOPASSWD: ALL +<---- Extra Fields Script -----> +DROP TABLE IF EXISTS `display_images`; +CREATE TABLE `display_images` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `shop_id` int(11) DEFAULT NULL, + `image` blob, + `created_by` varchar(255) DEFAULT NULL, + `created_at` datetime NOT NULL, + `updated_at` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +ALTER TABLE customers +ADD COLUMN image_path VARCHAR(255); + +ALTER TABLE employees +ADD COLUMN image_path VARCHAR(255); + +ALTER TABLE commissioners +ADD COLUMN image_path VARCHAR(255); + +ALTER TABLE sales +ADD COLUMN equal_persons INT(11) after rebate_status; +<---- Extra Fields Script -----> + * ToDo list 1. Migration diff --git a/app/controllers/reports/saleitem_controller.rb b/app/controllers/reports/saleitem_controller.rb index a9d23797..4e337e30 100755 --- a/app/controllers/reports/saleitem_controller.rb +++ b/app/controllers/reports/saleitem_controller.rb @@ -34,6 +34,8 @@ class Reports::SaleitemController < BaseReportController @from = from @to = to + puts "foc_data" + puts @foc_data # get printer info @print_settings = PrintSetting.get_precision_delimiter() diff --git a/app/controllers/reports/stock_check_controller.rb b/app/controllers/reports/stock_check_controller.rb index a9188f5d..10eab5a3 100755 --- a/app/controllers/reports/stock_check_controller.rb +++ b/app/controllers/reports/stock_check_controller.rb @@ -17,7 +17,7 @@ class Reports::StockCheckController < BaseReportController @from = from_date @to = to_date # get printer info - @print_settings = PrintSetting.get_precision_delimiter() + @print_settings = PrintSetting.get_precision_delimiter() respond_to do |format| format.html format.xls diff --git a/app/views/reports/dailysale/index.html.erb b/app/views/reports/dailysale/index.html.erb index 76438060..6dd1ebac 100755 --- a/app/views/reports/dailysale/index.html.erb +++ b/app/views/reports/dailysale/index.html.erb @@ -91,37 +91,37 @@ - - - - - - - - - + + + + + + + + + - - - + + + <% count = count + 1 %> <% end %> - - + + - - - - + + + + - - - - + + + + <% total_tax = 0 %> @@ -130,7 +130,7 @@ <% total_tax += tax.tax_amount.to_f %> - + @@ -138,7 +138,7 @@ <% net = total - total_tax %> - + <% end %> diff --git a/app/views/reports/saleitem/index.html.erb b/app/views/reports/saleitem/index.html.erb index 387a97a5..3c03dec2 100644 --- a/app/views/reports/saleitem/index.html.erb +++ b/app/views/reports/saleitem/index.html.erb @@ -101,7 +101,7 @@ - - + + @@ -140,7 +140,7 @@ - + <% sub_total = 0.0%> <% sub_qty = 0 %> @@ -168,8 +168,8 @@ - - + + @@ -179,7 +179,7 @@ - + <%end%> @@ -188,29 +188,35 @@ - + <% end %> - + - + - + - + <% @sale_taxes.each do |tax| %> @@ -226,7 +232,7 @@ - + - - + + - + - + <% grand_total = result[:grand_total].to_f %> @@ -126,15 +126,15 @@ - - + + - - + + - +
<% @sale_data.each do |data| %> <% pay_mth = payment.payment_method %> - <%= data[""+pay_mth+""] %> + <%= number_with_precision(data[""+pay_mth+""], precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <% end %>
<%= t("views.right_panel.detail.card_sale") %> : - <%= total_card["card"].to_f %> + <%= number_with_precision(total_card["card"], precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %>
<%= count %> <%= sale[:sale_date].strftime("#{sale[:sale_date].day.ordinalize} %b") rescue '-' %><%= number_with_precision(sale[:void_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:mpu_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:master_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:visa_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:jcb_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:paypar_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:cash_amount]-sale[:total_change_amount], precision:precision, delimiter: delimiter) rescue '-'%><%= number_with_precision(sale[:credit_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:foc_amount], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:void_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:mpu_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:master_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:visa_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:jcb_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:paypar_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:cash_amount]-sale[:total_change_amount], precision:precision.to_i, delimiter: delimiter) rescue '-'%><%= number_with_precision(sale[:credit_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:foc_amount], precision:precision.to_i,delimiter:delimiter) rescue '-'%> (<%= number_with_precision(sale[:total_discount], precision:precision,delimiter:delimiter) rescue '-'%>)<%= number_with_precision(sale[:grand_total].to_f + sale[:rounding_adj].to_f , precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:rounding_adj].to_f, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:grand_total], precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:grand_total].to_f + sale[:rounding_adj].to_f , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:rounding_adj].to_f, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale[:grand_total], precision:precision.to_i,delimiter:delimiter) rescue '-'%>
Total<%= number_with_precision(mpu , precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(master, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(mpu , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(master, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(visa, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(jcb, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(paypar, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(cash, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(credit, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(jcb, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(paypar, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(cash, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(credit, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(foc, precision:precision,delimiter:delimiter) rescue '-'%>(<%= number_with_precision(discount, precision:precision,delimiter:delimiter) rescue '-'%>)<%= number_with_precision(total, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(rounding_adj, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(grand_total, precision:precision,delimiter:delimiter) rescue '-'%>(<%= number_with_precision(discount, precision:precision.to_i,delimiter:delimiter) rescue '-'%>)<%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(rounding_adj, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(grand_total, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
<%= tax.tax_name rescue '-'%><%= number_with_precision(tax.tax_amount, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(tax.tax_amount, precision:precision.to_i,delimiter:delimiter) rescue '-'%>  
<%= t("views.right_panel.detail.net_amount") %><%= number_with_precision(net, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(net, precision:precision.to_i,delimiter:delimiter) rescue '-'%>  
<% @totalByAccount.each do |account, total| %> <% if sale.account_id == account %> - <%= number_with_precision(total, precision:precision,delimiter:delimiter) %> + <%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) %> <% grand_total += total %> <% end %> <% end %> @@ -120,8 +120,8 @@ <%= sale.item_code rescue '-' %> <%= sale.product_name rescue '-' %> <%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
Total <%= sale.account_name %> Qty <%= sub_qty %> <%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(sub_total , precision:precision,delimiter:delimiter)%> <%= number_with_precision(sub_total , precision:precision.to_i,delimiter:delimiter)%>
<%= other.item_code rescue '-' %> <%= other.product_name rescue '-' %> <%= other.total_item rescue '-' %> <%= number_with_precision(other.unit_price , precision:precision,delimiter:delimiter) rescue '-'%> <%= number_with_precision(other.grand_total , precision:precision,delimiter:delimiter) rescue '-'%> <%= number_with_precision(other.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(other.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
  <%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(other_sub_total , precision:precision,delimiter:delimiter)%><%= number_with_precision(other_sub_total , precision:precision.to_i,delimiter:delimiter)%>
<%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %> <%= total_qty%> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(grand_total , precision:precision,delimiter:delimiter)%><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter)%>
  <%= t("views.right_panel.detail.foc_item") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(total_item_foc , precision:precision,delimiter:delimiter) %><%= number_with_precision(total_item_foc , precision:precision.to_i,delimiter:delimiter) %>
  <%= t("views.right_panel.detail.item_discount") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(total_item_dis , precision:precision,delimiter:delimiter) %><%= number_with_precision(total_item_dis , precision:precision.to_i,delimiter:delimiter) %>
  <%= t("views.right_panel.detail.foc_sales") %><%= number_with_precision(@foc_data , precision:precision,delimiter:delimiter) %> + <%= number_with_precision(@foc_data, precision:precision.to_i, delimiter:delimiter) %> +
  <%= t("views.right_panel.detail.discount") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(@discount_data , precision:precision,delimiter:delimiter) %> + + <%= number_with_precision(@discount_data , precision: precision.to_i,delimiter: delimiter) %> + +
  Net Amount <%= number_with_precision(grand_total.to_f - @discount_data.to_f , precision:precision,delimiter:delimiter)%><%= number_with_precision(grand_total.to_f - @discount_data.to_f , precision:precision.to_i,delimiter:delimiter)%>
<%= number_with_precision(result[:cash_sales].to_f, precision:precision,delimiter:delimiter) %><%= number_with_precision(result[:credit_sales].to_f, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(result[:cash_sales].to_f, precision:precision.to_i,delimiter:delimiter) %><%= number_with_precision(result[:credit_sales].to_f, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(result[:other_sales].to_f, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(result[:other_sales].to_f, precision:precision.to_i,delimiter:delimiter) rescue '-'%> - <%= number_with_precision(result[:foc_sales].to_f, precision:precision,delimiter:delimiter) rescue '-'%> + <%= number_with_precision(result[:foc_sales].to_f, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(result[:grand_total].to_f, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(result[:grand_total].to_f, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
<%= number_with_precision(cash, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(credit, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(cash, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(credit, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(card, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(foc, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(card, precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(foc, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(g_total, precision:precision,delimiter:delimiter) rescue '-'%><%= number_with_precision(g_total, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
diff --git a/app/views/reports/stock_check/index.html.erb b/app/views/reports/stock_check/index.html.erb index 1305abe5..e470a294 100755 --- a/app/views/reports/stock_check/index.html.erb +++ b/app/views/reports/stock_check/index.html.erb @@ -28,6 +28,18 @@
+ <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end + %> @@ -63,7 +75,7 @@ <% end %> - + @@ -76,7 +88,7 @@ - + From 7c171057d1fc0e05d9062b1aa32ebbab01acfad6 Mon Sep 17 00:00:00 2001 From: phyusin Date: Fri, 23 Feb 2018 14:03:25 +0630 Subject: [PATCH 20/31] product sale --- .../javascripts/reports/product_sale.coffee | 3 + .../stylesheets/reports/product_sale.scss | 3 + .../reports/product_sale_controller.rb | 80 +++++ .../reports/saleitem_controller.rb | 4 +- app/helpers/reports/product_sale_helper.rb | 2 + app/models/sale.rb | 47 ++- app/views/layouts/_left_sidebar.html.erb | 3 + .../_shift_sale_report_filter.html.erb | 120 +++++++ app/views/reports/product_sale/index.html.erb | 292 ++++++++++++++++++ app/views/reports/product_sale/index.xls.erb | 175 +++++++++++ config/routes.rb | 1 + .../reports/product_sale_controller_spec.rb | 5 + .../reports/product_sale_helper_spec.rb | 15 + 13 files changed, 746 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/reports/product_sale.coffee create mode 100644 app/assets/stylesheets/reports/product_sale.scss create mode 100644 app/controllers/reports/product_sale_controller.rb create mode 100644 app/helpers/reports/product_sale_helper.rb create mode 100755 app/views/reports/product_sale/_shift_sale_report_filter.html.erb create mode 100644 app/views/reports/product_sale/index.html.erb create mode 100755 app/views/reports/product_sale/index.xls.erb create mode 100644 spec/controllers/reports/product_sale_controller_spec.rb create mode 100644 spec/helpers/reports/product_sale_helper_spec.rb diff --git a/app/assets/javascripts/reports/product_sale.coffee b/app/assets/javascripts/reports/product_sale.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/reports/product_sale.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/reports/product_sale.scss b/app/assets/stylesheets/reports/product_sale.scss new file mode 100644 index 00000000..cb0b9a13 --- /dev/null +++ b/app/assets/stylesheets/reports/product_sale.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the reports/ProductSale controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb new file mode 100644 index 00000000..0061818a --- /dev/null +++ b/app/controllers/reports/product_sale_controller.rb @@ -0,0 +1,80 @@ +class Reports::ProductSaleController < BaseReportController + authorize_resource :class => false + + def index + from, to = get_date_range_from_params + shift_sale_range = Sale.get_by_shift_sale(from,to,Sale::SALE_STATUS_COMPLETED) + + shift = '' + if params[:shift_name].to_i != 0 + shift_sale = ShiftSale.find(params[:shift_name]) + if to.blank? + shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL ',shift_sale.shift_started_at) + else + + shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',shift_sale.shift_started_at, shift_sale.shift_closed_at) + end + end + @sale_data, @grand_total = Sale.get_by_items(shift_sale_range,shift, from, to) + + @sale_taxes = Sale.get_separate_tax(shift_sale_range,shift,from,to,nil) + + @account_cate_count = Hash.new {|hash, key| hash[key] = 0} + + + @sale_data.each {|acc_cate| @account_cate_count[acc_cate.account_id] += 1} + + @menu_cate_count = Hash.new {|hash, key| hash[key] = 0} + @sale_data.each {|cate| @menu_cate_count[cate.account_id] += 1} + + + @totalByAccount = Hash.new {|hash, key| hash[key] = 0} + @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} + + @from = from + @to = to + + # get printer info + @print_settings = PrintSetting.get_precision_delimiter() + + if shift.present? + shift.each do |sh| + @shift_from = sh.shift_started_at.nil? ? '-' : sh.shift_started_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_to = sh.shift_closed_at.nil? ? '-' : sh.shift_closed_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_data = sh + end + end + + respond_to do |format| + format.html + format.xls + end + end + + # def show + # from, to, report_type = get_date_range_from_params + + # @sale_data = Sale.get_by_shift_sale(from,to,Sale::SALE_STATUS_COMPLETED) + + # date_arr = Array.new + # @sale_data.each do |sale| + # local_opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc.getlocal.strftime("%e %b %I:%M%p") + # local_closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc.getlocal.strftime("%e %b %I:%M%p") + # opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc + # closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc + # shift_id = sale.id.nil? ? '-' : sale.id + # str = {:shift_id => shift_id, :local_opening_date => local_opening_date, :local_closing_date => local_closing_date, :opening_date => opening_date, :closing_date => closing_date} + # date_arr.push(str) + # end + + # @totalByAccount = Hash.new {|hash, key| hash[key] = 0} + # @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} + + # out = {:status => 'ok', :message => date_arr} + + # respond_to do |format| + # format.json { render json: out } + # end + # end + +end diff --git a/app/controllers/reports/saleitem_controller.rb b/app/controllers/reports/saleitem_controller.rb index 4e337e30..06124265 100755 --- a/app/controllers/reports/saleitem_controller.rb +++ b/app/controllers/reports/saleitem_controller.rb @@ -33,9 +33,7 @@ class Reports::SaleitemController < BaseReportController @from = from @to = to - - puts "foc_data" - puts @foc_data + # get printer info @print_settings = PrintSetting.get_precision_delimiter() diff --git a/app/helpers/reports/product_sale_helper.rb b/app/helpers/reports/product_sale_helper.rb new file mode 100644 index 00000000..103bd624 --- /dev/null +++ b/app/helpers/reports/product_sale_helper.rb @@ -0,0 +1,2 @@ +module Reports::ProductSaleHelper +end diff --git a/app/models/sale.rb b/app/models/sale.rb index 2265a135..cd896f5f 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -785,7 +785,6 @@ end def self.get_by_shift_items(shift_sale_range, shift, from, to, status) # date_type_selection = get_sql_function_for_report_type(report_type) - query = self.get_item_query() discount_query = 0 @@ -861,6 +860,52 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) return query,other_charges, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount end +#product sale report query +def self.get_product_item_query() + query = Sale.select("acc.title as account_name,mi.account_id, i.item_instance_code as item_code,i.account_id as account_id, " + + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item,i.qty as qty," + + "i.remark as status_type,"+ + " i.unit_price,i.price as price,i.product_name as product_name, mc.name as" + + " menu_category_name,mc.id as menu_category_id ") + + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + + " JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + + " JOIN menu_items mi ON mi.id = mii.menu_item_id" + + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + + " JOIN menu_categories mc ON mc.id = mi.menu_category_id ") + # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") + query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") + # query = query.where("i.item_instance_code IS NOT NULL") + query = query.group("acc.title,mi.account_id,mi.menu_category_id,mi.name,mii.price") + .order("acc.title desc, mi.account_id desc, mi.menu_category_id desc, mii.price asc") +end + +def self.get_by_items(shift_sale_range, shift, from, to) + query = self.get_product_item_query() + + puts "query" + total_grand_total = 0 + + # if shift.present? + # query = query.where("sales.shift_sale_id IN (?)",shift.to_a) + + # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f + + # ### => get all sales range in shift_sales + # elsif shift_sale_range.present? + # query = query.where("sales.shift_sale_id IN (?)",shift_sale_range.to_a) + + # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f + + # else + # query = query.where("sales.receipt_date between ? and ?",from,to) + # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f + # end + + return query, total_grand_total +end +#product sale report query + def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type) ## => left join -> show all sales although no orders if payment_type.blank? diff --git a/app/views/layouts/_left_sidebar.html.erb b/app/views/layouts/_left_sidebar.html.erb index 9bffd4d7..de42e719 100755 --- a/app/views/layouts/_left_sidebar.html.erb +++ b/app/views/layouts/_left_sidebar.html.erb @@ -122,6 +122,9 @@
  • Sale Items
  • +
  • + Product Sale +
  • Receipt
  • diff --git a/app/views/reports/product_sale/_shift_sale_report_filter.html.erb b/app/views/reports/product_sale/_shift_sale_report_filter.html.erb new file mode 100755 index 00000000..88f26c1f --- /dev/null +++ b/app/views/reports/product_sale/_shift_sale_report_filter.html.erb @@ -0,0 +1,120 @@ +
    + <%= form_tag report_path, :method => :get, :id=>"frm_report", :class => "form" do %> + <% if period_type != false %> +
    +
    + + +
    + +
    + + + +
    +
    + + +
    +
    + + +
    +
    +
    + +
    +
    + <% end %> + + <% end %> +
    + + diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb new file mode 100644 index 00000000..12e1dfe1 --- /dev/null +++ b/app/views/reports/product_sale/index.html.erb @@ -0,0 +1,292 @@ + +
    +
    + <%= render :partial=>'shift_sale_report_filter', + :locals=>{ :period_type => true, :shift_name => true, :report_path =>reports_product_sale_index_path} %> + +
    + + + +
    +
    +
    +
    <%= result.stock_count rescue '-' %><%= result.stock_balance rescue '-' %><%= number_with_precision(result.stock_balance, precision:precision.to_i,delimiter:delimiter) rescue '-' %> <%= result.different rescue '-' %> <%= result.remark rescue '-' %> <%= result.created_at.strftime('%e %b %Y %I:%M %p') rescue '-' %>
    <%= total_stock_count rescue '-' %><%= total_stock_balance rescue '-' %><%= number_with_precision(total_stock_balance, precision:precision.to_i,delimiter:delimiter) rescue '-' %> <%= total_different rescue '-' %>
    + + + + + <% if @shift_from %> + + <% if @shift_data.employee %> + <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> + <% end %> + + + <% end %> + + + + + + + + + + + + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end + %> + <% unless @sale_data.blank? %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + + <% sub_qty = 0 %> + <% sub_total = 0 %> + <% other_sub_total = 0 %> + <% count = 0 %> + <% total_price = 0 %> + <% cate_count = 0 %> + <% acc_count = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> + <% total_amount = 0 %> + <% discount = 0 %> + <% total_item_foc = 0 %> + <% total_item_dis = 0.0 %> + <% total_tax = 0 %> + + <% @sale_data.each do |sale| %> + + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + end %> + + <% if sale.status_type == "foc" && sale.grand_total < 0 + total_item_foc += sale.grand_total*(-1) + end %> + + <% if sale.status_type == "Discount" && sale.grand_total < 0 + total_item_dis += sale.grand_total*(-1) + end %> + + <% if !acc_arr.include?(sale.account_id) %> + + + + + + + <% acc_arr.push(sale.account_id) %> + <% end %> + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + + + + + <% @menu_cate_count.each do |key,value| %> + <% if sale.account_id == key %> + <% count = count + 1 %> + <% sub_total += sale.grand_total %> + <% #sub_qty += sale.total_item %> + <% if sale.status_type!="Discount" && (!sale.product_name.include? "FOC") + sub_qty += sale.total_item + end %> + + <% if count == value %> + + + + + + + + <% sub_total = 0.0%> + <% sub_qty = 0 %> + <% count = 0%> + <% end %> + <% end %> + <% end %> + + <% end %> + + + + + + + + + <% end %> + + + + + + + + +
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> + <% @totalByAccount.each do |account, total| %> + <% if sale.account_id == account %> + <%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) %> + <% grand_total += total %> + <% end %> + <% end %> +
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total <%= sale.account_name %> Qty <%= sub_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(sub_total , precision:precision.to_i,delimiter:delimiter)%>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter)%>
     Net Amount<%= number_with_precision(grand_total.to_f - @discount_data.to_f , precision:precision.to_i,delimiter:delimiter)%>
    +
    +
    +
    +
    +
    + + \ No newline at end of file diff --git a/app/views/reports/product_sale/index.xls.erb b/app/views/reports/product_sale/index.xls.erb new file mode 100755 index 00000000..f97edbb8 --- /dev/null +++ b/app/views/reports/product_sale/index.xls.erb @@ -0,0 +1,175 @@ +
    +
    +
    +
    +
    + + + + + + <% if @shift_from %> + + <% if @shift_data.employee %> + <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> + <% end %> + + + <% end %> + + + + + + + + + + + + <% unless @sale_data.blank? %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + + <% sub_total = 0 %> + <% other_sub_total = 0 %> + <% count = 0 %> + <% total_price = 0 %> + <% cate_count = 0 %> + <% acc_count = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> + <% total_amount = 0 %> + <% discount = 0 %> + <% total_item_foc = 0 %> + <% total_item_dis = 0 %> + + <% @sale_data.each do |sale| %> + + <% if sale.total_item > 0 + total_qty += sale.total_item + end %> + + <% if !acc_arr.include?(sale.account_id) %> + + + + + + + <% acc_arr.push(sale.account_id) %> + <% end %> + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + + + + <% @menu_cate_count.each do |key,value| %> + <% if sale.menu_category_id == key %> + + <% count = count + 1 %> + <% sub_total += sale.grand_total %> + <% if count == value %> + + + + + + <% if sale.status_type === "foc" + total_item_foc += sale.grand_total + end %> + + <% if sale.status_type === "Discount" + total_item_dis += sale.grand_total + end %> + <% sub_total = 0.0%> + <% count = 0%> + <% end %> + <% end %> + <% end %> + + <% end %> + + + + + + + <% @other_charges.each do |other| %> + <% if other.total_item > 0 + total_qty += other.total_item + end %> + <% grand_total +=other.grand_total%> + + + + + + + + + + + + <% other_sub_total += other.grand_total %> + + <% end %> + + + + + + + + + + + + + + <% end %> + + + + + + + + + + + + + + + + + + + + + + +
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> + <% @totalByAccount.each do |account, total| %> + <% if sale.account_id == account %> + <%= total %> + <% grand_total += total %> + <% end %> + <% end %> +
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.unit_price rescue '-' %><%= sale.grand_total rescue '-' %>
     <%= t("views.right_panel.detail.sub_total") %><%= sub_total %>
    Other Charges 
     Other Charges<%= other.item_code rescue '-' %><%= other.product_name rescue '-' %><%= other.total_item rescue '-' %><%= other.unit_price rescue '-' %><%= other.grand_total rescue '-' %>
     <%= t("views.right_panel.detail.sub_total") %><%= other_sub_total %>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.net_amount") %><%= grand_total%>
     <%= t("views.right_panel.detail.foc_item") %> <%= t("views.right_panel.detail.amount") %><%= total_item_foc %>
     <%= t("views.right_panel.detail.item_discount") %> <%= t("views.right_panel.detail.amount") %><%= total_item_dis %>
     <%= t("views.right_panel.detail.foc_sales") %><%= @foc_data %>
     <%= t("views.right_panel.detail.discount") %> <%= t("views.right_panel.detail.amount") %><%= @discount_data %>
    +
    +
    +
    +
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7d21aeb5..a189da13 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -373,6 +373,7 @@ scope "(:locale)", locale: /en|mm/ do resources :commission, :only => [:index, :show] resources :stock_check, :only => [:index, :show] resources :payment_method + resources :product_sale, :only => [:index, :show] get "receipt_no/get_shift_by_date", to: "receipt_no#get_shift_by_date", as: "get_shift_by_date" end diff --git a/spec/controllers/reports/product_sale_controller_spec.rb b/spec/controllers/reports/product_sale_controller_spec.rb new file mode 100644 index 00000000..1f87b3bc --- /dev/null +++ b/spec/controllers/reports/product_sale_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Reports::ProductSaleController, type: :controller do + +end diff --git a/spec/helpers/reports/product_sale_helper_spec.rb b/spec/helpers/reports/product_sale_helper_spec.rb new file mode 100644 index 00000000..17d29e63 --- /dev/null +++ b/spec/helpers/reports/product_sale_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Reports::ProductSaleHelper. For example: +# +# describe Reports::ProductSaleHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Reports::ProductSaleHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From 970a9bb430968f9bf0e343e950a9281deb90359a Mon Sep 17 00:00:00 2001 From: phyusin Date: Fri, 23 Feb 2018 15:26:24 +0630 Subject: [PATCH 21/31] sale --- app/models/sale.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index cd896f5f..54806964 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -861,27 +861,24 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) end #product sale report query -def self.get_product_item_query() - query = Sale.select("acc.title as account_name,mi.account_id, i.item_instance_code as item_code,i.account_id as account_id, " + - "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item,i.qty as qty," + - "i.remark as status_type,"+ - " i.unit_price,i.price as price,i.product_name as product_name, mc.name as" + +def self.get_menu_item_query() + query = MenuItem.select("acc.id as account_id, acc.title as account_name, + i.item_instance_code as item_code, " + + "i.qty as qty," + + "i.product_name as product_name, + mc.name as" + " menu_category_name,mc.id as menu_category_id ") - query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + - " JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + - " JOIN menu_items mi ON mi.id = mii.menu_item_id" + - " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + - " JOIN menu_categories mc ON mc.id = mi.menu_category_id ") - # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") - query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") - # query = query.where("i.item_instance_code IS NOT NULL") + query = query.joins(" LEFT JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + + " LEFT JOIN menu_items mi ON mi.id = mii.menu_item_id" + + " LEFT JOIN menu_categories mc ON mc.id = mi.menu_category_id" + + " LEFT JOIN accounts acc ON acc.id = mi.account_id") query = query.group("acc.title,mi.account_id,mi.menu_category_id,mi.name,mii.price") .order("acc.title desc, mi.account_id desc, mi.menu_category_id desc, mii.price asc") end def self.get_by_items(shift_sale_range, shift, from, to) - query = self.get_product_item_query() + query = self.get_menu_item_query() puts "query" total_grand_total = 0 From f559aa0f14bc0d7dca6bc2a76b7980248f701a09 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 26 Feb 2018 09:20:57 +0630 Subject: [PATCH 22/31] product sale query --- .../reports/product_sale_controller.rb | 70 +---- app/models/sale.rb | 54 +--- .../_shift_sale_report_filter.html.erb | 120 ------- app/views/reports/product_sale/index.html.erb | 294 ++++-------------- app/views/reports/product_sale/index.xls.erb | 177 ++--------- 5 files changed, 119 insertions(+), 596 deletions(-) delete mode 100755 app/views/reports/product_sale/_shift_sale_report_filter.html.erb diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb index 0061818a..afb4b537 100644 --- a/app/controllers/reports/product_sale_controller.rb +++ b/app/controllers/reports/product_sale_controller.rb @@ -2,79 +2,19 @@ class Reports::ProductSaleController < BaseReportController authorize_resource :class => false def index - from, to = get_date_range_from_params - shift_sale_range = Sale.get_by_shift_sale(from,to,Sale::SALE_STATUS_COMPLETED) + order_by = 'desc' + if !params[:order_by].nil? + order_by = params[:order_by] + end - shift = '' - if params[:shift_name].to_i != 0 - shift_sale = ShiftSale.find(params[:shift_name]) - if to.blank? - shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL ',shift_sale.shift_started_at) - else - - shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',shift_sale.shift_started_at, shift_sale.shift_closed_at) - end - end - @sale_data, @grand_total = Sale.get_by_items(shift_sale_range,shift, from, to) - - @sale_taxes = Sale.get_separate_tax(shift_sale_range,shift,from,to,nil) - - @account_cate_count = Hash.new {|hash, key| hash[key] = 0} - - - @sale_data.each {|acc_cate| @account_cate_count[acc_cate.account_id] += 1} - - @menu_cate_count = Hash.new {|hash, key| hash[key] = 0} - @sale_data.each {|cate| @menu_cate_count[cate.account_id] += 1} - - - @totalByAccount = Hash.new {|hash, key| hash[key] = 0} - @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} - - @from = from - @to = to + @sale_data = Sale.get_menu_item_query(order_by) # get printer info @print_settings = PrintSetting.get_precision_delimiter() - if shift.present? - shift.each do |sh| - @shift_from = sh.shift_started_at.nil? ? '-' : sh.shift_started_at.utc.getlocal.strftime("%e %b %I:%M%p") - @shift_to = sh.shift_closed_at.nil? ? '-' : sh.shift_closed_at.utc.getlocal.strftime("%e %b %I:%M%p") - @shift_data = sh - end - end - respond_to do |format| format.html format.xls end end - - # def show - # from, to, report_type = get_date_range_from_params - - # @sale_data = Sale.get_by_shift_sale(from,to,Sale::SALE_STATUS_COMPLETED) - - # date_arr = Array.new - # @sale_data.each do |sale| - # local_opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc.getlocal.strftime("%e %b %I:%M%p") - # local_closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc.getlocal.strftime("%e %b %I:%M%p") - # opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc - # closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc - # shift_id = sale.id.nil? ? '-' : sale.id - # str = {:shift_id => shift_id, :local_opening_date => local_opening_date, :local_closing_date => local_closing_date, :opening_date => opening_date, :closing_date => closing_date} - # date_arr.push(str) - # end - - # @totalByAccount = Hash.new {|hash, key| hash[key] = 0} - # @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} - - # out = {:status => 'ok', :message => date_arr} - - # respond_to do |format| - # format.json { render json: out } - # end - # end - end diff --git a/app/models/sale.rb b/app/models/sale.rb index 54806964..4167d549 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -861,45 +861,23 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) end #product sale report query -def self.get_menu_item_query() - query = MenuItem.select("acc.id as account_id, acc.title as account_name, - i.item_instance_code as item_code, " + - "i.qty as qty," + - "i.product_name as product_name, +def self.get_menu_item_query(order_by) + query = MenuItem.unscoped.select("acc.id as account_id, + acc.title as account_name, + mii.item_instance_code as item_code, " + + "(CASE WHEN si.qty > 0 THEN SUM(si.qty) ELSE 0 END) as total_item," + + "(CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END) as unit_price," + + "(CASE WHEN (si.qty * si.unit_price) > 0 THEN (si.qty * si.unit_price) ELSE 0 END) as grand_total," + + "mii.price as unit_price, menu_items.name as product_name, mc.name as" + - " menu_category_name,mc.id as menu_category_id ") - - query = query.joins(" LEFT JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + - " LEFT JOIN menu_items mi ON mi.id = mii.menu_item_id" + - " LEFT JOIN menu_categories mc ON mc.id = mi.menu_category_id" + - " LEFT JOIN accounts acc ON acc.id = mi.account_id") - query = query.group("acc.title,mi.account_id,mi.menu_category_id,mi.name,mii.price") - .order("acc.title desc, mi.account_id desc, mi.menu_category_id desc, mii.price asc") -end - -def self.get_by_items(shift_sale_range, shift, from, to) - query = self.get_menu_item_query() - - puts "query" - total_grand_total = 0 - - # if shift.present? - # query = query.where("sales.shift_sale_id IN (?)",shift.to_a) - - # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f - - # ### => get all sales range in shift_sales - # elsif shift_sale_range.present? - # query = query.where("sales.shift_sale_id IN (?)",shift_sale_range.to_a) - - # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f - - # else - # query = query.where("sales.receipt_date between ? and ?",from,to) - # # total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f - # end - - return query, total_grand_total + " menu_category_name,mc.id as menu_category_id, si.remark as status_type, si.price as price ") + .joins(" LEFT JOIN menu_item_instances mii ON menu_items.id = mii.menu_item_id" + + " LEFT JOIN menu_categories mc ON mc.id = menu_items.menu_category_id" + + " LEFT JOIN accounts acc ON acc.id = menu_items.account_id" + + " LEFT JOIN sale_items si ON si.item_instance_code = mii.item_instance_code" + + " LEFT JOIN sales s ON s.sale_id = si.sale_id") + .group("mc.id, menu_items.name, si.qty, (CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END)") + .order("si.qty #{order_by}, menu_items.menu_category_id desc") end #product sale report query diff --git a/app/views/reports/product_sale/_shift_sale_report_filter.html.erb b/app/views/reports/product_sale/_shift_sale_report_filter.html.erb deleted file mode 100755 index 88f26c1f..00000000 --- a/app/views/reports/product_sale/_shift_sale_report_filter.html.erb +++ /dev/null @@ -1,120 +0,0 @@ -
    - <%= form_tag report_path, :method => :get, :id=>"frm_report", :class => "form" do %> - <% if period_type != false %> -
    -
    - - -
    - -
    - - - -
    -
    - - -
    -
    - - -
    -
    -
    - -
    -
    - <% end %> - - <% end %> -
    - - diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb index 12e1dfe1..ab3136a3 100644 --- a/app/views/reports/product_sale/index.html.erb +++ b/app/views/reports/product_sale/index.html.erb @@ -9,31 +9,25 @@
    - <%= render :partial=>'shift_sale_report_filter', - :locals=>{ :period_type => true, :shift_name => true, :report_path =>reports_product_sale_index_path} %> - -
    -
    +
    +
    + + +
    +
    +
    - - - - <% if @shift_from %> - - <% if @shift_data.employee %> - <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> - <% end %> - - - <% end %> @@ -41,7 +35,7 @@ - + @@ -58,114 +52,48 @@ end %> <% unless @sale_data.blank? %> - <% acc_arr = Array.new %> - <% cate_arr = Array.new %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> - <% sub_qty = 0 %> - <% sub_total = 0 %> - <% other_sub_total = 0 %> - <% count = 0 %> - <% total_price = 0 %> - <% cate_count = 0 %> - <% acc_count = 0 %> - <% grand_total = 0 %> - <% total_qty = 0 %> - <% total_amount = 0 %> - <% discount = 0 %> - <% total_item_foc = 0 %> - <% total_item_dis = 0.0 %> - <% total_tax = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> - <% @sale_data.each do |sale| %> - - <% if sale.status_type != "Discount" && sale.status_type != "foc" - total_qty += sale.total_item - end %> - <% if sale.status_type == "foc" && sale.price > 0 - total_qty += sale.total_item - end %> - - <% if sale.status_type == "foc" && sale.grand_total < 0 - total_item_foc += sale.grand_total*(-1) - end %> - - <% if sale.status_type == "Discount" && sale.grand_total < 0 - total_item_dis += sale.grand_total*(-1) - end %> - - <% if !acc_arr.include?(sale.account_id) %> - - - - - - - <% acc_arr.push(sale.account_id) %> - <% end %> - - - <% if !cate_arr.include?(sale.menu_category_id) %> - - <% cate_arr.push(sale.menu_category_id) %> - <% else %> - - <% end %> - - - - - - - - - <% @menu_cate_count.each do |key,value| %> - <% if sale.account_id == key %> - <% count = count + 1 %> - <% sub_total += sale.grand_total %> - <% #sub_qty += sale.total_item %> - <% if sale.status_type!="Discount" && (!sale.product_name.include? "FOC") - sub_qty += sale.total_item + <% @sale_data.each do |sale| %> + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + grand_total += sale.grand_total end %> - <% if count == value %> - - - - - - - - <% sub_total = 0.0%> - <% sub_qty = 0 %> - <% count = 0%> - <% end %> - <% end %> - <% end %> - - <% end %> - - - - - - - - - <% end %> - - + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + + + + + <% end %> - - - + + + + + + + <% end %>
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
      <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.product") %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %> <%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %><%= t("views.right_panel.detail.total") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> - <% @totalByAccount.each do |account, total| %> - <% if sale.account_id == account %> - <%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) %> - <% grand_total += total %> - <% end %> - <% end %> -
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total <%= sale.account_name %> Qty <%= sub_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(sub_total , precision:precision.to_i,delimiter:delimiter)%>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter)%>
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
     Net Amount<%= number_with_precision(grand_total.to_f - @discount_data.to_f , precision:precision.to_i,delimiter:delimiter)%>Total<%= total_qty %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
    @@ -175,118 +103,26 @@
    \ No newline at end of file diff --git a/app/views/reports/product_sale/index.xls.erb b/app/views/reports/product_sale/index.xls.erb index f97edbb8..7123588c 100755 --- a/app/views/reports/product_sale/index.xls.erb +++ b/app/views/reports/product_sale/index.xls.erb @@ -5,17 +5,6 @@
    - - - - <% if @shift_from %> - - <% if @shift_data.employee %> - <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> - <% end %> - - - <% end %> @@ -23,149 +12,49 @@ - + <% unless @sale_data.blank? %> - <% acc_arr = Array.new %> - <% cate_arr = Array.new %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + <% grand_total = 0 %> + <% total_qty = 0 %> - <% sub_total = 0 %> - <% other_sub_total = 0 %> - <% count = 0 %> - <% total_price = 0 %> - <% cate_count = 0 %> - <% acc_count = 0 %> - <% grand_total = 0 %> - <% total_qty = 0 %> - <% total_amount = 0 %> - <% discount = 0 %> - <% total_item_foc = 0 %> - <% total_item_dis = 0 %> + <% @sale_data.each do |sale| %> + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + grand_total += sale.grand_total + end %> - <% @sale_data.each do |sale| %> - - <% if sale.total_item > 0 - total_qty += sale.total_item - end %> - - <% if !acc_arr.include?(sale.account_id) %> - - - - + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + - <% acc_arr.push(sale.account_id) %> <% end %> - - <% if !cate_arr.include?(sale.menu_category_id) %> - - <% cate_arr.push(sale.menu_category_id) %> - <% else %> - - <% end %> - - - - - - - - <% @menu_cate_count.each do |key,value| %> - <% if sale.menu_category_id == key %> - - <% count = count + 1 %> - <% sub_total += sale.grand_total %> - <% if count == value %> - - - - - - <% if sale.status_type === "foc" - total_item_foc += sale.grand_total - end %> - - <% if sale.status_type === "Discount" - total_item_dis += sale.grand_total - end %> - <% sub_total = 0.0%> - <% count = 0%> - <% end %> - <% end %> - <% end %> - - <% end %> - - - - - - - <% @other_charges.each do |other| %> - <% if other.total_item > 0 - total_qty += other.total_item - end %> - <% grand_total +=other.grand_total%> - - - - - - - - - - - - <% other_sub_total += other.grand_total %> - - <% end %> - - - - - - - - - - - - - - <% end %> - - - - - - - - - - - - - - - - - - - - - + + + + + + + <% end %>
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
      <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.product") %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %> <%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %><%= t("views.right_panel.detail.total") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> - <% @totalByAccount.each do |account, total| %> - <% if sale.account_id == account %> - <%= total %> - <% grand_total += total %> - <% end %> - <% end %> -  <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.unit_price rescue '-' %><%= sale.grand_total rescue '-' %>
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.unit_price rescue '-' %><%= sale.grand_total rescue '-' %>
     <%= t("views.right_panel.detail.sub_total") %><%= sub_total %>
    Other Charges 
     Other Charges<%= other.item_code rescue '-' %><%= other.product_name rescue '-' %><%= other.total_item rescue '-' %><%= other.unit_price rescue '-' %><%= other.grand_total rescue '-' %>
     <%= t("views.right_panel.detail.sub_total") %><%= other_sub_total %>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.net_amount") %><%= grand_total%>
     <%= t("views.right_panel.detail.foc_item") %> <%= t("views.right_panel.detail.amount") %><%= total_item_foc %>
     <%= t("views.right_panel.detail.item_discount") %> <%= t("views.right_panel.detail.amount") %><%= total_item_dis %>
     <%= t("views.right_panel.detail.foc_sales") %><%= @foc_data %>
     <%= t("views.right_panel.detail.discount") %> <%= t("views.right_panel.detail.amount") %><%= @discount_data %>
    Total<%= total_qty %><%= grand_total rescue '-' %>
    From 5523ff6fc17be50b8e57831cf70e49ddaab9873f Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 26 Feb 2018 11:24:47 +0630 Subject: [PATCH 23/31] check product sale report --- app/controllers/oqs/home_controller.rb | 26 +-- .../origami/payments_controller.rb | 2 +- .../reports/product_sale_controller.rb | 9 +- app/models/sale.rb | 2 +- app/views/oqs/home/index.html.erb | 12 +- app/views/origami/payments/show.html.erb | 2 +- app/views/reports/product_sale/index.html.erb | 202 +++++++++--------- 7 files changed, 125 insertions(+), 130 deletions(-) diff --git a/app/controllers/oqs/home_controller.rb b/app/controllers/oqs/home_controller.rb index d8642df4..78c2434b 100755 --- a/app/controllers/oqs/home_controller.rb +++ b/app/controllers/oqs/home_controller.rb @@ -14,19 +14,19 @@ class Oqs::HomeController < BaseOqsController @queue_stations = OrderQueueStation.all @queue_completed_item = completed_order(@filter) - # if !@queue_completed_item.empty? - # @queue_completed_item.each do |queue_item| - # if !queue_item.set_menu_items.nil? - # instance_item_sets = JSON.parse(JSON.parse(queue_item.set_menu_items)) - # arr_instance_item_sets = Array.new - # instance_item_sets.each do |instance_item| - # item_instance_name = MenuItemInstance.find_by_item_instance_code(instance_item["item_instance_code"]).item_instance_name - # arr_instance_item_sets.push(item_instance_name) - # end - # queue_item.set_menu_items = arr_instance_item_sets - # end - # end - # end + if !@queue_completed_item.empty? + @queue_completed_item.each do |queue_item| + if !queue_item.set_menu_items.nil? + instance_item_sets = JSON.parse(JSON.parse(queue_item.set_menu_items)) + arr_instance_item_sets = Array.new + instance_item_sets.each do |instance_item| + item_instance_name = MenuItemInstance.find_by_item_instance_code(instance_item["item_instance_code"]).item_instance_name + arr_instance_item_sets.push(item_instance_name) + end + queue_item.set_menu_items = arr_instance_item_sets + end + end + end if !@filter.nil? @count = queue_items_count_query(false,@filter) diff --git a/app/controllers/origami/payments_controller.rb b/app/controllers/origami/payments_controller.rb index 317743dd..80f429a3 100755 --- a/app/controllers/origami/payments_controller.rb +++ b/app/controllers/origami/payments_controller.rb @@ -87,7 +87,7 @@ class Origami::PaymentsController < BaseOrigamiController #end rounding adjustment sale_payment = SalePayment.new - sale_payment.process_payment(saleObj, @usercurrent_user.name, cash, "cash") + sale_payment.process_payment(saleObj, current_user.name, cash, "cash") render json: JSON.generate({:status => saleObj.rebate_status, :message => "Can't Rebate coz of Sever Error "}) rebate_amount = nil diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb index afb4b537..a2ba1c7f 100644 --- a/app/controllers/reports/product_sale_controller.rb +++ b/app/controllers/reports/product_sale_controller.rb @@ -2,18 +2,19 @@ class Reports::ProductSaleController < BaseReportController authorize_resource :class => false def index - order_by = 'desc' + @order_by = 'desc' if !params[:order_by].nil? - order_by = params[:order_by] + @order_by = params[:order_by] end - - @sale_data = Sale.get_menu_item_query(order_by) + + @sale_data = Sale.get_menu_item_query(@order_by) # get printer info @print_settings = PrintSetting.get_precision_delimiter() respond_to do |format| format.html + format.json format.xls end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 4167d549..ee24ed15 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -877,7 +877,7 @@ def self.get_menu_item_query(order_by) " LEFT JOIN sale_items si ON si.item_instance_code = mii.item_instance_code" + " LEFT JOIN sales s ON s.sale_id = si.sale_id") .group("mc.id, menu_items.name, si.qty, (CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END)") - .order("si.qty #{order_by}, menu_items.menu_category_id desc") + .order("si.qty #{order_by}, menu_items.menu_category_id #{order_by}") end #product sale report query diff --git a/app/views/oqs/home/index.html.erb b/app/views/oqs/home/index.html.erb index 1f25e275..2bb5a94e 100755 --- a/app/views/oqs/home/index.html.erb +++ b/app/views/oqs/home/index.html.erb @@ -83,13 +83,13 @@ <%= qid.qty %> ] - <% if !qid.set_menu_items.nil? - qid.set_menu_items.each do |item_instance|%> -
    + <% if !qid.set_menu_items.nil? %> + <% qid.set_menu_items.each do |item_instance|%> +
    <%= item_instance %> - - <% end - end %> +
    + <% end %> + <% end %>


    <%= qid.options == "[]"? "" : qid.options %>

    diff --git a/app/views/origami/payments/show.html.erb b/app/views/origami/payments/show.html.erb index c6f8f941..64ca49bd 100755 --- a/app/views/origami/payments/show.html.erb +++ b/app/views/origami/payments/show.html.erb @@ -441,7 +441,7 @@ var othertotal = parseFloat(credit1) + parseFloat(card1) + parseFloat(paypar1) + parseFloat(visa1) + parseFloat(jcb1) + parseFloat(master1) + parseFloat(unionpay1); var total = $('#amount_due').text(); var amt = parseFloat(total) - parseFloat(othertotal); - $('#cash').text(parseFloat(amt).toFixed(1)); + $('#cash').text(parseFloat(amt).toFixed(2)); update_balance(); break; } diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb index ab3136a3..7c0f3108 100644 --- a/app/views/reports/product_sale/index.html.erb +++ b/app/views/reports/product_sale/index.html.erb @@ -1,127 +1,121 @@ - -
    -
    - +
    + +
    +
    + -
    -
    -
    -
    - - +
    +
    +
    +
    + + +
    -
    -
    - - - - - - - - - - - - - - <% if @print_settings.precision.to_i > 0 - precision = @print_settings.precision - else - precision = 0 - end - #check delimiter - if @print_settings.delimiter - delimiter = "," - else - delimiter = "" - end - %> - <% unless @sale_data.blank? %> - <% acc_arr = Array.new %> - <% cate_arr = Array.new %> +
    + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end %> +
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.total") %>
    + + + + + + + + + + + + + <% unless @sale_data.blank? %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> - <% grand_total = 0 %> - <% total_qty = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> - <% @sale_data.each do |sale| %> - <% if sale.status_type != "Discount" && sale.status_type != "foc" - total_qty += sale.total_item - grand_total += sale.grand_total - end %> - <% if sale.status_type == "foc" && sale.price > 0 - total_qty += sale.total_item - grand_total += sale.grand_total - end %> + <% @sale_data.each do |sale| %> + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + grand_total += sale.grand_total + end %> - - - <% if !cate_arr.include?(sale.menu_category_id) %> - - <% cate_arr.push(sale.menu_category_id) %> - <% else %> - - <% end %> - - - - - - - - - <% end %> - - - - - - - - - <% end %> - -
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.total") %>
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
    Total<%= total_qty %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
    + +   + <% if !cate_arr.include?(sale.menu_category_id) %> + <%= sale.menu_category_name %> + <% cate_arr.push(sale.menu_category_id) %> + <% else %> +   + <% end %> + <%= sale.item_code rescue '-' %> + <%= sale.product_name rescue '-' %> + <%= sale.total_item rescue '-' %> + <%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> + <%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> + + + + <% end %> + + + Total + <%= total_qty %> + + <%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> + + + <% end %> + + +
    - \ No newline at end of file From 13b7b1f800652101261686bab556e55ed88ebb6b Mon Sep 17 00:00:00 2001 From: Yan Date: Mon, 26 Feb 2018 17:42:54 +0630 Subject: [PATCH 25/31] add action base and update base ctrls --- app/controllers/application_controller.rb | 14 +--- app/controllers/base_crm_controller.rb | 3 +- app/controllers/base_inventory_controller.rb | 3 +- app/controllers/base_oqs_controller.rb | 3 +- app/controllers/base_origami_controller.rb | 3 +- app/controllers/base_report_controller.rb | 11 +-- app/controllers/base_waiter_controller.rb | 3 +- .../concerns/login_verification.rb | 2 +- config/initializers/action_controller.rb | 81 +++++++++++++++++++ 9 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 config/initializers/action_controller.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3c061feb..833b49d6 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,11 +1,11 @@ class ApplicationController < ActionController::Base - include LicenseVerification + include LoginVerification #before_action :check_installation protect_from_forgery with: :exception # lookup domain for db from provision - before_action :set_locale + # before_action :set_locale helper_method :current_company,:current_login_employee,:current_user # alias_method :current_user, :current_login_employee,:current_user @@ -13,16 +13,6 @@ class ApplicationController < ActionController::Base #all token authentication must be done here #response format must be set to JSON - #change locallization - def set_locale - I18n.locale = params[:locale] || I18n.default_locale - end - - # RESTful url for localize - def default_url_options - { locale: I18n.locale } - end - rescue_from CanCan::AccessDenied do |exception| flash[:warning] = exception.message redirect_to root_path diff --git a/app/controllers/base_crm_controller.rb b/app/controllers/base_crm_controller.rb index 5c412fcf..91da2a72 100755 --- a/app/controllers/base_crm_controller.rb +++ b/app/controllers/base_crm_controller.rb @@ -1,4 +1,5 @@ -class BaseCrmController < ApplicationController +class BaseCrmController < ActionController::Base + include LoginVerification layout "CRM" #before_action :check_installation diff --git a/app/controllers/base_inventory_controller.rb b/app/controllers/base_inventory_controller.rb index f44747d3..2fe167c0 100755 --- a/app/controllers/base_inventory_controller.rb +++ b/app/controllers/base_inventory_controller.rb @@ -1,4 +1,5 @@ -class BaseInventoryController < ApplicationController +class BaseInventoryController < ActionController::Base + include LoginVerification layout "inventory" #before_action :check_installation diff --git a/app/controllers/base_oqs_controller.rb b/app/controllers/base_oqs_controller.rb index f1f2831a..db9f8231 100755 --- a/app/controllers/base_oqs_controller.rb +++ b/app/controllers/base_oqs_controller.rb @@ -1,4 +1,5 @@ -class BaseOqsController < ApplicationController +class BaseOqsController < ActionController::Base + include LoginVerification layout "OQS" #before_action :check_installation diff --git a/app/controllers/base_origami_controller.rb b/app/controllers/base_origami_controller.rb index 58d9b801..ec415bad 100755 --- a/app/controllers/base_origami_controller.rb +++ b/app/controllers/base_origami_controller.rb @@ -1,4 +1,5 @@ -class BaseOrigamiController < ApplicationController +class BaseOrigamiController < ActionController::Base + include LoginVerification layout "origami" # before_action :checkin_process diff --git a/app/controllers/base_report_controller.rb b/app/controllers/base_report_controller.rb index f7551f22..733e48f9 100755 --- a/app/controllers/base_report_controller.rb +++ b/app/controllers/base_report_controller.rb @@ -1,10 +1,11 @@ -class BaseReportController < ApplicationController - layout "application" +class BaseReportController < ActionController::Base + include LoginVerification + layout "application" - #before_action :check_installation - protect_from_forgery with: :exception + #before_action :check_installation + protect_from_forgery with: :exception - rescue_from CanCan::AccessDenied do |exception| + rescue_from CanCan::AccessDenied do |exception| flash[:warning] = exception.message redirect_to root_path end diff --git a/app/controllers/base_waiter_controller.rb b/app/controllers/base_waiter_controller.rb index 248e32a6..ca4622db 100755 --- a/app/controllers/base_waiter_controller.rb +++ b/app/controllers/base_waiter_controller.rb @@ -1,4 +1,5 @@ -class BaseWaiterController < ApplicationController +class BaseWaiterController < ActionController::Base + include LoginVerification layout "waiter" #before_action :check_installation diff --git a/app/controllers/concerns/login_verification.rb b/app/controllers/concerns/login_verification.rb index aa9d8319..fa588574 100755 --- a/app/controllers/concerns/login_verification.rb +++ b/app/controllers/concerns/login_verification.rb @@ -2,7 +2,7 @@ module LoginVerification extend ActiveSupport::Concern included do - before_action :authenticate_session_token + before_action :authenticate helper_method :current_company,:current_login_employee end diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb new file mode 100644 index 00000000..f33ebfed --- /dev/null +++ b/config/initializers/action_controller.rb @@ -0,0 +1,81 @@ +# ActionController::Base are used by both Peatio controllers and +# Doorkeeper controllers. +class ActionController::Base + + before_action :lookup_domain, :set_language + + private + + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" + from = request.subdomain.downcase + "." + request.domain.downcase + @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + if (!@license.nil?) + # logger.info "Location - " + @license.name + ActiveRecord::Base.establish_connection(website_connection(@license)) + # authenticate_session_token + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else + # reconnect_default_db + logger.info 'License is nil' + # redirect_to root_url(:host => request.domain) + "store_error" + render :json => [{ status: false, message: 'Invalid Access!'}] + end + else + # check for license file + # if check_license + # current_license(ENV["SX_PROVISION_URL"]) + # else + # redirect_to activate_path + # end + end + end + + def current_license(url) + @license = License.new(url) + flag = @license.detail_with_local_file() + if (flag == 0) + flash[:notice] = 'Expired or No License!' + elsif (flag == 2) + flash[:notice] = 'Expiring! Please, License extend...' + else + puts "RUN SAY BYAR" + end + end + + def cache_license(url, lookup) + @license = License.new(url, lookup) + + if (@license.detail_with_local_cache(lookup) == true) + return @license + else + return nil + end + end + + def website_connection(license) + default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase, + :username => license.dbusername, :password => license.dbpassword) + + end + + def reconnect_default_db + ActiveRecord::Base.establish_connection(Rails.env) + end + + # Regular database.yml configuration hash + def default_connection + @default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup + end + + #change locallization + def set_locale + I18n.locale = params[:locale] || I18n.default_locale + end + + # RESTful url for localize + def default_url_options + { locale: I18n.locale } + end + +end From 7d8442fd098d8d1d2a90ccad58815c2cb3dd9d5c Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Mon, 26 Feb 2018 17:49:19 +0630 Subject: [PATCH 26/31] To add the condition for CloseCashierCustmisePdf --- app/controllers/origami/shifts_controller.rb | 16 +++++++++- app/models/printer/cashier_station_printer.rb | 31 +++++++++++++++++-- app/pdf/close_cashier_customise_pdf.rb | 2 +- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/app/controllers/origami/shifts_controller.rb b/app/controllers/origami/shifts_controller.rb index e05922fd..1647ba37 100755 --- a/app/controllers/origami/shifts_controller.rb +++ b/app/controllers/origami/shifts_controller.rb @@ -61,7 +61,21 @@ class Origami::ShiftsController < BaseOrigamiController end if ENV["SERVER_MODE"] != "cloud" #no print in cloud server + + close_cashier_pdf = Lookup.collection_of("print_settings") unique_code = "CloseCashierPdf" + + if !close_cashier_pdf.empty? + close_cashier_pdf.each do |close_cashier| + if close_cashier[0] == 'CloseCashierCustomisePdf' + if close_cashier[1] == '1' + unique_code="CloseCashierCustomisePdf" + else + unique_code="CloseCashierPdf" + end + end + end + end shop_details = Shop.find(1) #get tax shift_obj = ShiftSale.where('id =?',@shift.id) @@ -79,7 +93,7 @@ class Origami::ShiftsController < BaseOrigamiController # get printer info print_settings = PrintSetting.find_by_unique_code(unique_code) - + #byebug printer = Printer::CashierStationPrinter.new(print_settings) printer.print_close_cashier(print_settings,cashier_terminal,@shift,shop_details,@sale_taxes,@other_payment,@total_amount_by_account,@total_discount_by_account,@total_member_discount,@total_dinein,@total_takeway,@total_other_charges) diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index b1cab253..ba3483ca 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -22,16 +22,43 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker # end #Bill Receipt Print + # def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + # #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 = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + # filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" + # pdf.render_file filename + # self.print(filename, cashier_terminal.printer_name) + # end + def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + #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") - + filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) - filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" + close_cashier_pdf = Lookup.collection_of("print_settings") #print_settings with name:CloseCashierPdf + + if !close_cashier_pdf.empty? + close_cashier_pdf.each do |close_cashier| + if close_cashier[0] == 'CloseCashierCustomisePdf' + if close_cashier[1] == '1' + pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + else + pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + end + end + end + end pdf.render_file filename + self.print(filename, cashier_terminal.printer_name) end diff --git a/app/pdf/close_cashier_customise_pdf.rb b/app/pdf/close_cashier_customise_pdf.rb index cd9d7b3b..65089a74 100644 --- a/app/pdf/close_cashier_customise_pdf.rb +++ b/app/pdf/close_cashier_customise_pdf.rb @@ -416,7 +416,7 @@ class CloseCashierCustomisePdf < Prawn::Document #end total amount by Account #start total other charges amount - if total_other_charges > 0 + if total_other_charges.present? y_position = cursor bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do text "Total Other Charges :", :size => self.item_font_size, :align => :right From 72b5701ec9d30705fbec045d13d30d21dfe3e03d Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Mon, 26 Feb 2018 18:29:58 +0630 Subject: [PATCH 27/31] To add the condition for CloseCashierCustmisePdf --- app/controllers/origami/shifts_controller.rb | 4 ++-- app/models/printer/cashier_station_printer.rb | 4 ++-- app/pdf/close_cashier_customise_pdf.rb | 7 ++++--- app/pdf/close_cashier_pdf.rb | 11 ++++------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/controllers/origami/shifts_controller.rb b/app/controllers/origami/shifts_controller.rb index 1647ba37..19def9b6 100755 --- a/app/controllers/origami/shifts_controller.rb +++ b/app/controllers/origami/shifts_controller.rb @@ -66,7 +66,7 @@ class Origami::ShiftsController < BaseOrigamiController unique_code = "CloseCashierPdf" if !close_cashier_pdf.empty? - close_cashier_pdf.each do |close_cashier| + close_cashier_pdf.each do |close_cashier| if close_cashier[0] == 'CloseCashierCustomisePdf' if close_cashier[1] == '1' unique_code="CloseCashierCustomisePdf" @@ -90,7 +90,7 @@ class Origami::ShiftsController < BaseOrigamiController @total_dinein = ShiftSale.get_total_dinein(@shift).total_dinein_amount @total_takeway = ShiftSale.get_total_takeway(@shift).total_takeway_amount @total_other_charges = ShiftSale.get_total_other_charges(@shift).total_other_charges_amount - + # get printer info print_settings = PrintSetting.find_by_unique_code(unique_code) #byebug diff --git a/app/models/printer/cashier_station_printer.rb b/app/models/printer/cashier_station_printer.rb index ba3483ca..c9404b3d 100755 --- a/app/models/printer/cashier_station_printer.rb +++ b/app/models/printer/cashier_station_printer.rb @@ -43,7 +43,7 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker 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") filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf" - pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount) close_cashier_pdf = Lookup.collection_of("print_settings") #print_settings with name:CloseCashierPdf if !close_cashier_pdf.empty? @@ -52,7 +52,7 @@ class Printer::CashierStationPrinter < Printer::PrinterWorker if close_cashier[1] == '1' pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) else - pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges) + pdf = CloseCashierPdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount) end end end diff --git a/app/pdf/close_cashier_customise_pdf.rb b/app/pdf/close_cashier_customise_pdf.rb index 65089a74..bcc27724 100644 --- a/app/pdf/close_cashier_customise_pdf.rb +++ b/app/pdf/close_cashier_customise_pdf.rb @@ -455,12 +455,13 @@ class CloseCashierCustomisePdf < Prawn::Document bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "-#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right end + move_down -5 + stroke_horizontal_rule + move_down 7 end #end total over all discount - 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 diff --git a/app/pdf/close_cashier_pdf.rb b/app/pdf/close_cashier_pdf.rb index 412cb9a4..7ac7a130 100755 --- a/app/pdf/close_cashier_pdf.rb +++ b/app/pdf/close_cashier_pdf.rb @@ -234,14 +234,12 @@ class CloseCashierPdf < Prawn::Document text "Rounding Adjustments :", :size => self.item_font_size, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do - # text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - text "#{ number_with_precision(shift_sale.total_dinein, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right - + text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :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 "Gross Sale :", :style => :bold, :size => self.header_font_size - 1, :align => :right + text "Total :", :style => :bold, :size => self.header_font_size - 1, :align => :right end bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do text "#{ number_with_precision(shift_sale.grand_total, :precision => precision.to_i, :delimiter => delimiter)}", :style => :bold, :size => self.header_font_size - 1, :align => :right @@ -394,7 +392,6 @@ class CloseCashierPdf < Prawn::Document move_down 5 stroke_horizontal_rule move_down 5 - move_down 5 + move_down 5 end -end - +end \ No newline at end of file From 1b1206d7354050b7328a0076e4ff96a7fedb8c78 Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 10:02:14 +0630 Subject: [PATCH 28/31] add Old Town req: --- app/controllers/home_controller.rb | 64 +- .../reports/product_sale_controller.rb | 18 + app/models/sale.rb | 982 +++++++++++++++--- app/views/home/dashboard.html.erb | 98 +- app/views/reports/product_sale/index.html.erb | 113 ++ app/views/reports/product_sale/index.xls.erb | 64 ++ config/routes.rb | 1 + 7 files changed, 1163 insertions(+), 177 deletions(-) create mode 100755 app/controllers/reports/product_sale_controller.rb create mode 100755 app/views/reports/product_sale/index.html.erb create mode 100755 app/views/reports/product_sale/index.xls.erb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 25fb0d70..97ac4051 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -62,57 +62,59 @@ class HomeController < ApplicationController end def dashboard + @from, @to = get_date_range_from_params + @shop = Shop.first today = DateTime.now.strftime('%Y-%m-%d') @orders = Sale::where("payment_status='new' and sale_status='bill' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() @sales = Sale::where("payment_status='paid' and sale_status='completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'").count() - @top_products = Sale.top_products(today).sum('i.qty') - @bottom_products = Sale.bottom_products(today).sum('i.qty') - @hourly_sales = Sale.hourly_sales(today).sum(:grand_total) + @top_products = Sale.top_products(today,@from,@to).sum('i.qty') + @bottom_products = Sale.bottom_products(today,@from,@to).sum('i.qty') + @hourly_sales = Sale.hourly_sales(today,@from,@to).sum(:grand_total) # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) - @employee_sales = Sale.employee_sales(today) + @employee_sales = Sale.employee_sales(today,@from,@to) .sum('(CASE WHEN sp.payment_method="cash" THEN (sp.payment_amount - sales.amount_changed) ELSE sp.payment_amount END)') - @inventories = StockJournal.inventory_balances(today).sum(:balance) + @inventories = StockJournal.inventory_balances(today,@from,@to).sum(:balance) - @total_sale = Sale.total_sale(today) - @total_count = Sale.total_count(today) - @total_card = Sale.total_card_sale(today) - @total_credit = Sale.credit_payment(today) + @total_sale = Sale.total_sale(today,current_user,@from,@to) + @total_count = Sale.total_count(today,current_user,@from,@to) + @total_card = Sale.total_card_sale(today,current_user,@from,@to) + @total_credit = Sale.credit_payment(today,current_user,@from,@to) @sale_data = Array.new - @total_payment_methods = Sale.total_payment_methods(today) + @total_payment_methods = Sale.total_payment_methods(today,current_user,@from,@to) @total_payment_methods.each do |payment| if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" - pay = Sale.payment_sale('card', today) + pay = Sale.payment_sale('card', today, current_user,@from,@to) @sale_data.push({'card' => pay.payment_amount}) else - pay = Sale.payment_sale(payment.payment_method, today) + pay = Sale.payment_sale(payment.payment_method, today, current_user,@from,@to) @sale_data.push({payment.payment_method => pay.payment_amount}) end end - @summ_sale = Sale.summary_sale_receipt(today) - @total_customer = Sale.total_customer(today) - @total_dinein = Sale.total_dinein(today) - @total_takeaway = Sale.total_takeaway(today) - @total_other_customer = Sale.total_other_customer(today) - @total_membership = Sale.total_membership(today) + @summ_sale = Sale.summary_sale_receipt(today,current_user,@from,@to) + @total_customer = Sale.total_customer(today,current_user,@from,@to) + @total_dinein = Sale.total_dinein(today,current_user,@from,@to) + @total_takeaway = Sale.total_takeaway(today,current_user,@from,@to) + @total_other_customer = Sale.total_other_customer(today,current_user,@from,@to) + @total_membership = Sale.total_membership(today,current_user,@from,@to) - @total_order = Sale.total_order(today) - @total_accounts = Sale.total_account(today) + @total_order = Sale.total_order(today,current_user,@from,@to) + @total_accounts = Sale.total_account(today,current_user,@from,@to) @account_data = Array.new @total_accounts.each do |account| - acc = Sale.account_data(account.account_id, today) + acc = Sale.account_data(account.account_id, today,current_user,@from,@to) if !acc.nil? @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) end end - @top_items = Sale.top_items(today) - @total_foc_items = Sale.total_foc_items(today) + @top_items = Sale.top_items(today,current_user,@from,@to) + @total_foc_items = Sale.total_foc_items(today,current_user,@from,@to) # get printer info @print_settings = PrintSetting.get_precision_delimiter() @@ -162,4 +164,20 @@ class HomeController < ApplicationController redirect_to reports_dailysale_index_path end end + + def get_date_range_from_params + from = params[:from] + to = params[:to] + + if from.present? && to.present? + f_date = DateTime.parse(from) + t_date = DateTime.parse(to) + f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec) + t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec) + from = f_time.beginning_of_day.utc.getlocal + to = t_time.end_of_day.utc.getlocal + end + + return from, to + end end diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb new file mode 100755 index 00000000..748c8716 --- /dev/null +++ b/app/controllers/reports/product_sale_controller.rb @@ -0,0 +1,18 @@ +class Reports::ProductSaleController < BaseReportController + authorize_resource :class => false + + def index + @order_by = params[:order_by] + + @sale_data = Sale.get_menu_item_query(@order_by) + + # get printer info + @print_settings = PrintSetting.get_precision_delimiter() + + respond_to do |format| + format.html + format.json + format.xls + end + end +end diff --git a/app/models/sale.rb b/app/models/sale.rb index f42b62a0..5196672c 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -78,14 +78,29 @@ class Sale < ApplicationRecord self.tax_type = "exclusive" # set cashier - if cashier != nil + open_cashier = Employee.where("role = 'cashier' AND token_session <> ''") + current_shift = ShiftSale.current_shift + shift = ShiftSale.current_open_shift(cashier.id) + + # set cashier + if shift != nil self.cashier_id = cashier.id self.cashier_name = cashier.name + self.shift_sale_id = shift.id else - self.cashier_id = requested_by.id - self.cashier_name = requested_by.name + if open_cashier.count>0 + self.cashier_id = open_cashier[0].id + self.cashier_name = open_cashier[0].name + shift_id = ShiftSale.current_open_shift(open_cashier[0].id) + self.shift_sale_id = current_shift.id + else + self.cashier_id = current_shift.employee_id + self.cashier_name = Employee.find(current_shift.employee_id).name + self.shift_sale_id = current_shift.id + end end + # set waiter self.requested_by = requested_by.name @@ -212,7 +227,7 @@ class Sale < ApplicationRecord sale_item.product_name = item.item_name sale_item.product_alt_name = item.alt_name sale_item.account_id = item.account_id - sale_item.remark = item.remark + sale_item.status = item.remark sale_item.qty = item.qty sale_item.unit_price = item.price @@ -240,7 +255,7 @@ class Sale < ApplicationRecord sale_item.product_name = instance.item_instance_name sale_item.product_alt_name = menu_item.alt_name sale_item.account_id = menu_item.account_id - sale_item.remark = nil + sale_item.status = nil sale_item.qty = item["quantity"] sale_item.unit_price = item["price"] @@ -366,7 +381,7 @@ class Sale < ApplicationRecord rounding_adjustment = 0 sales_items.each do |item| - if item.remark != 'void' && item.remark != 'foc' + if item.status != 'void' && item.status != 'foc' #compute each item and added to total subtotal_price = subtotal_price + item.price @@ -402,35 +417,36 @@ class Sale < ApplicationRecord tax_profiles = TaxProfile.all.order("order_by asc") customer = Customer.find(sale.customer_id) # #Creat new tax records - tax_profiles.each do |tax| - customer.tax_profiles.each do |cus_tax| - if cus_tax.to_i == tax.id - sale_tax = SaleTax.new(:sale => sale) - sale_tax.tax_name = tax.name - sale_tax.tax_rate = tax.rate + if sale.payment_status != 'foc' + tax_profiles.each do |tax| + customer.tax_profiles.each do |cus_tax| + if cus_tax.to_i == tax.id + sale_tax = SaleTax.new(:sale => sale) + sale_tax.tax_name = tax.name + sale_tax.tax_rate = tax.rate - # substract , to give after discount - total_tax = total_taxable - total_discount - #include or execulive - if tax.inclusive - rate = tax.rate - divided_value = (100 + rate)/rate - sale_tax.tax_payable_amount = total_tax / divided_value - else - sale_tax.tax_payable_amount = total_tax * tax.rate / 100 - total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount - end - #new taxable amount is standard rule for step by step - if shop.calc_tax_order - total_taxable = total_taxable + sale_tax.tax_payable_amount - end + # substract , to give after discount + total_tax = total_taxable - total_discount + #include or execulive + if tax.inclusive + rate = tax.rate + divided_value = (100 + rate)/rate + sale_tax.tax_payable_amount = total_tax / divided_value + else + sale_tax.tax_payable_amount = total_tax * tax.rate / 100 + total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount + end + #new taxable amount is standard rule for step by step + if shop.calc_tax_order + total_taxable = total_taxable + sale_tax.tax_payable_amount + end - sale_tax.inclusive = tax.inclusive - sale_tax.save + sale_tax.inclusive = tax.inclusive + sale_tax.save + end end end end - sale.total_tax = total_tax_amount end @@ -742,8 +758,8 @@ end def self.get_item_query() query = Sale.select("acc.title as account_name,mi.account_id, i.item_instance_code as item_code,i.account_id as account_id, " + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item,i.qty as qty," + - "i.remark as status_type,"+ - " mii.price as unit_price,i.price as price,i.product_name as product_name, mc.name as" + + "i.status as status_type,"+ + " i.unit_price,i.price as price,i.product_name as product_name, mc.name as" + " menu_category_name,mc.id as menu_category_id ") query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + @@ -754,14 +770,14 @@ def self.get_item_query() # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") # query = query.where("i.item_instance_code IS NOT NULL") - query = query.group("acc.title,mi.account_id,mi.menu_category_id,i.product_name") + query = query.group("acc.title,mi.account_id,mi.menu_category_id,i.product_name,i.unit_price") .order("acc.title desc, mi.account_id desc, mi.menu_category_id desc, i.unit_price asc") end def self.get_other_charges() query = Sale.select("i.account_id as account_id, " + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," + - "i.remark as status_type,"+ + "i.status as status_type,"+ " i.unit_price as unit_price,i.product_name as product_name") query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id") query = query.where("i.item_instance_code IS NULL AND i.product_code = 'Other Charges'") @@ -770,7 +786,6 @@ end def self.get_by_shift_items(shift_sale_range, shift, from, to, status) # date_type_selection = get_sql_function_for_report_type(report_type) - query = self.get_item_query() discount_query = 0 @@ -846,6 +861,27 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) return query,other_charges, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount end +#product sale report query +def self.get_menu_item_query(order_by) + query = MenuItem.unscoped.select("acc.id as account_id, + acc.title as account_name, + mii.item_instance_code as item_code, " + + "(CASE WHEN si.qty > 0 THEN SUM(si.qty) ELSE 0 END) as total_item," + + "(CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END) as unit_price," + + "(CASE WHEN (si.qty * si.unit_price) > 0 THEN (si.qty * si.unit_price) ELSE 0 END) as grand_total," + + "mii.price as unit_price, menu_items.name as product_name, + mc.name as" + + " menu_category_name,mc.id as menu_category_id, si.remark as status_type, si.price as price ") + .joins(" LEFT JOIN menu_item_instances mii ON menu_items.id = mii.menu_item_id" + + " LEFT JOIN menu_categories mc ON mc.id = menu_items.menu_category_id" + + " LEFT JOIN accounts acc ON acc.id = menu_items.account_id" + + " LEFT JOIN sale_items si ON si.item_instance_code = mii.item_instance_code" + + " LEFT JOIN sales s ON s.sale_id = si.sale_id") + .group("mc.id, menu_items.name, si.qty, (CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END)") + .order("si.qty #{order_by}, menu_items.menu_category_id #{order_by}") +end +#product sale report query + def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type) ## => left join -> show all sales although no orders if payment_type.blank? @@ -1053,95 +1089,376 @@ end return tax end - def self.top_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.top_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) DESC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) DESC").limit(10) + .order("SUM(i.qty) DESC").limit(20) + end end - def self.bottom_products(today) - query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + def self.bottom_products(today,from,to) + if !from.nil? && !to.nil? + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + + " i.price as unit_price,mi.name as product_name") + .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") + .where("(i.qty > 0 and i.price > 0) and receipt_date between '#{from}' and '#{to}'"+ + "and payment_status='paid' and sale_status= 'completed'") + .group('mi.name') + .order("SUM(i.qty) ASC").limit(20) + else + query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," + " i.price as unit_price,mi.name as product_name") .joins("JOIN sale_items i ON i.sale_id = sales.sale_id JOIN menu_items mi ON i.product_code = mi.item_code") .where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+ "and payment_status='paid' and sale_status= 'completed'") .group('mi.name') - .order("SUM(i.qty) ASC").limit(10) + .order("SUM(i.qty) ASC").limit(20) + end end - def self.hourly_sales(today) - query= Sale.select("grand_total") + def self.hourly_sales(today,from,to) + if !from.nil? && !to.nil? + query= Sale.select("grand_total") + .where('payment_status="paid" and sale_status = "completed" and receipt_date between ? and ?',from,to) + .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") + .order('receipt_date') + else + query= Sale.select("grand_total") .where('payment_status="paid" and sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) .group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')") .order('receipt_date') + end end - def self.employee_sales(today) - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + def self.employee_sales(today,from,to) + if !from.nil? && !to.nil? + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") + .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") + .where("sales.payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between '#{from}' and '#{to}'") + .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") + .order("e.name") + else + query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") .where("sales.payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = '#{today}'") .group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay') THEN 'card' ELSE sp.payment_method END)","e.name") .order("e.name") + end end - def self.total_sale(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") - - end - - def self.total_count(today) - total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count - end - - def self.total_card_sale(today) - payment_type = " and payment_method = 'mpu' or payment_method = 'visa' or payment_method = 'master' or payment_method = 'jcb' or payment_method = 'unionpay' " - # query = Sale.select("SUM(tax_payable_amount) AS st_amount,tax_name") - # .where('sale_status = "completed" #{payment_type} and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - # .joins("join sale_payments on sale_id = sales.sale_id") - # .group("sales.sale_id") - - query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") - .sum("sp.payment_amount") - - end - - def self.credit_payment(today) - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") - end - - def self.summary_sale_receipt(today) - query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') - .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) - .first() - end - - def self.total_payment_methods(today) - query = Sale.select("distinct sp.payment_method") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - end - - def self.payment_sale(payment_method, today) - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - if payment_method == 'card' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) - else - query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + def self.total_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and sales.receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .sum("grand_total") end - query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + else + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).sum("grand_total") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .sum("grand_total") + end + end + end + end end - def self.total_customer(today) - dinein_cnt = self.total_dinein(today) - takeaway_cnt = self.total_takeaway(today) - membership_cnt = self.total_membership(today) + def self.total_count(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and receipt_date between ? and ?',from,to).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id = ?',from,to,shift.id).count + end + end + end + else + if current_user.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + if current_user.role == 'administrator' + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today).count + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + total = Sale.where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id = ?',today,shift.id).count + end + end + end + end + end + + def self.total_card_sale(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end + else + if current_user.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + if current_user.role == 'administrator' + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") + end + end + end + end + end + + def self.credit_payment(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ?',from,to) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and s.receipt_date between ? and ? and s.shift_sale_id=?',from,to,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end + else + if current_user.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + if current_user.role == 'administrator' + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ? and s.shift_sale_id=?',today,shift.id) + .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") + .sum("payment_amount") + end + end + end + end + end + + def self.summary_sale_receipt(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ?',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and receipt_date between ? and ? and shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) + .first() + end + end + end + end + end + + def self.total_payment_methods(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end + else + if current_user.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + if current_user.role == 'administrator' + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + end + end + end + end + + def self.payment_sale(payment_method, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',from,to) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ?",from,to) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',from,to,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end + else + if current_user.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay")',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay") and sales.shift_sale_id=?',today,shift.id) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + end + query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + end + end + end + end + end + + def self.total_customer(today,current_user=nil,from,to) + dinein_cnt = self.total_dinein(today,current_user,from,to) + takeaway_cnt = self.total_takeaway(today,current_user,from,to) + membership_cnt = self.total_membership(today,current_user,from,to) total_cus = 0 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1151,69 +1468,448 @@ end return total_cus end - def self.total_dinein(today) - query = Sale.select("count(sales.customer_id) as total_dinein_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) - .first() + def self.total_dinein(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_takeaway(today) - query = Sale.select("count(sales.customer_id) as total_take_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) - .first() + def self.total_takeaway(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_membership(today) - query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) - .first() + def self.total_membership(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null)) and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_other_customer(today) - query = Sale.select("count(sales.customer_id) as total_cus") - .joins("JOIN customers as c ON c.customer_id = sales.customer_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) - .first() + def self.total_other_customer(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_order(today) - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) - .first() + def self.total_order(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ?',from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(distinct a.order_id) as total_order") + .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") + .joins("JOIN orders as b ON b.order_id = a.order_id") + .where('b.status = "billed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + .first() + end + end + end + end end - def self.total_account(today) - query = Sale.select("distinct b.id as account_id, b.title as title") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .joins("JOIN accounts as b ON b.id = a.account_id") - .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + def self.total_account(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ?',from,to) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and sales.receipt_date between ? and ? and sales.shift_sale_id=?',from,to,shift.id) + end + end + end + else + if current_user.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + if current_user.role == 'administrator' + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) + end + end + end + end end - def self.account_data(account_id, today) - query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .first() + def self.account_data(account_id, today, current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ?",from,to) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + if current_user.role == 'administrator' + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .first() + end + end + end + end end - def self.top_items(today) - query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") - .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .group("a.product_code") - .order("SUM(a.qty) DESC") - .first() + def self.top_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ?",from,to) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end + else + if current_user.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + if current_user.role == 'administrator' + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .group("a.product_code") + .order("SUM(a.qty) DESC") + .first() + end + end + end + end end - def self.total_foc_items(today) - query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") - .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - .sum("a.qty") + def self.total_foc_items(today,current_user=nil,from,to) + if !from.nil? && !to.nil? + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ?",from,to) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and sales.receipt_date between ? and ? and sales.shift_sale_id=?",from,to,shift.id) + .sum("a.qty") + end + end + end + else + if current_user.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .sum("a.qty") + else + if current_user.role == 'administrator' + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .sum("a.qty") + else + shift = ShiftSale.current_open_shift(current_user.id) + if !shift.nil? + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and a.product_name not like '%FOC%' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) + .sum("a.qty") + end + end + end + end end #card sale trans data diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 104c4c67..91cce7e3 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -1,6 +1,7 @@
    -

    <%= t :dashboard %>

    + +

    <%= t :date_time %> : <%= Time.zone.now.utc.getlocal.strftime("%Y-%m-%d %I:%M %p") %>

    <% if @print_settings.precision.to_i > 0 precision = @print_settings.precision @@ -65,8 +66,28 @@ - <% if current_user.role == 'administrator' || current_user.role == 'manager' %> + +
    +
    + + + +
    +
    + + + +
    +
    +
    + +
    +
    +
    + +
    + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
    @@ -122,10 +143,18 @@
    -
    + <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'cashier' %> <% if !@summ_sale.nil? %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %> +
    + <% else %> +
    +
    +
    + <% end %>
    <%= t :sale %>
    @@ -136,19 +165,19 @@ <%= t("views.right_panel.detail.total") %> <%= t :sale %> : - <%= number_with_precision( @summ_sale.total_amount, precision: precision.to_i ,delimiter: delimiter) %> + <%= number_with_precision( @summ_sale.total_amount, precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.discount") %> : - <%= number_with_precision( @summ_sale.total_discount, precision: precision.to_i ,delimiter: delimiter) %> + <%= number_with_precision( @summ_sale.total_discount, precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.tax") %> : - <%= number_with_precision( @summ_sale.total_tax , precision: precision.to_i ,delimiter: delimiter)%> + <%= number_with_precision( @summ_sale.total_tax , precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <%= t("views.right_panel.detail.grand_total") %> : - <%= number_with_precision( @summ_sale.grand_total , precision: precision.to_i ,delimiter: delimiter)%> + <%= number_with_precision( @summ_sale.grand_total , precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> @@ -161,7 +190,7 @@ @@ -174,7 +203,7 @@ <% end %> @@ -184,11 +213,17 @@ + <% if current_user.role == 'administrator' || current_user.role == 'manager' %> <% end %> + <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
    + <% else %> +
    + <% end %>
    <%= t :customer %>
    @@ -227,10 +262,16 @@
    + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
    + <% end %> + <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
    + <% else %> +
    + <% end %>
    <%= t("views.right_panel.detail.order") %>
    @@ -274,6 +315,41 @@
    + <% end %>
    - <% end %> -
    \ No newline at end of file +
    + + \ No newline at end of file diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb new file mode 100755 index 00000000..eb797122 --- /dev/null +++ b/app/views/reports/product_sale/index.html.erb @@ -0,0 +1,113 @@ + +
    +
    + + +
    +
    +
    +
    + + +
    +
    + +
    + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end %> +
    <% @sale_data.each do |data| %> <% pay_mth = payment.payment_method %> - <%= data[""+pay_mth+""] %> + <%= number_with_precision(data[""+pay_mth+""], precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> <% end %>
    <%= t("views.right_panel.detail.card_sale") %> : - <%= total_card["card"].to_f %> + <%= number_with_precision(total_card["card"], precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %>
    + + + + + + + + + + + + + <% unless @sale_data.blank? %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + + <% grand_total = 0 %> + <% total_qty = 0 %> + + <% @sale_data.each do |sale| %> + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + + + + + <% end %> + + + + + + + + + <% end %> + +
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.total") %>
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
    Total<%= total_qty %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %>
    +
    +
    +
    +
    +
    + + \ No newline at end of file diff --git a/app/views/reports/product_sale/index.xls.erb b/app/views/reports/product_sale/index.xls.erb new file mode 100755 index 00000000..7123588c --- /dev/null +++ b/app/views/reports/product_sale/index.xls.erb @@ -0,0 +1,64 @@ +
    +
    +
    +
    +
    + + + + + + + + + + + + + + <% unless @sale_data.blank? %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + <% grand_total = 0 %> + <% total_qty = 0 %> + + <% @sale_data.each do |sale| %> + <% if sale.status_type != "Discount" && sale.status_type != "foc" + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + grand_total += sale.grand_total + end %> + + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + + + + + <% end %> + + + + + + + + <% end %> + +
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.total") %>
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.unit_price rescue '-' %><%= sale.grand_total rescue '-' %>
    Total<%= total_qty %><%= grand_total rescue '-' %>
    +
    +
    +
    +
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7d21aeb5..a189da13 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -373,6 +373,7 @@ scope "(:locale)", locale: /en|mm/ do resources :commission, :only => [:index, :show] resources :stock_check, :only => [:index, :show] resources :payment_method + resources :product_sale, :only => [:index, :show] get "receipt_no/get_shift_by_date", to: "receipt_no#get_shift_by_date", as: "get_shift_by_date" end From 90b7a7f201475396f88e4799cf6b24028f124069 Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 10:04:48 +0630 Subject: [PATCH 29/31] change stock check for dashboard query --- app/models/stock_journal.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/stock_journal.rb b/app/models/stock_journal.rb index 35bebca2..c2a12f4a 100755 --- a/app/models/stock_journal.rb +++ b/app/models/stock_journal.rb @@ -36,12 +36,20 @@ class StockJournal < ApplicationRecord journal.save end - def self.inventory_balances(today) - query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") + def self.inventory_balances(today,from,to) + if !from.nil? && !to.nil? + query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") + .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") + .where("stock_journals.created_at between '#{from}' and '#{to}'") + .group("mii.item_instance_name") + .order("mii.item_instance_name ASC") + else + query = StockJournal.select("mii.item_instance_name as item_instance_name,balance") .joins("join menu_item_instances mii on mii.item_instance_code=stock_journals.item_code") .where("DATE_FORMAT(stock_journals.created_at,'%Y-%m-%d') = '#{today}'") .group("mii.item_instance_name") .order("mii.item_instance_name ASC") + end end end From 700ff91127f12940764141717a6fa5705836c89d Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 10:27:52 +0630 Subject: [PATCH 30/31] product sale --- app/controllers/reports/product_sale_controller.rb | 5 ++++- app/views/layouts/_left_sidebar.html.erb | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/reports/product_sale_controller.rb b/app/controllers/reports/product_sale_controller.rb index 748c8716..38a77115 100755 --- a/app/controllers/reports/product_sale_controller.rb +++ b/app/controllers/reports/product_sale_controller.rb @@ -2,7 +2,10 @@ class Reports::ProductSaleController < BaseReportController authorize_resource :class => false def index - @order_by = params[:order_by] + @order_by = 'desc' + if !params[:order_by].nil? + @order_by = params[:order_by] + end @sale_data = Sale.get_menu_item_query(@order_by) diff --git a/app/views/layouts/_left_sidebar.html.erb b/app/views/layouts/_left_sidebar.html.erb index 9bffd4d7..de42e719 100755 --- a/app/views/layouts/_left_sidebar.html.erb +++ b/app/views/layouts/_left_sidebar.html.erb @@ -122,6 +122,9 @@
  • Sale Items
  • +
  • + Product Sale +
  • Receipt
  • From ac470c5747e86f0a778ad8b9e60839ac053616ee Mon Sep 17 00:00:00 2001 From: Yan Date: Tue, 27 Feb 2018 11:41:24 +0630 Subject: [PATCH 31/31] license and local move to base action and update other base --- app/controllers/application_controller.rb | 6 ++-- app/controllers/base_crm_controller.rb | 15 -------- app/controllers/base_inventory_controller.rb | 5 --- app/controllers/base_oqs_controller.rb | 15 -------- app/controllers/base_origami_controller.rb | 9 +---- app/controllers/base_report_controller.rb | 34 +++++++++++++++---- app/controllers/base_waiter_controller.rb | 27 +++++++++++++++ .../concerns/login_verification.rb | 22 ++++++++---- config/initializers/action_controller.rb | 8 ++--- 9 files changed, 77 insertions(+), 64 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 833b49d6..5f19ddaf 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,7 +7,8 @@ class ApplicationController < ActionController::Base # lookup domain for db from provision # before_action :set_locale - helper_method :current_company,:current_login_employee,:current_user + # helper_method :current_company,:current_login_employee,:current_user + # alias_method :current_user, :current_login_employee,:current_user #this is base api base controller to need to inherit. #all token authentication must be done here @@ -18,6 +19,7 @@ class ApplicationController < ActionController::Base redirect_to root_path end + private def current_user @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] end @@ -36,7 +38,7 @@ class ApplicationController < ActionController::Base end - def current_login_employee + def current_login_employee if (!session[:session_token].nil?) @employee = Employee.find_by_token_session(session[:session_token]) end diff --git a/app/controllers/base_crm_controller.rb b/app/controllers/base_crm_controller.rb index 91da2a72..d7c008f6 100755 --- a/app/controllers/base_crm_controller.rb +++ b/app/controllers/base_crm_controller.rb @@ -9,19 +9,4 @@ class BaseCrmController < ActionController::Base flash[:warning] = exception.message redirect_to root_path end - - #change locallization - def set_locale - I18n.locale = params[:locale] || I18n.default_locale - end - - # RESTful url for localize - def default_url_options - { locale: I18n.locale } - end - - def current_user - @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] - end - end diff --git a/app/controllers/base_inventory_controller.rb b/app/controllers/base_inventory_controller.rb index 2fe167c0..fc9c0c47 100755 --- a/app/controllers/base_inventory_controller.rb +++ b/app/controllers/base_inventory_controller.rb @@ -9,9 +9,4 @@ class BaseInventoryController < ActionController::Base flash[:warning] = exception.message redirect_to root_path end - - def current_user - @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] - end - end diff --git a/app/controllers/base_oqs_controller.rb b/app/controllers/base_oqs_controller.rb index db9f8231..19b01ea3 100755 --- a/app/controllers/base_oqs_controller.rb +++ b/app/controllers/base_oqs_controller.rb @@ -9,19 +9,4 @@ class BaseOqsController < ActionController::Base flash[:warning] = exception.message redirect_to root_path end - - #change locallization - def set_locale - I18n.locale = params[:locale] || I18n.default_locale - end - - # RESTful url for localize - def default_url_options - { locale: I18n.locale } - end - - def current_user - @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] - end - end diff --git a/app/controllers/base_origami_controller.rb b/app/controllers/base_origami_controller.rb index ec415bad..a67aa58f 100755 --- a/app/controllers/base_origami_controller.rb +++ b/app/controllers/base_origami_controller.rb @@ -6,16 +6,9 @@ class BaseOrigamiController < ActionController::Base #before_action :check_installation protect_from_forgery with: :exception + rescue_from CanCan::AccessDenied do |exception| flash[:warning] = exception.message redirect_to origami_root_path end - - def current_user - @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] - end - - # def checkin_process - # CheckinJob.set(wait: 1.minute).perform_later() - # end end diff --git a/app/controllers/base_report_controller.rb b/app/controllers/base_report_controller.rb index 733e48f9..154a0bbb 100755 --- a/app/controllers/base_report_controller.rb +++ b/app/controllers/base_report_controller.rb @@ -5,15 +5,13 @@ class BaseReportController < ActionController::Base #before_action :check_installation protect_from_forgery with: :exception + helper_method :current_company,:current_login_employee,:current_user + rescue_from CanCan::AccessDenied do |exception| flash[:warning] = exception.message redirect_to root_path end - def current_user - @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] - end - PERIOD = { "today" => 0, "yesterday" => 1, @@ -83,8 +81,30 @@ class BaseReportController < ActionController::Base end return from, to - - end - + + private + def current_user + @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] + end + + # Get current Cashier + def get_cashier + @cashier = Employee.where("role = 'cashier' AND token_session <> ''") + end + + def current_company + begin + return Company.first + rescue + return nil + end + + end + + def current_login_employee + if (!session[:session_token].nil?) + @employee = Employee.find_by_token_session(session[:session_token]) + end + end end diff --git a/app/controllers/base_waiter_controller.rb b/app/controllers/base_waiter_controller.rb index ca4622db..32ae0853 100755 --- a/app/controllers/base_waiter_controller.rb +++ b/app/controllers/base_waiter_controller.rb @@ -4,4 +4,31 @@ class BaseWaiterController < ActionController::Base #before_action :check_installation protect_from_forgery with: :exception + + helper_method :current_company,:current_login_employee,:current_user + + private + def current_user + @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] + end + + # Get current Cashier + def get_cashier + @cashier = Employee.where("role = 'cashier' AND token_session <> ''") + end + + def current_company + begin + return Company.first + rescue + return nil + end + + end + + def current_login_employee + if (!session[:session_token].nil?) + @employee = Employee.find_by_token_session(session[:session_token]) + end + end end diff --git a/app/controllers/concerns/login_verification.rb b/app/controllers/concerns/login_verification.rb index fa588574..a82a5c7f 100755 --- a/app/controllers/concerns/login_verification.rb +++ b/app/controllers/concerns/login_verification.rb @@ -2,8 +2,8 @@ module LoginVerification extend ActiveSupport::Concern included do - before_action :authenticate - helper_method :current_company,:current_login_employee + before_action :authenticate_session_token + helper_method :current_company, :current_login_employee, :current_user, :get_cashier end #this is base api base controller to need to inherit. @@ -15,16 +15,24 @@ module LoginVerification rescue return nil end - end def current_login_employee @employee = Employee.find_by_token_session(session[:session_token]) end + def current_user + @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token] + end + + # Get current Cashiers + def get_cashier + @cashier = Employee.where("role = 'cashier' AND token_session <> ''") + end + protected # Authenticate the user with token based authentication - def authenticate + def authenticate authenticate_session_token || render_unauthorized end @@ -38,9 +46,9 @@ module LoginVerification if @user return true #Maybe log - login? - else - flash[:notice] = 'Invalid Access!' - # return false + # else + # flash[:notice] = 'Invalid Access!' + # # return false end end end diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index f33ebfed..7cabe7f7 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -1,17 +1,15 @@ -# ActionController::Base are used by both Peatio controllers and -# Doorkeeper controllers. class ActionController::Base - before_action :lookup_domain, :set_language + before_action :lookup_domain, :set_locale private - def lookup_domain + def lookup_domain if request.subdomain.present? && request.subdomain != "www" from = request.subdomain.downcase + "." + request.domain.downcase @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase if (!@license.nil?) - # logger.info "Location - " + @license.name + logger.info "Location - " + @license.dbhost ActiveRecord::Base.establish_connection(website_connection(@license)) # authenticate_session_token # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema