From c1865f5356050c7a6c8d9dd7f911bfa8d1adaec3 Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 17:40:48 +0630 Subject: [PATCH 01/11] change sale.rb file --- app/models/sale.rb | 958 +++++++-------------------------------------- 1 file changed, 131 insertions(+), 827 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 5196672c..f42b62a0 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -78,29 +78,14 @@ class Sale < ApplicationRecord self.tax_type = "exclusive" # set cashier - 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 + if cashier != nil self.cashier_id = cashier.id self.cashier_name = cashier.name - self.shift_sale_id = shift.id else - 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 + self.cashier_id = requested_by.id + self.cashier_name = requested_by.name end - # set waiter self.requested_by = requested_by.name @@ -227,7 +212,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.status = item.remark + sale_item.remark = item.remark sale_item.qty = item.qty sale_item.unit_price = item.price @@ -255,7 +240,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.status = nil + sale_item.remark = nil sale_item.qty = item["quantity"] sale_item.unit_price = item["price"] @@ -381,7 +366,7 @@ class Sale < ApplicationRecord rounding_adjustment = 0 sales_items.each do |item| - if item.status != 'void' && item.status != 'foc' + if item.remark != 'void' && item.remark != 'foc' #compute each item and added to total subtotal_price = subtotal_price + item.price @@ -417,36 +402,35 @@ class Sale < ApplicationRecord tax_profiles = TaxProfile.all.order("order_by asc") customer = Customer.find(sale.customer_id) # #Creat new tax records - 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 + 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 - - sale_tax.inclusive = tax.inclusive - sale_tax.save + # 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 end end end + sale.total_tax = total_tax_amount end @@ -758,8 +742,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.status as status_type,"+ - " i.unit_price,i.price as price,i.product_name as product_name, mc.name as" + + "i.remark as status_type,"+ + " mii.price as 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" + @@ -770,14 +754,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,i.unit_price") + query = query.group("acc.title,mi.account_id,mi.menu_category_id,i.product_name") .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.status as status_type,"+ + "i.remark 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'") @@ -786,6 +770,7 @@ 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,27 +846,6 @@ 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? @@ -1089,376 +1053,95 @@ end return tax end - 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," + + def self.top_products(today) + 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 + .order("SUM(i.qty) DESC").limit(10) end - 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," + + def self.bottom_products(today) + 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 + .order("SUM(i.qty) ASC").limit(10) end - 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") + def self.hourly_sales(today) + 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,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") + def self.employee_sales(today) + 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,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.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_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,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 + 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,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 + 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,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 + 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,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 + 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,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 + 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, 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() + 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) 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 + 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 - 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) + def self.total_customer(today) + dinein_cnt = self.total_dinein(today) + takeaway_cnt = self.total_takeaway(today) + membership_cnt = self.total_membership(today) total_cus = 0 if !dinein_cnt.nil? || !takeaway_cnt.nil? || !membership_cnt.nil? @@ -1468,448 +1151,69 @@ end return total_cus end - 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 + 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() end - 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 + 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() end - 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 + 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() end - 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 + 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() end - 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 + 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() end - 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 + 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) end - 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 + 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() end - 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 + 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() end - 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 + 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") end #card sale trans data From 98795478b15d889b37f6904f9a8c982f355f91d6 Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 17:46:02 +0630 Subject: [PATCH 02/11] add product sale data --- app/models/sale.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/models/sale.rb b/app/models/sale.rb index f42b62a0..d8a91daf 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -846,6 +846,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? From 9daf5c92ef7b0498dc4d2c9a9be7ce786e835b7d Mon Sep 17 00:00:00 2001 From: phyusin Date: Tue, 27 Feb 2018 18:07:15 +0630 Subject: [PATCH 03/11] check sale data --- app/models/sale.rb | 876 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 768 insertions(+), 108 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index d8a91daf..7aa18282 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1074,95 +1074,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? @@ -1172,69 +1453,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 From 300c1ea4030eb840566aa49ac87d2bd0ba9ad45c Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 10:05:40 +0630 Subject: [PATCH 04/11] cadd close cashier customise pdf in readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 97f43595..70b821d1 100755 --- a/README.md +++ b/README.md @@ -108,6 +108,11 @@ sudo cat /etc/sudoers ### cope and parse %superuser ALL=(ALL) NOPASSWD: ALL +For CloseCashierCustomisePdf in lookups + *** change CloseCashierPdf to CloseCashierCustomisePdf + 1) settings/print_settings + 2) settings/lookups => { type:print_settings, name: CloseCashierCustomisePdf, value:1 } + * ToDo list 1. Migration From b073ebbeebcad845be1efcd7c4b59e1ba56976e2 Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 10:20:50 +0630 Subject: [PATCH 05/11] change ReceiptBillPdf --- app/models/print_setting.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/print_setting.rb b/app/models/print_setting.rb index b4f17a62..d28b852b 100755 --- a/app/models/print_setting.rb +++ b/app/models/print_setting.rb @@ -3,6 +3,6 @@ class PrintSetting < ApplicationRecord validates_presence_of :name, :unique_code, :printer_name, :page_width, :page_height, :print_copies def self.get_precision_delimiter - PrintSetting.find_by_unique_code("CloseCashierPdf") + PrintSetting.find_by_unique_code("ReceiptBillPdf") end end From 8e4c3864daf111cf42ca09834f8325c75fdfa870 Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 15:27:29 +0630 Subject: [PATCH 06/11] check query for product sales --- app/models/sale.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 7aa18282..2138405c 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -852,7 +852,7 @@ def self.get_menu_item_query(order_by) 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.unit_price != mii.price 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" + @@ -862,8 +862,8 @@ def self.get_menu_item_query(order_by) " 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}") + .group("mc.id, menu_items.name, si.qty, (CASE WHEN si.unit_price != mii.price THEN si.unit_price ELSE mii.price END)") + .order("menu_items.menu_category_id #{order_by}, si.qty #{order_by}") end #product sale report query @@ -1080,7 +1080,7 @@ end " 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'") + "and sale_status= 'completed' or i.remark!='void'") .group('mi.name') .order("SUM(i.qty) DESC").limit(20) else @@ -1088,7 +1088,7 @@ end " 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'") + "and sale_status= 'completed' or i.remark!='void'") .group('mi.name') .order("SUM(i.qty) DESC").limit(20) end @@ -1100,7 +1100,7 @@ end " 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'") + "and sale_status= 'completed'") .group('mi.name') .order("SUM(i.qty) ASC").limit(20) else @@ -1108,7 +1108,7 @@ end " 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'") + "and sale_status= 'completed'") .group('mi.name') .order("SUM(i.qty) ASC").limit(20) end From 7c8b5def810f66c1561383faa5355ba254d2da21 Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 15:42:07 +0630 Subject: [PATCH 07/11] check product sale --- app/models/sale.rb | 4 ++-- app/views/reports/product_sale/index.html.erb | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 2138405c..e433a999 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -862,8 +862,8 @@ def self.get_menu_item_query(order_by) " 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 != mii.price THEN si.unit_price ELSE mii.price END)") - .order("menu_items.menu_category_id #{order_by}, si.qty #{order_by}") + .group("si.qty, mc.id, menu_items.name, (CASE WHEN si.unit_price != mii.price 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 diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb index eb797122..b8ab6598 100755 --- a/app/views/reports/product_sale/index.html.erb +++ b/app/views/reports/product_sale/index.html.erb @@ -69,12 +69,7 @@   - <% 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 '-' %> From 32818abf4917fb63c36c6317786a359c2310ac2c Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 15:44:15 +0630 Subject: [PATCH 08/11] change in xls --- app/views/reports/product_sale/index.xls.erb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/views/reports/product_sale/index.xls.erb b/app/views/reports/product_sale/index.xls.erb index 7123588c..f0410810 100755 --- a/app/views/reports/product_sale/index.xls.erb +++ b/app/views/reports/product_sale/index.xls.erb @@ -34,12 +34,7 @@   - <% 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 '-' %> From bb6b3794b3ca213c97654fcbf285f56ebd7d73fa Mon Sep 17 00:00:00 2001 From: San Wai Lwin Date: Wed, 28 Feb 2018 16:16:14 +0630 Subject: [PATCH 09/11] Edit in dashboard Total Sale->Net Sale, Grand Total->Gross Sale to same with close_cashier_customise_pdf.rb --- app/views/home/dashboard.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index 91cce7e3..a30cae23 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -164,7 +164,7 @@ <%= @summ_sale.total_receipt %> - <%= t("views.right_panel.detail.total") %> <%= t :sale %> : + <%= t("views.right_panel.detail.net") %> <%= t :sale %> : <%= number_with_precision( @summ_sale.total_amount, precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> @@ -176,7 +176,7 @@ <%= 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") %> : + <%= t("views.right_panel.detail.gross_sale") %> : <%= number_with_precision( @summ_sale.grand_total , precision: precision.to_i ,delimiter: delimiter) rescue number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) %> From 2f7d9523ec9c9571937703b51732562072d15955 Mon Sep 17 00:00:00 2001 From: phyusin Date: Wed, 28 Feb 2018 16:26:40 +0630 Subject: [PATCH 10/11] check product sale --- app/models/sale.rb | 9 +++-- app/views/reports/product_sale/index.html.erb | 38 ++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index e433a999..4a12715f 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -851,18 +851,19 @@ 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.qty != '' THEN SUM(si.qty) ELSE 0 END) as total_item," + "(CASE WHEN si.unit_price != mii.price 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, + "mii.price as unit_price, mii.item_instance_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 ") + " 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("si.qty, mc.id, menu_items.name, (CASE WHEN si.unit_price != mii.price THEN si.unit_price ELSE mii.price END)") + .group("mc.id, mii.item_instance_name, (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 diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb index b8ab6598..a62e0d73 100755 --- a/app/views/reports/product_sale/index.html.erb +++ b/app/views/reports/product_sale/index.html.erb @@ -47,6 +47,7 @@ <%= 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") %> + <%= t("views.right_panel.detail.remark") %> @@ -58,24 +59,25 @@ <% 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 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.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 '-' %> - + +   + <%= 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 '-' %> + <%= sale.status_type rescue '' %> + <% end %> @@ -84,7 +86,7 @@ Total <%= total_qty %> - <%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> + <%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> <% end %> From 5eec1ad37714329f956278af1f47228207ef1a42 Mon Sep 17 00:00:00 2001 From: phyusin Date: Mon, 5 Mar 2018 14:12:30 +0630 Subject: [PATCH 11/11] check product sale query --- app/models/sale.rb | 9 +++++---- app/views/reports/product_sale/index.html.erb | 13 ++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 4a12715f..32f196ff 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -851,10 +851,10 @@ 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 != '' THEN SUM(si.qty) ELSE 0 END) as total_item," + + "(CASE WHEN si.qty IS NOT NULL THEN SUM(si.qty) ELSE 0 END) as total_item," + "(CASE WHEN si.unit_price != mii.price 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, mii.item_instance_name as product_name, + "(CASE WHEN si.qty IS NOT NULL THEN (SUM(si.qty) * si.unit_price) ELSE 0 END) as grand_total," + + "mii.price as unit_price, (CASE WHEN si.product_name IS NOT NULL THEN si.product_name ELSE mii.item_instance_name END) as product_name, mc.name as" + " menu_category_name,mc.id as menu_category_id, si.remark as status_type, si.price as price ") @@ -863,7 +863,8 @@ def self.get_menu_item_query(order_by) " 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, mii.item_instance_name, (CASE WHEN si.unit_price > 0 THEN si.unit_price ELSE mii.price END)") + .where("(CASE WHEN s.sale_status IS NOT NULL THEN s.sale_status='completed' ELSE 1 END)") + .group("mc.id, (CASE WHEN si.product_name IS NOT NULL THEN si.product_name ELSE mii.item_instance_name END)") .order("si.qty #{order_by}, menu_items.menu_category_id #{order_by}") end #product sale report query diff --git a/app/views/reports/product_sale/index.html.erb b/app/views/reports/product_sale/index.html.erb index a62e0d73..ffad886a 100755 --- a/app/views/reports/product_sale/index.html.erb +++ b/app/views/reports/product_sale/index.html.erb @@ -47,7 +47,6 @@ <%= 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") %> - <%= t("views.right_panel.detail.remark") %> @@ -59,24 +58,28 @@ <% total_qty = 0 %> <% @sale_data.each do |sale| %> + <% grand_total += sale.grand_total %> <% 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) %> <%= sale.menu_category_name %> + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + <% cate_arr = Array.new %> +   + <% 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 '-' %> - <%= sale.status_type rescue '' %> @@ -86,7 +89,7 @@ Total <%= total_qty %> - <%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> + <%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-' %> <% end %>