Merge branch 'master' of bitbucket.org:code2lab/sxrestaurant

This commit is contained in:
Nweni
2017-07-01 09:46:44 +06:30
21 changed files with 346 additions and 254 deletions

View File

@@ -41,8 +41,10 @@ class Sale < ApplicationRecord
end
booking.sale_id = sale_id
end
order = booking.booking_orders.take.order
link_order_sale(order.id)
return status, sale_id
end
end
@@ -93,7 +95,6 @@ class Sale < ApplicationRecord
end
self.save!
#compute sales summary
@@ -148,6 +149,7 @@ class Sale < ApplicationRecord
#pull
sale_item.product_code = item.item_code
sale_item.product_name = item.item_name
sale_item.product_alt_name = item.alt_name
sale_item.account_id = item.account_id
sale_item.remark = item.remark
@@ -206,6 +208,33 @@ class Sale < ApplicationRecord
end
#compute - invoice total
def compute_by_sale_items(sale_id, sale_itemss, total_discount)
sale = Sale.find(sale_id)
sales_items = sale_itemss
#Computation Fields
subtotal_price = 0
total_taxable = 0
rounding_adjustment = 0
sales_items.each do |item|
#compute each item and added to total
subtotal_price = subtotal_price + item.price
total_taxable = total_taxable + (item.taxable_price * item.qty)
end
compute_tax(sale, total_taxable)
sale.total_amount = subtotal_price
sale.total_discount = total_discount
sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax
#compute rounding adjustment
# adjust_rounding
sale.save!
end
def compute_without_void
sales_items = self.sale_items
@@ -231,6 +260,39 @@ class Sale < ApplicationRecord
self.save!
end
# Tax Re-Calculte
def compute_tax(sale, total_taxable)
#if tax is not apply create new record
SaleTax.where("sale_id='#{sale.sale_id}'").find_each do |existing_tax|
#delete existing and create new
existing_tax.delete
end
total_tax_amount = 0
#tax_profile - list by order_by
tax_profiles = TaxProfile.all.order("order_by asc")
# #Creat new tax records
tax_profiles.each do |tax|
sale_tax = SaleTax.new(:sale => sale)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = tax.rate
#include or execulive
# sale_tax.tax_payable_amount = total_taxable * tax.rate
sale_tax.tax_payable_amount = total_taxable * tax.rate / 100
#new taxable amount
total_taxable = total_taxable + sale_tax.tax_payable_amount
sale_tax.inclusive = tax.inclusive
sale_tax.save
total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
end
sale.total_tax = total_tax_amount
end
# Tax Calculate
def apply_tax(total_taxable)
#if tax is not apply create new record
@@ -447,6 +509,63 @@ def self.get_by_shiftsales(from,to)
return ShiftSale.where("(shift_started_at between ? and ? OR shift_closed_at between ? and ? )", from, to, from, to)
end
# def self.get_by_shiftsales(employee,from,to)
# shift_sales = ShiftSale.select('shift_sales.id, cs.name as cashier_station_name, shift_sales.shift_started_at as opening_date, shift_sales.shift_closeed_at as closing_date')
# .joins(" INNER JOIN cashier_terminals cs ON cs.id = shift_sales.cashier_terminal_id")
# .where("shift_sales.employee_id = ? and (shift_sales.shift_started_at between ? and ? OR shift_sales.shift_closeed_at between ? and ? )", employee, from, to, from, to)
# .order("shift_sales.id DESC")
# sale_arr = Array.new
# shift_sales.each do |shift|
# all_total= Sale.select("grand_total,sr.payment_method, sr.payment_amount, rounding_adjustment")
# .joins("join employees e on sales.cashier_id = e.id join shift_sales sh on sh.id = sales.shift_sale_id")
# .joins("INNER JOIN (select * from sale_payments group by sale_payments.sale_id, sale_payments.payment_method) sr ON sr.sale_id = sales.sale_id")
# .where("sales.shift_sale_id =? and sales.sale_status = 'completed' and sales.total_amount != 0",shift.id)
# void = Sale.select("SUM(sales.grand_total) AS grand_total")
# .joins("join shift_sales sh on sh.id = sales.shift_sale_id")
# .where('sales.sales_status = "void" and sales.total_amount != 0 and sales.shift_sale_id = ?', shift.id)
# .sum(:grand_total)
# cash = all_total.select('sr.payment_type')
# .where('sr.payment_type = "cash"')
# .sum(:amount)
# credit = all_total.where('sr.payment_type = "credit"')
# .sum(:amount)
# accept_credit = all_total.select('ci.amout')
# .joins("INNER JOIN credit_items ci ON ci.sale_id = sales.id")
# .where('sr.payment_type = "credit"')
# .sum(:amout)
# foc = all_total.where('sales.payment_type = "foc" and sales.sales_status = "completed"')
# .sum(:grand_total)
# card = all_total.select('payment_type')
# .where('sr.payment_type = "card"')
# .sum(:amount).to_f
# rounding_adj = all_total.sum(:rounding_adjustment)
# discount = all_total.sum(:discount_amount)
# void = void.nil? ? 0 : void
# cash = cash.nil? ? 0 : cash
# credit = credit.nil? ? 0 : credit
# foc = foc.nil? ? 0 : foc
# card = card.nil? ? 0 : card
# accept_credit = accept_credit.nil? ? 0 : accept_credit
# # all_payments = void.to_d + cash.to_d + credit.to_d + foc.to_d + card.to_d + accept_credit.to_d
# all_payments = cash.to_d + credit.to_d + foc.to_d + card.to_d + accept_credit.to_d
# payments = { void_amount: void, cash_amount: cash, credit_amount: credit,accept_credit_amount: accept_credit, foc_amount: foc, card_amount: card, grand_total: all_payments , cashier_station_name: shift.cashier_station_name, opening_date: shift.opening_date, closing_date: shift.closing_date, rounding_adj: rounding_adj }
# sale_arr.push(payments)
# end
# return sale_arr
# end
def get_cash_amount
cash = 0.0
self.sale_payments.each do |pay|
@@ -487,7 +606,6 @@ end
return tax
end
private
def generate_custom_id