fixed conflict after merge with dev branch for report query updated
This commit is contained in:
@@ -23,20 +23,9 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def self.find_product_in_inventory(item,shop_code=nil)
|
||||
# unless instance_code.empty?
|
||||
# instance_code = instance_code.to_s
|
||||
# instance_code[0] = ""
|
||||
# instance_code = instance_code.chomp("]")
|
||||
# else
|
||||
# instance_code = '"0"'
|
||||
# end
|
||||
#
|
||||
# if prod = InventoryDefinition.where("item_code IN(#{instance_code})")
|
||||
# puts "found prodcut+++++++++++++++++++++++++++++++++++==="
|
||||
# puts prod.to_json
|
||||
# end
|
||||
if product = InventoryDefinition.find_by_item_code_and_shop_code(item.item_instance_code,shop_code)
|
||||
|
||||
def self.find_product_in_inventory(item)
|
||||
if product = InventoryDefinition.find_by_item_code_and_shop_code(item.item_instance_code,shop_code)
|
||||
if stock_check_item = StockCheckItem.find_by_item_code_and_shop_code(item.item_instance_code,shop_code)
|
||||
return true, product
|
||||
end
|
||||
@@ -44,13 +33,13 @@ class InventoryDefinition < ApplicationRecord
|
||||
end
|
||||
|
||||
def self.check_balance(item, inventory_definition) # item => saleItemOBj
|
||||
stock = StockJournal.where("shop_code='#{inventory_definition.shop_code}' and item_code=?", item.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
modify_balance(item, stock, inventory_definition)
|
||||
else
|
||||
puts "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< OUT OF STOCK >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
|
||||
StockJournal.add_to_journal(item.item_instance_code, item.qty, 0, "out of stock", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
||||
end
|
||||
|
||||
stock = StockJournal.where("shop_code='#{inventory_definition.shop_code}' and item_code=?", item.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
modify_balance(item, stock, inventory_definition)
|
||||
else
|
||||
StockJournal.add_to_journal(item.item_instance_code, item.qty, 0, "out of stock", inventory_definition, item.id, StockJournal::SALES_TRANS)
|
||||
end
|
||||
end
|
||||
|
||||
def self.modify_balance(item, stock, inventory_definition) #saleitemObj
|
||||
@@ -75,6 +64,7 @@ class InventoryDefinition < ApplicationRecord
|
||||
elsif item.is_a? SaleItem
|
||||
trans_type = StockJournal::SALES_TRANS
|
||||
end
|
||||
# StockJournal.add_to_journal(instance_code, qty, stock.balance, remark, inventory_definition, item.id, trans_type)
|
||||
StockJournal.add_to_journal(item.item_instance_code, qty, stock.balance, remark, inventory_definition, item.id, trans_type)
|
||||
check_item.different = check_item.different - qty
|
||||
check_item.save
|
||||
|
||||
@@ -3,6 +3,8 @@ class Lookup < ApplicationRecord
|
||||
has_many :accounts
|
||||
belongs_to :shop
|
||||
|
||||
scope :number_formats, -> { where(lookup_type: 'number_format')}
|
||||
|
||||
def available_types
|
||||
{'Employee Roles' => 'employee_roles',
|
||||
'Dining Facilities Status' => 'dining_facilities_status',
|
||||
|
||||
@@ -125,42 +125,14 @@ class OrderItem < ApplicationRecord
|
||||
end
|
||||
|
||||
def update_stock_journal
|
||||
if self.set_menu_items.present?
|
||||
puts "set menu itemsssssssss???????????????????????????????"
|
||||
puts items = JSON.parse(self.set_menu_items)
|
||||
instance_code = Array.new
|
||||
count = 0
|
||||
items.each { |i|
|
||||
instance_code.push(i["item_instance_code"])
|
||||
}
|
||||
print instance_code
|
||||
end
|
||||
if self.qty != self.qty_before_last_save
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,self.order.shop_code)
|
||||
if found
|
||||
InventoryDefinition.check_balance(self, inventory_definition)
|
||||
|
||||
unless MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||
if self.qty != self.qty_before_last_save
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
InventoryDefinition.check_balance(self, inventory_definition)
|
||||
end
|
||||
end
|
||||
end
|
||||
# if self.qty > self.qty_was
|
||||
# credit = 0
|
||||
# debit = self.qty.to_i - self.qty_was.to_i
|
||||
# else
|
||||
# credit = self.qty_was.to_i - self.qty.to_i
|
||||
# debit = 0
|
||||
# end
|
||||
# if credit != debit
|
||||
# defination = InventoryDefinition.find_by_item_code(self.item_instance_code)
|
||||
# stock = StockJournal.where('item_code = ?', self.item_instance_code).order("id DESC").first
|
||||
# journal = StockJournal.create(
|
||||
# item_code: self.item_instance_code,
|
||||
# credit: credit,
|
||||
# debit: debit,
|
||||
# balance: stock.balance - debit + credit,
|
||||
# inventory_definition_id: defination.id,
|
||||
# remark: 'ok',
|
||||
# trans_ref: self.order.id,
|
||||
# trans_type: StockJournal::SALES_TRANS
|
||||
# )
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -6,7 +6,7 @@ class SaleAudit < ApplicationRecord
|
||||
|
||||
belongs_to :sale
|
||||
|
||||
belongs_to :credit_payment, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id", class_name: "SalePayment"
|
||||
belongs_to :sale_payments_for_credit, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id", class_name: "SalePayment"
|
||||
|
||||
def self.sync_sale_audit_records(sale_audits)
|
||||
if !sale_audits.nil?
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
class SaleItem < ApplicationRecord
|
||||
include NumberFormattable
|
||||
self.primary_key = "sale_item_id"
|
||||
|
||||
#primary key - need to be unique generated for multiple shops
|
||||
@@ -13,6 +14,7 @@ class SaleItem < ApplicationRecord
|
||||
|
||||
before_validation :round_to_precision
|
||||
after_update :update_stock_journal
|
||||
after_save :update_stock_journal_set_item
|
||||
|
||||
# Add Sale Items
|
||||
def self.add_sale_items(sale_items)
|
||||
@@ -293,8 +295,6 @@ class SaleItem < ApplicationRecord
|
||||
if self.unit_price != self.unit_price_was || self.price != self.price_was
|
||||
if unit_price_fraction > 0 || price_fraction > 0
|
||||
if ['Discount', 'promotion'].include?(self.status)
|
||||
precision = PrintSetting.get_precision_delimiter().precision.to_i
|
||||
|
||||
self.unit_price = self.unit_price.round(precision)
|
||||
self.price = (self.unit_price * self.qty).round(precision)
|
||||
self.taxable_price = self.price
|
||||
@@ -304,40 +304,42 @@ class SaleItem < ApplicationRecord
|
||||
end
|
||||
|
||||
def update_stock_journal
|
||||
is_void = self.status == "void" && self.status_before_last_save != "void"
|
||||
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
||||
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
||||
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
||||
cancel_foc = self.status_before_last_save == "foc"
|
||||
unless MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||
is_void = self.status == "void" && self.status_before_last_save != "void"
|
||||
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
||||
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
||||
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
||||
cancel_foc = self.status_before_last_save == "foc"
|
||||
|
||||
if is_void or cancel_void or is_edit or is_foc or cancel_foc
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,self.sale.shop_code)
|
||||
if found
|
||||
stock = StockJournal.where("shop_code='#{self.sale.shop_code}' and item_code=?", self.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where("shop_code='#{self.sale.shop_code}' and item_code=?", self.item_instance_code).order("id DESC").first
|
||||
if is_void or cancel_void or is_edit
|
||||
if is_void
|
||||
qty = -self.qty
|
||||
remark = "void"
|
||||
elsif cancel_void
|
||||
qty = self.qty
|
||||
remark = "cancel void"
|
||||
elsif is_edit
|
||||
qty = self.qty - self.qty_before_last_save
|
||||
remark = "edit"
|
||||
end
|
||||
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different + qty
|
||||
check_item.save
|
||||
else is_foc or cancel_foc
|
||||
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
||||
if order_item_id = self.sale.bookings.first.order_items.where(item_instance_code: self.item_instance_code, qty: self.qty + qty).select(:order_items_id).first.order_items_id
|
||||
if stock_journal = StockJournal.find_by_trans_ref(order_item_id)
|
||||
if is_foc
|
||||
stock_journal.update(remark: "foc")
|
||||
elsif cancel_foc
|
||||
stock_journal.update(remark: "cancel_foc")
|
||||
if is_void or cancel_void or is_edit or is_foc or cancel_foc
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
if is_void or cancel_void or is_edit
|
||||
if is_void
|
||||
qty = -self.qty
|
||||
remark = "void"
|
||||
elsif cancel_void
|
||||
qty = self.qty
|
||||
remark = "cancel void"
|
||||
elsif is_edit
|
||||
qty = self.qty - self.qty_before_last_save
|
||||
remark = "edit"
|
||||
end
|
||||
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different + qty
|
||||
check_item.save
|
||||
else is_foc or cancel_foc
|
||||
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
||||
if order_item_id = self.sale.bookings.first.order_items.where(item_instance_code: self.item_instance_code, qty: self.qty + qty).select(:order_items_id).first.order_items_id
|
||||
if stock_journal = StockJournal.find_by_trans_ref(order_item_id)
|
||||
if is_foc
|
||||
stock_journal.update(remark: "foc")
|
||||
elsif cancel_foc
|
||||
stock_journal.update(remark: "cancel_foc")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -346,4 +348,79 @@ class SaleItem < ApplicationRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def update_stock_journal_set_item
|
||||
is_void = self.status == "void" && self.status_before_last_save != "void" && self.qty > 0
|
||||
cancel_void = self.status_before_last_save == "void" && self.status.nil?
|
||||
is_edit = self.qty >= 0 && self.qty != self.qty_before_last_save
|
||||
is_foc = self.status == "foc" && self.status_before_last_save != "foc"
|
||||
cancel_foc = self.status_before_last_save == "foc"
|
||||
|
||||
is_waste = self.status == "waste"
|
||||
is_spoile = self.status == "spoile"
|
||||
|
||||
if MenuItemInstance.where("item_instance_name <> ''").pluck(:item_instance_code).include?(self.item_instance_code)
|
||||
if self.qty == 1 && self.qty != self.qty_before_last_save
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
if self.qty.to_i >= 0
|
||||
qty = self.qty - self.qty_was
|
||||
if stock.balance.to_i >= qty
|
||||
puts ">> stock is greater than order qty"
|
||||
remark = "ok"
|
||||
else
|
||||
puts " << stock is less than order qty"
|
||||
remark = "out of stock"
|
||||
end
|
||||
end
|
||||
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different - qty
|
||||
check_item.save
|
||||
else
|
||||
StockJournal.add_to_journal(self.item_instance_code, self.qty, 0, "out of stock", inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||
end
|
||||
end
|
||||
elsif is_void or cancel_void or is_edit
|
||||
if is_void
|
||||
qty = -self.qty
|
||||
remark = "void"
|
||||
elsif cancel_void
|
||||
qty = self.qty
|
||||
remark = "cancel void"
|
||||
elsif is_edit
|
||||
qty = self.qty - self.qty_before_last_save
|
||||
remark = "edit"
|
||||
end
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
stock = StockJournal.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
unless stock.nil?
|
||||
check_item = StockCheckItem.where('item_code=?', self.item_instance_code).order("id DESC").first
|
||||
end
|
||||
end
|
||||
StockJournal.add_to_journal(self.item_instance_code, qty, stock.balance, remark, inventory_definition, self.id, StockJournal::SALES_TRANS)
|
||||
check_item.different = check_item.different + qty
|
||||
check_item.save
|
||||
elsif is_foc or cancel_foc
|
||||
qty = StockJournal.where(trans_ref: self.sale_item_id).sum("credit-debit")
|
||||
if stock_journal = StockJournal.where(trans_ref: self.sale_item_id, item_code: self.item_instance_code).order(id: :desc).first
|
||||
if is_foc
|
||||
stock_journal.update(remark: "foc")
|
||||
elsif cancel_foc
|
||||
stock_journal.update(remark: "cancel_foc")
|
||||
end
|
||||
end
|
||||
elsif is_waste or is_spoile
|
||||
found, inventory_definition = InventoryDefinition.find_product_in_inventory(self)
|
||||
if found
|
||||
if stock_journal = StockJournal.where(trans_ref: self.sale_item_id, item_code: self.item_instance_code)
|
||||
stock_journal.update(remark: self.status)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
class SaleTax < ApplicationRecord
|
||||
include NumberFormattable
|
||||
self.primary_key = "sale_tax_id"
|
||||
|
||||
#primary key - need to be unique generated for multiple shops
|
||||
@@ -44,7 +45,6 @@ class SaleTax < ApplicationRecord
|
||||
def round_to_precision
|
||||
if self.tax_payable_amount != self.tax_payable_amount_was
|
||||
if self.tax_payable_amount % 1 > 0
|
||||
precision = PrintSetting.get_precision_delimiter().precision.to_i
|
||||
self.tax_payable_amount = self.tax_payable_amount.round(precision)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user