Merge branch 'r-1902001-01' of gitlab.com:code2lab/SXRestaurant into r-1902001-01-dev

This commit is contained in:
NyanLinHtut
2019-11-13 13:34:29 +06:30
34 changed files with 930 additions and 1009 deletions

View File

@@ -14,14 +14,20 @@ class Sale < ApplicationRecord
has_many :sale_payments
has_many :sale_orders
has_many :orders, through: :sale_orders
has_many :order_items, through: :sale_orders
has_many :bookings
has_many :product_commissions
before_validation :round_to_precision
after_update :update_stock_journal
scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") }
scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") }
def qty_of(item_instance_code)
order_items.select(:order_items_id, :item_instance_code, 'SUM(qty) as qty').where(item_instance_code: item_instance_code).group(:item_instance_code).first
end
REPORT_TYPE = {
"daily" => 0,
"monthly" => 1,
@@ -418,11 +424,11 @@ class Sale < ApplicationRecord
apply_tax(total_taxable, order_source, tax_type)
self.total_amount = subtotal_price
self.total_discount = total_discount
# self.total_discount = total_discount
self.grand_total = (self.total_amount - self.total_discount) + self.total_tax
self.grand_total_round
# self.grand_total_round
#compute rounding adjustment
adjust_rounding
# adjust_rounding
self.save!
@@ -455,7 +461,7 @@ class Sale < ApplicationRecord
sale.grand_total = 0
else
sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax
sale.grand_total_round
# sale.grand_total_round
end
if discount_type == "member_discount"
@@ -463,7 +469,6 @@ class Sale < ApplicationRecord
end
#compute rounding adjustment
# adjust_rounding
sale.rounding_adjustment = compute_adjust_rounding(sale.grand_total)
sale.save!
end
@@ -491,11 +496,11 @@ class Sale < ApplicationRecord
apply_tax(total_taxable, order_source)
self.total_amount = subtotal_price
self.total_discount = total_discount
# self.total_discount = total_discount
self.grand_total = (self.total_amount - self.total_discount) + self.total_tax
self.grand_total_round
# self.grand_total_round
#compute rounding adjustment
adjust_rounding
# adjust_rounding
self.save!
end
@@ -509,89 +514,45 @@ class Sale < ApplicationRecord
# #delete existing and create new
# existing_tax.delete
# end
taxes = SaleTax.where("sale_id='#{sale.sale_id}'").pluck(:sale_tax_id)
SaleTax.where("sale_tax_id IN (?)", taxes).destroy_all
taxes = SaleTax.where("sale_id='#{self.sale_id}'").destroy_all
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)
arr_tax = []
arr_tax = unique_tax_profiles(order_source, self.customer_id)
if !arr_tax.empty?
if tax_type.nil?
tax_profiles = TaxProfile.where(:id => arr_tax)
else
tax_profiles = TaxProfile.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.where("group_type=?",order_source)
end
# #Creat new tax records
if order_source.to_s == "emenu"
order_source = "cashier"
end
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
# #Creat new tax records
if sale.payment_status != 'foc'
tax_profiles.each do |tax|
if tax.group_type.to_s == order_source.to_s
if tax_type
if tax_type.to_s == tax.name.to_s || tax_type == 'all'
sale_tax = SaleTax.new(:sale => sale)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
sale_tax = SaleTax.new(:sale => sale)
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
else
# customer.tax_profiles.each do |cus_tax|
# if cus_tax.to_i == tax.id
sale_tax = SaleTax.new(:sale => sale)
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
# 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
sale.tax_type = tax_incl_exec
@@ -608,8 +569,7 @@ class Sale < ApplicationRecord
# existing_tax.delete
# end
taxes = SaleTax.where("sale_id='#{self.sale_id}'").pluck(:sale_tax_id)
SaleTax.where("sale_tax_id IN (?)", taxes).destroy_all
taxes = SaleTax.where("sale_id='#{self.sale_id}'").destroy_all
total_tax_amount = 0
tax_incl_exec = "exclusive"
@@ -622,77 +582,34 @@ class Sale < ApplicationRecord
# tax_data = TaxProfile.unscoped.where("group_type=?",order_source).pluck(:id)
# customer = Customer.find(self.customer_id).tax_profiles
arr_tax = []
arr_tax = unique_tax_profiles(order_source, self.customer_id)
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
if !arr_tax.empty?
if tax_type.nil?
tax_profiles = TaxProfile.where(:id => arr_tax)
else
tax_profiles = TaxProfile.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.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
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
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
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
# 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
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount
@@ -727,42 +644,16 @@ class Sale < ApplicationRecord
shop_details = Shop.first
# rounding adjustment
if shop_details.is_rounding_adj
a = self.grand_total % 25 # Modulus
b = self.grand_total / 25 # Division
#not calculate rounding if modulus is 0 and division is even
#calculate rounding if modulus is zero or not zero and division are not even
if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0)
new_total = Sale.get_rounding_adjustment(self.grand_total)
self.rounding_adjustment = new_total - self.grand_total
else
self.rounding_adjustment = 0.00
end
new_total = Sale.get_rounding_adjustment(self.grand_total)
self.rounding_adjustment = new_total - self.grand_total
self.old_grand_total = self.grand_total
self.grand_total = new_total
else
self.rounding_adjustment = 0.00
end
end
def compute_adjust_rounding(grand_total)
shop_details = Shop.first
# rounding adjustment
if shop_details.is_rounding_adj
a = grand_total % 25 # Modulus
b = grand_total / 25 # Division
#not calculate rounding if modulus is 0 and division is even
#calculate rounding if modulus is zero or not zero and division are not even
if (a != 0.0 && b%2 != 0.0) || (a==0.0 && b%2 !=0)
new_total = Sale.get_rounding_adjustment(grand_total)
rounding_adjustment = new_total - grand_total
else
rounding_adjustment = 0.00
end
else
rounding_adjustment = 0.00
end
return rounding_adjustment
end
#Generate new Receipt No when it is not assigned
def generate_receipt_no
#shop_code and client_code
@@ -857,36 +748,16 @@ class Sale < ApplicationRecord
# if get_rounded_amt == true
value = 0
num = num.to_f.round
get_last_no = num.to_s.last(2).to_f
if get_last_no.between?(0,25)
## down to 0
num -= get_last_no
else
if get_last_no.between?(26,50)
## up to 50
value = 50 - get_last_no.to_f
num += value
puts 'up to 50'
else
if get_last_no.between?(51, 75)
## down to 50
value = get_last_no.to_f - 50
num -= value
puts 'down to 50'
else
## up to 100
value = 100 - get_last_no.to_f
num += value
puts 'up to 100'
end
end
# num = num.to_f.round
mod = num % 50
if mod > 0 && mod <= 25
num -= mod
elsif mod > 25
num += 50 - mod
end
# end
return num
end
def self.daily_sales_list(from,to)
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN
(SUM(sale_payments.payment_amount) + SUM(sale_payments.outstanding_amount)) ELSE 0 END)
@@ -2646,20 +2517,15 @@ def self.get_sale_data_for_other_payment_credit(sale_id)
end
def unique_tax_profiles(order_source, customer_id)
tax_data = TaxProfile.where("group_type='#{order_source}'").pluck(:id)
customer_tax_profiles = Customer.find(customer_id).tax_profiles
tax_data = TaxProfile
.where(group_type: order_source)
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
customer_tax_profiles = Customer.select(:tax_profiles).where(customer_id: customer_id).first
if customer_tax_profiles.present?
tax_data = tax_data.where(id: customer_tax_profiles.tax_profiles)
end
return tax_data
end
def self.top_bottom(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
@@ -3026,6 +2892,20 @@ private
end
end
def round_to_precision
if (self.total_amount != self.total_amount_was || self.total_discount != self.total_discount_was || self.total_tax != self.total_tax_was)
if (self.total_amount % 1 > 0 || self.total_discount % 1 > 0 || self.total_tax % 1 > 0)
precision = PrintSetting.get_precision_delimiter().precision.to_i
self.total_amount = self.total_amount.round(precision)
self.total_discount = self.total_discount.round(precision)
self.total_tax = self.total_tax.round(precision)
self.grand_total = (self.total_amount - self.total_discount) + self.total_tax
end
adjust_rounding
end
end
def update_stock_journal
if self.sale_status == "void" && self.sale_status_before_last_save != "void"
self.sale_items.each do |item|