Files
sx-fc/app/models/sale_tax.rb
Thein Lin Kyaw 3c1cc737b5 improve receipt/details reports and implement number formatting
1) eager load reports for receipt/details
2) introduce number_format lookups to replace print_settings for number formatting
3) implement NumberFormattable concern, reference number_format lookups or print_settings if not exist, to get number format settings and number formatting
4) replace rails NumberHelper.number_with_precision with NumberFormattable.number_format hopefully to reduce overhead, formatting numbers for huge lists of data
2019-11-25 23:17:53 +06:30

53 lines
1.6 KiB
Ruby
Executable File

class SaleTax < ApplicationRecord
include NumberFormattable
self.primary_key = "sale_tax_id"
#primary key - need to be unique generated for multiple shops
before_create :generate_custom_id
belongs_to :sale
before_validation :round_to_precision
def self.sync_sale_tax_records(sale_taxes)
if !sale_taxes.nil?
sale_taxes.each do |t|
tax = SaleTax.find_by_sale_tax_id(t['sale_tax_id'])
# unless SaleTax.exists?(t['sale_tax_id'])
if tax.nil?
tax = SaleTax.new
end
tax.sale_tax_id = t['sale_tax_id']
tax.sale_id = t['sale_id']
tax.tax_name = t['tax_name']
tax.tax_rate = t['tax_rate']
tax.tax_payable_amount = t['tax_payable_amount']
tax.inclusive = t['inclusive']
tax.save
end
Rails.logger.debug '...... Sale Tax sync completed .....'
end
end
def self.get_tax(from,to)
query = SaleTax.select("sale_taxes.tax_name,SUM(sale_taxes.tax_payable_amount) as tax_amount")
.joins("join sales on sales.sale_id = sale_taxes.sale_id")
.where("sale_status = ? AND sales.receipt_date between ? and ? AND total_amount != 0 AND inclusive = 0", 'completed', from, to)
.group("sale_taxes.tax_name")
end
private
def generate_custom_id
if self.sale_tax_id.nil?
self.sale_tax_id = SeedGenerator.generate_id(self.class.name, "STI")
end
end
def round_to_precision
if self.tax_payable_amount != self.tax_payable_amount_was
if self.tax_payable_amount % 1 > 0
self.tax_payable_amount = self.tax_payable_amount.round(precision)
end
end
end
end