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

@@ -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) shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',shift_sale.shift_started_at, shift_sale.shift_closed_at)
end end
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} @account_cate_count = Hash.new {|hash, key| hash[key] = 0}

View File

@@ -608,20 +608,7 @@ def self.get_by_shift_sale(from,to,status)
end end
def self.get_item_query() def self.get_item_query()
# query = SaleItem.select("acc.title as account_name,account_id, item_instance_code as item_code, " + 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(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 = 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 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," + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," +
"i.remark as status_type,"+ "i.remark as status_type,"+
" i.unit_price as unit_price,i.product_name as product_name, mc.name as" + " i.unit_price as unit_price,i.product_name as product_name, mc.name as" +
@@ -633,16 +620,26 @@ def self.get_item_query()
" JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" + " JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id" +
" JOIN menu_categories mc ON mc.id = mi.menu_category_id ") " JOIN menu_categories mc ON mc.id = mi.menu_category_id ")
# "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_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.joins(" JOIN accounts acc ON acc.id = mi.account_id")
query = query.where("i.item_instance_code IS NOT NULL") # query = query.where("i.item_instance_code IS NOT NULL")
query = query.group("i.product_name").order("mi.account_id, mi.menu_category_id") query = query.group("i.product_name").order("mi.account_id, mi.menu_category_id")
end end
def self.get_other_charges()
query = Sale.select("i.account_id as account_id, " +
"SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," +
"i.remark as status_type,"+
" i.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) def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
# date_type_selection = get_sql_function_for_report_type(report_type) # date_type_selection = get_sql_function_for_report_type(report_type)
query = self.get_item_query() query = self.get_item_query()
discount_query = 0 discount_query = 0
total_card_amount = 0 total_card_amount = 0
total_cash_amount = 0 total_cash_amount = 0
@@ -650,8 +647,12 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
total_foc_amount = 0 total_foc_amount = 0
total_grand_total = 0 total_grand_total = 0
other_charges = self.get_other_charges()
puts other_charges.to_json
if shift.present? if shift.present?
query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a) 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) 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, 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,
@@ -671,6 +672,7 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
### => get all sales range in shift_sales ### => get all sales range in shift_sales
elsif shift_sale_range.present? elsif shift_sale_range.present?
query = query.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a) 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) 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, 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,
@@ -685,10 +687,12 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
total_credit_amount += s_c.credit_amount.to_f total_credit_amount += s_c.credit_amount.to_f
total_foc_amount += s_c.foc_amount.to_f total_foc_amount += s_c.foc_amount.to_f
end end
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
else else
query = query.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to) 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) 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, 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,
@@ -707,7 +711,7 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status)
end 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 end
def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type) def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_type)

View File

@@ -50,6 +50,7 @@
<% cate_arr = Array.new %> <% cate_arr = Array.new %>
<% sub_total = 0 %> <% sub_total = 0 %>
<% other_sub_total = 0 %>
<% count = 0 %> <% count = 0 %>
<% total_price = 0 %> <% total_price = 0 %>
<% cate_count = 0 %> <% cate_count = 0 %>
@@ -82,7 +83,6 @@
</td> </td>
</tr> </tr>
<% acc_arr.push(sale.account_id) %> <% acc_arr.push(sale.account_id) %>
<% end %> <% end %>
<tr> <tr>
<td>&nbsp;</td> <td>&nbsp;</td>
@@ -124,7 +124,37 @@
<% end %> <% end %>
<!-- end sub total --> <!-- end sub total -->
<% end %> <% end %>
<!--Other Charges -->
<tr>
<td><b>Other Charges</b></td>
<td colspan="4">&nbsp;</td>
<td></td>
</tr>
<% @other_charges.each do |other| %>
<% if other.total_item > 0
total_qty += other.total_item
end %>
<% grand_total +=other.grand_total%>
<tr>
<td>&nbsp;</td>
<td>Other Charges</td>
<td><%= other.item_code rescue '-' %></td>
<td><%= other.product_name rescue '-' %></td>
<td><%= other.total_item rescue '-' %></td>
<td><%= other.unit_price rescue '-' %></td>
<td><%= other.grand_total rescue '-' %></td>
</tr>
<!-- sub total -->
<% other_sub_total += other.grand_total %>
<!-- end sub total -->
<% end %>
<tr>
<td colspan="5">&nbsp;</td>
<td><%= t("views.right_panel.detail.sub_total") %></td>
<td ><span class="underline"><%= other_sub_total %></span></td>
</tr>
<!-- End Other Charges -->
<tr style="border-top:2px solid grey;"> <tr style="border-top:2px solid grey;">
<td colspan="3">&nbsp;</td> <td colspan="3">&nbsp;</td>
<td><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %></td> <td><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %></td>