Merge branch 'r-1902001-01' into foodcourt

This commit is contained in:
Thein Lin Kyaw
2020-05-29 13:32:16 +06:30
23 changed files with 724 additions and 508 deletions

View File

@@ -748,43 +748,29 @@ class Sale < ApplicationRecord
end
def self.search_credit_sales(customer,filter,from,to,order_source="")
if filter.blank?
keyword = ''
else
keyword = "and sales.receipt_no LIKE ? OR sales.cashier_name LIKE ? OR sales.sale_status ='#{filter}'","%#{filter}%","%#{filter}%"
end
if customer.blank?
custo = ''
else
custo = "and sales.customer_id = '#{customer}'"
end
order_source_query = "(select orders.source FROM orders JOIN sale_orders so ON so.order_id=orders.order_id WHERE so.sale_id=sales.sale_id GROUP BY so.sale_id)"
if order_source.blank?
source = ""
else
if order_source == "cashier"
source = "and #{order_source_query}='cashier' or #{order_source_query}='emenu'"
else
source = "and #{order_source_query}='#{order_source}'"
end
end
sale = Sale.select(Sale.column_names)
.select(SalePayment.column_names)
.select(:source).includes(:customer)
.joins(:sale_payments, :orders)
.where(sale_payments: {payment_method: 'creditnote'})
.group(:sale_payment_id)
if from.present? && to.present?
sale = Sale.select("sales.*,#{order_source_query} as source").joins("JOIN sale_payments sp on sp.sale_id = sales.sale_id")
.joins(" JOIN bookings ON bookings.sale_id=sales.sale_id")
.joins(" JOIN booking_orders ON booking_orders.booking_id=bookings.booking_id")
.joins(" JOIN orders ON orders.order_id=booking_orders.order_id")
.where("DATE_FORMAT(receipt_date,'%d-%m-%Y') >= ?" + " AND DATE_FORMAT(receipt_date,'%d-%m-%Y') <= ? and (CASE WHEN (sales.grand_total + sales.amount_changed)=(select SUM(sale_payments.payment_amount)
FROM sale_payments WHERE sale_payments.sale_id=sales.sale_id AND sale_payments.payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) #{keyword} #{custo} #{source}", from,to)
else
sale = Sale.select("sales.*,#{order_source_query} as source").joins(" JOIN sale_payments sp on sp.sale_id = sales.sale_id")
.joins(" JOIN bookings ON bookings.sale_id=sales.sale_id")
.joins(" JOIN booking_orders ON booking_orders.booking_id=bookings.booking_id")
.joins(" JOIN orders ON orders.order_id=booking_orders.order_id")
.where("(CASE WHEN (sales.grand_total + sales.amount_changed)=(select SUM(sale_payments.payment_amount)
FROM sale_payments WHERE sale_payments.sale_id=sales.sale_id AND sale_payments.payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) #{keyword} #{custo} #{source}")
sale = sale.receipt_date_between(from, to)
end
if filter.present?
sale = sale.where("sales.receipt_no LIKE ? OR sales.cashier_name LIKE ? OR sales.sale_status = ?", "%#{filter}%", "%#{filter}%", filter)
end
if customer.present?
sale = sale.where(customer_id: customer)
end
if order_source.present?
sale = sale.where(orders: {source: order_source})
end
return sale
end
def self.get_rounding_adjustment(num)
@@ -817,8 +803,10 @@ def self.daily_sales_list(from,to)
SUM(case when (sale_payments.payment_method='dinga') then sale_payments.payment_amount else 0 end) as dinga_amount,
SUM(case when (sale_payments.payment_method='JunctionPay') then sale_payments.payment_amount else 0 end) as junctionpay_amount,
SUM(case when (sale_payments.payment_method='cash') then sale_payments.payment_amount else 0 end) as cash_amount,
CASE WHEN SUM(case when sale_payments.payment_method not in('creditnote') then sale_payments.payment_amount end) < sales.grand_total
THEN sales.grand_total - SUM(case when sale_payments.payment_method not in('creditnote') then sale_payments.payment_amount end)
CASE WHEN SUM(case when sale_payments.payment_method not in('creditnote')
then sale_payments.payment_amount else 0 end) < sales.grand_total
THEN sales.grand_total - SUM(case when sale_payments.payment_method not in('creditnote')
then sale_payments.payment_amount else 0 end)
ELSE 0 END as credit_amount,
SUM(case when (sale_payments.payment_method='giftvoucher') then sale_payments.payment_amount else 0 end) as giftvoucher_amount,
SUM(case when (sale_payments.payment_method='foc') then sale_payments.payment_amount else 0 end) as foc_amount")
@@ -1475,33 +1463,32 @@ def grand_total_after_rounding
end
def get_cash_amount
cash = 0.0
self.sale_payments.each do |pay|
if pay.payment_method == 'cash'
cash += pay.payment_amount
end
end
return cash - self.amount_changed
self.sale_payments.where(payment_method: 'cash', payment_status: 'paid')
.pluck(:payment_amount).reduce(0, :+) - self.amount_changed
end
def get_credit_amount
credit = 0.0
self.sale_payments.each do |pay|
if pay.payment_method == 'creditnote'
credit += pay.payment_amount
end
end
return credit
self.sale_payments.where(payment_method: 'creditnote')
.pluck(:payment_status, :payment_amount)
.inject(0.0) { |sum, pay|
if pay[0] == 'outstanding'
sum += pay[1]
else
sum -= pay[1]
end
}
end
def get_other_amount
other = 0.0
self.sale_payments.each do |pay|
if pay.payment_method != 'cash' && pay.payment_method != 'creditnote'
other += pay.payment_amount
end
end
return other
self.sale_payments.where.not(payment_method: ['cash', 'creditnote'])
.pluck(:payment_status, :payment_amount)
.inject(0.0) { |sum, pay|
if pay[0] == 'paid'
sum += pay[1]
else
sum -= pay[1]
end
}
end
def get_commerical_tax
@@ -2439,15 +2426,13 @@ private
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)
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
if (self.total_amount % 1 > 0 || self.total_discount % 1 > 0 || self.total_tax % 1 > 0)
self.total_amount = self.total_amount.round(precision)
self.total_discount = self.total_discount.round(precision)
self.total_tax = self.total_tax.round(precision)
end
self.grand_total = (self.total_amount - self.total_discount) + self.total_tax
adjust_rounding
end
def update_stock_journal