api discount and change tax

This commit is contained in:
Thein Lin Kyaw
2023-10-17 16:52:12 +06:30
parent 81eb30c03a
commit 3441811bb0
27 changed files with 1225 additions and 1067 deletions

View File

@@ -440,8 +440,7 @@ class Sale < ApplicationRecord
end
# Tax Re-Calculte
def compute_tax(total_taxable, total_discount = 0, order_source = nil, tax_type=nil)
def compute_tax(total_taxable, total_discount = 0, order_source = nil, tax_type = nil)
shop = Shop.current_shop
#if tax is not apply create new record
@@ -459,50 +458,31 @@ class Sale < ApplicationRecord
if order_source.to_s == "emenu"
order_source = "cashier"
elsif order_source.to_s == "app"
order_source = "online_order"
end
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
if tax_type.nil?
#in previous >> if tax_profiles.count ==2
if tax_profiles.count >= 2
tax_type = "all"
elsif tax_profiles.count == 1
if tax_profiles.name.downcase == "service charges"
tax_type = "Service Charges"
else
tax_type = "Commercial Tax"
end
elsif tax_profiles.count < 1
tax_type = "no_tax"
end
end
# #Creat new tax records
if self.payment_status != 'foc' && tax_type.to_s == "all"
if self.payment_status != 'foc'
tax_profiles.each do |tax|
sale_tax = SaleTax.new(:sale => self)
next if self.customer_id_before_last_save.nil? && !self.customer_id_changed? && tax_type.blank? && !taxes.map(&:tax_name).include?(tax.name)
next unless tax_type == 'all' || tax_type.blank? || tax_type.include?(tax.name)
sale_tax = self.sale_taxes.build
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
# substract , to give after discount
total_tax = total_taxable - total_discount
#include or execulive
if tax.tax_type.to_s =="Net"
sale_tax.tax_payable_amount = tax.rate
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
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
end
sale_tax.tax_payable_amount = total_tax * tax.rate / 100
end
sale_tax.inclusive = tax.inclusive
sale_tax.save
if !tax.inclusive
total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
@@ -513,67 +493,6 @@ class Sale < ApplicationRecord
total_taxable = total_taxable + sale_tax.tax_payable_amount
end
end
elsif tax_type.to_s == "no_tax"
tax_profiles.each do |tax|
sale_tax = SaleTax.new(:sale => self)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = 0
sale_tax.tax_payable_amount = 0
sale_tax.inclusive = tax.inclusive
sale_tax.save
end
elsif tax_type.to_s == "Commercial Tax"
tax_profiles.each do |tax|
if tax.name == tax_type.to_s
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 - 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
elsif tax_type.to_s == "Service Charges"
tax_profiles.each do |tax|
if tax.name == tax_type.to_s
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 - 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
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount

View File

@@ -16,6 +16,12 @@ class SaleItem < ApplicationRecord
after_update :update_stock_journal
after_save :update_stock_journal_set_item
has_one :discount_item, -> { where(status: 'Discount') }, class_name: 'SaleItem', foreign_key: 'parent_id'
has_one :foc_item, -> { where(status: 'foc') }, class_name: 'SaleItem', foreign_key: 'parent_id'
has_one :void_item, -> { where(status: 'void') }, class_name: 'SaleItem', foreign_key: 'parent_id'
enum discount_type: { nett: 'nett', percentage: 'percentage' }
# Add Sale Items
def self.add_sale_items(sale_items)
sale_items.each do|saleitemObj|