diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 49b4ff5d..d9c67962 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -118,7 +118,7 @@ class HomeController < ApplicationController # .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p') # .sum(:grand_total) logger.debug 'hourly_sales<>><><><<<<<<>><<<><><><><><><><><><<>><' - logger.debug @hourly_sales.to_json + logger.debug @hourly_sales.to_json employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time) # .sum("(CASE WHEN sp.payment_method='cash' THEN ((sp.payment_amount) - (sales.amount_changed)) ELSE (sp.payment_amount) END)") @employee_sales = [] diff --git a/app/controllers/origami/dashboard_controller.rb b/app/controllers/origami/dashboard_controller.rb index 25ea8390..1daa652b 100644 --- a/app/controllers/origami/dashboard_controller.rb +++ b/app/controllers/origami/dashboard_controller.rb @@ -4,7 +4,7 @@ class Origami::DashboardController < BaseOrigamiController @shop = Shop.first today = DateTime.now.strftime('%Y-%m-%d') - + @display_type = Lookup.find_by_lookup_type("display_type") @sale_data = Array.new @@ -14,7 +14,7 @@ class Origami::DashboardController < BaseOrigamiController if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" || payment.payment_method == "alipay" pay = Sale.payment_sale('card', today, current_user) @sale_data.push({'card' => pay.payment_amount}) - else + else pay = Sale.payment_sale(payment.payment_method, today, current_user) @sale_data.push({payment.payment_method => pay.payment_amount}) end @@ -38,10 +38,10 @@ class Origami::DashboardController < BaseOrigamiController end else @account_data = nil - end + end @top_items = Sale.top_items(today,current_user) - @total_foc_items = Sale.total_foc_items(today,current_user) + @total_foc_items = Sale.total_foc_items(today,current_user) # get printer info @print_settings = PrintSetting.get_precision_delimiter() @@ -104,12 +104,14 @@ class Origami::DashboardController < BaseOrigamiController end def get_all_menu - @menus = Menu.active.all + @menus = Menu.includes(:menu_categories => {:menu_items => :menu_item_instances}).includes(:menu_categories => {:menu_items => :item_sets }).active.all + @item_attributes = MenuItemAttribute.all.load + @item_options = MenuItemOption.all.load end def get_credit_sales credit_sales = SalePayment.get_credit_sales(params) - if !credit_sales.nil? + if !credit_sales.nil? result = {:status=> true, :data=> credit_sales } else result = {:status=> false, :message=>"There is no record." } diff --git a/app/models/menu_category.rb b/app/models/menu_category.rb index 746bcb79..d8597afa 100755 --- a/app/models/menu_category.rb +++ b/app/models/menu_category.rb @@ -1,7 +1,7 @@ -class MenuCategory < ApplicationRecord +class MenuCategory < ApplicationRecord # before_create :generate_menu_category_code - belongs_to :menu + belongs_to :menu has_many :children, :class_name => "MenuCategory", foreign_key: "menu_category_id" belongs_to :parent, :class_name => "MenuCategory", foreign_key: "menu_category_id", optional: true has_many :menu_items @@ -38,9 +38,7 @@ class MenuCategory < ApplicationRecord end def valid_time - - menu_category = MenuCategory.find(self.id) - menu = Menu.find(menu_category.menu_id) + menu = self.menu from_t = Time.parse(menu.valid_time_from.strftime("%H:%M:%S")) to_t = Time.parse(menu.valid_time_to.strftime("%H:%M:%S")) @@ -64,7 +62,7 @@ class MenuCategory < ApplicationRecord c +=24 current = c*3600 + current_t.min* 60 + current_t.sec - + end end else # (after) noon @@ -77,8 +75,8 @@ class MenuCategory < ApplicationRecord day = Date.today.wday dayresult = menu.valid_days.include?(day.to_s) - - + + if current.between?(from, to) && menu.valid_days.include?(day.to_s) return true else @@ -104,12 +102,12 @@ class MenuCategory < ApplicationRecord private - # def generate_menu_category_code - # self.code = SeedGenerator.generate_code(self.class.name, "C") + # def generate_menu_category_code + # self.code = SeedGenerator.generate_code(self.class.name, "C") # end def self.to_csv - + mc_attributes = %w{id menu_id code name alt_name order_by created_by menu_category_id is_available created_at updated_at} CSV.generate(headers: true) do |csv| csv << mc_attributes @@ -123,7 +121,3 @@ class MenuCategory < ApplicationRecord end end - - - - diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 03e90011..59791cb2 100755 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -11,11 +11,11 @@ class MenuItem < ApplicationRecord belongs_to :account has_many :menu_item_sets - has_many :item_sets, through: :menu_item_sets + has_and_belongs_to_many :item_sets, join_table: "menu_item_sets" validates_presence_of :item_code, :name, :type, :min_qty,:account_id validates_uniqueness_of :item_code - + default_scope { order('item_code asc') } scope :simple_menu_item, -> { where(type: 'SimpleMenuItem') } diff --git a/app/models/sale.rb b/app/models/sale.rb index 98457d0e..98252dbb 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -4,9 +4,9 @@ class Sale < ApplicationRecord #primary key - need to be unique generated for multiple shops before_create :generate_custom_id before_create :generate_receipt_no - belongs_to :cashier, :optional => true + belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee" belongs_to :customer, :optional => true - belongs_to :employees + belongs_to :shift_sale has_many :sale_items has_many :sale_discount_items has_many :sale_discounts @@ -23,6 +23,12 @@ class Sale < ApplicationRecord scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") } scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") } + scope :paid, -> { where(payment_status: 'paid')} + scope :completed, -> { where(sale_status: 'completed') } + scope :date_on, -> (date) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) = ?", Time.zone.formatted_offset, date) } + scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) } + scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) } + scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") } def qty_of(item_instance_code) order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first @@ -770,7 +776,7 @@ def self.daily_sales_list(from,to) ELSE 0 END as credit_amount, SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount, SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount") - .joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") + .along_with_sale_payments_except_void .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) .group("sale_id").to_sql @@ -1104,41 +1110,36 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay end def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter,order_source) - order_sources = Order.select("sale_orders.sale_id, orders.source") - .joins(:sale_orders).to_sql - - credit_payments = SalePayment.select(" + payments_for_credits = SalePayment.select(" sales.sale_id, DATE_FORMAT(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30'),'%d %b %y %h:%i%p') as credit_payment_receipt_date, sale_payments.payment_amount as credit_payment, employees.name as credit_payment_cashier_name, - CONCAT(DATE_FORMAT(CONVERT_TZ(shift_started_at,'+00:00','+06:30'),'%d %b %y %h:%i%p'),' - ',DATE_FORMAT(CONVERT_TZ(shift_closed_at,'+00:00','+06:30'),'%d %b %y %h:%i%p')) as credit_payment_shift_name") - .joins("JOIN sales ON sales.sale_id = sale_payments.sale_id") - .joins("JOIN sale_audits ON sale_audits.sale_id = sales.sale_id AND SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id") - .joins("JOIN shift_sales ON shift_sales.id = sales.shift_sale_id") - .joins("JOIN employees ON employees.id = shift_sales.employee_id").to_sql + CONCAT(DATE_FORMAT(CONVERT_TZ(shift_sales.shift_started_at,'+00:00','+06:30'),'%d %b %y %h:%i%p'),' - ',DATE_FORMAT(CONVERT_TZ(shift_sales.shift_closed_at,'+00:00','+06:30'),'%d %b %y %h:%i%p')) as credit_payment_shift_name") + .joins(:sale_audit) + .joins(:sale => :shift_sale) + .joins(:sale => :cashier).to_sql credits = SalePayment.select(" sale_payments.sale_payment_id, sale_payments.payment_method, sale_payments.payment_amount, sale_payments.payment_status, - sales.sale_id, - sales.receipt_no, - sales.receipt_date as sale_date, - order_sources.source as order_source, - sales.cashier_name, + sales_sale_payments.sale_id, + sales_sale_payments.receipt_no, + sales_sale_payments.receipt_date as sale_date, + sales_sale_payments.cashier_name, + orders.source as order_source, customers.name as customer_name, - IFNULL(credit_payments.credit_payment_receipt_date, '-') as credit_payment_receipt_date, - IFNULL(credit_payments.credit_payment, 0) as credit_payment, - IFNULL(credit_payments.credit_payment_cashier_name, '-') as credit_payment_cashier_name, - IFNULL(credit_payments.credit_payment_shift_name, '-') as credit_payment_shift_name") - .joins("JOIN sales ON sales.sale_id = sale_payments.sale_id") - .joins("JOIN shift_sales ON shift_sales.id = sales.shift_sale_id") - .joins("JOIN customers ON customers.customer_id = sales.customer_id") - .joins("JOIN (#{order_sources}) order_sources ON order_sources.sale_id = sales.sale_id") - .joins("LEFT JOIN (#{credit_payments}) credit_payments ON credit_payments.sale_id = sales.sale_id") - .where("sale_payments.payment_method= ? AND sales.sale_status = ?", 'creditnote', 'completed') + IFNULL(payments_for_credits.credit_payment_receipt_date, '-') as credit_payment_receipt_date, + IFNULL(payments_for_credits.credit_payment, 0) as credit_payment, + IFNULL(payments_for_credits.credit_payment_cashier_name, '-') as credit_payment_cashier_name, + IFNULL(payments_for_credits.credit_payment_shift_name, '-') as credit_payment_shift_name") + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales_sale_payments.sale_id") + .joins(:sale => :customer) + .joins(:sale => :orders) + .credits + .where("sales_sale_payments.sale_status = ?", 'completed') if order_source.present? if order_source == "cashier" @@ -1149,19 +1150,19 @@ def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter, end if filter == 'paid' - credits = credits.where("credit_payment IS NOT NULL") + credits = credits.where("payments_for_credits IS NOT NULL") elsif filter == 'unpaid' - credits = credits.where("credit_payment IS NULL") + credits = credits.where("payments_for_credits IS NULL") end if shift.present? - credits = credits.where("sales.shift_sale_id in (?)",shift.to_a) + credits = credits.where("sales_sale_payments.shift_sale_id in (?)",shift.to_a) elsif shift_sale_range.present? - credits = credits.where("sales.shift_sale_id in (?)",shift_sale_range.to_a) + credits = credits.where("sales_sale_payments.shift_sale_id in (?)",shift_sale_range.to_a) else - credits = credits.where("sales.receipt_date between ? and ?",from,to) + credits = credits.where("sales_sale_payments.receipt_date between ? and ?",from,to) end - credits = credits.group("sales.sale_id") + credits = credits.group("sale_payments.sale_payment_id, sales_sale_payments.sale_id") end def self.get_void_sale(shift,from,to) @@ -1480,329 +1481,96 @@ end end def self.employee_sales(today,current_user,from,to,from_time,to_time) - #sub query for credit payment - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" - end - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + ShiftSale.current_open_shift(current_user.id) end - sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) - FROM sale_payments - INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - INNER JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sales.sale_status='completed'" + payments_for_credits = SalePayment.joins(:sale_audit).to_sql - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - #sub query for credit payment - - changed_query = "SELECT SUM(s.amount_changed) - FROM `sales` s - WHERE s.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - query = Sale.employee_sale(today,nil,from,to,from_time,to_time) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - changed_query += " AND s.shift_sale_id=sales.shift_sale_id" - query = Sale.employee_sale(today,nil,from,to,from_time,to_time) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id=#{shift.id}" - query = Sale.employee_sale(today,shift,from,to,from_time,to_time) - end - end - end - else - if current_user.nil? - query = Sale.employee_sale(today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - changed_query += " AND s.shift_sale_id=sales.shift_sale_id" - query = Sale.employee_sale(today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id=#{shift.id}" - query = Sale.employee_sale(today,shift) - end - end - end - end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = employee_sale(today, shift, from, to, from_time, to_time) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") + .select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount, + CASE WHEN sale_payments.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' + ELSE sale_payments.payment_method END AS payment_method, employees.name AS e_name") end def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count") .where('sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',today,today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end + return query end def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") .where('sales.sale_status = "completed" 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" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.sum("sp.payment_amount") end def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" + payments_for_credits = SalePayment.joins(:sale_audit).to_sql + + query = SalePayment.credits + .joins(:sale) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sale_payments.sale_id") + .where("sale_payments.payment_method= ? AND sales.sale_status = ?", 'creditnote', 'completed') if (!from.nil? && !to.nil?) && (from != "" && to!="") + query = query.merge(Sale.date_between(from, to)) if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" + query = query.merge(Sale.time_between(from_time, to_time)) end else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + query = query.merge(Sale.date_on(today)) end - - sub_query = SalePayment.select("(CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) as total_credit_payment") - .joins(" JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id") - .joins(" JOIN sales ON sale_payments.sale_id = sales.sale_id") - .where("sales.sale_status='completed'") - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'") - else - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'") - end - else - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'") - end - - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote"') - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',from,to,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - end - end - end - else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',today,today,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end - return query + + return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)") end def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=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"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -1813,147 +1581,50 @@ end .where('sales.sale_status = "completed"') .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end + return query end def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - time_query = '' - if !from_time.nil? && !to_time.nil? - time_query = " and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - end + payments_for_credits = SalePayment.joins(:sale_audit).to_sql - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" + query = Sale.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount") + .joins(:sale_payments) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") + .completed - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" - end - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) - FROM sale_payments - INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - INNER JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sales.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - changed_query = "SELECT SUM(s.amount_changed) - FROM `sales` s - WHERE s.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - where('sales.sale_status = "completed"') if payment_method == 'card' - query = query.where('(sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method="alipay" or sp.payment_method="paymal" or sp.payment_method="dinga" or sp.payment_method="JunctionPay" or sp.payment_method = "giftvoucher")') + query = query.merge(SalePayment.cards) else - query = query.where("sp.payment_method = '#{payment_method}'") + query = query.where("sale_payments.payment_method = ?", payment_method) end + if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id = #{shift.id}" - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time,to_time) end else - if current_user.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id = #{shift.id}" - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - end - end + query = query.date_on(today) end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.where("sales.shift_sale_id = ?", shift.id) + end + end + query.first end def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -1984,45 +1655,18 @@ end .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and c.membership_id is null') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end - query = query.first() end @@ -2031,42 +1675,16 @@ end .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?)',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id).first() - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id).first() - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -2119,50 +1737,25 @@ end # end def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=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"') + query = Sale.select("count(distinct sale_orders.order_id) as total_order") + .joins(:sale_orders) + .completed if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end - query = query.first() + + query = query.first end def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2171,44 +1764,19 @@ end .joins("JOIN accounts as b ON b.id = a.account_id") .where('sales.sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end + return query end def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2216,45 +1784,19 @@ end .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end - query = query.first() + query = query.first end def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2262,42 +1804,16 @@ end .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'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end query = query.group("a.product_code") @@ -2309,42 +1825,16 @@ end query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + query = query.date_between(from, to) + if !from_time.nil? && !to_time.nil? + query = query.time_between(from_time, to_time) end else - if current_user.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - end + query = query.date_on(today) + end + if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + if shift = ShiftSale.current_open_shift(current_user.id) + query = query.joins("sales.shift_sale_id = ?", shift.id) end end query = query.count() @@ -2540,27 +2030,24 @@ def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time= end def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=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' ") + query = Sale.joins(:cashier) + .joins(:sale_payments) + .paid.completed if !from.nil? && !to.nil? + query = query.date_between(from, to) if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'") - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + query = query.time_between(from_time, to_time) end else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = '#{today}'") + query = query.date_on(today) end if !shift.nil? - query = query.where("sales.shift_sale_id ='#{shift.id}'") + query = query.where("sales.shift_sale_id = ?", shift.id) end - query = query.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' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") - - return query + query = query.group("payment_method","employees.name") + .order("employees.name") end # Start hourly sale item report diff --git a/app/models/sale_audit.rb b/app/models/sale_audit.rb index d96034ad..1488cad1 100755 --- a/app/models/sale_audit.rb +++ b/app/models/sale_audit.rb @@ -6,6 +6,8 @@ class SaleAudit < ApplicationRecord belongs_to :sale + belongs_to :credit_payment, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id", class_name: "SalePayment" + def self.sync_sale_audit_records(sale_audits) if !sale_audits.nil? sale_audits.each do |sa| @@ -163,7 +165,7 @@ class SaleAudit < ApplicationRecord end end end - + return card_balance_amount end diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index 5a1fdb72..e0ad2383 100755 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -6,8 +6,13 @@ class SalePayment < ApplicationRecord belongs_to :sale + has_one :sale_audit, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id" + attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status,:action_by + scope :credits, -> { where(payment_method: 'creditnote') } + scope :cards, -> { where(payment_method: ['mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher']) } + def self.sync_sale_payment_records(sale_payments) if !sale_payments.nil? sale_payments.each do |sp| diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index 2d263c21..07217103 100755 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -15,6 +15,7 @@ class ShiftSale < ApplicationRecord belongs_to :cashier_terminal belongs_to :employee, :foreign_key => 'employee_id' + has_many :sales def self.current_shift # today_date = DateTime.now.strftime("%Y-%m-%d") @@ -25,7 +26,7 @@ class ShiftSale < ApplicationRecord def self.current_open_shift(current_user) #if current_user #find open shift where is open today and is not closed and login by current cashier - #DATE(shift_started_at)=? and + #DATE(shift_started_at)=? and today_date = DateTime.now.strftime("%Y-%m-%d") shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take return shift @@ -43,7 +44,7 @@ class ShiftSale < ApplicationRecord # status = 'updated' if shift_sale.nil? shift_sale = ShiftSale.new - # status = 'created' + # status = 'created' end shift_sale.id = ss['id'] @@ -118,7 +119,7 @@ class ShiftSale < ApplicationRecord credit = saleobj.get_credit_amount other_sales = saleobj.get_other_amount tax = saleobj.get_commerical_tax - + if type == "void" self.total_revenue = self.total_revenue.to_f - saleobj.total_amount.to_f self.total_discounts = self.total_discounts - saleobj.total_discount @@ -137,7 +138,7 @@ class ShiftSale < ApplicationRecord self.takeaway_count = self.takeaway_count - 1 end - + self.save end end @@ -149,11 +150,11 @@ class ShiftSale < ApplicationRecord end def self.get_by_shift_other_payment(shift) - + other_payment = Sale.select("sale_payments.payment_method as name, - SUM(case when (sale_payments.payment_method='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_amount, - SUM(case when (sale_payments.payment_method='visa') then (sale_payments.payment_amount) else 0 end) as visa_amount, - SUM(case when (sale_payments.payment_method='master') then (sale_payments.payment_amount) else 0 end) as master_amount, + SUM(case when (sale_payments.payment_method='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_amount, + SUM(case when (sale_payments.payment_method='visa') then (sale_payments.payment_amount) else 0 end) as visa_amount, + SUM(case when (sale_payments.payment_method='master') then (sale_payments.payment_amount) else 0 end) as master_amount, SUM(case when (sale_payments.payment_method='jcb') then (sale_payments.payment_amount) else 0 end) as jcb_amount, SUM(case when (sale_payments.payment_method='unionpay') then (sale_payments.payment_amount) else 0 end) as unionpay_amount, SUM(case when (sale_payments.payment_method='alipay') then (sale_payments.payment_amount) else 0 end) as alipay_amount, @@ -161,19 +162,19 @@ class ShiftSale < ApplicationRecord SUM(case when (sale_payments.payment_method='dinga') then (sale_payments.payment_amount) else 0 end) as dinga_amount, SUM(case when (sale_payments.payment_method='giftvoucher') then (sale_payments.payment_amount) else 0 end) as giftvoucher_amount, SUM(case when (sale_payments.payment_method='JunctionPay') then (sale_payments.payment_amount) else 0 end) as junctionpay_amount, - SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount, - SUM(case when (sale_payments.payment_method='paymal') then (sale_payments.payment_amount) else 0 end) as paymal_amount, + SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount, + SUM(case when (sale_payments.payment_method='paymal') then (sale_payments.payment_amount) else 0 end) as paymal_amount, SUM(case when (sale_payments.payment_method='paypar') then (sale_payments.payment_amount) else 0 end) as paypar_amount") .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") .where("sales.shift_sale_id =? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.id) end def self.calculate_total_price_by_accounts(shift,type) - query = Sale.select("acc.title as account_name," + + query = Sale.select("acc.title as account_name," + "SUM(case when (acc.id=i.account_id) then (i.price) else 0 end) as total_price") - + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id "+ - "JOIN accounts acc ON acc.id = i.account_id" + + "JOIN accounts acc ON acc.id = i.account_id" + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id") if type == 'discount' query = query.where("sales.shift_sale_id =? and sale_status = 'completed' and i.remark = 'Discount'", shift.id) @@ -187,17 +188,17 @@ class ShiftSale < ApplicationRecord def self.get_total_member_discount(shift) query = Sale.select("SUM(sales.total_discount) as member_discount") .where("shift_sale_id =? and sale_status = 'completed' and discount_type = 'member_discount'", shift.id) - - end - def self.get_total_dinein(shift) + 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 + end - def self.get_total_takeway(shift) + 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) @@ -211,12 +212,12 @@ class ShiftSale < ApplicationRecord # .where('s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null and s.receipt_date between ? and ?',from, to) # end - def self.get_total_other_charges(shift) + 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 + end def self.search(filter,from,to) if filter.blank? diff --git a/app/views/origami/addorders/_menu_item.json.jbuilder b/app/views/origami/addorders/_menu_item.json.jbuilder index a18645c8..2768fa9c 100755 --- a/app/views/origami/addorders/_menu_item.json.jbuilder +++ b/app/views/origami/addorders/_menu_item.json.jbuilder @@ -1,53 +1,20 @@ # Format for attributes json attr_format = [] -# Format for attributes json -if item.item_attributes.count > 0 - item.item_attributes.each do|attr_id| - menu_attr = MenuItemAttribute.find(attr_id) - if attr_format.count == 0 - attr_format.push({ type: menu_attr.attribute_type, values: [menu_attr.name] }) - next - end - - attr_format.each do |af| - if menu_attr.attribute_type.in? attr_format.map {|k| k[:type]} - if menu_attr.attribute_type == af[:type] - af[:values].push(menu_attr.name) - end - else - new_attr = {type: menu_attr.attribute_type, values: [ menu_attr.name ] } - attr_format.push(new_attr) - break - end - end - end +# Format for attributes json +if item.item_attributes.count > 0 + item_attributes = MenuItemAttribute.where(id: item.item_attributes) + attr_format = item_attributes.group_by {|att| att.attribute_type }.map { |type, values| {type: type, values: values.map(&:name)} } end # Format for option json opt_format = [] -# Format for attributes json -if item.item_options.count > 0 - item.item_options.each do|opt| - menu_opt = MenuItemOption.find(opt) - if opt_format.count == 0 - opt_format.push({ type: menu_opt.option_type, values: [menu_opt.name] }) - next - end - - opt_format.each do |of| - if menu_opt.option_type.in? opt_format.map {|k| k[:type]} - if menu_opt.option_type == of[:type] - of[:values].push(menu_opt.name) - end - else - new_opt = {type: menu_opt.option_type, values: [ menu_opt.name ] } - opt_format.push(new_opt) - break - end - end - end +# Format for attributes json +if item.item_options.count > 0 + item_options = MenuItemOption.where(id: item.item_options) + opt_format = item_options.group_by {|opt| opt.option_type }.map { |type, values| {type: type, values: values.map(&:name)} } end + #Menu Item Information json.id item.id json.code item.item_code @@ -63,8 +30,8 @@ json.is_available item.is_available json.is_sub_item item.is_sub_item json.unit item.unit -# Item Sets of Menu Item -json.item_sets item.item_sets do |its| +# Item Sets of Menu Item +json.item_sets item.item_sets.includes(:menu_item_instances) do |its| json.id its.id json.name its.name json.alt_name its.alt_name @@ -73,7 +40,7 @@ json.item_sets item.item_sets do |its| json.instances its.menu_item_instances do |i| json.id i.id end -end +end json.attributes attr_format json.options opt_format @@ -92,22 +59,17 @@ json.options opt_format json.instances item.menu_item_instances do |is| if is.is_available # Convert id to name for attributes - instance_attr = [] - is.item_attributes.each do |ia| - mItemAttr = MenuItemAttribute.find(ia).name - instance_attr.push(mItemAttr) - end - - json.id is.id - json.code is.item_instance_code - json.name is.item_instance_name - json.price is.price - json.is_available is.is_available - json.is_default is.is_default - json.is_on_promotion is.is_on_promotion - json.promotion_price is.promotion_price - json.values instance_attr - # json.item_sets is.item_sets + instance_attr = MenuItemAttribute.where(id: item.item_attributes).pluck(:name) + + json.id is.id + json.code is.item_instance_code + json.name is.item_instance_name + json.price is.price + json.is_available is.is_available + json.is_default is.is_default + json.is_on_promotion is.is_on_promotion + json.promotion_price is.promotion_price + json.values instance_attr end end @@ -116,4 +78,4 @@ end # json.set_items item.children.each do |item| # json.partial! 'api/restaurant/menu/menu_item', item: item # end -# end \ No newline at end of file +# end diff --git a/app/views/origami/dashboard/_menu.json.jbuilder b/app/views/origami/dashboard/_menu.json.jbuilder index d558c4ea..9c9f1963 100644 --- a/app/views/origami/dashboard/_menu.json.jbuilder +++ b/app/views/origami/dashboard/_menu.json.jbuilder @@ -12,11 +12,11 @@ if (menu.menu_categories) # else # categories = menu.menu_categories # end - categories = menu.menu_categories + categories = menu.menu_categories json.categories categories do |category| if category.is_available - menu_category = MenuCategory.find_by_menu_category_id(category.id) - if !menu_category.nil? + parent_category = category.parent + if !parent_category.nil? json.sub_category "true" else json.sub_category "false" @@ -34,7 +34,7 @@ if (menu.menu_categories) json.is_available category.is_available if !order_by.nil? && order_by.value == "name" - menu_items = MenuItem.unscoped.where("menu_category_id = ?",category.id).order("name asc") + menu_items = category.menu_items.sort_by(&:name) else menu_items = category.menu_items end @@ -42,7 +42,68 @@ if (menu.menu_categories) if category.menu_items json.items menu_items do |item| if item.is_available - json.partial! 'origami/addorders/menu_item', item: item + # Format for attributes json + attr_format = [] + # Format for attributes json + if item.item_attributes.count > 0 + item_attributes = @item_attributes.select{ |x| item.item_attributes.include?(x.id.to_s) } + attr_format = item_attributes.group_by {|att| att.attribute_type }.map { |type, values| {type: type, values: values.map(&:name)} } + end + + # Format for option json + opt_format = [] + # Format for attributes json + if item.item_options.count > 0 + item_options = @item_options.select{ |x| item.item_options.include?(x.id.to_s) } + opt_format = item_options.group_by {|opt| opt.option_type }.map { |type, values| {type: type, values: values.map(&:name)} } + end + + #Menu Item Information + json.id item.id + json.code item.item_code + json.name item.name + json.alt_name item.alt_name + json.image item.image_path.url + json.description item.description + json.information item.information + json.type item.type + json.account_id item.account_id + json.min_qty item.min_qty + json.is_available item.is_available + json.is_sub_item item.is_sub_item + json.unit item.unit + + # Item Sets of Menu Item + json.item_sets item.item_sets.map { |its| + { id: its.id, + name: its.name, + alt_name: its.alt_name, + min_selectable_qty: its.min_selectable_qty, + max_selectable_qty: its.max_selectable_qty, + instances: its.menu_item_instances.pluck(:id).map { |id| {id: id}} + } + } + + json.attributes attr_format + json.options opt_format + + json.instances item.menu_item_instances do |is| + if is.is_available + # Convert id to name for attributes + instance_attr = @item_attributes.select{ |x| item.item_attributes.include?(x.id) }.pluck(:name) + + json.id is.id + json.code is.item_instance_code + json.name is.item_instance_name + json.price is.price + json.is_available is.is_available + json.is_default is.is_default + json.is_on_promotion is.is_on_promotion + json.promotion_price is.promotion_price + json.values instance_attr + end + end + end end end