Pull from master

This commit is contained in:
San Wai Lwin
2018-08-06 17:52:03 +06:30
parent 35ac07b0a6
commit e99e907bc4
33 changed files with 455 additions and 126 deletions

View File

@@ -70,6 +70,7 @@ class OrderReservation < ApplicationRecord
order_reservation.discount_amount = order_reserve[:payment_info][:discount_amount]
order_reservation.convenience_charge = order_reserve[:payment_info][:convenience_charge]
order_reservation.grand_total = order_reserve[:payment_info][:grand_total]
order_reservation.transaction_fee = order_reserve[:payment_info][:transaction_fee]
order_reservation.order_remark = order_reserve[:order_info][:order_remark]
end
if order_reserve[:reservation_info]

View File

@@ -83,10 +83,15 @@ class Sale < ApplicationRecord
self.tax_type = "exclusive"
# set cashier
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
shift = ShiftSale.current_open_shift(cashier.id)
if order_source.downcase == "emenu"
table = DiningFacility.find(booking.dining_facility_id)
cashier_zone = CashierTerminalByZone.find_by_zone_id(table.zone_id)
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{cashier_zone.cashier_terminal_id}").first
else
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
current_shift = ShiftSale.current_shift
shift = ShiftSale.current_open_shift(cashier.id)
end
# set cashier
if shift != nil
self.cashier_id = cashier.id
@@ -318,7 +323,7 @@ class Sale < ApplicationRecord
end
#compute - invoice total
def compute(order_source = nil)
def compute(order_source = nil, tax_type = nil)
sales_items = self.sale_items
#Computation Fields
@@ -337,7 +342,7 @@ class Sale < ApplicationRecord
# total_taxable = total_taxable + (item.taxable_price * item.qty)
end
apply_tax(total_taxable, order_source)
apply_tax(total_taxable, order_source, tax_type)
self.total_amount = subtotal_price
self.total_discount = total_discount
@@ -430,8 +435,21 @@ class Sale < ApplicationRecord
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)
# 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.unscoped.where(:id => arr_tax)
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
# #Creat new tax records
if order_source.to_s == "emenu"
order_source = "cashier"
@@ -501,7 +519,7 @@ class Sale < ApplicationRecord
end
# Tax Calculate
def apply_tax(total_taxable, order_source = nil)
def apply_tax(total_taxable, order_source = nil, tax_type = nil)
shop = Shop.first
#if tax is not apply create new record
@@ -513,24 +531,37 @@ class Sale < ApplicationRecord
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(self.customer_id)
# tax_profiles = TaxProfile.all.order("order_by asc")
if order_source.to_s == "emenu"
order_source = "cashier"
end
# 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)
if !arr_tax.empty?
if tax_type.nil?
tax_profiles = TaxProfile.unscoped.where(:id => arr_tax)
else
tax_profiles = TaxProfile.unscoped.where("group_type=?",order_source)
end
else
tax_profiles = TaxProfile.unscoped.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
# customer.tax_profiles.each do |cus_tax|
# if cus_tax.to_i == tax.id
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
# substract , to give after discount
total_tax = total_taxable - self.total_discount
total_tax = total_taxable - total_discount
#include or execulive
if tax.inclusive
tax_incl_exec = "inclusive"
@@ -541,20 +572,47 @@ class Sale < ApplicationRecord
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
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
end
end
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount
end
def product_get_unit_price(item_code)
@@ -726,12 +784,13 @@ class Sale < ApplicationRecord
def self.daily_sales_list(from,to)
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END)
FROM sale_payments
INNER JOIN sale_audits sa
ON SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id
INNER JOIN sales ON sa.sale_id = sales.sale_id
WHERE sales.sale_status='completed' AND sales.receipt_date between '#{from}' and '#{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)
FROM sale_payments
INNER JOIN sale_audits sa ON SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id
INNER JOIN sales s ON s.sale_id=sa.sale_id
WHERE s.sale_status='completed'
AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d')"
payments_total = Sale.select("CAST((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')) AS DATE) as sale_date,
SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount,
@@ -748,7 +807,7 @@ def self.daily_sales_list(from,to)
(CASE WHEN (SUM(case when (sale_payments.payment_method='creditnote') then sale_payments.payment_amount else 0 end)) > 0 THEN (SUM(case when (sale_payments.payment_method='creditnote') then sale_payments.payment_amount else 0 end) - (#{sub_query})) 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")
.joins(" left join sale_payments on sale_payments.sale_id = sales.sale_id")
.joins(" join sale_payments on sale_payments.sale_id = sales.sale_id")
.where("sale_status = ? AND sales.receipt_date between ? and ? ", 'completed', from, to)
.group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')")
@@ -1114,7 +1173,8 @@ def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter)
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
WHERE sale_audits.sale_id = s.sale_id"
sub_query1 = "SELECT (CASE WHEN SUM(payment_amount) > 0 THEN SUM(payment_amount) ELSE 0 END)
sub_query1 = "SELECT (CASE WHEN SUM(payment_amount) > 0 THEN SUM(payment_amount) -
(SUM(payment_amount) - (SELECT SUM(payment_amount) FROM sale_payments WHERE payment_method='creditnote' AND sale_id=s.sale_id)) ELSE 0 END)
FROM `sale_payments`
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
WHERE sale_audits.sale_id = s.sale_id"
@@ -1499,7 +1559,7 @@ end
else
query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to)
end
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')")
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
@@ -1509,7 +1569,7 @@ end
else
query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to)
end
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', 'SYSTEM'), '%I %p')")
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
shift = ShiftSale.current_open_shift(current_user.id)
@@ -1552,16 +1612,33 @@ end
def self.employee_sales(today,current_user,from,to,from_time,to_time)
#sub query for credit payment
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount
FROM sale_payments
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
INNER JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sales.sale_status='completed'"
outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END
FROM sale_payments
JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'"
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
end
else
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
end
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END)
FROM sale_payments
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
INNER JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sales.sale_status='completed'"
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'"
end
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
@@ -1742,16 +1819,40 @@ end
end
def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
sub_query = SalePayment.select("(CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) as total_credit_payment")
outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END
FROM sale_payments
JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'"
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
else
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
end
else
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
end
sub_query = SalePayment.select("(CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) as total_credit_payment")
.joins(" JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id")
.joins(" JOIN sales ON sale_audits.sale_id = sales.sale_id")
.where("sales.sale_status='completed'")
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'")
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'")
end
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'")
end
if !from.nil? && !to.nil?
if current_user.nil?
if !from_time.nil? && !to_time.nil?
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1759,7 +1860,6 @@ end
query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0)
end
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1770,8 +1870,6 @@ end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
if !from_time.nil? && !to_time.nil?
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1779,7 +1877,6 @@ end
query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0)
end
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1791,7 +1888,6 @@ end
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
if !from_time.nil? && !to_time.nil?
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1799,7 +1895,6 @@ end
query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0)
end
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'")
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',from,to,shift.id)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
.sum("payment_amount")
@@ -1811,7 +1906,6 @@ end
end
end
else
sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{today}' AND '#{today}'")
if current_user.nil?
query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote" and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today)
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
@@ -1970,16 +2064,41 @@ end
time_query = " and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
end
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END)
outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END
FROM sale_payments
JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'"
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
else
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
end
else
outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
end
sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END)
FROM sale_payments
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
INNER JOIN sales ON sale_audits.sale_id = sales.sale_id
WHERE sales.sale_status='completed'"
if !from.nil? && !to.nil?
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'"
end
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
end
if !from.nil? && !to.nil?
if current_user.nil?
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method="alipay" or sp.payment_method="paymal" or sp.payment_method="dinga" or sp.payment_method="JunctionPay" or sp.payment_method = "giftvoucher")',from,to,from_time,to_time)
@@ -1988,7 +2107,6 @@ end
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to)
@@ -2000,7 +2118,6 @@ end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to,from_time,to_time)
@@ -2009,7 +2126,6 @@ end
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher")',from,to)
@@ -2022,7 +2138,6 @@ end
shift = ShiftSale.current_open_shift(current_user.id)
if !shift.nil?
if !from_time.nil? && !to_time.nil?
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher") and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id)
@@ -2031,7 +2146,6 @@ end
end
query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - SUM(sales.amount_changed)) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first()
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay" or sp.payment_method = "giftvoucher") and sales.shift_sale_id=?',from,to,shift.id)
@@ -2044,7 +2158,6 @@ end
end
end
else
sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'"
if current_user.nil?
query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id")
if payment_method == 'card'
@@ -2775,6 +2888,23 @@ def self.get_sale_data_for_other_payment_credit(sale_id)
return query
end
def unique_tax_profiles(order_source, customer_id)
tax_data = TaxProfile.unscoped.where("group_type='#{order_source}'").pluck(:id)
customer_tax_profiles = Customer.find(customer_id).tax_profiles
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
end
end
private
def generate_custom_id

View File

@@ -16,12 +16,12 @@ class SalePayment < ApplicationRecord
#get all payment for this invoices
if payment_for
invoice_sale_payments = SalePayment.get_sale_payment_for_credit(invoice)
amount_due = SalePayment.get_credit_total_left(self.sale_id)[0] ? SalePayment.get_credit_total_left(self.sale_id)[0].payment_amount.to_f : 0
amount_due = SalePayment.get_credit_amount_due_left(self.sale_id)[0] ? SalePayment.get_credit_amount_due_left(self.sale_id)[0].payment_amount.to_f : 0
else
invoice_sale_payments = invoice.sale_payments
amount_due = invoice.grand_total
end
invoice_sale_payments.each do |payment|
if (payment.payment_status == "paid" )
amount_due = amount_due - payment.payment_amount
@@ -866,22 +866,31 @@ class SalePayment < ApplicationRecord
.joins("INNER JOIN orders o ON o.order_id = so.order_id")
if params[:type] == "cashier"
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) and s.sale_status = 'completed' AND o.source='#{params[:type]}' OR o.source='emenu' #{receipt_no} #{customer}")
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND o.source='#{params[:type]}' OR o.source='emenu' END) and s.sale_status = 'completed' #{receipt_no} #{customer}")
else
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' END) and s.sale_status = 'completed' AND o.source='#{params[:type]}' #{receipt_no} #{customer}")
query = query.where("(CASE WHEN (s.grand_total + s.amount_changed)=(select SUM(payment_amount) FROM sale_payments WHERE sale_id=s.sale_id AND payment_method!='creditnote') THEN NULL ELSE payment_method='creditnote' AND o.source='#{params[:type]}' END) and s.sale_status = 'completed' #{receipt_no} #{customer}")
end
query = query.group("s.receipt_no")
.order("s.receipt_date ASC, s.receipt_no ASC")
return query
end
def self.get_credit_total_left(sale_id)
def self.get_credit_amount_due_left(sale_id)
query = SalePayment.select("(SUM(sale_payments.payment_amount) -
(CASE WHEN SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id
THEN SUM(sale_payments.payment_amount) ELSE 0 END)) as payment_amount")
.joins(" LEFT JOIN sale_audits sa on SUBSTRING_INDEX(sa.remark,'||',1)=sale_payments.sale_payment_id")
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
.group("sale_payments.sale_id")
return query
end
def self.get_credit_total_left(sale_id)
query = SalePayment.select("(SUM(sale_payments.payment_amount) -
(SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) ELSE 0 END) AS payment_amount
FROM sale_payments
INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id
WHERE sale_payments.sale_id = '#{sale_id}')) as payment_amount")
.where("sale_payments.payment_method = 'creditnote' AND sale_payments.sale_id = '#{sale_id}'")
return query
end