From d94dde63fef45e40087721ca913fcb20fe9d0436 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Fri, 15 Nov 2019 17:12:50 +0630 Subject: [PATCH 1/5] fix report for receipt, daily sales and credit sales. --- .../reports/credit_payment/index.html.erb | 5 +- .../reports/credit_payment/index.xls.erb | 5 +- app/views/reports/dailysale/index.html.erb | 13 ++- app/views/reports/dailysale/index.xls.erb | 63 ++++++------- app/views/reports/receipt_no/index.html.erb | 90 ++++--------------- app/views/reports/receipt_no/index.xls.erb | 80 ++++------------- 6 files changed, 81 insertions(+), 175 deletions(-) diff --git a/app/views/reports/credit_payment/index.html.erb b/app/views/reports/credit_payment/index.html.erb index d5621850..117100dc 100755 --- a/app/views/reports/credit_payment/index.html.erb +++ b/app/views/reports/credit_payment/index.html.erb @@ -97,7 +97,10 @@ <% end %> Total - <%= number_with_precision(total_credit_amount, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> + <%= number_with_precision(total_credit_amount, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> +   +   +   <%= number_with_precision(total_credit_payment, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> diff --git a/app/views/reports/credit_payment/index.xls.erb b/app/views/reports/credit_payment/index.xls.erb index 6568e086..e765af67 100755 --- a/app/views/reports/credit_payment/index.xls.erb +++ b/app/views/reports/credit_payment/index.xls.erb @@ -69,7 +69,10 @@ <% end %> Total - <%= total_credit_amount rescue '-' %> + <%= total_credit_amount rescue '-' %> +   +   +   <%= total_credit_payment rescue '-' %> diff --git a/app/views/reports/dailysale/index.html.erb b/app/views/reports/dailysale/index.html.erb index 592a8cca..58679bab 100755 --- a/app/views/reports/dailysale/index.html.erb +++ b/app/views/reports/dailysale/index.html.erb @@ -24,7 +24,7 @@ - + <% @payment_methods.each_slice(10) do |slice| %> @@ -45,7 +45,6 @@ - + + + <% total_tax = 0 %> <% net = 0 %> <% unless @tax.blank? %> @@ -326,8 +327,7 @@ total_tax += tax.tax_amount.to_f %> - - + <% end %> <% end %> @@ -337,8 +337,7 @@ <% net = net - total_tax %> - - + <% end %> diff --git a/app/views/reports/dailysale/index.xls.erb b/app/views/reports/dailysale/index.xls.erb index 1e3871cf..bc50f903 100755 --- a/app/views/reports/dailysale/index.xls.erb +++ b/app/views/reports/dailysale/index.xls.erb @@ -12,12 +12,11 @@
<%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%> <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
<%= alph = alph.ord.next.chr %> <%= alph = alph.ord.next.chr %> <%= alph = alph.ord.next.chr %><%= alph = alph.ord.next.chr %>
<%= number_with_precision(rounding_adj, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(grand_total, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
 
<%= tax.tax_name rescue '-'%><%= number_with_precision(tax.tax_amount, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(tax.tax_amount, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
<%= t("views.right_panel.detail.net_amount") %><%= number_with_precision(net, precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(net, precision:precision.to_i,delimiter:delimiter) rescue '-'%>
- + - <% if @payment_methods.include? ("MPU") %> <% end %> @@ -53,14 +52,15 @@ <% end %> + <% if @payment_methods.include? ("GiftVoucher") %> <% end %> - + @@ -125,7 +125,6 @@ - <% if @payment_methods.include? ("MPU") %> <% end %> @@ -161,87 +160,90 @@ <% end %> + <% if @payment_methods.include? ("GiftVoucher") %> <% end %> + - <% count = count + 1 %> <% end %> <% colspan = 7 %> - + <% if @payment_methods.include? ("MPU") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("Master") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("VISA") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("JCB") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("UNIONPAY") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("Alipay") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("KBZPay") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("PAYMAL") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("DINGA") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("JunctionPay") %> <% colspan += 1 %> - + <% end %> <% if @payment_methods.include? ("Redeem") %> <% colspan += 1 %> - + <% end %> - - - + + + + <% if @payment_methods.include? ("GiftVoucher") %> <% colspan += 1 %> - + <% end %> - - - - + + + + + + <% total_tax = 0 %> <% net = 0 %> <% unless @tax.blank? %> <% @tax.each do |tax| total_tax += tax.tax_amount.to_f %> - - - + + <% end %> <% end %> @@ -250,9 +252,8 @@ <% net = net - rounding_adj%> <% net = net - total_tax %> - - - + + <% end %> diff --git a/app/views/reports/receipt_no/index.html.erb b/app/views/reports/receipt_no/index.html.erb index 419a5b10..7733ab0d 100755 --- a/app/views/reports/receipt_no/index.html.erb +++ b/app/views/reports/receipt_no/index.html.erb @@ -81,40 +81,21 @@ <% discount_amt = 0 %> <% other_amt = 0 %> <% total_nett = 0 %> - <% rounding_adj = 0%> <% gov_tax = 0 %> <% service_charge = 0 %> - <% tax_profile_count = @tax_profiles.length %> + <% rounding_adj = 0%> + <% gov_tax = 0 %> + <% service_charge = 0 %> - <% - ttax_count = tax_profile_count - @sale_taxes.length - ttax_flag = true - @sale_taxes.each do |tax| - if tax.tax_name.downcase.include?("service") - ttax_flag = false - end - end - %> + <% if !@sale_data.nil? %> - - <%if !@sale_data.nil? %> <% @sale_data.each do |result| %> - <% grand_total += result.grand_total.to_f %> <% old_grand_total += result.grand_total.to_f - result.rounding_adjustment.to_f %> <% total_tax += result.total_tax.to_f %> <% total_sum += result.total_amount.to_f %> <% discount_amt += result.total_discount.to_f %> <% rounding_adj += result.rounding_adjustment.to_f %> - <% sale_tax_count = result.sale_taxes.length %> - <% tax_count = tax_profile_count - sale_tax_count %> - <% tax_flag = true %> - <% result.sale_taxes.each do |tax| - if tax.tax_name.downcase.include?("service") - tax_flag = false - end - end %> - - - <% if !result.sale_taxes.empty? %> - <% num = 1 %> - <% if tax_flag && tax_count > 0 %> - <% while num <= tax_count %> - - <% num += 1 %> - <% end %> - <% end %> - <% result.sale_taxes.each do |tax| %> - - <%end%> - <% num = 1 %> - <% if !tax_flag && tax_count > 0 %> - <% while num <= tax_count %> - - <% num += 1 %> - <% end %> - <% end %> - <% else %> - <% @tax_profiles.each do |tax| %> - - <% end %> - <%end%> + + <% @tax_profiles.each do |tax| %> + <% if sale_tax = result.sale_taxes.find { |sale_tax| sale_tax.tax_name == tax.name } %> + + <% else %> + + <% end %> + <% end %> @@ -172,29 +136,13 @@ - <% if !@sale_taxes.empty? %> - <% num = 1 - if ttax_flag && ttax_count > 0 %> - <% while num <= ttax_count %> - - <% num += 1 - end %> - <% end %> - <% @sale_taxes.each do |tax| %> - - <%end%> - <% num = 1 - if ttax_flag==false && ttax_count > 0 %> - <% while num <= ttax_count %> - - <% num += 1 - end %> - <% end %> - <% else %> - <% @tax_profiles.each do |tax| %> - - <% end %> - <%end%> + <% @tax_profiles.each do |tax| %> + <% if sale_tax = @sale_taxes.find { |sale_tax| sale_tax.tax_name == tax.name } %> + + <% else %> + + <% end %> + <% end %> diff --git a/app/views/reports/receipt_no/index.xls.erb b/app/views/reports/receipt_no/index.xls.erb index 29d8abe4..7cf1f14d 100755 --- a/app/views/reports/receipt_no/index.xls.erb +++ b/app/views/reports/receipt_no/index.xls.erb @@ -61,20 +61,11 @@ <% discount_amt = 0 %> <% other_amt = 0 %> <% total_nett = 0 %> - <% rounding_adj = 0%> <% gov_tax = 0 %> <% service_charge = 0 %> - <% tax_profile_count = @tax_profiles.length %> + <% rounding_adj = 0%> + <% gov_tax = 0 %> + <% service_charge = 0 %> - <% - ttax_count = tax_profile_count - @sale_taxes.length - ttax_flag = true - @sale_taxes.each do |tax| - if tax.tax_name.downcase.include?("service") - ttax_flag = false - end - end - %> - - <%if @sale_data %> + <% if @sale_data %> <% @sale_data.each do |result| %> <% grand_total += result.grand_total.to_f %> @@ -83,14 +74,7 @@ <% total_sum += result.total_amount.to_f %> <% discount_amt += result.total_discount.to_f %> <% rounding_adj += result.rounding_adjustment.to_f %> - <% sale_tax_count = result.sale_taxes.length %> - <% tax_count = tax_profile_count - sale_tax_count %> - <% tax_flag = true %> - <% result.sale_taxes.each do |tax| - if tax.tax_name.downcase.include?("service") - tax_flag = false - end - end %> + - <% if !result.sale_taxes.empty? %> - <% num = 1 %> - <% if tax_flag && tax_count > 0 %> - <% while num <= tax_count %> + <% @tax_profiles.each do |tax| %> + <% if sale_tax = result.sale_taxes.find { |sale_tax| sale_tax.tax_name == tax.name } %> + + <% else %> - <% num += 1 %> - <% end %> - <% end %> - <% result.sale_taxes.each do |tax| %> - <% end %> - <% num = 1 - if tax_flag==false && tax_count > 0 %> - <% while num <= tax_count %> - - <% num += 1 - end %> - <% end %> - <% else %> - <% @tax_profiles.each do |tax| %> - - <% end %> - <%end%> + <% end %> @@ -135,29 +103,13 @@ - <% if !@sale_taxes.empty? %> - <% num = 1 - if ttax_flag && ttax_count > 0 %> - <% while num <= ttax_count %> + <% @tax_profiles.each do |tax| %> + <% if sale_tax = @sale_taxes.find { |sale_tax| sale_tax.tax_name == tax.name } %> + + <% else %> - <% num += 1 - end %> - <% end %> - <% @sale_taxes.each do |tax| %> - - <%end%> - <% num = 1 - if ttax_flag==false && ttax_count > 0 %> - <% while num <= ttax_count %> - - <% num += 1 - end %> - <% end %> - <% else %> - <% @tax_profiles.each do |tax| %> - - <% end %> - <%end%> + <% end %> + <% end %> From 0d9e3636485114c2e6c8b50bec49d3dc30f50534 Mon Sep 17 00:00:00 2001 From: NyanLinHtut Date: Mon, 18 Nov 2019 09:57:55 +0630 Subject: [PATCH 2/5] add staff meal --- .../reports/staff_meal_controller.rb | 133 ++++++ app/models/inventory_definition.rb | 15 +- app/models/order_item.rb | 12 +- app/models/sale.rb | 134 +++++- app/models/sale_item.rb | 107 +++-- app/views/layouts/_left_sidebar.html.erb | 6 + ...> _hourly_saleitem_report_filter.html.erb} | 4 +- .../reports/hourly_saleitem/index.html.erb | 2 +- .../reports/hourly_saleitem/index.xls.erb | 263 ++++++------ .../_staff_meal_report_filter.html.erb | 146 +++++++ app/views/reports/staff_meal/index.html.erb | 390 ++++++++++++++++++ app/views/reports/staff_meal/index.xls.erb | 277 +++++++++++++ config/routes.rb | 1 + 13 files changed, 1291 insertions(+), 199 deletions(-) create mode 100644 app/controllers/reports/staff_meal_controller.rb rename app/views/reports/hourly_saleitem/{_shift_sale_report_filter.html.erb => _hourly_saleitem_report_filter.html.erb} (97%) create mode 100644 app/views/reports/staff_meal/_staff_meal_report_filter.html.erb create mode 100644 app/views/reports/staff_meal/index.html.erb create mode 100644 app/views/reports/staff_meal/index.xls.erb diff --git a/app/controllers/reports/staff_meal_controller.rb b/app/controllers/reports/staff_meal_controller.rb new file mode 100644 index 00000000..b0a8b4d2 --- /dev/null +++ b/app/controllers/reports/staff_meal_controller.rb @@ -0,0 +1,133 @@ +class Reports::StaffMealController < BaseReportController + authorize_resource :class => false + def index + + @account = Account.all + from, to = get_date_range_from_params + + shift_sale_range = '' + + shift = '' + if params[:shift_name].to_i != 0 + + shift_sale_range = Sale.get_by_shift_sale_by_item(from,to,Sale::SALE_STATUS_COMPLETED) + + shift_sale = ShiftSale.find(params[:shift_name]) + if to.blank? + shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL ',shift_sale.shift_started_at) + else + if shift_sale.shift_closed_at.blank? + shift = ShiftSale.where('shift_started_at = ? and shift_closed_at is NULL',shift_sale.shift_started_at) + else + shift = ShiftSale.where('shift_started_at = ? and shift_closed_at = ? ',shift_sale.shift_started_at, shift_sale.shift_closed_at) + end + end + end + + staff = Customer.where(customer_type: 'staff') + customer_id = Array.new + staff.each { |s| + customer_id.push(s.customer_id) + } + + account_type = params[:account_type] + + @sale_data, @other_charges,@product, @discount_data , @cash_data , @card_data , @credit_data , @foc_data , @grand_total , @change_amount = Sale.get_staff_meal_items(shift_sale_range,shift, from, to, Sale::SALE_STATUS_COMPLETED,account_type,customer_id) + + @sale_taxes = Sale.get_separate_tax(shift_sale_range,shift,from,to,nil) + + @account_cate_count = Hash.new {|hash, key| hash[key] = 0} + @sale_data.each {|acc_cate| @account_cate_count[acc_cate.account_id] += 1} + + + @menu_cate_count = Hash.new {|hash, key| hash[key] = 0} + @sale_data.each {|cate| @menu_cate_count[cate.account_id] += 1} + + + @totalByAccount = Hash.new {|hash, key| hash[key] = 0} + @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} + + @from = from + @to = to + + # get printer info + @print_settings = PrintSetting.get_precision_delimiter() + + if shift.present? + shift.each do |sh| + @shift_from = sh.shift_started_at.nil? ? '-' : sh.shift_started_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_to = sh.shift_closed_at.nil? ? '-' : sh.shift_closed_at.utc.getlocal.strftime("%e %b %I:%M%p") + @shift_data = sh + end + end + respond_to do |format| + format.html + format.xls + end + end + + def show + from, to, report_type = get_date_range_from_params + @sale_data = Sale.get_by_shift_sale_by_item(from,to,Sale::SALE_STATUS_COMPLETED) + + date_arr = Array.new + @sale_data.each do |sale| + local_opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc.getlocal.strftime("%e %b %I:%M%p") + local_closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc.getlocal.strftime("%e %b %I:%M%p") + opening_date = sale.opening_date.nil? ? '-' : sale.opening_date.utc + closing_date = sale.closing_date.nil? ? '-' : sale.closing_date.utc + shift_id = sale.id.nil? ? '-' : sale.id + str = {:shift_id => shift_id, :local_opening_date => local_opening_date, :local_closing_date => local_closing_date, :opening_date => opening_date, :closing_date => closing_date} + date_arr.push(str) + end + + # @totalByAccount = Hash.new {|hash, key| hash[key] = 0} + # @sale_data.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total} + + out = {:status => 'ok', :message => date_arr} + + respond_to do |format| + format.json { render json: out } + end + end + + def get_period_name(period) + period_name = '-' + unless period.nil? or period.blank? + case period.to_i + when PERIOD["today"] + period_name = "Today" + + when PERIOD["yesterday"] + period_name = "Yesterday" + + when PERIOD["this_week"] + period_name = "This Week" + + when PERIOD["last_week"] + period_name = "Last Week" + + when PERIOD["last_7"] + period_name = "Last 7 days" + + when PERIOD["this_month"] + period_name = "This Month" + + when PERIOD["last_month"] + period_name = "Last Month" + + when PERIOD["last_30"] + period_name = "Last 30 Days" + + when PERIOD["this_year"] + period_name = "This Year" + + when PERIOD["last_year"] + period_name = "Last Year" + + end + end + return period_name + end + +end diff --git a/app/models/inventory_definition.rb b/app/models/inventory_definition.rb index 4ff649e0..dd2f3c3c 100755 --- a/app/models/inventory_definition.rb +++ b/app/models/inventory_definition.rb @@ -23,7 +23,20 @@ class InventoryDefinition < ApplicationRecord end end - def self.find_product_in_inventory(item) + def self.find_product_in_inventory(item,instance_code) + 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(item.item_instance_code) if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code) return true, product diff --git a/app/models/order_item.rb b/app/models/order_item.rb index f265be2b..005bfe67 100755 --- a/app/models/order_item.rb +++ b/app/models/order_item.rb @@ -125,8 +125,18 @@ 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) + found, inventory_definition = InventoryDefinition.find_product_in_inventory(self,instance_code) if found InventoryDefinition.check_balance(self, inventory_definition) end diff --git a/app/models/sale.rb b/app/models/sale.rb index 82ccff3d..29404ac9 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -942,6 +942,28 @@ def self.get_item_query(type) # query = query.order("i.menu_category_name asc, SUM(i.qty) desc") end +def self.get_staff_meal_query() + sale_type = "foc" + query = Sale.select("cus.name as staff_name,sales.sale_id,acc.title as account_name, + i.item_instance_code as item_code,i.account_id as account_id, " + + "SUM(i.qty * i.unit_price) as grand_total, + SUM(i.qty) as total_item,i.qty as qty," + + "i.status as status_type,i.remark as remark,"+ + "i.unit_price,i.price as price,i.product_name as product_name, " + + "i.menu_category_name,i.menu_category_code as menu_category_id, " + + "date_format(CONVERT_TZ(receipt_date,'+00:00', '+06:30'), '%I %p') + as date_format") + + query = query.joins("JOIN sale_items i ON i.sale_id = sales.sale_id " + + "JOIN shift_sales sh ON sh.`id` = sales.shift_sale_id " + + "JOIN customers cus ON cus.customer_id = sales.customer_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") + .order("acc.title desc, i.account_id desc, i.menu_category_code desc, i.item_instance_code asc, SUM(i.qty) desc, i.unit_price asc") + # query = query.order("i.menu_category_name asc, SUM(i.qty) desc") +end + def self.get_other_charges() query = Sale.select("i.account_id as account_id, " + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," + @@ -1048,6 +1070,111 @@ 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) + # date_type_selection = get_sql_function_for_report_type(report_type) + if account_type.blank? + account_type = '' + else + account_type = " and acc.title = '#{account_type}'" + end + + unless customer_id.empty? + 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() + + discount_query = 0 + total_card_amount = 0 + total_cash_amount = 0 + total_credit_amount = 0 + total_foc_amount = 0 + total_grand_total = 0 + other_charges = 0 + + # if type.nil? || type == 'all' || type == "other" + # other_charges = self.get_other_charges() + # end + product = self.get_product_sale() + + if shift.present? + query = query.where("sales.shift_sale_id IN (?) #{account_type} and sale_status='completed' and sales.customer_id IN(#{customer_id})",shift.to_a) + # if type.nil? || type == 'all' || type == "other" + # other_charges = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a) + # end + product = product.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift.to_a) + discount_query = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:total_discount) + change_amount = Sale.where("sales.shift_sale_id in (?) and sale_status= 'completed' ", shift.to_a).sum(:amount_changed) + sale_cash = Sale.select("SUM(case when (sale_payments.payment_method ='mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar' or sale_payments.payment_method = 'unionpay' or sale_payments.payment_method = 'alipay' or sale_payments.payment_method = 'paymal' or sale_payments.payment_method = 'dinga' or sale_payments.payment_method = 'JunctionPay' or sale_payments.payment_method = 'giftvoucher') then (sale_payments.payment_amount) else 0 end) as card_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='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, + SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift.to_a) + sale_cash.each do |s_c| + total_cash_amount += s_c.cash_amount.to_f + total_card_amount += s_c.card_amount.to_f + total_credit_amount += s_c.credit_amount.to_f + total_foc_amount += s_c.foc_amount.to_f + end + total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f + + ### => get all sales range in shift_sales + elsif shift_sale_range.present? + query = query.where("sales.shift_sale_id IN (?) #{account_type} and sale_status='completed' and sales.customer_id IN(#{customer_id})",shift_sale_range.to_a) + # if type.nil? || type == 'all' || type == "other" + # other_charges = other_charges.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a) + # end + product = product.where("sales.shift_sale_id IN (?) and sale_status='completed'",shift_sale_range.to_a) + discount_query = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:total_discount) + change_amount = Sale.where("sales.shift_sale_id IN (?) and sale_status ='completed'", shift_sale_range.to_a).sum(:amount_changed) + sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar' or sale_payments.payment_method = 'unionpay' or sale_payments.payment_method = 'alipay' sale_payments.payment_method = 'paymal' or sale_payments.payment_method = 'dinga' or sale_payments.payment_method = 'JunctionPay' or sale_payments.payment_method = 'giftvoucher') then (sale_payments.payment_amount) else 0 end) as card_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='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, + SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .where("sales.shift_sale_id in (?) and sale_status = 'completed' and sale_payments.payment_amount != 0 ", shift_sale_range.to_a) + sale_cash.each do |s_c| + total_cash_amount += s_c.cash_amount.to_f + total_card_amount += s_c.card_amount.to_f + total_credit_amount += s_c.credit_amount.to_f + total_foc_amount += s_c.foc_amount.to_f + end + + 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' and sales.customer_id IN(#{customer_id})",from,to) + # if type.nil? || type == 'all' || type == "other" + # other_charges = other_charges.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to) + # end + product = product.where("sales.receipt_date between ? and ? and sale_status='completed'",from,to) + + discount_query = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:total_discount) + change_amount = Sale.where("sales.receipt_date between ? and ? and sale_status ='completed'", from,to).sum(:amount_changed) + sale_cash = Sale.select("SUM(case when (sale_payments.payment_method = 'mpu' or sale_payments.payment_method = 'visa' or sale_payments.payment_method = 'master' or sale_payments.payment_method = 'jcb' or sale_payments.payment_method = 'paypar' or sale_payments.payment_method = 'unionpay' or sale_payments.payment_method = 'alipay' or sale_payments.payment_method = 'paymal' or sale_payments.payment_method = 'dinga' or sale_payments.payment_method = 'JunctionPay' or sale_payments.payment_method = 'giftvoucher') then (sale_payments.payment_amount) else 0 end) as card_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='creditnote') then (sale_payments.payment_amount) else 0 end) as credit_amount, + SUM(case when (sale_payments.payment_method='foc') then (sale_payments.payment_amount) else 0 end) as foc_amount") + .joins("join sale_payments on sale_payments.sale_id = sales.sale_id") + .where("sales.receipt_date between ? and ? and sale_status = 'completed' and sale_payments.payment_amount != 0 ", from,to) + sale_cash.each do |s_c| + total_cash_amount += s_c.cash_amount.to_f + total_card_amount += s_c.card_amount.to_f + total_credit_amount += s_c.credit_amount.to_f + total_foc_amount += s_c.foc_amount.to_f + end + total_grand_total = total_cash_amount.to_f + total_card_amount.to_f + total_credit_amount.to_f + + end + + 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_product_sale() query = Sale.select("i.account_id as account_id, " + "SUM(i.qty * i.unit_price) as grand_total,SUM(i.qty) as total_item," + @@ -1478,13 +1605,6 @@ end end def self.hourly_sales(today,current_user,from,to,from_time,to_time) - logger.debug 'hourly_sales<<<<<<<<<<<<<<<<<<<<<<<<' - logger.debug today - logger.debug current_user.to_json - logger.debug from - logger.debug to - logger.debug from_time - logger.debug to_time if (!from.nil? && !to.nil?) && (from != "" && to!="") if current_user.nil? query = Sale.hourly_sale_data(today,nil,from,to,from_time,to_time) diff --git a/app/models/sale_item.rb b/app/models/sale_item.rb index 9a8c3d87..8473da78 100755 --- a/app/models/sale_item.rb +++ b/app/models/sale_item.rb @@ -225,6 +225,59 @@ class SaleItem < ApplicationRecord return sale_items end + # Loader Service SFTP Start + # Detail Sale Data + def self.get_detail_sale_data(transaction_date) + query = SaleItem.select(" + sale_items.sale_item_id as id, + sale_items.sale_id as parent_id, + s.receipt_no as check_num, + s.receipt_date as business_date, + s.receipt_date as transaction_date, + '' as item_seq, + sale_items.menu_category_code as category_code, + sale_items.menu_category_name as category_name, + '' as sub_category_code, + '' as sub_category_name, + '' as report_group_code, + '' as report_group_name, + sale_items.product_code as item_id, + sale_items.product_name as item_name, + sale_items.qty as qty, + CASE + WHEN s.sale_status = 'completed' OR s.sale_status = 'void' THEN 'Sales' + WHEN s.sale_status = 'waste' THEN 'Waste' + WHEN s.sale_status = 'spoile' THEN 'Spoil' + END as transaction_type, + sale_items.price as gross_sales, + '' as discount_code, + CASE + WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 + END as discount_amt, + (sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END)) as sales, + ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END))/21) as tax_amt, + '' as service_charges, + ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END)) - ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END))/21)) as net_sales, + '0' as is_set_item, + '0' as is_staff_meal, + '0' as is_raw_wastage, + '0' as is_semi_wastage, + CASE WHEN s.sale_status = 'waste' THEN 1 ELSE 0 END as is_wastage, + CASE WHEN s.sale_status = 'spoile' THEN 1 ELSE 0 END as is_spoilage, + '0' as is_sampling, + '1' as tax_able, + CASE + WHEN s.sale_status = 'void' THEN 1 ELSE 0 + END as is_void + ") + .joins("LEFT JOIN sales s ON s.sale_id = sale_items.sale_id") + .joins("LEFT JOIN sale_items i ON sale_items.sale_id = i.sale_id AND sale_items.item_instance_code = i.item_instance_code AND i.status = 'Discount' AND sale_items.qty = abs(i.qty)") + .where("DATE(s.receipt_date) = ? AND s.sale_status != 'void' AND (sale_items.status NOT IN('Discount', 'void','foc') OR sale_items.status IS NULL)", transaction_date) + .order("s.receipt_no") + end + + # Loader Service SFTP End + private def generate_custom_id if self.sale_item_id.nil? @@ -293,58 +346,4 @@ class SaleItem < ApplicationRecord end end end - - # Loader Service SFTP Start - # Detail Sale Data - def self.get_detail_sale_data(transaction_date) - query = SaleItem.select(" - sale_items.sale_item_id as id, - sale_items.sale_id as parent_id, - s.receipt_no as check_num, - s.receipt_date as business_date, - s.receipt_date as transaction_date, - '' as item_seq, - sale_items.menu_category_code as category_code, - sale_items.menu_category_name as category_name, - '' as sub_category_code, - '' as sub_category_name, - '' as report_group_code, - '' as report_group_name, - sale_items.product_code as item_id, - sale_items.product_name as item_name, - sale_items.qty as qty, - CASE - WHEN s.sale_status = 'completed' OR s.sale_status = 'void' THEN 'Sales' - WHEN s.sale_status = 'waste' THEN 'Waste' - WHEN s.sale_status = 'spoile' THEN 'Spoil' - END as transaction_type, - sale_items.price as gross_sales, - '' as discount_code, - CASE - WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 - END as discount_amt, - (sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END)) as sales, - ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END))/21) as tax_amt, - '' as service_charges, - ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END)) - ((sale_items.price - (CASE WHEN i.unit_price IS NOT NULL THEN i.unit_price ELSE 0 END))/21)) as net_sales, - '0' as is_set_item, - '0' as is_staff_meal, - '0' as is_raw_wastage, - '0' as is_semi_wastage, - CASE WHEN s.sale_status = 'waste' THEN 1 ELSE 0 END as is_wastage, - CASE WHEN s.sale_status = 'spoile' THEN 1 ELSE 0 END as is_spoilage, - '0' as is_sampling, - '1' as tax_able, - CASE - WHEN s.sale_status = 'void' THEN 1 ELSE 0 - END as is_void - ") - .joins("LEFT JOIN sales s ON s.sale_id = sale_items.sale_id") - .joins("LEFT JOIN sale_items i ON sale_items.sale_id = i.sale_id AND sale_items.item_instance_code = i.item_instance_code AND i.status = 'Discount' AND sale_items.qty = abs(i.qty)") - .where("DATE(s.receipt_date) = ? AND s.sale_status != 'void' AND (sale_items.status NOT IN('Discount', 'void','foc') OR sale_items.status IS NULL)", transaction_date) - .order("s.receipt_no") - end - - # Loader Service SFTP End - end diff --git a/app/views/layouts/_left_sidebar.html.erb b/app/views/layouts/_left_sidebar.html.erb index c948964d..1dbb3b98 100644 --- a/app/views/layouts/_left_sidebar.html.erb +++ b/app/views/layouts/_left_sidebar.html.erb @@ -138,6 +138,9 @@
  • Induty
  • +
  • + Staff Meal +
  • Stock Check
  • @@ -324,6 +327,9 @@
  • Induty
  • +
  • + Staff Meal +
  • Stock Check
  • diff --git a/app/views/reports/hourly_saleitem/_shift_sale_report_filter.html.erb b/app/views/reports/hourly_saleitem/_hourly_saleitem_report_filter.html.erb similarity index 97% rename from app/views/reports/hourly_saleitem/_shift_sale_report_filter.html.erb rename to app/views/reports/hourly_saleitem/_hourly_saleitem_report_filter.html.erb index 4ca6ccf9..3814746e 100644 --- a/app/views/reports/hourly_saleitem/_shift_sale_report_filter.html.erb +++ b/app/views/reports/hourly_saleitem/_hourly_saleitem_report_filter.html.erb @@ -56,9 +56,9 @@ -
    +

    - +
    <% end %> diff --git a/app/views/reports/hourly_saleitem/index.html.erb b/app/views/reports/hourly_saleitem/index.html.erb index a42ce2b3..4d3316de 100644 --- a/app/views/reports/hourly_saleitem/index.html.erb +++ b/app/views/reports/hourly_saleitem/index.html.erb @@ -10,7 +10,7 @@
    - <%= render :partial=>'shift_sale_report_filter', + <%= render :partial=>'hourly_saleitem_report_filter', :locals=>{ :period_type => true, :shift_name => true,:payments => true, :report_path =>reports_hourly_saleitem_index_path} %>
    diff --git a/app/views/reports/hourly_saleitem/index.xls.erb b/app/views/reports/hourly_saleitem/index.xls.erb index df37f29e..831f6190 100644 --- a/app/views/reports/hourly_saleitem/index.xls.erb +++ b/app/views/reports/hourly_saleitem/index.xls.erb @@ -5,142 +5,139 @@ -
    -
    -
    -
    -
    -
    -
    +
    +
    +
    +
    +
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%> <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.sr") %> <%= t("views.right_panel.detail.date") %><%= t("views.right_panel.detail.void_amount") %><%= t("views.right_panel.detail.mpu_sales") %><%= t("views.right_panel.detail.cash_sales") %> <%= t("views.right_panel.detail.credit_sales") %><%= t("views.right_panel.detail.void_amount") %> <%= t("views.right_panel.detail.foc_sales") %><%= t("views.right_panel.detail.giftvoucher_sales") %>(<%= t("views.right_panel.detail.discount") %>) <%= t("views.right_panel.detail.grand_total") %> <%= t("views.right_panel.detail.rnd_adj_sh") %><%= t("views.right_panel.detail.grand_total") %>
    <%= count %> <%= sale[:sale_date].strftime("#{sale[:sale_date].day.ordinalize} %b") rescue '-' %><%= number_with_delimiter(sprintf("%.2f",sale[:void_amount]), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",sale[:mpu_amount]),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",sale[:cash_amount]-sale[:total_change_amount]), delimiter: delimiter) rescue '-'%> <%= number_with_delimiter(sprintf("%.2f",sale[:credit_amount]),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",sale[:void_amount]), delimiter => ',') rescue '-'%> <%= number_with_delimiter(sprintf("%.2f",sale[:foc_amount]),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",sale[:giftvoucher_amount]),delimiter => ',') rescue '-'%>(<%= number_with_delimiter(sprintf("%.2f",sale[:total_discount]), delimiter => ',') rescue '-'%>)<%= number_with_delimiter(sprintf("%.2f",sale[:rounding_adj].to_f),delimiter => ',') rescue '-'%> <%= number_with_delimiter(sprintf("%.2f",sale[:grand_total]),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",sale[:rounding_adj].to_f),delimiter => ',') rescue '-'%>
    TotalTotal<%= number_with_delimiter(sprintf("%.2f",mpu),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",mpu),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",master),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",master),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",visa), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",visa), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",jcb),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",jcb),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",unionpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",unionpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",alipay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",alipay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",kbzpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",kbzpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",paymal),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",paymal),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",dinga),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",dinga),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",junctionpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",junctionpay),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",paypar),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",paypar),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",cash),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",credit),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",foc), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",cash),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",credit),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",void),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",foc), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",giftvoucher), delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",giftvoucher), delimiter => ',') rescue '-'%>(<%= number_with_delimiter(discount,delimiter => ',') rescue '-'%>)<%= number_with_delimiter(sprintf("%.2f",grand_total),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",rounding_adj),delimiter => ',') rescue '-'%>(<%= number_with_delimiter(discount,delimiter => ',') rescue '-'%>)<%= number_with_delimiter(sprintf("%.2f",rounding_adj),delimiter => ',') rescue '-'%><%= number_with_delimiter(sprintf("%.2f",grand_total),delimiter => ',') rescue '-'%>
     
    <%= tax.tax_name rescue '-'%><%= number_with_delimiter(sprintf("%.2f",tax.tax_amount),delimiter => ',') rescue '-'%> <%= tax.tax_name rescue '-'%><%= number_with_delimiter(sprintf("%.2f",tax.tax_amount),delimiter => ',') rescue '-'%>
    <%= t("views.right_panel.detail.net_amount") %><%= number_with_delimiter(sprintf("%.2f",net),delimiter => ',') rescue '-'%> <%= t("views.right_panel.detail.net_amount") %><%= number_with_delimiter(sprintf("%.2f",net),delimiter => ',') rescue '-'%>
    <%if result.type %> <%= result.type %> - <%= result.name %> @@ -125,31 +106,14 @@ <%= result.receipt_no rescue '-' %> <%= result.cashier_name rescue '-' %> <%= number_with_precision(result.total_amount, precision: precision.to_i, delimiter: delimiter) %><%= number_with_precision(result.total_discount, precision: precision.to_i, delimiter: delimiter) rescue '0' %> - <%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(tax.tax_payable_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(result.total_discount, precision: precision.to_i, delimiter: delimiter) rescue '0' %><%= number_with_precision(sale_tax.tax_payable_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(result.grand_total - result.rounding_adjustment, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(result.rounding_adjustment.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(result.grand_total, precision: precision.to_i, delimiter: delimiter) rescue '-' %>  <%= number_with_precision(total_sum, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(discount_amt, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) %><%= number_with_precision(tax.st_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(sale_tax.st_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(old_grand_total.to_f, precision: precision.to_i, delimiter: delimiter) rescue '0' %> <%= number_with_precision(rounding_adj.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(grand_total.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %>
    <%if result.type %> @@ -103,29 +87,13 @@ <%= result.cashier_name rescue '-' %> <%= number_with_precision(result.total_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(result.total_discount, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(sale_tax.tax_payable_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(tax.tax_payable_amount, precision: precision.to_i ,delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) rescue '-' %><%= number_with_precision(result.grand_total - result.rounding_adjustment, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(result.rounding_adjustment.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(result.grand_total, precision: precision.to_i, delimiter: delimiter) rescue '-' %>  <%= number_with_precision(total_sum, precision: precision.to_i ,delimiter: delimiter) rescue '-' %> <%= number_with_precision(discount_amt, precision: precision.to_i ,delimiter: delimiter) rescue '-' %><%= number_with_precision(sale_tax.st_amount, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(tax.st_amount, precision: precision.to_i ,delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i, delimiter: delimiter) rescue '-' %><%= number_with_precision(0, precision: precision.to_i ,delimiter: delimiter) rescue '-' %><%= number_with_precision(old_grand_total.to_f, precision: precision.to_i, delimiter: delimiter) rescue '0' %> <%= number_with_precision(rounding_adj.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %> <%= number_with_precision(grand_total.to_f, precision: precision.to_i, delimiter: delimiter) rescue '-' %>
    + <% time_arr = Array.new %> + <% acc_arr = Array.new %> + <% sale_item_count =0 %> + <% menu_cat_arr = Array.new %> + <% footer_arr = Array.new %> + <% count = 0 %> + <% waste_and_spoil_item_count = 0%> + <% total_qty = 0 %> + <% time_count = 0 %> + <% grand_total = 0 %> + <% @sale_data.each do |sale| %> + <% if !time_arr.include?(sale.date_format) %> + <% sale_item_count =1 %> + <% time_count = time_count + 1 %> + + + + + + + + + + + + + + + + + + <% time_arr.push(sale.date_format) %> + <% menu_cat_arr.clear %> + <% count = 0 %> + <% else %> + <% sale_item_count =sale_item_count +1 %> + <% end %> + + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end + %> + + <% if sale.status_type != "Discount" && sale.status_type != "foc" && sale.status_type != "promotion" + total_qty += sale.total_item + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + end %> + <% if sale.status_type == "Discount" + total_qty += sale.total_item*(-1) + end %> + <% if sale.status_type =="promotion" && @type == "promotion" + total_qty += sale.total_item*(-1) + end %> -
     
    + Time :<%= sale.date_format %> +
    Menu CategoryItem CodeItem NameQtyPriceTotal Price
    - <% time_arr = Array.new %> - <% acc_arr = Array.new %> - <% sale_item_count =0 %> - <% menu_cat_arr = Array.new %> - <% footer_arr = Array.new %> - <% count = 0 %> - <% waste_and_spoil_item_count = 0%> - <% total_qty = 0 %> - <% time_count = 0 %> - <% grand_total = 0 %> - <% @sale_data.each do |sale| %> - <% if !time_arr.include?(sale.date_format) %> - <% sale_item_count =1 %> - <% time_count = time_count + 1 %> - - - - - - - - - - - - - - - - - - <% time_arr.push(sale.date_format) %> - <% menu_cat_arr.clear %> - <% count = 0 %> + + <% if sale.status_type == "foc" && sale.grand_total < 0 + grand_total += sale.grand_total*(-1) + end %> + + <% if sale.status_type == "Discount" && sale.grand_total < 0 + grand_total += sale.grand_total*(-1) + end %> + + <% if sale.status_type == "promotion" && sale.grand_total < 0 + grand_total += sale.grand_total*(-1) + end %> + + <% grand_total += sale.grand_total %> + + <% if !sale.item_code.nil?%> + <% waste_and_spoil_item_count += sale.qty.to_i %> + + <% if !menu_cat_arr.include?(sale.menu_category_name) %> + + <% menu_cat_arr.push(sale.menu_category_name) %> + <% else %> + + <% end %> + + + + + + + + <% end %> + + <% count = count + 1 %> + <% @hourly_total_qty.each do |hr| %> + <% if hr["date"].to_s == sale.date_format.to_s && hr["total_qty"].to_i ==sale_item_count%> + + + + + + + + <% footer_arr.push(sale.sale_id) %> <% else %> - <% sale_item_count =sale_item_count +1 %> <% end %> - - <% if @print_settings.precision.to_i > 0 - precision = @print_settings.precision - else - precision = 0 - end - #check delimiter - if @print_settings.delimiter - delimiter = "," - else - delimiter = "" - end - %> - - <% if sale.status_type != "Discount" && sale.status_type != "foc" && sale.status_type != "promotion" - total_qty += sale.total_item - end %> - <% if sale.status_type == "foc" && sale.price > 0 - total_qty += sale.total_item - end %> - <% if sale.status_type == "Discount" - total_qty += sale.total_item*(-1) - end %> - <% if sale.status_type =="promotion" && @type == "promotion" - total_qty += sale.total_item*(-1) - end %> - - - <% if sale.status_type == "foc" && sale.grand_total < 0 - grand_total += sale.grand_total*(-1) - end %> - - <% if sale.status_type == "Discount" && sale.grand_total < 0 - grand_total += sale.grand_total*(-1) - end %> - - <% if sale.status_type == "promotion" && sale.grand_total < 0 - grand_total += sale.grand_total*(-1) - end %> - - <% grand_total += sale.grand_total %> - - <% if !sale.item_code.nil?%> - <% waste_and_spoil_item_count += sale.qty.to_i %> - - <% if !menu_cat_arr.include?(sale.menu_category_name) %> - - <% menu_cat_arr.push(sale.menu_category_name) %> - <% else %> - - <% end %> - - - - - - - - <% end %> - - <% count = count + 1 %> - <% @hourly_total_qty.each do |hr| %> - <% if hr["date"].to_s == sale.date_format.to_s && hr["total_qty"].to_i ==sale_item_count%> - - - - - - - - <% footer_arr.push(sale.sale_id) %> - <% else %> - <% end %> - <% end %> - <% end %> -
     
    - Time :<%= sale.date_format %> -
    Menu CategoryItem CodeItem NameQtyPriceTotal Price
    <%= sale.menu_category_name %> <%= sale.item_code %><%= sale.product_name %><%= sale.total_item.to_i %><%= number_with_precision(sale.unit_price.to_i, precision:precision.to_i,delimiter:delimiter) %><%= number_with_precision(sale.grand_total.to_i, precision:precision.to_i,delimiter:delimiter) %>
    Total Qty: + + <%= total_qty.to_i %> + <% total_qty = 0%> + Grand Total: + + <%= number_with_precision(grand_total.to_i, precision:precision.to_i,delimiter:delimiter) %> + <% grand_total = 0 %> + +
    <%= sale.menu_category_name %> <%= sale.item_code %><%= sale.product_name %><%= sale.total_item.to_i %><%= sale.unit_price.to_i %><%= sale.grand_total.to_i %><%= sale.date_format %>
    Total Qty: - - <%= total_qty.to_i %> - <% total_qty = 0%> - Grand Total: - - <%= grand_total.to_i %> - <% grand_total = 0 %> - -
    - - - + + <% end %> + + + + + diff --git a/app/views/reports/staff_meal/_staff_meal_report_filter.html.erb b/app/views/reports/staff_meal/_staff_meal_report_filter.html.erb new file mode 100644 index 00000000..04108f48 --- /dev/null +++ b/app/views/reports/staff_meal/_staff_meal_report_filter.html.erb @@ -0,0 +1,146 @@ +
    + <%= form_tag report_path, :method => :get, :id=>"frm_report", :class => "form" do %> + <% if period_type != false %> +
    +
    + + +
    + + +
    + + + +
    +
    + + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + <% end %> + + <% end %> +
    + + diff --git a/app/views/reports/staff_meal/index.html.erb b/app/views/reports/staff_meal/index.html.erb new file mode 100644 index 00000000..ea92f55c --- /dev/null +++ b/app/views/reports/staff_meal/index.html.erb @@ -0,0 +1,390 @@ + +
    +
    + <%= render :partial=>'staff_meal_report_filter', + :locals=>{ :period_type => true, :shift_name => true, :report_path =>reports_staff_meal_index_path} %> + +
    + + + +
    +
    +
    + + + + + + <% if @shift_from %> + + <% if @shift_data.employee %> + <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> + <% end %> + + + <% end %> + + + + + + + + + + + + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end + %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + <% p_qty = 0 %> + <% sub_qty = 0 %> + <% sub_total = 0 %> + <% other_sub_total = 0 %> + <% product_sub_total = 0 %> + <% count = 0 %> + <% row_count = 0 %> + <% total_price = 0 %> + <% cate_count = 0 %> + <% acc_count = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> + <% total_amount = 0 %> + <% discount = 0 %> + <% total_item_foc = 0 %> + <% total_item_dis = 0.0 %> + <% total_tax = 0 %> + <% unless @sale_data.blank? %> + <% @sale_data.each do |sale| %> + <% row_count += 1 %> + + + <% if sale.status_type != "Discount" && sale.status_type != "foc" && sale.status_type != "promotion" + total_qty += sale.total_item + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + end %> + <% if sale.status_type == "Discount" + total_qty += sale.total_item*(-1) + end %> + <% if sale.status_type =="promotion" && @type == "promotion" + total_qty += sale.total_item*(-1) + end %> + + <% if sale.status_type == "foc" && sale.grand_total < 0 + total_item_foc += sale.grand_total*(-1) + end %> + + <% if sale.status_type == "Discount" && sale.grand_total < 0 + total_item_dis += sale.grand_total*(-1) + end %> + + <% if !acc_arr.include?(sale.account_id) %> + + + + + + + <% acc_arr.push(sale.account_id) %> + <% end %> + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + <% if sale.status_type != "Discount" %> + + <%else%> + + <% end %> + + + + + + + <% @menu_cate_count.each do |key,value| %> + <% if sale.account_id == key %> + <% count = count + 1 %> + <% sub_total += sale.grand_total %> + <% #sub_qty += sale.total_item %> + <% if sale.status_type !="Discount" && (!sale.product_name.include? "FOC") && sale.status_type != "promotion" + sub_qty += sale.total_item + end %> + <% if sale.status_type =="Discount" + sub_qty += sale.total_item*(-1) + end %> + + <% if sale.status_type == "promotion" && @type == "promotion" + sub_qty += sale.total_item*(-1) + end %> + <% if count == value %> + + + + + + + + <% sub_total = 0.0%> + <% sub_qty = 0 %> + <% count = 0%> + <% end %> + <% end %> + <% end %> + + <% end %> + + <% if @product.present?%> + + + + + + + <% @product.each do |product| %> + <% if product.total_item > 0 + total_qty += product.total_item + end %> + <% grand_total +=product.grand_total + p_qty += product.total_item%> + + + + + + + + + + + + <% product_sub_total += product.grand_total %> + + <% end %> + + + + + + + + + <%end%> + + + + + + + + + + + + + <% if @type =="" || @type =="all" || @type.nil? %> + + + <% end %> + <% end %> + <% if @type == "other"%> + + + + + + + <% @other_charges.each do |other| %> + <% if other.total_item > 0 + total_qty += other.total_item + end %> + <% grand_total +=other.grand_total%> + + + + + + + + + + + + <% other_sub_total += other.grand_total %> + + <% end %> + + + + + + <%end%> + +
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> + <% @totalByAccount.each do |account, total| %> + <% if sale.account_id == account %> + <%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) %> + <% grand_total += total %> + <% end %> + <% end %> +
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.total_item*(-1) rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total <%= sale.account_name %> Qty <%= sub_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(sub_total , precision:precision.to_i,delimiter:delimiter)%>
    Product 
     Product<%= product.product_code rescue '-' %><%= product.product_name rescue '-' %><%= product.total_item rescue '-' %> <%= number_with_precision(product.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(product.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total Product Qty <%= p_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(product_sub_total , precision:precision.to_i,delimiter:delimiter)%>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter)%>
    Other Charges 
     Other Charges<%= other.item_code rescue '-' %><%= other.product_name rescue '-' %><%= other.total_item rescue '-' %> <%= number_with_precision(other.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(other.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     <%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(other_sub_total , precision:precision.to_i,delimiter:delimiter)%>
    + +
    +
    +
    +
    +
    + + diff --git a/app/views/reports/staff_meal/index.xls.erb b/app/views/reports/staff_meal/index.xls.erb new file mode 100644 index 00000000..5bd390fd --- /dev/null +++ b/app/views/reports/staff_meal/index.xls.erb @@ -0,0 +1,277 @@ + + + + + + + +
    +
    +
    +
    +
    + + + + + + <% if @shift_from %> + + <% if @shift_data.employee %> + <% cashier_name = !@shift_data.nil? ? @shift_data.employee.name : '-' %> + <% end %> + + + <% end %> + + + + + + + + + + + + <% if @print_settings.precision.to_i > 0 + precision = @print_settings.precision + else + precision = 0 + end + #check delimiter + if @print_settings.delimiter + delimiter = "," + else + delimiter = "" + end + %> + <% acc_arr = Array.new %> + <% cate_arr = Array.new %> + <% p_qty = 0 %> + <% sub_qty = 0 %> + <% sub_total = 0 %> + <% other_sub_total = 0 %> + <% product_sub_total = 0 %> + <% count = 0 %> + <% row_count = 0 %> + <% total_price = 0 %> + <% cate_count = 0 %> + <% acc_count = 0 %> + <% grand_total = 0 %> + <% total_qty = 0 %> + <% total_amount = 0 %> + <% discount = 0 %> + <% total_item_foc = 0 %> + <% total_item_dis = 0.0 %> + <% total_tax = 0 %> + <% unless @sale_data.blank? %> + <% @sale_data.each do |sale| %> + <% row_count += 1 %> + + + <% if sale.status_type != "Discount" && sale.status_type != "foc" && sale.status_type != "promotion" + total_qty += sale.total_item + end %> + <% if sale.status_type == "foc" && sale.price > 0 + total_qty += sale.total_item + end %> + <% if sale.status_type == "Discount" + total_qty += sale.total_item*(-1) + end %> + <% if sale.status_type =="promotion" && @type == "promotion" + total_qty += sale.total_item*(-1) + end %> + + <% if sale.status_type == "foc" && sale.grand_total < 0 + total_item_foc += sale.grand_total*(-1) + end %> + + <% if sale.status_type == "Discount" && sale.grand_total < 0 + total_item_dis += sale.grand_total*(-1) + end %> + + <% if !acc_arr.include?(sale.account_id) %> + + + + + + + <% acc_arr.push(sale.account_id) %> + <% end %> + + + <% if !cate_arr.include?(sale.menu_category_id) %> + + <% cate_arr.push(sale.menu_category_id) %> + <% else %> + + <% end %> + + + <% if sale.status_type != "Discount" %> + + <%else%> + + <% end %> + + + + + + + <% @menu_cate_count.each do |key,value| %> + <% if sale.account_id == key %> + <% count = count + 1 %> + <% sub_total += sale.grand_total %> + <% #sub_qty += sale.total_item %> + <% if sale.status_type !="Discount" && (!sale.product_name.include? "FOC") && sale.status_type != "promotion" + sub_qty += sale.total_item + end %> + <% if sale.status_type =="Discount" + sub_qty += sale.total_item*(-1) + end %> + + <% if sale.status_type == "promotion" && @type == "promotion" + sub_qty += sale.total_item*(-1) + end %> + <% if count == value %> + + + + + + + + <% sub_total = 0.0%> + <% sub_qty = 0 %> + <% count = 0%> + <% end %> + <% end %> + <% end %> + + <% end %> + + <% if @product.present?%> + + + + + + + <% @product.each do |product| %> + <% if product.total_item > 0 + total_qty += product.total_item + end %> + <% grand_total +=product.grand_total + p_qty += product.total_item%> + + + + + + + + + + + + <% product_sub_total += product.grand_total %> + + <% end %> + + + + + + + + + <%end%> + + + + + + + + + + + + + <% if @type =="" || @type =="all" || @type.nil? %> + + + <% end %> + <% end %> + <% if @type == "other"%> + + + + + + <% @other_charges.each do |other| %> + <% if other.total_item > 0 + total_qty += other.total_item + end %> + <% grand_total +=other.grand_total%> + + + + + + + + + + + + <% other_sub_total += other.grand_total %> + + <% end %> + + + + + + <%end%> + +
    <%= t("views.right_panel.detail.from_date") %> : <%= @from.utc.getlocal.strftime("%Y-%b-%d") rescue '-' %> - <%= t("views.right_panel.detail.to_date") %> : <%= @to.utc.getlocal.strftime("%Y-%b-%d") rescue '-'%>
    <%= t("views.right_panel.detail.shift_name") %> = <%= @shift_from %> - <%= @shift_to %> ( <%= cashier_name %> )
     <%= t("views.right_panel.header.menu_category") %><%= t("views.right_panel.detail.code") %><%= t("views.right_panel.detail.product") %><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= t("views.right_panel.detail.unit_price") %><%= t("views.right_panel.detail.revenue") %>
    <%= sale.account_name %> <%= t("views.right_panel.detail.total_price_by") %> <%= sale.account_name %> + <% @totalByAccount.each do |account, total| %> + <% if sale.account_id == account %> + <%= number_with_precision(total, precision:precision.to_i,delimiter:delimiter) %> + <% grand_total += total %> + <% end %> + <% end %> +
     <%= sale.menu_category_name %> <%= sale.item_code rescue '-' %><%= sale.product_name rescue '-' %><%= sale.total_item rescue '-' %><%= sale.total_item*(-1) rescue '-' %><%= number_with_precision(sale.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%><%= number_with_precision(sale.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total <%= sale.account_name %> Qty <%= sub_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(sub_total , precision:precision.to_i,delimiter:delimiter)%>
    Product 
     Product<%= product.product_code rescue '-' %><%= product.product_name rescue '-' %><%= product.total_item rescue '-' %> <%= number_with_precision(product.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(product.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     Total Product Qty <%= p_qty %><%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(product_sub_total , precision:precision.to_i,delimiter:delimiter)%>
     <%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.item") %><%= total_qty%><%= t("views.right_panel.detail.total") %> <%= t("views.right_panel.detail.amount") %><%= number_with_precision(grand_total , precision:precision.to_i,delimiter:delimiter)%>
    Other Charges 
     Other Charges<%= other.item_code rescue '-' %><%= other.product_name rescue '-' %><%= other.total_item rescue '-' %> <%= number_with_precision(other.unit_price , precision:precision.to_i,delimiter:delimiter) rescue '-'%> <%= number_with_precision(other.grand_total , precision:precision.to_i,delimiter:delimiter) rescue '-'%>
     <%= t("views.right_panel.detail.sub_total") %><%= number_with_precision(other_sub_total , precision:precision.to_i,delimiter:delimiter)%>
    + +
    +
    +
    +
    +
    + + + diff --git a/config/routes.rb b/config/routes.rb index b3c5ff6e..933002d9 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -532,6 +532,7 @@ scope "(:locale)", locale: /en|mm/ do resources :dailysale, :only => [:index, :show] resources :saleitem, :only => [:index, :show] resources :hourly_saleitem, :only => [:index, :show] + resources :staff_meal, :only => [:index, :show] resources :shiftsale, :only => [:index, :show] resources :credit_payment, :only => [:index, :show] resources :void_sale, :only => [:index, :show] From a3e4cc899360f4b1699c713c7bac6273913571e2 Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Mon, 18 Nov 2019 10:09:40 +0630 Subject: [PATCH 3/5] optimize for dashboards --- app/controllers/home_controller.rb | 2 +- .../origami/dashboard_controller.rb | 14 +- app/models/menu_category.rb | 24 +- app/models/menu_item.rb | 4 +- app/models/sale.rb | 877 ++++-------------- app/models/sale_audit.rb | 4 +- app/models/sale_payment.rb | 5 + app/models/shift_sale.rb | 41 +- .../addorders/_menu_item.json.jbuilder | 86 +- .../origami/dashboard/_menu.json.jbuilder | 71 +- 10 files changed, 321 insertions(+), 807 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 49b4ff5d..d9c67962 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -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 = [] diff --git a/app/controllers/origami/dashboard_controller.rb b/app/controllers/origami/dashboard_controller.rb index 25ea8390..1daa652b 100644 --- a/app/controllers/origami/dashboard_controller.rb +++ b/app/controllers/origami/dashboard_controller.rb @@ -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." } diff --git a/app/models/menu_category.rb b/app/models/menu_category.rb index 746bcb79..d8597afa 100755 --- a/app/models/menu_category.rb +++ b/app/models/menu_category.rb @@ -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 - - - - diff --git a/app/models/menu_item.rb b/app/models/menu_item.rb index 03e90011..59791cb2 100755 --- a/app/models/menu_item.rb +++ b/app/models/menu_item.rb @@ -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') } diff --git a/app/models/sale.rb b/app/models/sale.rb index 98457d0e..98252dbb 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -4,9 +4,9 @@ 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, :optional => true + belongs_to :cashier, foreign_key: "cashier_id", class_name: "Employee" belongs_to :customer, :optional => true - belongs_to :employees + belongs_to :shift_sale has_many :sale_items has_many :sale_discount_items has_many :sale_discounts @@ -23,6 +23,12 @@ class Sale < ApplicationRecord scope :open_invoices, -> { where("sale_status = 'new' and receipt_date BETWEEN '#{DateTime.now.utc.end_of_day}' AND '#{DateTime.now.utc.beginning_of_day}'") } scope :complete_sale, -> { where("sale_status = 'completed' and receipt_date BETWEEN '#{DateTime.now.utc.beginning_of_day}' AND '#{DateTime.now.utc.end_of_day}'") } + scope :paid, -> { where(payment_status: 'paid')} + scope :completed, -> { where(sale_status: 'completed') } + 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'))") } 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 @@ -770,7 +776,7 @@ def self.daily_sales_list(from,to) 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("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'))") + .along_with_sale_payments_except_void .where("(sale_status = ? OR sale_status = ?) AND sales.receipt_date between ? AND ? ", 'completed', 'void', from, to) .group("sale_id").to_sql @@ -1104,41 +1110,36 @@ def self.get_shift_sales_by_receipt_no_detail(shift_sale_range,shift,from,to,pay end def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter,order_source) - order_sources = Order.select("sale_orders.sale_id, orders.source") - .joins(:sale_orders).to_sql - - credit_payments = SalePayment.select(" + payments_for_credits = SalePayment.select(" sales.sale_id, DATE_FORMAT(CONVERT_TZ(sale_payments.created_at,'+00:00','+06:30'),'%d %b %y %h:%i%p') as credit_payment_receipt_date, sale_payments.payment_amount as credit_payment, employees.name as credit_payment_cashier_name, - CONCAT(DATE_FORMAT(CONVERT_TZ(shift_started_at,'+00:00','+06:30'),'%d %b %y %h:%i%p'),' - ',DATE_FORMAT(CONVERT_TZ(shift_closed_at,'+00:00','+06:30'),'%d %b %y %h:%i%p')) as credit_payment_shift_name") - .joins("JOIN sales ON sales.sale_id = sale_payments.sale_id") - .joins("JOIN sale_audits ON sale_audits.sale_id = sales.sale_id AND SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id") - .joins("JOIN shift_sales ON shift_sales.id = sales.shift_sale_id") - .joins("JOIN employees ON employees.id = shift_sales.employee_id").to_sql + CONCAT(DATE_FORMAT(CONVERT_TZ(shift_sales.shift_started_at,'+00:00','+06:30'),'%d %b %y %h:%i%p'),' - ',DATE_FORMAT(CONVERT_TZ(shift_sales.shift_closed_at,'+00:00','+06:30'),'%d %b %y %h:%i%p')) as credit_payment_shift_name") + .joins(:sale_audit) + .joins(:sale => :shift_sale) + .joins(:sale => :cashier).to_sql credits = SalePayment.select(" sale_payments.sale_payment_id, sale_payments.payment_method, sale_payments.payment_amount, sale_payments.payment_status, - sales.sale_id, - sales.receipt_no, - sales.receipt_date as sale_date, - order_sources.source as order_source, - sales.cashier_name, + sales_sale_payments.sale_id, + sales_sale_payments.receipt_no, + sales_sale_payments.receipt_date as sale_date, + sales_sale_payments.cashier_name, + orders.source as order_source, customers.name as customer_name, - IFNULL(credit_payments.credit_payment_receipt_date, '-') as credit_payment_receipt_date, - IFNULL(credit_payments.credit_payment, 0) as credit_payment, - IFNULL(credit_payments.credit_payment_cashier_name, '-') as credit_payment_cashier_name, - IFNULL(credit_payments.credit_payment_shift_name, '-') as credit_payment_shift_name") - .joins("JOIN sales ON sales.sale_id = sale_payments.sale_id") - .joins("JOIN shift_sales ON shift_sales.id = sales.shift_sale_id") - .joins("JOIN customers ON customers.customer_id = sales.customer_id") - .joins("JOIN (#{order_sources}) order_sources ON order_sources.sale_id = sales.sale_id") - .joins("LEFT JOIN (#{credit_payments}) credit_payments ON credit_payments.sale_id = sales.sale_id") - .where("sale_payments.payment_method= ? AND sales.sale_status = ?", 'creditnote', 'completed') + IFNULL(payments_for_credits.credit_payment_receipt_date, '-') as credit_payment_receipt_date, + IFNULL(payments_for_credits.credit_payment, 0) as credit_payment, + IFNULL(payments_for_credits.credit_payment_cashier_name, '-') as credit_payment_cashier_name, + IFNULL(payments_for_credits.credit_payment_shift_name, '-') as credit_payment_shift_name") + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales_sale_payments.sale_id") + .joins(:sale => :customer) + .joins(:sale => :orders) + .credits + .where("sales_sale_payments.sale_status = ?", 'completed') if order_source.present? if order_source == "cashier" @@ -1149,19 +1150,19 @@ def self.get_by_shift_sale_credit_payment(shift_sale_range,shift,from,to,filter, end if filter == 'paid' - credits = credits.where("credit_payment IS NOT NULL") + credits = credits.where("payments_for_credits IS NOT NULL") elsif filter == 'unpaid' - credits = credits.where("credit_payment IS NULL") + credits = credits.where("payments_for_credits IS NULL") end if shift.present? - credits = credits.where("sales.shift_sale_id in (?)",shift.to_a) + credits = credits.where("sales_sale_payments.shift_sale_id in (?)",shift.to_a) elsif shift_sale_range.present? - credits = credits.where("sales.shift_sale_id in (?)",shift_sale_range.to_a) + credits = credits.where("sales_sale_payments.shift_sale_id in (?)",shift_sale_range.to_a) else - credits = credits.where("sales.receipt_date between ? and ?",from,to) + credits = credits.where("sales_sale_payments.receipt_date between ? and ?",from,to) end - credits = credits.group("sales.sale_id") + credits = credits.group("sale_payments.sale_payment_id, sales_sale_payments.sale_id") end def self.get_void_sale(shift,from,to) @@ -1480,329 +1481,96 @@ end end def self.employee_sales(today,current_user,from,to,from_time,to_time) - #sub query for credit payment - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" - end - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + shift = if current_user.present? && !(current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor') + ShiftSale.current_open_shift(current_user.id) end - sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) - FROM sale_payments - INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - INNER JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sales.sale_status='completed'" + payments_for_credits = SalePayment.joins(:sale_audit).to_sql - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - #sub query for credit payment - - changed_query = "SELECT SUM(s.amount_changed) - FROM `sales` s - WHERE s.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - query = Sale.employee_sale(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' - changed_query += " AND s.shift_sale_id=sales.shift_sale_id" - query = Sale.employee_sale(today,nil,from,to,from_time,to_time) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id=#{shift.id}" - query = Sale.employee_sale(today,shift,from,to,from_time,to_time) - end - end - end - else - if current_user.nil? - query = Sale.employee_sale(today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - changed_query += " AND s.shift_sale_id=sales.shift_sale_id" - query = Sale.employee_sale(today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id=#{shift.id}" - query = Sale.employee_sale(today,shift) - end - end - end - end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN (SUM(sp.payment_amount) - (#{sub_query})) ELSE SUM(sp.payment_amount) END) AS payment_amount, (CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END) AS payment_method, e.name AS e_name") + query = employee_sale(today, shift, from, to, from_time, to_time) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") + .select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount, + CASE WHEN sale_payments.payment_method IN ('mpu', 'visa', 'master', 'jcb', 'unionpay', 'alipay', 'paymal', 'dinga', 'JunctionPay', 'giftvoucher') THEN 'card' + ELSE sale_payments.payment_method END AS payment_method, employees.name AS e_name") end def self.total_trans(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.select("SUM(grand_total) as total_sale, COUNT(sales.sale_id) as total_count") .where('sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - total = query.where('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:%i") between ? and ?',from,to,from_time,to_time) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - total = query.where('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:%i") between ? and ?',from,to,from_time,to_time) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - total = query.where('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:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - total = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + 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? - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - total = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',today,today,shift.id) - 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("sales.shift_sale_id = ?", shift.id) end end + return query end def self.total_card_sale(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") .where('sales.sale_status = "completed" and (sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method = "alipay" or sp.payment_method = "paymal" or sp.payment_method = "dinga" or sp.payment_method = "JunctionPay")') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - 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("sales.shift_sale_id = ?", shift.id) end end query = query.sum("sp.payment_amount") end def self.credit_payment(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" + payments_for_credits = SalePayment.joins(:sale_audit).to_sql + + query = SalePayment.credits + .joins(:sale) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sale_payments.sale_id") + .where("sale_payments.payment_method= ? AND sales.sale_status = ?", 'creditnote', 'completed') if (!from.nil? && !to.nil?) && (from != "" && to!="") + query = query.merge(Sale.date_between(from, to)) if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" + query = query.merge(Sale.time_between(from_time, to_time)) end else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" + query = query.merge(Sale.date_on(today)) end - - sub_query = SalePayment.select("(CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) as total_credit_payment") - .joins(" JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id") - .joins(" JOIN sales ON sale_payments.sale_id = sales.sale_id") - .where("sales.sale_status='completed'") - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'") - else - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'") - end - else - sub_query = sub_query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'") - end - - query = SalePayment.where('s.sale_status = "completed" and payment_method="creditnote"') - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and s.shift_sale_id=?',from,to,from_time,to_time,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',from,to,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - end - end - end - else - if current_user.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',today,today) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(s.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and s.shift_sale_id=?',today,today,shift.id) - .sum("payment_amount") - if !sub_query.nil? - query = query.to_f - (sub_query[0].total_credit_payment.to_f > 0 ? sub_query[0].total_credit_payment.to_f : 0) - end - end - 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("sales.shift_sale_id = ?", shift.id) end end - return query + + return query.sum("sale_payments.payment_amount - IFNULL(payments_for_credits.payment_amount, 0)") end def self.summary_sale_receipt(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) query = Sale.select('count(sale_id) as total_receipt, (case when sum(total_amount) > 0 then sum(total_amount) else 0.0 end) as total_amount, (case when sum(grand_total) > 0 then sum(grand_total) else 0.0 end) as grand_total, (case when sum(total_discount) > 0 then sum(total_discount) else 0.0 end) as total_discount, (case when sum(total_tax) > 0 then sum(total_tax) else 0.0 end) as total_tax') .where('sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('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:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('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:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('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:%i") between ? and ? and shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and shift_sale_id=?',today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -1813,147 +1581,50 @@ end .where('sales.sale_status = "completed"') .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date, "+00:00", "+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end + return query end def self.payment_sale(payment_method, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - time_query = '' - if !from_time.nil? && !to_time.nil? - time_query = " and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - end + payments_for_credits = SalePayment.joins(:sale_audit).to_sql - outstanding_query = "SELECT CASE WHEN SUM(sale_payments.outstanding_amount) < 0 THEN SUM(sale_payments.outstanding_amount) ELSE 0 END - FROM sale_payments - JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sale_payments.outstanding_amount LIKE '%-%' AND sales.sale_status='completed'" + query = Sale.select("SUM(sale_payments.payment_amount) - CASE WHEN sale_payments.payment_method = 'creditnote' THEN IFNULL(SUM(payments_for_credits.payment_amount), 0) ELSE ABS(SUM(CASE WHEN sale_payments.outstanding_amount < 0 THEN sale_payments.outstanding_amount ELSE 0 END)) END AS payment_amount") + .joins(:sale_payments) + .joins("LEFT JOIN (#{payments_for_credits}) payments_for_credits ON payments_for_credits.sale_id = sales.sale_id") + .completed - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'" - end - else - outstanding_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - sub_query = "SELECT (CASE WHEN SUM(sale_payments.payment_amount) > 0 THEN SUM(sale_payments.payment_amount) + (#{outstanding_query}) ELSE 0 END) - FROM sale_payments - INNER JOIN sale_audits ON SUBSTRING_INDEX(sale_audits.remark,'||',1)=sale_payments.sale_payment_id - INNER JOIN sales ON sale_payments.sale_id = sales.sale_id - WHERE sales.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - sub_query += " AND DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - changed_query = "SELECT SUM(s.amount_changed) - FROM `sales` s - WHERE s.sale_status='completed'" - - if (!from.nil? && !to.nil?) && (from != "" && to!="") - if !from_time.nil? && !to_time.nil? - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}' AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'" - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' AND '#{to}'" - end - else - changed_query += " AND DATE_FORMAT(CONVERT_TZ(s.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') = '#{today}'" - end - - query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") - where('sales.sale_status = "completed"') if payment_method == 'card' - query = query.where('(sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb" or sp.payment_method = "unionpay" or sp.payment_method="alipay" or sp.payment_method="paymal" or sp.payment_method="dinga" or sp.payment_method="JunctionPay" or sp.payment_method = "giftvoucher")') + query = query.merge(SalePayment.cards) else - query = query.where("sp.payment_method = '#{payment_method}'") + query = query.where("sale_payments.payment_method = ?", payment_method) end + if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id = #{shift.id}" - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date, '+00:00', '+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + 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 = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - changed_query += " AND s.shift_sale_id = #{shift.id}" - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - end - end - end + query = query.date_on(today) end - query = query.select("(CASE WHEN sp.payment_method='cash' THEN (SUM(sp.payment_amount) - (#{changed_query})) WHEN sp.payment_method='creditnote' THEN SUM(sp.payment_amount) - (#{sub_query}) ELSE SUM(sp.payment_amount) END) as payment_amount").first() + + 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("sales.shift_sale_id = ?", shift.id) + end + end + query.first end def self.total_customer(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -1984,45 +1655,18 @@ end .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and c.membership_id is null') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end - query = query.first() end @@ -2031,42 +1675,16 @@ end .joins("JOIN customers as c ON c.customer_id = sales.customer_id") .where('sales.sale_status = "completed" and ((c.customer_type = "Dinein" and c.membership_id is not null) or (c.customer_type = "Takeaway" and c.membership_id is not null))') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?)',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id).first() - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id).first() - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('sales.sale_status = "completed" and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -2119,50 +1737,25 @@ end # end def self.total_order(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) - query = Sale.select("count(distinct a.order_id) as total_order") - .joins("JOIN sale_orders as a ON a.sale_id = sales.sale_id") - .joins("JOIN orders as b ON b.order_id = a.order_id") - .where('b.status = "billed"') + query = Sale.select("count(distinct sale_orders.order_id) as total_order") + .joins(:sale_orders) + .completed if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - 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("sales.shift_sale_id = ?", shift.id) end end - query = query.first() + + query = query.first end def self.total_account(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2171,44 +1764,19 @@ end .joins("JOIN accounts as b ON b.id = a.account_id") .where('sales.sale_status = "completed"') if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ?',from,to,from_time,to_time) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ?',from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%H:%i") between ? and ? and sales.shift_sale_id=?',from,to,from_time,to_time,shift.id) - else - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") between ? and ? and sales.shift_sale_id=?',from,to,shift.id) - end - end - end + 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 = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ?',today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where('DATE_FORMAT(CONVERT_TZ(sales.receipt_date,"+00:00","+06:30"),"%Y-%m-%d") = ? and sales.shift_sale_id=?',today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end + return query end def self.account_data(account_id, today, current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2216,45 +1784,19 @@ end .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + 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 = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end - query = query.first() + query = query.first end def self.top_items(today,current_user=nil,from=nil,to=nil,from_time=nil,to_time=nil) @@ -2262,42 +1804,16 @@ end .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("(a.qty > 0 and a.price > 0) and payment_status='paid' and sales.sale_status = 'completed'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + 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 = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end query = query.group("a.product_code") @@ -2309,42 +1825,16 @@ end query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") .where("sales.sale_status = 'completed' and a.status='foc' and a.product_name like '%FOC%'") if (!from.nil? && !to.nil?) && (from != "" && to!="") - if current_user.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ?",from,to,from_time,to_time) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ?",from,to) - end - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between ? and ? and sales.shift_sale_id=?",from,to,from_time,to_time,shift.id) - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between ? and ? and sales.shift_sale_id=?",from,to,shift.id) - end - end - end + 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 = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - if current_user.role == 'administrator' || current_user.role == 'manager' || current_user.role == 'account' || current_user.role == 'supervisor' - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) - else - shift = ShiftSale.current_open_shift(current_user.id) - if !shift.nil? - query = query.where("DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ? and sales.shift_sale_id=?",today,shift.id) - 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.joins("sales.shift_sale_id = ?", shift.id) end end query = query.count() @@ -2540,27 +2030,24 @@ def self.hourly_sale_data(today,shift=nil,from=nil,to=nil,from_time=nil,to_time= end def self.employee_sale(today,shift=nil,from=nil,to=nil,from_time=nil,to_time=nil) - query = Sale.joins("JOIN employees as e on e.id=sales.cashier_id") - .joins("JOIN sale_payments as sp on sp.sale_id=sales.sale_id") - .where("sales.payment_status='paid' and sales.sale_status = 'completed' ") + query = Sale.joins(:cashier) + .joins(:sale_payments) + .paid.completed if !from.nil? && !to.nil? + query = query.date_between(from, to) if !from_time.nil? && !to_time.nil? - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}' and DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%H:%i') between '#{from_time}' and '#{to_time}'") - else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') between '#{from}' and '#{to}'") + query = query.time_between(from_time, to_time) end else - query = query.where("DATE_FORMAT(CONVERT_TZ(sales.receipt_date,'+00:00','+06:30'),'%Y-%m-%d') = '#{today}'") + query = query.date_on(today) end if !shift.nil? - query = query.where("sales.shift_sale_id ='#{shift.id}'") + query = query.where("sales.shift_sale_id = ?", shift.id) end - query = query.group("(CASE WHEN (sp.payment_method='mpu' or sp.payment_method='visa' or sp.payment_method='master' or sp.payment_method='jcb' or sp.payment_method='unionpay' or sp.payment_method='alipay' or sp.payment_method='paymal' or sp.payment_method='dinga' or sp.payment_method='JunctionPay' or sp.payment_method='giftvoucher') THEN 'card' ELSE sp.payment_method END)","e.name") - .order("e.name") - - return query + query = query.group("payment_method","employees.name") + .order("employees.name") end # Start hourly sale item report diff --git a/app/models/sale_audit.rb b/app/models/sale_audit.rb index d96034ad..1488cad1 100755 --- a/app/models/sale_audit.rb +++ b/app/models/sale_audit.rb @@ -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 diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index 5a1fdb72..e0ad2383 100755 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -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| diff --git a/app/models/shift_sale.rb b/app/models/shift_sale.rb index 2d263c21..07217103 100755 --- a/app/models/shift_sale.rb +++ b/app/models/shift_sale.rb @@ -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? diff --git a/app/views/origami/addorders/_menu_item.json.jbuilder b/app/views/origami/addorders/_menu_item.json.jbuilder index a18645c8..2768fa9c 100755 --- a/app/views/origami/addorders/_menu_item.json.jbuilder +++ b/app/views/origami/addorders/_menu_item.json.jbuilder @@ -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 \ No newline at end of file +# end diff --git a/app/views/origami/dashboard/_menu.json.jbuilder b/app/views/origami/dashboard/_menu.json.jbuilder index d558c4ea..9c9f1963 100644 --- a/app/views/origami/dashboard/_menu.json.jbuilder +++ b/app/views/origami/dashboard/_menu.json.jbuilder @@ -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 From 77d0e0a56c7f238fd16927959e12cf12ca54a52e Mon Sep 17 00:00:00 2001 From: NyanLinHtut Date: Mon, 18 Nov 2019 10:22:54 +0630 Subject: [PATCH 4/5] add readme --- Gemfile | 2 +- README.md | 3 +++ app/models/inventory_definition.rb | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index ba95e3c9..c8139eb7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.6.3' +ruby '2.4.1' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" diff --git a/README.md b/README.md index 4d81b144..77a25926 100755 --- a/README.md +++ b/README.md @@ -299,6 +299,9 @@ settings/lookups => {type:display_type, name: Display Type, value: 2} For show total before tax in receipt bill settings/lookups => {type:show_total_before_tax, name:Show Total Before Tax, value: {0 or 1}} +For Using Staff Meal +settings/lookups => { type:customer_type, name: Staff, value:Staff } + * ToDo list 1. Migration diff --git a/app/models/inventory_definition.rb b/app/models/inventory_definition.rb index dd2f3c3c..f2ed207d 100755 --- a/app/models/inventory_definition.rb +++ b/app/models/inventory_definition.rb @@ -24,18 +24,18 @@ class InventoryDefinition < ApplicationRecord end def self.find_product_in_inventory(item,instance_code) - 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 + # 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(item.item_instance_code) if stock_check_item = StockCheckItem.find_by_item_code(item.item_instance_code) From 5702819b8b5591435e23b13b3e0500364fd647ec Mon Sep 17 00:00:00 2001 From: Thein Lin Kyaw Date: Mon, 18 Nov 2019 11:16:14 +0630 Subject: [PATCH 5/5] fix dashboard error for cashier --- app/models/sale.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/sale.rb b/app/models/sale.rb index 8e7f0b92..fe43ea69 100644 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -1690,7 +1690,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -1710,7 +1710,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end return query @@ -1784,7 +1784,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -1804,7 +1804,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.first() @@ -1893,7 +1893,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end return query @@ -1913,7 +1913,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.first @@ -1933,7 +1933,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.group("a.product_code") @@ -1954,7 +1954,7 @@ end 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.joins("sales.shift_sale_id = ?", shift.id) + query = query.where("sales.shift_sale_id = ?", shift.id) end end query = query.count()