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

@@ -41,7 +41,7 @@ class CardSaleTran < ApplicationRecord
if payment_type.blank?
payment = ''
else
payment = " app LIKE '#{payment_type}'"
payment = " app LIKE '#{payment_type}'"
end
if from.present? && to.present?
@@ -52,7 +52,7 @@ class CardSaleTran < ApplicationRecord
if shift.present?
query1 = cardSale.where("s.shift_sale_id in (?)", shift.to_a)
elsif shift_sale_range.present?
elsif shift_sale_range.present?
query1 = cardSale.where("s.shift_sale_id in (?)",shift_sale_range.to_a)
else
query1 = cardSale.where("DATE_FORMAT(s.receipt_date,'%Y-%m-%d') between ? and ?",from,to)
@@ -67,7 +67,6 @@ class CardSaleTran < ApplicationRecord
shift_closed_at As closing_date,")
.order("shift_sales.id DESC")
return query = query.where("shift_sales.shift_started_at >= ?" , from)
# byebug
end
end
end

View File

@@ -12,7 +12,7 @@ class CardSettleTran < ApplicationRecord
if payment_type.blank?
payment = ''
else
payment = " req_type LIKE '#{payment_type}'"
payment = " req_type LIKE '#{payment_type}'"
end
if status_type.blank?
@@ -46,10 +46,10 @@ class CardSettleTran < ApplicationRecord
query = CardSettleTran.joins("Join shift_sales ss ON ss.id = card_settle_trans.shift_sale_id"+
" JOIN employees e ON e.id = ss.employee_id")
cardSettle = query.where("DATE_FORMAT(req_date,'%Y-%m-%d') >= ? and DATE_FORMAT(req_date,'%Y-%m-%d') <= ? and status = 'Approved'",from,to)
if shift.present?
query1 = cardSettle.where("ss.id in (?)", shift.to_a)
elsif shift_sale_range.present?
elsif shift_sale_range.present?
query1 = cardSettle.where("ss.id in (?)",shift_sale_range.to_a)
else
query1 = cardSettle.where("DATE_FORMAT(req_date,'%Y-%m-%d') between ? and ?",from,to)
@@ -64,6 +64,5 @@ class CardSettleTran < ApplicationRecord
shift_closed_at As closing_date,")
.order("shift_sales.id DESC")
return query = query.where("shift_sales.shift_started_at >= ?" , from)
# byebug
end
end
end

View File

@@ -1,5 +1,8 @@
class InventoryDefinition < ApplicationRecord
has_many :stock_journals
has_one :menu_item_instance, foreign_key: "item_instance_code", primary_key: "item_code"
scope :active, -> {where(:is_active => true)}
def self.calculate_product_count(saleObj=nil,saleobj_after_req_bill=nil)
@@ -71,7 +74,7 @@ class InventoryDefinition < ApplicationRecord
end
def self.search_by_category(cat_id)
@inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
mi.name as item_name,mii.item_instance_name as instance_name," +
"mi.item_code as item_code,mii.item_instance_code as instance_code," +
"mc.name as menu_category_name,mc.id as menu_category_id ")
@@ -84,27 +87,29 @@ class InventoryDefinition < ApplicationRecord
end
def self.get_by_category(filter)
least_stock = StockJournal.select(:balance).joins("JOIN inventory_definitions ON stock_journals.item_code = inventory_definitions.item_code").order(:id => :desc).limit(1).to_sql
least_stock = StockJournal.select(:inventory_definition_id, :credit, :debit, :balance)
.joins("JOIN (
#{StockJournal.select("item_code, MAX(created_at) created_at").group(:item_code).to_sql}) last_stock_journals
ON last_stock_journals.item_code = stock_journals.item_code
AND last_stock_journals.created_at = stock_journals.created_at").to_sql
@inventory_definitions = InventoryDefinition.select("inventory_definitions.*,
(CASE WHEN sj.credit IS NULL THEN 0 ELSE sj.credit END) as credit,
(CASE WHEN sj.debit IS NULL THEN 0 ELSE sj.debit END) as debit,
(#{least_stock}) as balance,"+
"acc.title as account_name,mi.name as item_name,
mii.item_instance_name as instance_name," +
"mc.name as menu_category_name,mc.id as menu_category_id "
)
.joins(" LEFT JOIN stock_journals sj ON sj.inventory_definition_id=inventory_definitions.id")
.joins("JOIN menu_item_instances mii ON mii.item_instance_code = inventory_definitions.item_code" +
" JOIN menu_items mi ON mi.id = mii.menu_item_id" +
" JOIN menu_categories mc ON mc.id = mi.menu_category_id ")
.joins(" JOIN accounts acc ON acc.id = mi.account_id")
.where("(inventory_definitions.item_code LIKE ? OR inventory_definitions.min_order_level LIKE ?
OR inventory_definitions.max_stock_level LIKE ? OR sj.balance LIKE ? OR mi.name LIKE ?
OR mii.item_instance_name LIKE ? OR mc.name LIKE ?)","%#{filter}%","%#{filter}%","%#{filter}%",
"%#{filter}%","%#{filter}%","%#{filter}%","%#{filter}%")
.group("mi.menu_category_id,inventory_definitions.item_code")
.order("balance asc, mi.name asc,acc.title desc,mi.menu_category_id desc")
inventory_definitions = InventoryDefinition
.select("inventory_definitions.*,
stock_journals.credit as credit,
stock_journals.debit as debit,
stock_journals.balance as balance,
accounts.title as account_name,
menu_items.name as item_name,
menu_item_instances.item_instance_name as instance_name,
menu_categories.name as menu_category_name,
menu_categories.id as menu_category_id")
.joins("LEFT JOIN (#{least_stock}) stock_journals on stock_journals.inventory_definition_id = inventory_definitions.id")
.joins(menu_item_instance: { menu_item: [:menu_category, :account]})
.where("inventory_definitions.item_code LIKE :filter OR inventory_definitions.min_order_level LIKE :filter
OR inventory_definitions.max_stock_level LIKE :filter OR stock_journals.balance LIKE :filter OR menu_items.name LIKE :filter
OR menu_item_instances.item_instance_name LIKE :filter OR menu_categories.name LIKE :filter", filter: "%#{filter}%")
.group("menu_items.menu_category_id, inventory_definitions.item_code")
.order("balance asc, menu_items.name asc, accounts.title desc, menu_items.menu_category_id desc")
end
end

View File

@@ -119,11 +119,15 @@ class Printer::ReceiptPrinter < Printer::PrinterWorker
if count == 1
filename = directory_name + "/receipt_bill_#{sale_data.receipt_no}.pdf"
pdf.render_file filename
if printed_status != 'Paid'
if printed_status != 'Paid' && printed_status != 'credit_payment'
#no print in cloud server
if ENV["SERVER_MODE"] != "cloud"
self.print(directory_name + "/receipt_bill_#{sale_data.receipt_no}.pdf", cashier_terminal.printer_name)
end
elsif printed_status == 'credit_payment'
filename = directory_name + "/receipt_bill_credit_#{sale_data.receipt_no}.pdf"
pdf.render_file filename
self.print(directory_name + "/receipt_bill_credit_#{sale_data.receipt_no}.pdf", cashier_terminal.printer_name)
end
else
filename = directory_name + "/receipt_bill_#{sale_data.receipt_no}_#{count}.pdf"

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

View File

@@ -143,7 +143,7 @@ class SaleAudit < ApplicationRecord
remark = paymal[0].remark.split("}")
response = "["+remark[0]+'}]'
response = JSON.parse(response)
puts response
# puts response
if response[0]["status"] == true
if response[0]["current_rebate_amount"].present?
amount = response[0]["current_rebate_amount"]

View File

@@ -74,6 +74,8 @@ class SalePayment < ApplicationRecord
#get all payment for this invoices
if payment_for
amount_due = SalePayment.get_credit_amount_due_left(self.sale_id).first.payment_amount
elsif payment_method == 'foc'
amount_due = invoice.total_amount
else
amount_due = invoice.sale_payments
.map(&:payment_amount).reduce(invoice.grand_total, :-)
@@ -140,9 +142,6 @@ class SalePayment < ApplicationRecord
remark = "#{self.sale_payment_id}||#{shift_sale_id} -> #{remark}"
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name)
if payment_method == "cash"
update_shift_for_credit_payment
end
else
sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by.name)
end
@@ -235,14 +234,15 @@ class SalePayment < ApplicationRecord
end
def self.redeem(paypar_url,token,membership_id,received_amount,sale_id)
membership_actions_data = PaymentMethodSetting.find_by_payment_method("Redeem")
membership_actions_data = PaymentMethodSetting.find_by_payment_method("Redeem")
if !membership_actions_data.nil?
url = paypar_url.to_s + membership_actions_data.gateway_url.to_s
merchant_uid = membership_actions_data.merchant_account_id
auth_token = membership_actions_data.auth_token
campaign_type_id = JSON.parse(membership_actions_data.additional_parameters)["campaign_type_id"]
campaign_type_id = membership_actions_data.additional_parameter["campaign_type_id"]
sale_data = Sale.find_by_sale_id(sale_id)
account_no = Customer.find_by_customer_id(self.sale.customer_id).paypar_account_no
if sale_data
others = 0
@@ -259,7 +259,7 @@ class SalePayment < ApplicationRecord
redeem_amount:received_amount,
receipt_no:sale_data.receipt_no,
campaign_type_id:campaign_type_id,
account_no:"",
account_no: account_no,
merchant_uid:merchant_uid,
auth_token:auth_token}.to_json,
:headers => {
@@ -371,12 +371,8 @@ class SalePayment < ApplicationRecord
private
def cash_payment(payment_for=false)
status = false
sale_payments_data = SalePayment.find_by_sale_id(self.sale_id)
if sale_payments_data.nil?
status = true
end
sale_payments_data = SalePayment.find_by_sale_id(self.sale_id)
payment_status = false
self.payment_method = "cash"
@@ -391,8 +387,11 @@ class SalePayment < ApplicationRecord
self.payment_status = "paid"
payment_status = self.save!
if !payment_for
sale_update_payment_status(self.received_amount,status)
sale_update_payment_status(self.received_amount)
else
update_shift_for_credit_payment()
end
balance_amount =0.0
outstanding_amount =0.0
if self.sale.grand_total.to_f > self.received_amount.to_f
@@ -408,6 +407,7 @@ class SalePayment < ApplicationRecord
def foc_payment
payment_status = false
sale = self.sale
sale.sale_payments.update_all(payment_status: "cancelled")
# add to sale item with foc
sale_items = sale.sale_items
@@ -424,7 +424,7 @@ class SalePayment < ApplicationRecord
self.payment_status = "paid"
payment_status = self.save!
# sale_update_payment_status(self.received_amount)
sale_update_payment_status(0)
sale_update_payment_status(0, true)
return payment_status
end
@@ -501,7 +501,7 @@ class SalePayment < ApplicationRecord
#Next time - validate if the vochure number is valid - within
customer_data = Customer.find_by_customer_id(self.sale.customer_id)
membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
membership_data = SalePayment.redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id)
membership_data = redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id)
#record an payment in sale-audit
remark = "#{membership_data} Redeem- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
@@ -630,22 +630,32 @@ class SalePayment < ApplicationRecord
def sale_update_payment_status(paid_amount, check_foc = false)
#update amount_outstanding
if ['completed'].include? sale.sale_status
return
end
sale = self.sale
total_payment_amount = sale.sale_payments.reload.sum(&:payment_amount)
sale_payments = sale.sale_payments.reload
is_credit = sale_payments.any? { |x| x.payment_method == "creditnote" }
is_foc = sale_payments.any? { |x| x.payment_method == "foc" } || check_foc
if is_foc
total_payment_amount = 0.0
else
total_payment_amount = sale_payments.sum(&:payment_amount)
end
sale.amount_received = sale.amount_received.to_f + paid_amount.to_f
sale.amount_changed = total_payment_amount - sale.grand_total.to_f
is_credit = sale.sale_payments.any? { |x| x.payment_method == "creditnote" }
is_foc = sale.sale_payments.any? { |x| x.payment_method == "foc" }
if sale.grand_total <= total_payment_amount && sale.sale_status == "new"
if is_foc
sale.payment_status = 'foc'
sale.sale_status = 'completed'
elsif sale.grand_total <= total_payment_amount && sale.sale_status == "new"
sale.payment_status = "paid"
if is_credit
sale.payment_status = "outstanding"
end
if is_foc
sale.payment_status = "foc"
end
sale.sale_status = "completed"
@@ -672,26 +682,25 @@ class SalePayment < ApplicationRecord
end
end
end
end
sale.save!
sale.save!
if check_foc
table_update_status(sale)
update_shift
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
table_update_status(sale)
update_shift
elsif paid_amount.to_f == 0 && !is_credit
table_update_status(sale)
update_shift
end
if check_foc
table_update_status(sale)
update_shift
elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
table_update_status(sale)
update_shift
elsif paid_amount.to_f == 0 && !is_credit
table_update_status(sale)
update_shift
end
end
# update for cashier shift
def update_shift
current_shift_user = Employee.find_by_id(self.action_by.id)
shift = ShiftSale.current_open_shift(current_shift_user)
shift = ShiftSale.current_open_shift(self.action_by.id)
if shift.nil?
current_shift_user = Employee.find_by_id(self.sale.cashier_id)
@@ -717,7 +726,10 @@ class SalePayment < ApplicationRecord
# update for shift with credit payment
def update_shift_for_credit_payment
shift_credit = ShiftSale.find_by_id(self.sale.shift_sale_id)
shift = ShiftSale.find_by_id(ShiftSale.current_shift)
shift = self.action_by.current_shift
if !shift.nil?
shift = ShiftSale.current_shift
end
if !shift.nil?
credit_payment_left = get_credit_payment_left[0].payment_amount.to_f
if self.payment_method == "cash"
@@ -726,7 +738,6 @@ class SalePayment < ApplicationRecord
else
# extra_changed_amount = self.received_amount.to_f + credit_payment_left
shift.cash_sales = shift.cash_sales.to_f + (self.received_amount.to_f + credit_payment_left)
self.sale.amount_received = self.sale.amount_received.to_f - credit_payment_left
self.sale.amount_changed = self.sale.amount_changed.to_f - credit_payment_left
self.sale.save!