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 def display_name "#{self.tax_name} (#{'Incl. ' if self.inclusive}#{self.tax_rate}%)" 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