optimize for dashboards

This commit is contained in:
Thein Lin Kyaw
2019-11-18 10:09:40 +06:30
parent d94dde63fe
commit a3e4cc8993
10 changed files with 321 additions and 807 deletions

View File

@@ -118,7 +118,7 @@ class HomeController < ApplicationController
# .group_by_hour(:created_at, :time_zone => 'Asia/Rangoon',format: '%I:%p')
# .sum(:grand_total)
logger.debug 'hourly_sales<>><><><<<<<<>><<<><><><><><><><><><<>><'
logger.debug @hourly_sales.to_json
logger.debug @hourly_sales.to_json
employee_sales = Sale.employee_sales(today,current_user,@from,@to,@from_time,@to_time)
# .sum("(CASE WHEN sp.payment_method='cash' THEN ((sp.payment_amount) - (sales.amount_changed)) ELSE (sp.payment_amount) END)")
@employee_sales = []

View File

@@ -4,7 +4,7 @@ class Origami::DashboardController < BaseOrigamiController
@shop = Shop.first
today = DateTime.now.strftime('%Y-%m-%d')
@display_type = Lookup.find_by_lookup_type("display_type")
@sale_data = Array.new
@@ -14,7 +14,7 @@ class Origami::DashboardController < BaseOrigamiController
if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" || payment.payment_method == "unionpay" || payment.payment_method == "alipay"
pay = Sale.payment_sale('card', today, current_user)
@sale_data.push({'card' => pay.payment_amount})
else
else
pay = Sale.payment_sale(payment.payment_method, today, current_user)
@sale_data.push({payment.payment_method => pay.payment_amount})
end
@@ -38,10 +38,10 @@ class Origami::DashboardController < BaseOrigamiController
end
else
@account_data = nil
end
end
@top_items = Sale.top_items(today,current_user)
@total_foc_items = Sale.total_foc_items(today,current_user)
@total_foc_items = Sale.total_foc_items(today,current_user)
# get printer info
@print_settings = PrintSetting.get_precision_delimiter()
@@ -104,12 +104,14 @@ class Origami::DashboardController < BaseOrigamiController
end
def get_all_menu
@menus = Menu.active.all
@menus = Menu.includes(:menu_categories => {:menu_items => :menu_item_instances}).includes(:menu_categories => {:menu_items => :item_sets }).active.all
@item_attributes = MenuItemAttribute.all.load
@item_options = MenuItemOption.all.load
end
def get_credit_sales
credit_sales = SalePayment.get_credit_sales(params)
if !credit_sales.nil?
if !credit_sales.nil?
result = {:status=> true, :data=> credit_sales }
else
result = {:status=> false, :message=>"There is no record." }

View File

@@ -1,7 +1,7 @@
class MenuCategory < ApplicationRecord
class MenuCategory < ApplicationRecord
# before_create :generate_menu_category_code
belongs_to :menu
belongs_to :menu
has_many :children, :class_name => "MenuCategory", foreign_key: "menu_category_id"
belongs_to :parent, :class_name => "MenuCategory", foreign_key: "menu_category_id", optional: true
has_many :menu_items
@@ -38,9 +38,7 @@ class MenuCategory < ApplicationRecord
end
def valid_time
menu_category = MenuCategory.find(self.id)
menu = Menu.find(menu_category.menu_id)
menu = self.menu
from_t = Time.parse(menu.valid_time_from.strftime("%H:%M:%S"))
to_t = Time.parse(menu.valid_time_to.strftime("%H:%M:%S"))
@@ -64,7 +62,7 @@ class MenuCategory < ApplicationRecord
c +=24
current = c*3600 + current_t.min* 60 + current_t.sec
end
end
else # (after) noon
@@ -77,8 +75,8 @@ class MenuCategory < ApplicationRecord
day = Date.today.wday
dayresult = menu.valid_days.include?(day.to_s)
if current.between?(from, to) && menu.valid_days.include?(day.to_s)
return true
else
@@ -104,12 +102,12 @@ class MenuCategory < ApplicationRecord
private
# def generate_menu_category_code
# self.code = SeedGenerator.generate_code(self.class.name, "C")
# def generate_menu_category_code
# self.code = SeedGenerator.generate_code(self.class.name, "C")
# end
def self.to_csv
mc_attributes = %w{id menu_id code name alt_name order_by created_by menu_category_id is_available created_at updated_at}
CSV.generate(headers: true) do |csv|
csv << mc_attributes
@@ -123,7 +121,3 @@ class MenuCategory < ApplicationRecord
end
end

View File

@@ -11,11 +11,11 @@ class MenuItem < ApplicationRecord
belongs_to :account
has_many :menu_item_sets
has_many :item_sets, through: :menu_item_sets
has_and_belongs_to_many :item_sets, join_table: "menu_item_sets"
validates_presence_of :item_code, :name, :type, :min_qty,:account_id
validates_uniqueness_of :item_code
default_scope { order('item_code asc') }
scope :simple_menu_item, -> { where(type: 'SimpleMenuItem') }

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,8 @@ 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"
def self.sync_sale_audit_records(sale_audits)
if !sale_audits.nil?
sale_audits.each do |sa|
@@ -163,7 +165,7 @@ class SaleAudit < ApplicationRecord
end
end
end
return card_balance_amount
end

View File

@@ -6,8 +6,13 @@ class SalePayment < ApplicationRecord
belongs_to :sale
has_one :sale_audit, -> { where "SUBSTRING_INDEX(sale_audits.remark,'||',1) = sale_payments.sale_payment_id" }, foreign_key: "sale_id", primary_key: "sale_id"
attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status,:action_by
scope :credits, -> { where(payment_method: 'creditnote') }
scope :cards, -> { where(payment_method: ['mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher']) }
def self.sync_sale_payment_records(sale_payments)
if !sale_payments.nil?
sale_payments.each do |sp|

View File

@@ -15,6 +15,7 @@
class ShiftSale < ApplicationRecord
belongs_to :cashier_terminal
belongs_to :employee, :foreign_key => 'employee_id'
has_many :sales
def self.current_shift
# today_date = DateTime.now.strftime("%Y-%m-%d")
@@ -25,7 +26,7 @@ class ShiftSale < ApplicationRecord
def self.current_open_shift(current_user)
#if current_user
#find open shift where is open today and is not closed and login by current cashier
#DATE(shift_started_at)=? and
#DATE(shift_started_at)=? and
today_date = DateTime.now.strftime("%Y-%m-%d")
shift = ShiftSale.where("shift_started_at is not null and shift_closed_at is null and employee_id = #{current_user}").take
return shift
@@ -43,7 +44,7 @@ class ShiftSale < ApplicationRecord
# status = 'updated'
if shift_sale.nil?
shift_sale = ShiftSale.new
# status = 'created'
# status = 'created'
end
shift_sale.id = ss['id']
@@ -118,7 +119,7 @@ class ShiftSale < ApplicationRecord
credit = saleobj.get_credit_amount
other_sales = saleobj.get_other_amount
tax = saleobj.get_commerical_tax
if type == "void"
self.total_revenue = self.total_revenue.to_f - saleobj.total_amount.to_f
self.total_discounts = self.total_discounts - saleobj.total_discount
@@ -137,7 +138,7 @@ class ShiftSale < ApplicationRecord
self.takeaway_count = self.takeaway_count - 1
end
self.save
end
end
@@ -149,11 +150,11 @@ class ShiftSale < ApplicationRecord
end
def self.get_by_shift_other_payment(shift)
other_payment = Sale.select("sale_payments.payment_method as name,
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='visa') then (sale_payments.payment_amount) else 0 end) as visa_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='mpu') then (sale_payments.payment_amount) else 0 end) as mpu_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='master') then (sale_payments.payment_amount) else 0 end) as master_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='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,
@@ -161,19 +162,19 @@ class ShiftSale < ApplicationRecord
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='giftvoucher') then (sale_payments.payment_amount) else 0 end) as giftvoucher_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='foc') then (sale_payments.payment_amount) else 0 end) as foc_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='foc') then (sale_payments.payment_amount) else 0 end) as foc_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='paypar') then (sale_payments.payment_amount) else 0 end) as paypar_amount")
.joins("join sale_payments on sale_payments.sale_id = sales.sale_id")
.where("sales.shift_sale_id =? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.id)
end
def self.calculate_total_price_by_accounts(shift,type)
query = Sale.select("acc.title as account_name," +
query = Sale.select("acc.title as account_name," +
"SUM(case when (acc.id=i.account_id) then (i.price) else 0 end) as total_price")
query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id "+
"JOIN accounts acc ON acc.id = i.account_id" +
"JOIN accounts acc ON acc.id = i.account_id" +
" JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id")
if type == 'discount'
query = query.where("sales.shift_sale_id =? and sale_status = 'completed' and i.remark = 'Discount'", shift.id)
@@ -187,17 +188,17 @@ class ShiftSale < ApplicationRecord
def self.get_total_member_discount(shift)
query = Sale.select("SUM(sales.total_discount) as member_discount")
.where("shift_sale_id =? and sale_status = 'completed' and discount_type = 'member_discount'", shift.id)
end
def self.get_total_dinein(shift)
end
def self.get_total_dinein(shift)
query = Sale.select("sum(sales.grand_total) as total_dinein_amount")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Dinein" and c.membership_id is null',shift.id)
.first()
end
end
def self.get_total_takeway(shift)
def self.get_total_takeway(shift)
query = Sale.select("sum(sales.grand_total) as total_takeway_amount")
.joins("JOIN customers as c ON c.customer_id = sales.customer_id")
.where('shift_sale_id =? and sales.sale_status = "completed" and c.customer_type = "Takeaway" and c.membership_id is null',shift.id)
@@ -211,12 +212,12 @@ class ShiftSale < ApplicationRecord
# .where('s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null and s.receipt_date between ? and ?',from, to)
# end
def self.get_total_other_charges(shift)
def self.get_total_other_charges(shift)
query = SaleItem.select("sum(sale_items.qty * sale_items.unit_price) as total_other_charges_amount")
.joins("JOIN sales as s ON s.sale_id = sale_items.sale_id")
.where('shift_sale_id =? and s.sale_status = "completed" and sale_items.product_code = "Other Charges" and sale_items.item_instance_code is null',shift.id)
.first()
end
end
def self.search(filter,from,to)
if filter.blank?

View File

@@ -1,53 +1,20 @@
# Format for attributes json
attr_format = []
# Format for attributes json
if item.item_attributes.count > 0
item.item_attributes.each do|attr_id|
menu_attr = MenuItemAttribute.find(attr_id)
if attr_format.count == 0
attr_format.push({ type: menu_attr.attribute_type, values: [menu_attr.name] })
next
end
attr_format.each do |af|
if menu_attr.attribute_type.in? attr_format.map {|k| k[:type]}
if menu_attr.attribute_type == af[:type]
af[:values].push(menu_attr.name)
end
else
new_attr = {type: menu_attr.attribute_type, values: [ menu_attr.name ] }
attr_format.push(new_attr)
break
end
end
end
# Format for attributes json
if item.item_attributes.count > 0
item_attributes = MenuItemAttribute.where(id: item.item_attributes)
attr_format = item_attributes.group_by {|att| att.attribute_type }.map { |type, values| {type: type, values: values.map(&:name)} }
end
# Format for option json
opt_format = []
# Format for attributes json
if item.item_options.count > 0
item.item_options.each do|opt|
menu_opt = MenuItemOption.find(opt)
if opt_format.count == 0
opt_format.push({ type: menu_opt.option_type, values: [menu_opt.name] })
next
end
opt_format.each do |of|
if menu_opt.option_type.in? opt_format.map {|k| k[:type]}
if menu_opt.option_type == of[:type]
of[:values].push(menu_opt.name)
end
else
new_opt = {type: menu_opt.option_type, values: [ menu_opt.name ] }
opt_format.push(new_opt)
break
end
end
end
# Format for attributes json
if item.item_options.count > 0
item_options = MenuItemOption.where(id: item.item_options)
opt_format = item_options.group_by {|opt| opt.option_type }.map { |type, values| {type: type, values: values.map(&:name)} }
end
#Menu Item Information
json.id item.id
json.code item.item_code
@@ -63,8 +30,8 @@ json.is_available item.is_available
json.is_sub_item item.is_sub_item
json.unit item.unit
# Item Sets of Menu Item
json.item_sets item.item_sets do |its|
# Item Sets of Menu Item
json.item_sets item.item_sets.includes(:menu_item_instances) do |its|
json.id its.id
json.name its.name
json.alt_name its.alt_name
@@ -73,7 +40,7 @@ json.item_sets item.item_sets do |its|
json.instances its.menu_item_instances do |i|
json.id i.id
end
end
end
json.attributes attr_format
json.options opt_format
@@ -92,22 +59,17 @@ json.options opt_format
json.instances item.menu_item_instances do |is|
if is.is_available
# Convert id to name for attributes
instance_attr = []
is.item_attributes.each do |ia|
mItemAttr = MenuItemAttribute.find(ia).name
instance_attr.push(mItemAttr)
end
json.id is.id
json.code is.item_instance_code
json.name is.item_instance_name
json.price is.price
json.is_available is.is_available
json.is_default is.is_default
json.is_on_promotion is.is_on_promotion
json.promotion_price is.promotion_price
json.values instance_attr
# json.item_sets is.item_sets
instance_attr = MenuItemAttribute.where(id: item.item_attributes).pluck(:name)
json.id is.id
json.code is.item_instance_code
json.name is.item_instance_name
json.price is.price
json.is_available is.is_available
json.is_default is.is_default
json.is_on_promotion is.is_on_promotion
json.promotion_price is.promotion_price
json.values instance_attr
end
end
@@ -116,4 +78,4 @@ end
# json.set_items item.children.each do |item|
# json.partial! 'api/restaurant/menu/menu_item', item: item
# end
# end
# end

View File

@@ -12,11 +12,11 @@ if (menu.menu_categories)
# else
# categories = menu.menu_categories
# end
categories = menu.menu_categories
categories = menu.menu_categories
json.categories categories do |category|
if category.is_available
menu_category = MenuCategory.find_by_menu_category_id(category.id)
if !menu_category.nil?
parent_category = category.parent
if !parent_category.nil?
json.sub_category "true"
else
json.sub_category "false"
@@ -34,7 +34,7 @@ if (menu.menu_categories)
json.is_available category.is_available
if !order_by.nil? && order_by.value == "name"
menu_items = MenuItem.unscoped.where("menu_category_id = ?",category.id).order("name asc")
menu_items = category.menu_items.sort_by(&:name)
else
menu_items = category.menu_items
end
@@ -42,7 +42,68 @@ if (menu.menu_categories)
if category.menu_items
json.items menu_items do |item|
if item.is_available
json.partial! 'origami/addorders/menu_item', item: item
# Format for attributes json
attr_format = []
# Format for attributes json
if item.item_attributes.count > 0
item_attributes = @item_attributes.select{ |x| item.item_attributes.include?(x.id.to_s) }
attr_format = item_attributes.group_by {|att| att.attribute_type }.map { |type, values| {type: type, values: values.map(&:name)} }
end
# Format for option json
opt_format = []
# Format for attributes json
if item.item_options.count > 0
item_options = @item_options.select{ |x| item.item_options.include?(x.id.to_s) }
opt_format = item_options.group_by {|opt| opt.option_type }.map { |type, values| {type: type, values: values.map(&:name)} }
end
#Menu Item Information
json.id item.id
json.code item.item_code
json.name item.name
json.alt_name item.alt_name
json.image item.image_path.url
json.description item.description
json.information item.information
json.type item.type
json.account_id item.account_id
json.min_qty item.min_qty
json.is_available item.is_available
json.is_sub_item item.is_sub_item
json.unit item.unit
# Item Sets of Menu Item
json.item_sets item.item_sets.map { |its|
{ id: its.id,
name: its.name,
alt_name: its.alt_name,
min_selectable_qty: its.min_selectable_qty,
max_selectable_qty: its.max_selectable_qty,
instances: its.menu_item_instances.pluck(:id).map { |id| {id: id}}
}
}
json.attributes attr_format
json.options opt_format
json.instances item.menu_item_instances do |is|
if is.is_available
# Convert id to name for attributes
instance_attr = @item_attributes.select{ |x| item.item_attributes.include?(x.id) }.pluck(:name)
json.id is.id
json.code is.item_instance_code
json.name is.item_instance_name
json.price is.price
json.is_available is.is_available
json.is_default is.is_default
json.is_on_promotion is.is_on_promotion
json.promotion_price is.promotion_price
json.values instance_attr
end
end
end
end
end