fixed conflict after merge with dev branch for report query updated

This commit is contained in:
Nweni
2019-11-27 14:09:16 +06:30
75 changed files with 1509 additions and 1940 deletions

View File

@@ -1,4 +1,5 @@
class Sale < ApplicationRecord
include NumberFormattable
self.primary_key = "sale_id"
#primary key - need to be unique generated for multiple shops
@@ -7,12 +8,15 @@ class Sale < ApplicationRecord
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_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 :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
@@ -28,7 +32,9 @@ class Sale < ApplicationRecord
scope :date_on, -> (date) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) = ?", Time.zone.formatted_offset, date) }
scope :date_between, -> (from, to) { where("DATE(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :time_between, -> (from, to) { where("TIME(CONVERT_TZ(receipt_date, '+00:00', ?)) BETWEEN ? AND ?", Time.zone.formatted_offset, from, to) }
scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'))") }
scope :along_with_sale_payments_except_void, -> { joins("LEFT JOIN sale_payments on sales.sale_status != 'void' AND sale_payments.sale_id = sales.sale_id AND DATE(CONVERT_TZ(sale_payments.created_at,'+00:00', '#{Time.zone.formatted_offset}')) = DATE(CONVERT_TZ(sales.receipt_date,'+00:00', '#{Time.zone.formatted_offset}'))") }
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
end
@@ -524,7 +530,7 @@ class Sale < ApplicationRecord
tax_profiles = unique_tax_profiles(order_source, self.customer_id)
# #Creat new tax records
if self.payment_status != 'foc'
if self.payment_status != 'foc' && tax_type.to_s == "all"
tax_profiles.each do |tax|
sale_tax = SaleTax.new(:sale => self)
sale_tax.tax_name = tax.name
@@ -539,16 +545,82 @@ class Sale < ApplicationRecord
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
sale_tax.tax_payable_amount = total_tax * tax.rate / 100
end
sale_tax.inclusive = tax.inclusive
sale_tax.save
if !tax.inclusive
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
end
elsif tax_type.to_s == "no_tax"
tax_profiles.each do |tax|
sale_tax = SaleTax.new(:sale => self)
sale_tax.tax_name = tax.name
sale_tax.tax_rate = 0
sale_tax.tax_payable_amount = 0
sale_tax.inclusive = tax.inclusive
sale_tax.save
end
elsif tax_type.to_s == "Commercial Tax"
tax_profiles.each do |tax|
if tax.name == tax_type.to_s
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 - 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
elsif tax_type.to_s == "Service Charges"
tax_profiles.each do |tax|
if tax.name == tax_type.to_s
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 - 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
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount
@@ -595,6 +667,12 @@ class Sale < ApplicationRecord
sale_tax.tax_payable_amount = total_tax / divided_value
else
sale_tax.tax_payable_amount = total_tax * tax.rate / 100
end
sale_tax.inclusive = tax.inclusive
sale_tax.save
if !tax.inclusive
total_tax_amount = total_tax_amount + sale_tax.tax_payable_amount
end
@@ -602,9 +680,6 @@ class Sale < ApplicationRecord
if shop.calc_tax_order
total_taxable = total_taxable + sale_tax.tax_payable_amount
end
sale_tax.inclusive = tax.inclusive
sale_tax.save
end
self.tax_type = tax_incl_exec
self.total_tax = total_tax_amount
@@ -1194,7 +1269,7 @@ def self.get_shift_sales_by_receipt_no(shift_sale_range,shift,from,to,payment_ty
payment_type = " and sale_payments.payment_method = '#{payment_type}'"
end
query = Sale.all.select("sales.*,sale_payments.*,df.name,df.type")
query = Sale.includes(:sale_items).select("sales.*,sale_payments.*,df.name,df.type")
.where("sale_status= 'completed' and sale_payments.payment_amount != 0 #{payment_type}")
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
.joins("join bookings on bookings.sale_id = sales.sale_id")
@@ -1212,17 +1287,16 @@ end
def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,payment_type)
## => left join -> show all sales although no orders
if payment_type.blank?
payment_type = ''
else
payment_type = " and sale_payments.payment_method = '#{payment_type}'"
end
query = Sale.select("sales.*,bookings.dining_facility_id as table_id")
.where("sale_status= 'completed' and sale_payments.payment_amount != 0 #{payment_type}")
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
.joins("join bookings on bookings.sale_id = sales.sale_id")
.group("sales.sale_id")
query = Sale.includes([:customer, :survey, :sale_payments])
.includes(:bookings => :dining_facility)
.select("sales.*, SUM(sale_payments.payment_amount) AS payments_for_credits_amount")
.joins(:bookings)
.left_joins(:sale_payments_for_credits)
.completed
.where.not(total_amount: 0)
.group(:sale_id)
.order(:receipt_date)
if shift.present?
query = query.where("sales.shift_sale_id in (?)", shift.to_a)
elsif shift_sale_range.present?
@@ -1230,6 +1304,9 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay
else
query = query.where("sales.receipt_date between ? and ?",from,to)
end
ActiveRecord::Associations::Preloader.new.preload(query, :sale_items, SaleItem.where.not(price: 0))
return query
end
@@ -1502,102 +1579,55 @@ end
return tax
end
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type,shop)
if (!from.nil? && !to.nil?) && (from != "" && to!="")
if current_user.nil?
query = Sale.top_bottom(shop,today,nil,from,to,from_time,to_time)
def self.top_bottom_products(today,current_user,from,to,from_time,to_time,type)
query = Sale.joins("JOIN sale_items ON sale_items.sale_id = sales.sale_id")
.completed
.where("qty > 0 AND price > 0")
.group("SUBSTRING_INDEX(product_name, ' - ', 1)")
if type == "top"
query = query.group('i.product_name')
.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('i.product_name')
.order("SUM(i.qty) ASC").limit(20)
end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.top_bottom(shop,today,nil,from,to,from_time,to_time)
if type == "top"
query = query.group('i.product_name')
.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('i.product_name')
.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift,from,to,from_time,to_time)
if type == "top"
query = query.group('i.product_name')
.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.group('i.product_name')
.order("SUM(i.qty) ASC").limit(20)
end
end
end
if !from.nil? && !to.nil?
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
query = query.time_between(from_time, to_time)
end
else
if current_user.nil?
query = Sale.top_bottom(shop,today).group('i.product_name')
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id)
query = query.where("shift_sale_id='#{shift.id}'")
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.top_bottom(shop,today).group('i.product_name')
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
else
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.top_bottom(shop,today,shift).group('i.product_name')
if type == "top"
query = query.order("SUM(i.qty) DESC").limit(20)
elsif type == "bottom"
query = query.order("SUM(i.qty) ASC").limit(20)
end
end
end
end
end
if type == "top"
query = query.order("SUM(qty) DESC")
else
query = query.order("SUM(qty) ASC")
end
query.limit(20).sum('qty')
end
def self.hourly_sales(today,current_user,from,to,from_time,to_time,shop)
if (!from.nil? && !to.nil?) && (from != "" && to!="")
if current_user.nil?
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today,nil,from,to,from_time,to_time)
else
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift,from,to,from_time,to_time)
end
end
def self.hourly_sales(today,current_user,from,to,from_time,to_time)
query = Sale.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date').completed
if !from.nil? && !to.nil?
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
query = query.time_between(from_time, to_time)
end
else
if current_user.nil?
query = Sale.hourly_sale_data(shop,today)
else
if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor'
query = Sale.hourly_sale_data(shop,today)
else
shift = ShiftSale.current_open_shift(current_user)
if !shift.nil?
query = Sale.hourly_sale_data(shop,today,shift)
end
end
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user.id)
query = query.where("shift_sale_id='#{shift.id}'")
end
end
query.sum(:grand_total)
end
def self.employee_sales(today,current_user,from,to,from_time,to_time,shop)
@@ -1881,28 +1911,7 @@ end
query = query.first
end
def self.total_account(shop,today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("distinct b.id as account_id, b.title as title")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.joins("JOIN accounts as b ON b.id = a.account_id")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed'")
if (!from.nil? && !to.nil?) && (from != "" && to!="")
query = query.date_between(from, to)
if !from_time.nil? && !to_time.nil?
query = query.time_between(from_time, to_time)
end
else
query = query.date_on(today)
end
if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor')
if shift = ShiftSale.current_open_shift(current_user)
query = query.where("sales.shift_sale_id = ?", shift.id)
end
end
return query
end
def self.account_data(shop,account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc")
.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id")
.where("sales.shop_code='#{shop.shop_code}' and sales.sale_status = 'completed' and a.account_id ='#{account_id}'")
@@ -2097,61 +2106,8 @@ def unique_tax_profiles(order_source, customer_id)
return tax_data
end
def self.top_bottom(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
if !from_time.nil? && !to_time.nil?
query = query.where("(i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}' and sale_status= 'completed'")
else
query = query.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(CONVERT_TZ(receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'"+
" and sale_status= 'completed'")
end
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
else
query = Sale.select("(SUM(i.qty) * i.price) as grand_total,SUM(i.qty) as total_item," +
" i.price as unit_price,i.product_name")
.joins("JOIN sale_items i ON i.sale_id = sales.sale_id")
.where("shop_code='#{shop.shop_code}' and (i.qty > 0 and i.price > 0) and DATE_FORMAT(receipt_date,'%Y-%m-%d') = '#{today}'"+
" and sale_status= 'completed'")
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
end
return query
end
def self.hourly_sale_data(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
if !from.nil? && !to.nil?
query = Sale.select("grand_total")
if !from_time.nil? && !to_time.nil?
query = query.where('sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%H:%M") between ? and ?',from,to,from_time,to_time)
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
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
query = query.where("shop_code='#{shop.shop_code}'").group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
else
query = Sale.select("grand_total")
.where("shop_code='#{shop.shop_code}' and sale_status = 'completed' and DATE_FORMAT(receipt_date,'%Y-%m-%d') = ?",today)
if !shift.nil?
query = query.where("shift_sale_id='#{shift.id}'")
end
query = query.group("date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p')")
.order('receipt_date')
end
return query
end
def self.employee_sale(shop,today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil)
query = Sale.joins(:cashier)
.joins(:sale_payments)
.paid.completed.where("sales.shop_code='#{shop.shop_code}'")
@@ -2314,7 +2270,7 @@ end
def grand_total_round
print_settings = PrintSetting.get_precision_delimiter()
if !print_settings.nil?
self.grand_total =self.grand_total.round(print_settings.precision.to_i)
self.grand_total =self.grand_total.round(precision)
end
end
@@ -2461,8 +2417,6 @@ private
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)
precision = PrintSetting.get_precision_delimiter().precision.to_i
self.total_amount = self.total_amount.round(precision)
self.total_discount = self.total_discount.round(precision)
self.total_tax = self.total_tax.round(precision)
@@ -2491,7 +2445,11 @@ private
found, inventory_definition = InventoryDefinition.find_product_in_inventory(item,self.shop_code)
if found
if stock_journal = StockJournal.find_by_trans_ref(item.order_items_id)
stock_journal.update(remark: self.sale_status)
if self.payment_status == "foc" && self.payment_status_was != "foc"
stock_journal.update(remark: self.payment_status)
else
stock_journal.update(remark: self.sale_status)
end
end
end
end