Merge branch 'r-1902001-01' into foodcourt

This commit is contained in:
Thein Lin Kyaw
2020-08-25 11:56:57 +06:30
8 changed files with 124 additions and 91 deletions

View File

@@ -9,10 +9,10 @@ class DiningFacility < ApplicationRecord
has_many :order_queue_stations, -> { where(is_active: true) }, through: :order_queue_process_by_zones
has_many :bookings
has_many :current_bookings, -> { left_joins(:sale).assign.merge(Booking.where(checkout_at: nil).or(Booking.merge(Sale.where(sale_status: ['new', nil])))) }, class_name: "Booking"
has_one :current_checkin_booking, -> { left_joins(:sale).assign.merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_one :current_checkout_booking, -> { left_joins(:sale).assign.where.not(checkout_at: nil).merge(Sale.where(sale_status: 'new')) }, class_name: "Booking"
has_one :current_reserved_booking, -> { left_joins(:sale).assign.where.not(reserved_at: nil).merge(Sale.where(sale_status: nil)) }, class_name: "Booking"
has_many :current_bookings, -> { left_joins(:sale).assign.merge(Booking.unscoped.where(checkout_at: nil).or(Booking.merge(Sale.unscoped.where(sale_status: ['new', nil])))) }, class_name: "Booking"
has_one :current_checkin_booking, -> { left_joins(:sale).assign.merge(Sale.unscoped.where(sale_status: nil)) }, class_name: "Booking"
has_one :current_checkout_booking, -> { left_joins(:sale).assign.where.not(checkout_at: nil).merge(Sale.unscoped.where(sale_status: 'new')) }, class_name: "Booking"
has_one :current_reserved_booking, -> { left_joins(:sale).assign.where.not(reserved_at: nil).merge(Sale.unscoped.where(sale_status: nil)) }, class_name: "Booking"
has_many :current_sales, -> { where(sale_status: 'new').merge(Booking.assign) }, through: :bookings, class_name: "Sale", source: "sale"

View File

@@ -794,6 +794,8 @@ class Sale < ApplicationRecord
def self.daily_sales_list(from,to)
payment_methods = SalePayment.where.not(payment_method: ['cash', 'creditnote', 'foc']).distinct.pluck(:payment_method)
tax_profiles = TaxProfile.group(:name).order(:order_by).pluck(:name)
sales = select(Sale.column_names)
.select("#{payment_methods.map { |method| "SUM(case when (sale_payments.payment_method='#{method}') then sale_payments.payment_amount else 0 end) as `#{method == 'paypar' ? 'redeem' : method}`"}.push('').join(', ')}
SUM(case when (sale_payments.payment_method='cash') then sale_payments.payment_amount else 0 end) as cash_amount,
@@ -804,6 +806,15 @@ class Sale < ApplicationRecord
.where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to)
.group("sale_id").to_sql
sale_taxes = Sale.select('sales.sale_id, sale_taxes.tax_name')
.joins(:sale_taxes)
.where('(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ', 'completed', 'void', from, to)
.group('sale_id')
if tax_profiles.present?
sale_taxes = sale_taxes.select(tax_profiles.map { |name| "SUM(case when (sale_taxes.tax_name = '#{name}') then sale_taxes.tax_payable_amount else 0 end) as `#{name.parameterize}`"}.join(', '))
end
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,
@@ -812,8 +823,9 @@ class Sale < ApplicationRecord
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,
#{tax_profiles.map { |name| "SUM(`#{name.parameterize}`) as `#{name.parameterize}`"}.push('').join(', ') if tax_profiles.present?}
IFNULL(SUM(case when (sale_status='completed') then total_tax else 0 end),0) as tax,
(IFNULL(SUM(case when (sale_status='completed') then grand_total else 0 end),0)) - (IFNULL(SUM(case when (sale_status='completed') then total_tax else 0 end),0)) 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,
#{payment_methods.map { |method| pm = method == 'paypar' ? 'redeem' : method; "SUM(`#{pm}`) as `#{pm}`"}.push('').join(', ')}
@@ -823,6 +835,7 @@ class Sale < ApplicationRecord
FROM (
#{sales}
) as s
JOIN (#{sale_taxes.to_sql}) AS st ON s.sale_id = st.sale_id
GROUP BY DATE(CONVERT_TZ(receipt_date,'+00:00','+06:30'))").to_hash.map(&:symbolize_keys)
return daily_total
end