fix tax calculation on daily sale report
This commit is contained in:
@@ -796,14 +796,27 @@ class Sale < ApplicationRecord
|
||||
|
||||
tax_profiles = TaxProfile.group(:name).order(:order_by).pluck(:name)
|
||||
|
||||
total_item_foc = SaleItem.select("SUM(ABS(sale_items.price)) AS total_amount, sale_items.sale_id")
|
||||
.where("sale_items.status = ? AND sale_items.item_instance_code IS NOT NULL AND sale_items.qty < ?", 'foc', 0)
|
||||
.group("sale_id")
|
||||
|
||||
total_item_discount = SaleItem.select("SUM(ABS(sale_items.price)) AS total_amount, sale_items.sale_id")
|
||||
.where("sale_items.status = ? AND sale_items.item_instance_code IS NOT NULL", 'Discount')
|
||||
.group("sale_id")
|
||||
|
||||
sales = select(Sale.column_names)
|
||||
.select("#{payment_methods.map { |method| "SUM(case when (sale_payments.payment_method='#{method}') then sale_payments.payment_amount else 0 end) as `#{method == 'paypar' ? 'redeem' : method}`"}.push('').join(', ')}
|
||||
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) -
|
||||
SUM(case when (sale_payments.payment_method not in('creditnote') and sale_audits.sale_audit_id IS NOT NULL) then sale_payments.payment_amount else 0 end) as credit_amount,
|
||||
SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount")
|
||||
SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount",
|
||||
"case when (item_foc.total_amount) then item_foc.total_amount else 0 end as item_foc_amount",
|
||||
"case when (item_discount.total_amount) then item_discount.total_amount else 0 end as item_discount_amount")
|
||||
.joins("LEFT JOIN (#{total_item_foc.to_sql}) AS item_foc ON item_foc.sale_id = sales.sale_id")
|
||||
.joins("LEFT JOIN (#{total_item_discount.to_sql}) AS item_discount ON item_discount.sale_id = sales.sale_id")
|
||||
.sale_payments_with_audit_except_void_between(from, to)
|
||||
.where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to)
|
||||
.where("sale_payments.payment_status != ? OR sales.payment_status =? ", 'cancelled', 'void')
|
||||
.group("sale_id")
|
||||
|
||||
sale_taxes = Sale.select('sales.sale_id, sale_taxes.tax_name')
|
||||
@@ -811,32 +824,40 @@ class Sale < ApplicationRecord
|
||||
.where('(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ', 'completed', 'void', from, to)
|
||||
.group('sale_id')
|
||||
|
||||
commercial_tax = ''
|
||||
if tax_profiles.present?
|
||||
sale_taxes = sale_taxes.select(tax_profiles.map { |name| "SUM(case when (sale_taxes.tax_name = '#{name}') then sale_taxes.tax_payable_amount else 0 end) as `#{name.parameterize}`"}.join(', '))
|
||||
sale_taxes = sale_taxes.select(tax_profiles.map { |name| "SUM(case when (sale_taxes.tax_name = '#{name}') then sale_taxes.tax_payable_amount else 0 end) as `#{name.parameterize}`"}.join(', '))
|
||||
if tax_profiles.include? 'Commercial Tax'
|
||||
commercial_tax = "IFNULL(SUM(case when (sale_status = 'completed' and payment_status != 'foc') then `commercial-tax` else 0 end),0)"
|
||||
end
|
||||
end
|
||||
|
||||
daily_total = connection.select_all("SELECT
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as total_sale,
|
||||
IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount,
|
||||
IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj,
|
||||
#{tax_profiles.map { |name| "SUM(`#{name.parameterize}`) as `#{name.parameterize}`"}.push('').join(', ') if tax_profiles.present?}
|
||||
IFNULL(SUM(case when (sale_status='completed') then total_tax else 0 end),0) as tax,
|
||||
(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) - (IFNULL(SUM(case when (sale_status='completed') then total_tax else 0 end),0)) as net_sale,
|
||||
(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) + (IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0)) as gross_sale,
|
||||
CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date,
|
||||
#{payment_methods.map { |method| pm = method == 'paypar' ? 'redeem' : method; "SUM(`#{pm}`) as `#{pm}`"}.push('').join(', ')}
|
||||
SUM(`cash_amount`) as `cash_amount`,
|
||||
SUM(`credit_amount`) as `credit_amount`,
|
||||
SUM(`foc_amount`) as `foc_amount`
|
||||
FROM (
|
||||
#{sales.to_sql}
|
||||
) as s
|
||||
LEFT JOIN (#{sale_taxes.to_sql}) AS st ON s.sale_id = st.sale_id
|
||||
GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys)
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end) - SUM(foc_amount),0) as total_sale,
|
||||
IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount,
|
||||
IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj,
|
||||
#{tax_profiles.map { |name| "IFNULL(SUM(case when (sale_status='completed') then `#{name.parameterize}` else 0 end),0) as `#{name.parameterize}`"}.push('').join(', ') if tax_profiles.present?}
|
||||
IFNULL(SUM(case when (sale_status='completed') then total_tax else 0 end),0) as tax,
|
||||
IFNULL(SUM(case when (sale_status='completed' and payment_status!='foc') then grand_total else 0 end),0)
|
||||
- #{commercial_tax} as net_sale,
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)
|
||||
+ IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as gross_sale,
|
||||
CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date,
|
||||
#{payment_methods.map { |method| pm = method == 'paypar' ? 'redeem' : method; "SUM(`#{pm}`) as `#{pm}`"}.push('').join(', ')}
|
||||
SUM(`cash_amount`) as `cash_amount`,
|
||||
SUM(`credit_amount`) as `credit_amount`,
|
||||
SUM(`foc_amount`) as `foc_amount`,
|
||||
SUM(`item_foc_amount`) as `item_foc_amount`,
|
||||
SUM(`item_discount_amount`) as `item_discount_amount`
|
||||
FROM (
|
||||
#{sales.to_sql}
|
||||
) as s
|
||||
LEFT JOIN (#{sale_taxes.to_sql}) AS st ON s.sale_id = st.sale_id
|
||||
GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys)
|
||||
return daily_total
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user