diff --git a/app/controllers/reports/saleitem_controller.rb b/app/controllers/reports/saleitem_controller.rb index 422c3020..428bf9d7 100755 --- a/app/controllers/reports/saleitem_controller.rb +++ b/app/controllers/reports/saleitem_controller.rb @@ -15,7 +15,7 @@ class Reports::SaleitemController < BaseReportController shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',shift_sale.shift_started_at, shift_sale.shift_closed_at) end end - @sale_data, @discount_data , @cash_data , @card_data , @credit_data , @foc_data , @grand_total , @change_amount = Sale.get_by_shift_items(shift_sale_range,shift, from, to, Sale::SALE_STATUS_COMPLETED) + @sale_data, @other_charges,@discount_data , @cash_data , @card_data , @credit_data , @foc_data , @grand_total , @change_amount = Sale.get_by_shift_items(shift_sale_range,shift, from, to, Sale::SALE_STATUS_COMPLETED) @account_cate_count = Hash.new {|hash, key| hash[key] = 0} diff --git a/app/models/sale.rb b/app/models/sale.rb index 55f6f516..7397383a 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -608,41 +608,38 @@ def self.get_by_shift_sale(from,to,status) end def self.get_item_query() - # query = SaleItem.select("acc.title as account_name,account_id, item_instance_code as item_code, " + - # "SUM(qty * unit_price) as grand_total,SUM(qty) as total_item," + - # " unit_price as unit_price,product_name as product_name, 'cat' as" + - # " menu_category_name,'test' as menu_category_id ") + 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.remark as status_type,"+ + " i.unit_price as unit_price,i.product_name as product_name, mc.name as" + + " menu_category_name,mc.id as menu_category_id ") - # query = query.joins("JOIN sales s ON s.sale_id = sale_items.sale_id" + - # " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id") - # # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + + " JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + + " JOIN menu_items mi ON mi.id = mii.menu_item_id" + + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + + " JOIN menu_categories mc ON mc.id = mi.menu_category_id ") + # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") + query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") + # query = query.where("i.item_instance_code IS NOT NULL") + query = query.group("i.product_name").order("mi.account_id, mi.menu_category_id") +end - # query = query.joins(" JOIN accounts acc ON acc.id = account_id") - # query = query.where("item_instance_code is not NULL") - # query = query.group("item_instance_code").order("account_id") - - query = Sale.select("acc.title as account_name,mi.account_id, i.item_instance_code as item_code, " + - "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," + - "i.remark as status_type,"+ - " i.unit_price as unit_price,i.product_name as product_name, mc.name as" + - " menu_category_name,mc.id as menu_category_id ") - - query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id" + - " JOIN menu_item_instances mii ON i.item_instance_code = mii.item_instance_code" + - " JOIN menu_items mi ON mi.id = mii.menu_item_id" + - " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + - " JOIN menu_categories mc ON mc.id = mi.menu_category_id ") - # "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ") - - query = query.joins(" JOIN accounts acc ON acc.id = mi.account_id") - query = query.where("i.item_instance_code IS NOT NULL") - query = query.group("i.product_name").order("mi.account_id, mi.menu_category_id") +def self.get_other_charges() + query = Sale.select("i.account_id as account_id, " + + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," + + "i.remark as status_type,"+ + " i.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'") + query = query.group("i.sale_item_id") 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 total_card_amount = 0 total_cash_amount = 0 @@ -650,10 +647,14 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) total_foc_amount = 0 total_grand_total = 0 + other_charges = self.get_other_charges() + puts other_charges.to_json + if shift.present? query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a) + other_charges = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a) discount_query = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:total_discount) - change_amount = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:amount_changed) + change_amount = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:amount_changed) sale_cash = Sale.select("SUM(case when (sale_payments.payment_method ='mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount, SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_amount, SUM(case when (sale_payments.payment_method='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, @@ -671,8 +672,9 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) ### => get all sales range in shift_sales elsif shift_sale_range.present? query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a) + other_charges = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a) discount_query = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:total_discount) - change_amount = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:amount_changed) + change_amount = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:amount_changed) sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount, SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_amount, SUM(case when (sale_payments.payment_method='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, @@ -685,12 +687,14 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) total_credit_amount += s_c.credit_amount.to_f total_foc_amount += s_c.foc_amount.to_f end + total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f else query = query.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to) + other_charges = other_charges.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to) discount_query = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:total_discount) - change_amount = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:amount_changed) + change_amount = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:amount_changed) sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar') then (sale_payments.payment_amount) else 0 end) as card_amount, SUM(case when (sale_payments.payment_method='cash') then (sale_payments.payment_amount) else 0 end) as cash_amount, SUM(case when (sale_payments.payment_method='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, @@ -707,7 +711,7 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status) end - return query, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount + return query,other_charges, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount end def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type) diff --git a/app/views/reports/saleitem/index.html.erb b/app/views/reports/saleitem/index.html.erb index fe41805b..f1a775bd 100644 --- a/app/views/reports/saleitem/index.html.erb +++ b/app/views/reports/saleitem/index.html.erb @@ -50,6 +50,7 @@ <% cate_arr = Array.new %> <% sub_total = 0 %> + <% other_sub_total = 0 %> <% count = 0 %> <% total_price = 0 %> <% cate_count = 0 %> @@ -63,68 +64,97 @@ <% @sale_data.each do |sale| %> - <% if sale.total_item > 0 - total_qty += sale.total_item - end %> + <% if sale.total_item > 0 + total_qty += sale.total_item + end %> - <% if !acc_arr.include?(sale.account_id) %> -