merge with r-19
This commit is contained in:
@@ -5,21 +5,25 @@ class Sale < ApplicationRecord
|
||||
#primary key - need to be unique generated for multiple shops
|
||||
before_create :generate_custom_id
|
||||
before_create :generate_receipt_no
|
||||
|
||||
belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee"
|
||||
belongs_to :customer, :optional => true
|
||||
belongs_to :shift_sale
|
||||
has_one :survey, foreign_key: "receipt_no"
|
||||
has_one :current_shift_by_cashier, through: :cashier, source: :current_shift
|
||||
has_one :cashier_terminal_by_shift_sale, through: :shift_sale, source: :cashier_terminal
|
||||
has_one :survey, foreign_key: "receipt_no"
|
||||
has_many :sale_audits
|
||||
has_many :sale_items
|
||||
has_many :sale_discount_items
|
||||
has_many :sale_discounts
|
||||
has_many :sale_taxes
|
||||
has_many :sale_payments
|
||||
has_many :payments_for_credits, through: :sale_audits
|
||||
has_many :sale_orders
|
||||
has_many :sale_payments_for_credits, through: :sale_audits
|
||||
has_many :orders, through: :sale_orders
|
||||
has_many :order_items, through: :sale_orders
|
||||
has_many :bookings
|
||||
has_one :booking
|
||||
has_many :product_commissions
|
||||
|
||||
before_validation :round_to_precision
|
||||
@@ -31,9 +35,7 @@ class Sale < ApplicationRecord
|
||||
scope :completed, -> { where(sale_status: 'completed') }
|
||||
|
||||
scope :receipt_date_between, -> (from, to) { where(receipt_date: from..to) }
|
||||
scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) }
|
||||
scope :shop, -> { where("sales.shop_code=?",Shop.current_shop.shop_code) }
|
||||
|
||||
scope :along_with_sale_payments_except_void_between, -> (from, to) { joins(sanitize_sql_array(["LEFT JOIN sale_payments ON sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND sale_payments.created_at BETWEEN ? and ?", from, to])) }
|
||||
|
||||
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
|
||||
@@ -82,186 +84,114 @@ class Sale < ApplicationRecord
|
||||
Rails.logger.debug '........ Sale data sync completed .......'
|
||||
end
|
||||
end
|
||||
def self.generate_invoice_from_booking(booking, requested_by, cashier, order_source = nil, current_checkin_induties_count)
|
||||
if booking
|
||||
Rails.logger.debug "Booking -> " + booking.id.to_s
|
||||
Rails.logger.debug "Booking -> Booking Order Count -> " + booking.booking_orders.count.to_s
|
||||
#get all order attached to this booking and combine into 1 invoice
|
||||
|
||||
unless sale = booking.sale
|
||||
sale = booking.build_sale(
|
||||
def self.generate_invoice_from_booking(booking, requested_by, cashier, order_source = nil, in_duties_count = 0)
|
||||
Sale.transaction do
|
||||
if booking
|
||||
current = Time.now
|
||||
#get all order attached to this booking and combine into 1 invoice
|
||||
sale = booking.sale || booking.build_sale(
|
||||
{
|
||||
tax_type: "execulive" # Default Tax - Values
|
||||
}
|
||||
)
|
||||
end
|
||||
booking.booking_orders.each do |order|
|
||||
sale.generate_invoice_from_order(order.order_id, booking, requested_by, cashier, order_source)
|
||||
# saleObj = Sale.find(sale_id)
|
||||
# order = booking.booking_orders.take.order
|
||||
# link_order_sale(order.order_id)
|
||||
end
|
||||
# InventoryJob.perform_now(self.id)
|
||||
# InventoryDefinition.calculate_product_count(saleObj)
|
||||
|
||||
|
||||
# dining charges
|
||||
charges = DiningCharge.where('dining_facility_id=?', booking.dining_facility_id).take
|
||||
if !charges.nil?
|
||||
block_count, diningprice = DiningCharge.amount_calculate(charges, booking.checkin_at, booking.checkout_at)
|
||||
if charges.charge_type =='hr'
|
||||
dining_time = booking.checkin_at.strftime('%H:%M %p').to_s + " - " + booking.checkout_at.strftime('%H:%M %p').to_s
|
||||
else
|
||||
dining_time = booking.checkin_at.strftime('%B %d, %H:%M %p').to_s + " - " + booking.checkout_at.strftime('%B %d, %H:%M %p').to_s
|
||||
end
|
||||
later_time = booking.checkout_at
|
||||
early_time = booking.checkin_at
|
||||
distance_in_minutes = ((later_time - early_time)/60.0).round
|
||||
basic_pay_amount = 0
|
||||
name = ""
|
||||
if current_checkin_induties_count != "0"
|
||||
basic_pay = Commission.where('commission_type=?','Basic Pay')
|
||||
basic_pay.each do |pay|
|
||||
basic_pay_amount = pay.amount
|
||||
name = pay.name
|
||||
end
|
||||
induties_pay_amount = (current_checkin_induties_count.to_i * (distance_in_minutes / 60.0).to_f * basic_pay_amount).to_i
|
||||
sale.create_saleitem_indutycharges(charges, current_checkin_induties_count.to_i, induties_pay_amount, booking.dining_facility.name, dining_time, order_source, basic_pay_amount)
|
||||
end
|
||||
sale.create_saleitem_diningcharges(charges, block_count, diningprice, booking.dining_facility.name, dining_time, order_source)
|
||||
end
|
||||
|
||||
return sale
|
||||
end
|
||||
end
|
||||
|
||||
def generate_invoice_from_order(order_id, booking, requested_by, cashier = nil, order_source = nil)
|
||||
taxable = true
|
||||
|
||||
order = Order.find(order_id)
|
||||
|
||||
# current cashier login
|
||||
open_cashier = Employee.where("role = 'cashier' AND token_session <> ''")
|
||||
current_shift = ShiftSale.current_shift
|
||||
# shift with terminal zone
|
||||
|
||||
# set cashier
|
||||
if order_source.present? && order_source.downcase == "emenu"
|
||||
if !booking.dining_facility_id.nil?
|
||||
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
|
||||
#for multiple zone with terminal
|
||||
if shift.nil?
|
||||
multiple_zone = CashierTerminalByZone.where("zone_id = #{table.zone_id}")
|
||||
multiple_zone.each do |zone|
|
||||
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and cashier_terminal_id = #{zone.cashier_terminal_id}").first
|
||||
if !shift.nil? then
|
||||
break
|
||||
end
|
||||
if cashier.role == 'cashier'
|
||||
sale.cashier = cashier
|
||||
sale.shift_sale = cashier.current_shift
|
||||
elsif booking.dining_facility
|
||||
if sale.shift_sale = booking.current_shift_by_dining_facility
|
||||
sale.cashier = sale.shift_sale.employee
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
shift = ShiftSale.current_open_shift(cashier)
|
||||
end
|
||||
# set cashier
|
||||
if shift != nil #if current login employee open shift
|
||||
self.cashier_id = cashier.id
|
||||
self.cashier_name = cashier.name
|
||||
self.shift_sale_id = shift.id
|
||||
else
|
||||
if open_cashier.count>0 # if we have two open cashier
|
||||
# table and terminal in multiple shift
|
||||
self.cashier_id = open_cashier[0].id
|
||||
self.cashier_name = open_cashier[0].name
|
||||
shift_id = ShiftSale.current_open_shift(open_cashier[0])
|
||||
if shift_id
|
||||
self.shift_sale_id = shift_id.id
|
||||
|
||||
if sale.shift_sale.nil?
|
||||
if sale.cashier = Employee.where(role: 'cashier').where.not(token_session: [nil, '']).first
|
||||
sale.shift_sale = sale.current_shift_by_cashier
|
||||
else
|
||||
self.shift_sale_id = current_shift.id
|
||||
sale.shift_sale = ShiftSale.current_shift
|
||||
sale.cashier = sale.shift_sale.employee
|
||||
end
|
||||
else
|
||||
self.cashier_id = current_shift.employee_id
|
||||
self.cashier_name = Employee.find(current_shift.employee_id).name
|
||||
self.shift_sale_id = current_shift.id
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# set waiter
|
||||
self.requested_by = requested_by.name
|
||||
|
||||
self.requested_at = DateTime.now.utc.getlocal
|
||||
self.shop_code = Shop.current_shop.shop_code
|
||||
Rails.logger.debug "Order -> #{order.id} | order_status -> #{order.status}"
|
||||
if order
|
||||
self.customer_id = order.customer_id
|
||||
Rails.logger.debug "Order -> #{order.id} | Items Count -> #{order.order_items.count}"
|
||||
|
||||
order.order_items.each do |item|
|
||||
self.add_item(item)
|
||||
if !item.set_menu_items.nil?
|
||||
self.add_sub_item(item.set_menu_items)
|
||||
end
|
||||
|
||||
sale.cashier_name = sale.cashier.name
|
||||
sale.requested_by = requested_by.name
|
||||
sale.requested_at = current
|
||||
|
||||
sale.sale_items << booking.order_items.to_sale_items
|
||||
|
||||
if booking.dining_facility.present? && dining_charge = booking.dining_facility.dining_charge
|
||||
block_count, dining_price = DiningCharge.amount_calculate(dining_charge, booking.checkin_at, booking.checkout_at)
|
||||
format = "%I:%M %p" if dining_charge.charge_type == "hr"
|
||||
format ||= "%B %d, %I:%M %p"
|
||||
dining_time = "#{booking.checkin_at.strftime(format)} - #{booking.checkout_at.strftime(format)}"
|
||||
|
||||
sale.sale_items.build({
|
||||
menu_category_name: "Dining Charge",
|
||||
menu_category_code: "DiningCharge",
|
||||
product_name: "#{booking.dining_facility.name} ( #{dining_time} )",
|
||||
product_code: dining_charge.item_code,
|
||||
product_alt_name: '-',
|
||||
account_id: 0,
|
||||
is_taxable: dining_charge.taxable,
|
||||
qty: block_count,
|
||||
unit_price: dining_charge.unit_price,
|
||||
price: dining_price,
|
||||
taxable_price: dining_price,
|
||||
})
|
||||
|
||||
if in_duties_count.to_i > 0
|
||||
basic_pay = Commission.find_by(commission_type: 'Basic Pay')
|
||||
in_duties_pay_amount = (in_duties_count.to_i * ((booking.checkout_at - booking.checkin_at) / 1.hours) * basic_pay_amount).to_i
|
||||
|
||||
sale.sale_items.build({
|
||||
menu_category_name: "Induty Charge",
|
||||
menu_category_code: "IndutyCharge",
|
||||
product_name: "#{basic_pay.name} ( #{dining_time} )",
|
||||
product_code: "",
|
||||
product_alt_name: '-',
|
||||
account_id: 0,
|
||||
is_taxable: dining_charge.taxable,
|
||||
qty: in_duties_count,
|
||||
unit_price: basic_pay.amount,
|
||||
price: in_duties_pay_amount,
|
||||
taxable_price: in_duties_pay_amount,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
sale.orders << booking.orders
|
||||
sale.customer_id = booking.orders[0].customer_id
|
||||
|
||||
sale.compute(booking.orders[0].source)
|
||||
|
||||
booking.orders.update_all(status: "billed")
|
||||
booking.order_items.update_all(order_item_status: "billed")
|
||||
|
||||
booking.checkout_at = current unless booking.checkout_at && booking.checkout_at > current
|
||||
booking.checkout_by = requested_by.name
|
||||
|
||||
booking.save
|
||||
return sale
|
||||
end
|
||||
self.orders << order
|
||||
end
|
||||
|
||||
#compute sales summary
|
||||
if order_source.nil?
|
||||
order_source = order.source
|
||||
end
|
||||
self.compute(order_source)
|
||||
|
||||
#Update the order items that is billed
|
||||
order.update_items_status_to_billed(nil)
|
||||
order.status = "billed"
|
||||
order.save
|
||||
|
||||
if !booking.checkout_at.nil?
|
||||
if booking.checkout_at.utc < Time.now.utc
|
||||
booking.checkout_at = Time.now.utc.getlocal
|
||||
end
|
||||
else
|
||||
booking.checkout_at = Time.now.utc.getlocal
|
||||
end
|
||||
|
||||
booking.checkout_by = requested_by.name
|
||||
booking.save
|
||||
|
||||
# InventoryJob.perform_now(self.id)
|
||||
# saleObj = Sale.find(self.id)
|
||||
# InventoryDefinition.calculate_product_count(saleObj)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
#fOR Quick Service pay and create
|
||||
def self.request_bill(order,current_user,current_login_employee)
|
||||
def self.request_bill(order, current_user, current_login_employee)
|
||||
if !ShiftSale.current_shift.nil?
|
||||
order_id = order.order_id # order_id
|
||||
bk_order = BookingOrder.find_by_order_id(order_id)
|
||||
check_booking = Booking.find_by_booking_id(bk_order.booking_id)
|
||||
booking = order.booking
|
||||
|
||||
if @sale_data = check_booking.sale
|
||||
# Create Sale if it doesn't exist
|
||||
@sale_items = SaleItem.where("sale_id=?",@sale_id)
|
||||
elsif @sale_data = Sale.generate_invoice_from_booking(check_booking,current_login_employee,current_user,order.source)
|
||||
@sale_items = SaleItem.where("sale_id=?",@sale_data.sale_id)
|
||||
if booking.sale.nil?
|
||||
sale_data = Sale.generate_invoice_from_booking(booking, current_login_employee, current_user, order.source)
|
||||
# Promotion Activation
|
||||
Promotion.promo_activate(sale_data)
|
||||
end
|
||||
|
||||
# Bind shift sale id to sale
|
||||
# @sale_data.shift_sale_id = shift.id
|
||||
# @sale_data.save
|
||||
|
||||
# Promotion Activation
|
||||
Promotion.promo_activate(@sale_data)
|
||||
@status = true
|
||||
return @status, @sale_data
|
||||
return true, sale_data
|
||||
else
|
||||
@status = false
|
||||
@message = "No Current Open Shift for This Employee"
|
||||
return false, "No Current Open Shift for This Employee"
|
||||
end
|
||||
end
|
||||
#This is when spilt bill is request - then we cannot link order to invoice
|
||||
@@ -532,6 +462,19 @@ class Sale < ApplicationRecord
|
||||
end
|
||||
|
||||
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
|
||||
if tax_type.nil?
|
||||
if tax_profiles.count == 2
|
||||
tax_type = "all"
|
||||
elsif tax_profiles.count == 1
|
||||
if tax_profiles.name.downcase == "service charges"
|
||||
tax_type = "Service Charges"
|
||||
else
|
||||
tax_type = "Commercial Tax"
|
||||
end
|
||||
elsif tax_profiles.count < 1
|
||||
tax_type = "no_tax"
|
||||
end
|
||||
end
|
||||
|
||||
# #Creat new tax records
|
||||
if self.payment_status != 'foc' && tax_type.to_s == "all"
|
||||
@@ -690,14 +633,6 @@ class Sale < ApplicationRecord
|
||||
self.total_tax = total_tax_amount
|
||||
end
|
||||
|
||||
def product_get_unit_price(item_code)
|
||||
menu_item_hash =MenuItem.search_by_item_code(item_code)
|
||||
if (menu_instance_code)
|
||||
return menu_ item_hash[:item_instance_code], menu_item_hash[:price]
|
||||
end
|
||||
return nil,nil
|
||||
end
|
||||
|
||||
def link_order_sale(order_id)
|
||||
#create if it doesn't exist
|
||||
saleOrder = SaleOrder.where("sale_id=? and order_id=?", self.id, order_id).take
|
||||
@@ -861,11 +796,15 @@ def self.daily_sales_list(from,to)
|
||||
|
||||
daily_total = connection.select_all("SELECT
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) as total_sale,
|
||||
IFNULL(SUM(case when (sale_status='completed') then old_grand_total else 0 end),0) as old_grand_total,
|
||||
IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0) as total_discount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then amount_changed else 0 end),0) as total_change_amount,
|
||||
IFNULL(SUM(case when (sale_status='void') then grand_total else 0 end),0) as void_amount,
|
||||
IFNULL(SUM(case when (sale_status='completed') then rounding_adjustment else 0 end),0) as rounding_adj,
|
||||
IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21 as tax,
|
||||
(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0))-(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0) / 21) as net_sale,
|
||||
(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) + (IFNULL(SUM(case when (sale_status='completed') then total_discount else 0 end),0)) as gross_sale,
|
||||
CAST((CONVERT_TZ(receipt_date,'+00:00','+06:30')) AS DATE) as sale_date,
|
||||
SUM(kbzpay_amount) as kbzpay_amount,
|
||||
SUM(mpu_amount) as mpu_amount,
|
||||
@@ -1129,7 +1068,7 @@ def self.get_by_shift_items(shift_sale_range, shift, from, to, status,type,accou
|
||||
return query,other_charges, product, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount
|
||||
end
|
||||
|
||||
def self.get_staff_meal_items(shift_sale_range, shift, from, to, status,account_type,customer_id,shop_code)
|
||||
def self.get_staff_meal_items(shift_sale_range, shift, from, to, status, account_type, customer_id)
|
||||
# date_type_selection = get_sql_function_for_report_type(report_type)
|
||||
if account_type.blank?
|
||||
account_type = ''
|
||||
@@ -1141,8 +1080,6 @@ def self.get_staff_meal_items(shift_sale_range, shift, from, to, status,account_
|
||||
customer_id = customer_id.to_s
|
||||
customer_id[0] = ""
|
||||
customer_id = customer_id.chomp("]")
|
||||
else
|
||||
customer_id = '"CUS-000000000000"'
|
||||
end
|
||||
|
||||
query = self.get_staff_meal_query()
|
||||
@@ -1299,7 +1236,7 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay
|
||||
.includes(:bookings => :dining_facility)
|
||||
.select("sales.*, SUM(sale_payments.payment_amount) AS payments_for_credits_amount")
|
||||
.joins(:bookings)
|
||||
.left_joins(:sale_payments_for_credits)
|
||||
.left_joins(:payments_for_credits)
|
||||
.completed
|
||||
.where.not(total_amount: 0)
|
||||
.group(:sale_id)
|
||||
@@ -1352,16 +1289,16 @@ def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter,
|
||||
|
||||
if order_source.present?
|
||||
if order_source == "cashier"
|
||||
credits = credits.where("order_sources.source IN (?)", ['cashier', 'emenu'])
|
||||
credits = credits.where("orders.source IN (?)", ['cashier', 'emenu'])
|
||||
else
|
||||
credits = credits.where("order_sources.source = ?", order_source)
|
||||
credits = credits.where("orders.source = ?", order_source)
|
||||
end
|
||||
end
|
||||
|
||||
if filter == 'paid'
|
||||
credits = credits.where("payments_for_credits IS NOT NULL")
|
||||
credits = credits.where("payments_for_credits.sale_id IS NOT NULL")
|
||||
elsif filter == 'unpaid'
|
||||
credits = credits.where("payments_for_credits IS NULL")
|
||||
credits = credits.where("payments_for_credits.sale_id IS NULL")
|
||||
end
|
||||
|
||||
if shift.present?
|
||||
@@ -1428,96 +1365,57 @@ def self.get_separate_tax(shift_sale_range=nil,shift,from,to,payment_type)
|
||||
end
|
||||
end
|
||||
|
||||
def self.get_payment_method_by_shift(shift_sale_range,shift,from,to,payment_type,shop_code)
|
||||
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.shop_code='#{shop_code}'"
|
||||
def self.get_payment_method_by_shift(shift_sale_range,shift,from,to,payment_type)
|
||||
payments_total = SalePayment.select("
|
||||
sales.receipt_date as sale_date,
|
||||
SUM(case when (sale_payments.payment_method='mpu') then sale_payments.payment_amount else 0 end) as mpu_amount,
|
||||
SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount,
|
||||
SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount,
|
||||
SUM(case when (sale_payments.payment_method='jcb') then sale_payments.payment_amount else 0 end) as jcb_amount,
|
||||
SUM(case when (sale_payments.payment_method='paypar') then sale_payments.payment_amount else 0 end) as paypar_amount,
|
||||
SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='alipay') then sale_payments.payment_amount else 0 end) as alipay_amount,
|
||||
SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='paymal') then sale_payments.payment_amount else 0 end) as paymal_amount,
|
||||
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,
|
||||
SUM(case when (sale_payments.payment_method='cash') then
|
||||
sales.amount_changed else 0 end) as total_change_amount,
|
||||
SUM(case when (sale_payments.payment_method='creditnote') then
|
||||
sale_payments.payment_amount else 0 end) -
|
||||
SUM(case when (sale_audits.sale_audit_id IS NOT NULL) then
|
||||
sale_payments.payment_amount 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(:sale).left_joins(:sale_audit)
|
||||
.merge(Sale.completed.receipt_date_between(from, to))
|
||||
|
||||
sales = Sale.select(Sale.column_names)
|
||||
.select("SUM(sale_payments.payment_amount) as payment_for_credits_amount")
|
||||
.left_joins(:payments_for_credits)
|
||||
.group(:sale_id)
|
||||
|
||||
sale_payment = SalePayment.select("sales.amount_changed as change_amount,sales.receipt_no, sale_payments.*, sales.receipt_date as sale_date,
|
||||
sales.cashier_name as cashier_name")
|
||||
.joins("JOIN (#{sales.to_sql}) as sales ON sales.sale_id = sale_payments.sale_id")
|
||||
.where("sale_payments.payment_method != 'creditnote' OR payment_for_credits_amount - sale_payments.payment_amount <= 0")
|
||||
.where.not(payment_amount: 0)
|
||||
.merge(Sale.completed.receipt_date_between(from, to))
|
||||
|
||||
if shift.present?
|
||||
shift_ids = shift.map(&:id).join(",")
|
||||
sub_query += " AND sales.shift_sale_id in (#{shift_ids})"
|
||||
payments_total = payments_total.where(sales: {shift_sale_id: shift.to_a})
|
||||
sale_payment = sale_payment.merge(Sale.where(sales: {shift_sale_id: shift.to_a}))
|
||||
elsif shift_sale_range.present?
|
||||
shift_ids = shift_sale_range.map(&:id).join(",")
|
||||
sub_query += " AND sales.shift_sale_id in (#{shift_ids})"
|
||||
else
|
||||
sub_query += " AND sales.receipt_date between '#{from}' and '#{to}'"
|
||||
payments_total = payments_total.where(sales: {shift_sale_id: shift_sale_range.to_a})
|
||||
sale_payment = sale_payment.merge(Sale.where(sales: {shift_sale_id: shift_sale_range.to_a}))
|
||||
end
|
||||
|
||||
sub_query1 = "CASE WHEN (SELECT SUM(sale_payments.payment_amount)
|
||||
FROM sale_payments
|
||||
WHERE sale_payments.payment_method = 'creditnote'
|
||||
AND sale_payments.sale_id = s.sale_id
|
||||
) - (SELECT SUM(sale_payments.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 = s.sale_id) = 0
|
||||
THEN sale_payments.sale_id = s.sale_id
|
||||
AND sale_payments.payment_method!='creditnote' ELSE 1 END"
|
||||
|
||||
sale_payment = SalePayment.select("s.amount_changed as change_amount,s.receipt_no, sale_payments.*,s.receipt_date as sale_date,
|
||||
s.cashier_name as cashier_name")
|
||||
.joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id")
|
||||
.where("(#{sub_query1}) and s.shop_code='#{shop_code}'")
|
||||
.order('s.receipt_no DESC')
|
||||
|
||||
payments_total = SalePayment.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,
|
||||
SUM(case when (sale_payments.payment_method='master') then sale_payments.payment_amount else 0 end) as master_amount,
|
||||
SUM(case when (sale_payments.payment_method='visa') then sale_payments.payment_amount else 0 end) as visa_amount,
|
||||
SUM(case when (sale_payments.payment_method='jcb') then sale_payments.payment_amount else 0 end) as jcb_amount,
|
||||
SUM(case when (sale_payments.payment_method='paypar') then sale_payments.payment_amount else 0 end) as paypar_amount,
|
||||
SUM(case when (sale_payments.payment_method='unionpay') then sale_payments.payment_amount else 0 end) as unionpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='alipay') then sale_payments.payment_amount else 0 end) as alipay_amount,
|
||||
SUM(case when (sale_payments.payment_method='KBZPay') then sale_payments.payment_amount else 0 end) as kbzpay_amount,
|
||||
SUM(case when (sale_payments.payment_method='paymal') then sale_payments.payment_amount else 0 end) as paymal_amount,
|
||||
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,
|
||||
SUM(case when (sale_payments.payment_method='cash') then sales.amount_changed else 0 end) as total_change_amount,
|
||||
(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("join sales on sales.sale_id = sale_payments.sale_id")
|
||||
.where("sales.shop_code='#{shop_code}'")
|
||||
|
||||
if shift.present?
|
||||
|
||||
all_total = payments_total.where("sales.shift_sale_id in (?) and sale_status= 'completed' and sale_payments.payment_amount != 0", shift.to_a)
|
||||
# .group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')")
|
||||
if payment_type.blank?
|
||||
sale_type = sale_payment.where(" s.sale_status = 'completed' and payment_amount != 0 and s.shift_sale_id in (?)",shift.to_a)
|
||||
.order("payment_method")
|
||||
else
|
||||
sale_type = sale_payment.where(" payment_method= '#{payment_type}' and payment_amount != 0 and s.sale_status = 'completed' and s.shift_sale_id in (?)",shift.to_a)
|
||||
end
|
||||
|
||||
elsif shift_sale_range.present?
|
||||
|
||||
all_total = payments_total.where("sales.shift_sale_id in (?) and sale_status= 'completed' and sale_payments.payment_amount != 0", shift_sale_range.to_a)
|
||||
# .group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')")
|
||||
if payment_type.blank?
|
||||
sale_type = sale_payment.where(" s.sale_status = 'completed' and payment_amount != 0 and s.shift_sale_id in (?)",shift_sale_range.to_a)
|
||||
.order("payment_method")
|
||||
else
|
||||
sale_type = sale_payment.where(" payment_method='#{payment_type}' and payment_amount != 0 and s.sale_status = 'completed' and s.shift_sale_id in (?)",shift_sale_range.to_a)
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
all_total = payments_total.where("sales.receipt_date between ? and ? and sales.sale_status= 'completed' and sale_payments.payment_amount != 0", from,to)
|
||||
# .group("DATE_FORMAT((CONVERT_TZ(sales.receipt_date,'+00:00','+06:30')),'%Y-%m-%d')")
|
||||
if payment_type.blank?
|
||||
sale_type = sale_payment.where(" s.sale_status = 'completed' and payment_amount != 0 and s.receipt_date between ? and ? ",from,to)
|
||||
.order("payment_method")
|
||||
else
|
||||
sale_type = sale_payment.where(" payment_method='#{payment_type}' and payment_amount != 0 and s.sale_status = 'completed' and s.receipt_date between ? and ? ",from,to)
|
||||
end
|
||||
if payment_type.present?
|
||||
sale_payment = sale_payment.where(payment_method: payment_type)
|
||||
end
|
||||
|
||||
return all_total,sale_type
|
||||
return payments_total, sale_payment
|
||||
end
|
||||
|
||||
def self.get_wastes_and_spoilages(from,to,status)
|
||||
@@ -1550,10 +1448,10 @@ end
|
||||
cash = 0.0
|
||||
self.sale_payments.each do |pay|
|
||||
if pay.payment_method == 'cash'
|
||||
cash = pay.payment_amount-self.amount_changed
|
||||
cash += pay.payment_amount
|
||||
end
|
||||
end
|
||||
return cash
|
||||
return cash - self.amount_changed
|
||||
end
|
||||
|
||||
def get_credit_amount
|
||||
@@ -2130,6 +2028,7 @@ end
|
||||
|
||||
def unique_tax_profiles(order_source, customer_id)
|
||||
tax_data = TaxProfile.where(group_type: order_source)
|
||||
|
||||
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)
|
||||
@@ -2159,7 +2058,7 @@ def self.employee_sale(shift=nil,from=nil,to=nil,from_time=nil,to_time=nil, curr
|
||||
end
|
||||
|
||||
# Start hourly sale item report
|
||||
def self.get_by_hourly_items(shift_sale_range, shift, from, to, status,type,account_type,shop_code)
|
||||
def self.get_by_hourly_items(shift_sale_range, shift, from, to, status,type,account_type,filter,start_time,end_time)
|
||||
# date_type_selection = get_sql_function_for_report_type(report_type)
|
||||
if account_type.blank?
|
||||
account_type = ''
|
||||
@@ -2167,7 +2066,26 @@ def self.get_by_hourly_items(shift_sale_range, shift, from, to, status,type,acco
|
||||
account_type = " and acc.title = '#{account_type}'"
|
||||
end
|
||||
|
||||
query = self.get_hourly_item_query(type)
|
||||
if start_time.blank? || end_time.blank?
|
||||
receipt_date = "sales.receipt_date between '#{from}' and '#{to}'"
|
||||
query = self.get_hourly_item_query(type,filter,true)
|
||||
else
|
||||
start_time = Time.parse(start_time).utc
|
||||
end_time = Time.parse(end_time).utc
|
||||
receipt_date = "sales.receipt_date between '#{start_time}' and '#{end_time}'"
|
||||
query = self.get_hourly_item_query(type,filter,false)
|
||||
end
|
||||
|
||||
unless start_time.blank? && end_time.blank?
|
||||
from_date = from.to_date
|
||||
to_date = to.to_date
|
||||
from_date_time = DateTime.new(from_date.year, from_date.month, from_date.day, start_time.hour, start_time.min, start_time.sec, start_time.zone)
|
||||
to_date_time = DateTime.new(to_date.year, to_date.month, to_date.day, end_time.hour, end_time.min, end_time.sec, end_time.zone)
|
||||
receipt_date = "sales.receipt_date between '#{from_date_time}' and '#{to_date_time}'"
|
||||
query = self.get_hourly_item_query(type,filter,false)
|
||||
end
|
||||
|
||||
# query = self.get_hourly_item_query(type,filter)
|
||||
|
||||
discount_query = 0
|
||||
total_card_amount = 0
|
||||
@@ -2228,7 +2146,7 @@ def self.get_by_hourly_items(shift_sale_range, shift, from, to, status,type,acco
|
||||
total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f
|
||||
|
||||
else
|
||||
query = query.where("sales.receipt_date between ? and ? #{account_type} and sale_status='completed'",from,to)
|
||||
query = query.where("#{receipt_date} #{account_type} and sale_status='completed'")
|
||||
if type.nil? || type == 'all' || type == "other"
|
||||
other_charges = other_charges.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to)
|
||||
end
|
||||
@@ -2255,11 +2173,16 @@ def self.get_by_hourly_items(shift_sale_range, shift, from, to, status,type,acco
|
||||
return query,other_charges, product, discount_query , total_cash_amount , total_card_amount , total_credit_amount , total_foc_amount , total_grand_total , change_amount
|
||||
end
|
||||
|
||||
def self.get_hourly_item_query(type)
|
||||
def self.get_hourly_item_query(type,filter,status)
|
||||
if status
|
||||
group_by = "acc.title,i.account_id,i.menu_category_code,i.item_instance_code,i.product_name,i.unit_price,hour"
|
||||
else
|
||||
group_by = "acc.title,i.account_id,i.menu_category_code,i.item_instance_code,i.product_name,i.unit_price"
|
||||
end
|
||||
check_product = "i.menu_category_name != 'product'"
|
||||
if type == "revenue"
|
||||
sale_type = "i.qty > 0 and status IS NULL"
|
||||
elsif type == "all" || type.nil?
|
||||
elsif type == "all" || type.nil? || type.empty?
|
||||
sale_type = "#{check_product}"
|
||||
elsif type == "discount"
|
||||
sale_type = "#{check_product} and i.status = 'Discount'"
|
||||
@@ -2287,8 +2210,8 @@ def self.get_hourly_item_query(type)
|
||||
" JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id")
|
||||
# "JOIN employee_accesses ea ON ea.`employee_id` = sales.cashier_id ")
|
||||
query = query.joins(" JOIN accounts acc ON acc.id = i.account_id")
|
||||
query = query.where("#{sale_type}")
|
||||
query = query.group("acc.title,i.account_id,i.menu_category_code,i.item_instance_code,i.product_name,i.unit_price,hour")
|
||||
query = query.where("#{sale_type} and i.product_name LIKE '%#{filter}%'" )
|
||||
query = query.group("#{group_by}")
|
||||
.order("hour asc")
|
||||
# query = query.order("i.menu_category_name asc, SUM(i.qty) desc")
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user