update sale item with other charges

This commit is contained in:
Aung Myo
2017-12-01 11:47:17 +06:30
parent 3846b319af
commit dbd3e3e69b
3 changed files with 121 additions and 87 deletions

View File

@@ -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)