diff --git a/app/controllers/api/check_in_process_controller.rb b/app/controllers/api/check_in_process_controller.rb new file mode 100644 index 00000000..0baa258a --- /dev/null +++ b/app/controllers/api/check_in_process_controller.rb @@ -0,0 +1,78 @@ +class Api::CheckInProcessController < Api::ApiController + + def check_in_process + if params[:dining_id] + dining_facility = DiningFacility.find(params[:dining_id]) + if dining_facility.status == "available" + dining_charge = DiningCharge.select('charge_type','charge_block') + .where('dining_facility_id = ?',params[:dining_id]) + .first() + + checkout_at = Time.now.utc + + if !dining_charge.nil? + hr = (dining_charge.charge_block.utc.strftime("%H").to_i).to_int + min = (dining_charge.charge_block.utc.strftime("%M").to_i).to_int + # if dining_charge.charge_type == 'hr' + checkout_at = checkout_at + hr.hour + min.minutes + # else + + # end + end + + dining_facility.status = "occupied" + dining_facility.save! + + if dining_facility.type == "Table" + type = "TableBooking" + else + type = "RoomBooking" + end + + booking = Booking.create({:dining_facility_id => params[:dining_id],:type => type, + :checkin_at => Time.now.utc,:checkout_at =>checkout_at, :booking_status => "assign" }) + booking.save! + + render :json => { :status => true, :checkout_at => booking.checkout_at.utc.strftime("%Y-%m-%d %H:%M") } + else + error_message = "#{dining_facility.type} is not available!" + render :json => { :status => false, :error_message => error_message } + end + else + error_message = "dining_id is required!" + render :json => { :status => false, :error_message => error_message } + end + end + + def request_time + if !params[:booking_id].nil? && !params[:time].nil? + time = Time.parse(params[:time]) + booking = Booking.find(params[:booking_id]) + + checkout_at = booking.checkout_at.utc + + hr = (time.strftime("%H").to_i).to_int + min = (time.strftime("%M").to_i).to_int + checkout_at = checkout_at + hr.hour + min.minutes + + booking.checkout_at = checkout_at + booking.save! + + render :json => { :status => true, :checkout_at => booking.checkout_at.utc.strftime("%Y-%m-%d %H:%M") } + elsif !params[:booking_id].nil? && params[:time].nil? + error_message = "time is required!" + render :json => { :status => false, :error_message => error_message } + elsif params[:booking_id].nil? && !params[:time].nil? + error_message = "booking_id is required!" + render :json => { :status => false, :error_message => error_message } + else + error_message = "booking_id and time are required!" + render :json => { :status => false, :error_message => error_message } + end + end + + private + def check_in_process_params + params.permit(:dining_id,:booking_id,:time) + end +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 8cfdfd22..74cc9ba9 100755 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -74,18 +74,42 @@ class HomeController < ApplicationController @employee_sales = Sale.employee_sales(today).sum(:grand_total) @inventories = StockJournal.inventory_balances(today).sum(:balance) - #@employee_sales = Hash.new - #employee_sales.each do |key, value| - # if(@employee_sales.has_key? key[1]) - # @employee_sales[key[1]].push({key[0] => value}) - # else - # @employee_sales[key[1]] = [key[0] => value] - # end - #end @total_sale = Sale.total_sale(today) @total_count = Sale.total_count(today) @total_card = Sale.total_card_sale(today) - @total_credit = Sale.credit_payment(today) + @total_credit = Sale.credit_payment(today) + + @sale_data = Array.new + @total_payment_methods = Sale.total_payment_methods(today) + @total_payment_methods.each do |payment| + if payment.payment_method == "mpu" || payment.payment_method == "visa" || payment.payment_method == "master" || payment.payment_method == "jcb" + pay = Sale.payment_sale('card', today) + @sale_data.push({'card' => pay}) + else + pay = Sale.payment_sale(payment.payment_method, today) + @sale_data.push({payment.payment_method => pay}) + end + end + @summ_sale = Sale.summary_sale_receipt(today) + + @total_customer = Sale.total_customer(today) + @total_dinein = Sale.total_dinein(today) + @total_takeaway = Sale.total_takeaway(today) + @total_other_customer = Sale.total_other_customer(today) + @total_membership = Sale.total_membership(today) + + @total_order = Sale.total_order(today) + @total_accounts = Sale.total_account(today) + @account_data = Array.new + @total_accounts.each do |account| + acc = Sale.account_data(account.account_id, today) + if !acc.nil? + @account_data.push({account.title => acc.cnt_acc, account.title + '_amount' => acc.total_acc}) + end + end + + @top_items = Sale.top_items(today) + @total_foc_items = Sale.total_foc_items(today) end def destroy diff --git a/app/controllers/origami/check_in_process_controller.rb b/app/controllers/origami/check_in_process_controller.rb new file mode 100644 index 00000000..8e9a1f38 --- /dev/null +++ b/app/controllers/origami/check_in_process_controller.rb @@ -0,0 +1,38 @@ +class Origami::CheckInProcessController < BaseOrigamiController + + def check_in_process + dining_charge = DiningCharge.select('charge_type','charge_block') + .where('dining_facility_id = ?',params[:dining_id]) + .first() + + checkout_at = Time.now.utc + + if !dining_charge.nil? + hr = (dining_charge.charge_block.utc.strftime("%H").to_i).to_int + min = (dining_charge.charge_block.utc.strftime("%M").to_i).to_int + # if dining_charge.charge_type == 'hr' + checkout_at = checkout_at + hr.hour + min.minutes + # else + + # end + end + @dining_facility = DiningFacility.find(params[:dining_id]) + @dining_facility.status = "occupied" + @dining_facility.save! + + if @dining_facility.type == "Table" + type = "TableBooking" + else + type = "RoomBooking" + end + + @booking = Booking.create({:dining_facility_id => params[:dining_id],:type => type, + :checkin_at => Time.now.utc,:checkout_at =>checkout_at, :booking_status => "assign" }) + @booking.save! + + respond = {:status => 'ok'} + respond_to do |format| + format.json { render json: respond } + end + end +end diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index febf7e45..8e5b33c2 100755 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -7,7 +7,7 @@ class Origami::HomeController < BaseOrigamiController @complete = Sale.where("DATE_FORMAT(created_at,'%Y-%m-%d') = ? and sale_status != 'new'",DateTime.now.strftime('%Y-%m-%d')) @orders = Order.all.order('date desc') @shop = Shop.find_by_id(1) - + # @shift = ShiftSale.current_open_shift(current_user.id) end @@ -23,10 +23,12 @@ class Origami::HomeController < BaseOrigamiController @sale_array = Array.new @dining.bookings.active.each do |booking| - if booking.sale_id.nil? && booking.booking_status != 'moved' - - @order_items = Array.new - booking.booking_orders.each do |booking_order| + if booking.sale_id.nil? && booking.booking_status != 'moved' + @order_items = Array.new + if booking.booking_orders.empty? + @booking = booking + else + booking.booking_orders.each do |booking_order| order = Order.find(booking_order.order_id) if (order.status == "new") @obj_order = order @@ -43,29 +45,30 @@ class Origami::HomeController < BaseOrigamiController @account_arr.push(account) end end - end - @status_order = 'order' - else - sale = Sale.find(booking.sale_id) - if sale.sale_status != "completed" && sale.sale_status != 'void' - - @sale_array.push(sale) - if @status_order == 'order' - @status_order = 'sale' - end - @booking= booking - @date = sale.created_at - @status_sale = 'sale' - @obj_sale = sale - @customer = sale.customer - accounts = @customer.tax_profiles - @account_arr =[] - accounts.each do |acc| - account = TaxProfile.find(acc) - @account_arr.push(account) - end - end + end end + @status_order = 'order' + else + sale = Sale.find(booking.sale_id) + if sale.sale_status != "completed" && sale.sale_status != 'void' + + @sale_array.push(sale) + if @status_order == 'order' + @status_order = 'sale' + end + @booking= booking + @date = sale.created_at + @status_sale = 'sale' + @obj_sale = sale + @customer = sale.customer + accounts = @customer.tax_profiles + @account_arr =[] + accounts.each do |acc| + account = TaxProfile.find(acc) + @account_arr.push(account) + end + end + end end end diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 24a3e47b..9463738b 100755 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -32,7 +32,7 @@ class DiningFacility < ApplicationRecord end def get_current_booking - booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - 5.hours}' and '#{DateTime.now.utc}' and checkout_at is null").limit(1) + booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and checkin_at between '#{DateTime.now.utc - 5.hours}' and '#{DateTime.now.utc}'").limit(1) #and checkout_at is null if booking.count > 0 then return booking[0] else @@ -65,4 +65,32 @@ class DiningFacility < ApplicationRecord return nil end end + + def get_current_checkout_booking + booking = Booking.where("dining_facility_id = #{self.id} and booking_status ='assign' and sale_id is null and checkout_at is not null and checkout_by is null").limit(1) + if booking.count > 0 then + return booking[0] + else + return nil + end + end + + def get_checkout_booking + booking = self.get_current_checkout_booking + if booking + now = Time.now.utc + hr = (now.strftime("%H").to_i).to_int + min = (now.strftime("%M").to_i).to_int + + checkout_at = booking.checkout_at.utc + + checkout_at = checkout_at - hr.hour + checkout_at = checkout_at - min.minutes + checkout_at = checkout_at.utc.strftime("%M").to_i + + if checkout_at <= 15 + return booking + end + end + end end diff --git a/app/models/sale.rb b/app/models/sale.rb index 84eb86f8..c2bfbdd2 100755 --- a/app/models/sale.rb +++ b/app/models/sale.rb @@ -941,9 +941,9 @@ end # .joins("join sale_payments on sale_id = sales.sale_id") # .group("sales.sale_id") - query = SalePayment.where('s.sale_status = "completed" and payment_method = "mpu" or payment_method = "visa" or payment_method = "master" or payment_method = "jcb" and DATE_FORMAT(s.receipt_date,"%Y-%m-%d") = ?',today) - .joins("INNER JOIN sales s ON s.sale_id = sale_payments.sale_id") - .sum("payment_amount") + query = Sale.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb"',today) + .joins("JOIN sale_payments sp ON sp.sale_id = sales.sale_id") + .sum("sp.payment_amount") end @@ -953,6 +953,99 @@ end .sum("payment_amount") end + def self.summary_sale_receipt(today) + query = Sale.select('count(sale_id) as total_receipt, sum(total_amount) as total_amount, sum(grand_total) as grand_total, sum(total_discount) as total_discount, sum(total_tax) as total_tax') + .where('sale_status = "completed" and DATE_FORMAT(receipt_date,"%Y-%m-%d") = ?',today) + .first() + end + + def self.total_payment_methods(today) + query = Sale.select("sp.payment_method") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + end + + def self.payment_sale(payment_method, today) + query = Sale.joins("JOIN sale_payments as sp ON sp.sale_id = sales.sale_id") + if payment_method == 'card' + query = query.where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and sp.payment_method = "mpu" or sp.payment_method = "visa" or sp.payment_method = "master" or sp.payment_method = "jcb"',today) + else + query = query.where("sales.sale_status = 'completed' and sp.payment_method = '#{payment_method}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + end + query.sum("sp.payment_amount") + end + + def self.total_customer(today) + query = Sale.select("count(distinct sales.customer_id) as total_cus") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + end + + def self.total_dinein(today) + query = Sale.select("count(distinct sales.customer_id) as total_dinein_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Dinein" and c.membership_id is null',today) + .first() + end + + def self.total_takeaway(today) + query = Sale.select("count(distinct sales.customer_id) as total_take_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is null',today) + .first() + end + + def self.total_membership(today) + query = Sale.select("count(distinct sales.customer_id) as total_memb_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type = "Takeaway" and c.membership_id is not null',today) + .first() + end + + def self.total_other_customer(today) + query = Sale.select("count(distinct sales.customer_id) as total_cus") + .joins("JOIN customers as c ON c.customer_id = sales.customer_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ? and c.customer_type is null and c.membership_id is null',today) + .first() + end + + def self.total_order(today) + 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" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + .first() + end + + def self.total_account(today) + query = Sale.select("distinct b.id as account_id, b.title as title") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .joins("JOIN accounts as b ON b.id = a.account_id") + .where('sales.sale_status = "completed" and DATE_FORMAT(sales.receipt_date,"%Y-%m-%d") = ?',today) + end + + def self.account_data(account_id, today) + query = Sale.select("count(*) as cnt_acc, SUM(a.price) as total_acc") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.account_id ='#{account_id}' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .first() + end + + def self.top_items(today) + query = Sale.select("a.product_name as item_name, SUM(a.price) as item_total_price") + .joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .group("a.product_code") + .order("SUM(a.price) DESC") + .first() + end + + def self.total_foc_items(today) + query = Sale.joins("JOIN sale_items as a ON a.sale_id = sales.sale_id") + .where("sales.sale_status = 'completed' and a.remark='foc' and DATE_FORMAT(sales.receipt_date,'%Y-%m-%d') = ?",today) + .count() + end + private def generate_custom_id diff --git a/app/views/home/dashboard.html.erb b/app/views/home/dashboard.html.erb index a48a440a..0f7ee757 100755 --- a/app/views/home/dashboard.html.erb +++ b/app/views/home/dashboard.html.erb @@ -56,7 +56,7 @@ <% if current_user.role == 'administrator' || current_user.role == 'manager' %>
| Total Customer : | +<%= @total_customer.total_cus %> | +
| Dine in : | +<%= @total_dinein.total_dinein_cus %> | +
| Takeaway : | +<%= @total_takeaway.total_take_cus %> | +
| Customer : | +<%= @total_other_customer.total_cus %> | +
| Membership : | +<%= @total_membership.total_memb_cus %> | +
| Total Order : | +<%= @total_order.total_order %> | +
| <%= account.title %> (Account) : | ++ <% @account_data.each do |data| %> + <% acc = account.title %> + <%= data[""+acc+""] %> <% if !data[''+acc+''].nil? %> ( <%= data[''+acc+'_amount'] %> ) <% end %> + <% end %> + | +
| Top Item : | +<%= @top_items.item_name %> ( <%= @top_items.item_total_price %> ) | +
| Total FOC Item : | +<%= @total_foc_items %> | +
<%= @sale_array[0].customer_id rescue '' %>
- Customer : <%= @sale_array[0].customer.name rescue '' %> - <% else %> -<%= @customer.customer_id rescue "" %>
- Customer : <%= @customer.name rescue "" %> - <% end %> + <% if !@sale_array.empty? %> + <% if @status_sale == 'sale' %> +<%= @sale_array[0].customer_id rescue '' %>
+ Customer : <%= @sale_array[0].customer.name rescue '' %> + <% else %> +<%= @customer.customer_id rescue "" %>
+ Customer : <%= @customer.name rescue "" %> + <% end %> + <% end %>