check changable tax on/off settings and customer tax profiles calculation

This commit is contained in:
phyusin
2018-08-02 14:13:05 +06:30
parent 5b94a4604b
commit 444ecb9c68
10 changed files with 184 additions and 105 deletions

View File

@@ -322,7 +322,7 @@ class Sale < ApplicationRecord
end
#compute - invoice total
def compute(order_source = nil)
def compute(order_source = nil, tax_type = nil)
sales_items = self.sale_items
#Computation Fields
@@ -341,7 +341,7 @@ class Sale < ApplicationRecord
# total_taxable = total_taxable + (item.taxable_price * item.qty)
end
apply_tax(total_taxable, order_source)
apply_tax(total_taxable, order_source, tax_type)
self.total_amount = subtotal_price
self.total_discount = total_discount
@@ -434,8 +434,21 @@ class Sale < ApplicationRecord
total_tax_amount = 0
tax_incl_exec = "exclusive"
#tax_profile - list by order_by
tax_profiles = TaxProfile.all.order("order_by asc")
customer = Customer.find(sale.customer_id)
# tax_profiles = TaxProfile.all.order("order_by asc")
# customer = Customer.find(sale.customer_id)
arr_tax = []
arr_tax = unique_tax_profiles(order_source, self.customer_id)
if !arr_tax.empty?
if tax_type.nil?
tax_profiles = TaxProfile.unscoped.where(:id => arr_tax)
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
# #Creat new tax records
if order_source.to_s == "emenu"
order_source = "cashier"
@@ -505,7 +518,7 @@ class Sale < ApplicationRecord
end
# Tax Calculate
def apply_tax(total_taxable, order_source = nil)
def apply_tax(total_taxable, order_source = nil, tax_type = nil)
shop = Shop.first
#if tax is not apply create new record
@@ -522,29 +535,32 @@ class Sale < ApplicationRecord
order_source = "cashier"
end
tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id)
customer = Customer.find(self.customer_id).tax_profiles
# tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id)
# customer = Customer.find(self.customer_id).tax_profiles
arr_tax = []
arr_tax = customer - tax_data
arr_tax = unique_tax_profiles(order_source, self.customer_id)
if !arr_tax.empty?
tax_profiles = TaxProfile.unscoped.where("id in ?",arr_tax)
if tax_type.nil?
tax_profiles = TaxProfile.unscoped.where(:id => arr_tax)
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
#Create new tax records
tax_profiles.each do |tax|
if tax.group_type.to_s == order_source.to_s
# customer.tax_profiles.each do |cus_tax|
# if cus_tax.to_i == tax.id
if tax_type
if tax_type.to_s == tax.name.to_s || tax_type == 'all'
sale_tax = SaleTax.new(:sale => self)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
# substract , to give after discount
total_tax = total_taxable - self.total_discount
total_tax = total_taxable - total_discount
#include or execulive
if tax.inclusive
tax_incl_exec = "inclusive"
@@ -555,16 +571,43 @@ class Sale < ApplicationRecord
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
else
# customer.tax_profiles.each do |cus_tax|
# if cus_tax.to_i == tax.id
sale_tax = SaleTax.new(:sale => self)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
# substract , to give after discount
total_tax = total_taxable - self.total_discount
#include or execulive
if tax.inclusive
tax_incl_exec = "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
end
end
self.tax_type = tax_incl_exec
@@ -2844,9 +2887,21 @@ def self.get_sale_data_for_other_payment_credit(sale_id)
return query
end
def difference(other)
h = other.each_with_object(Hash.new(0)) { |e,h| h[e] += 1 }
reject { |e| h[e] > 0 && h[e] -= 1 }
def unique_tax_profiles(order_source, customer_id)
tax_data = TaxProfile.unscoped.where("group_type='#{order_source}'").pluck(:id)
customer_tax_profiles = Customer.find(customer_id).tax_profiles
arr_data = []
if !customer_tax_profiles.empty?
customer_tax_profiles.each do |value1|
if tax_data.include? value1.to_i
arr_data.push(value1.to_i)
end
end
return arr_data
else
return tax_data
end
end
private