From 0ea2b778d18314646fc4a3ba0b208cc7709a1e4c Mon Sep 17 00:00:00 2001 From: Nweni Date: Thu, 22 Jun 2017 14:26:23 +0630 Subject: [PATCH 1/2] multiple sale and order --- app/controllers/api/orders_controller.rb | 83 ++++++++----------- app/controllers/origami/home_controller.rb | 10 +-- app/controllers/origami/orders_controller.rb | 2 +- app/controllers/origami/rooms_controller.rb | 2 +- app/controllers/origami/sales_controller.rb | 36 +++++++- app/models/booking.rb | 1 + app/models/dining_facility.rb | 11 +++ app/models/sale_payment.rb | 2 + app/views/origami/home/show.html.erb | 41 ++++++--- .../origami/movetable/move_dining.html.erb | 2 +- app/views/origami/payments/show.html.erb | 14 +++- .../add_to_existing_invoice.json.jbuilder | 1 + config/routes.rb | 1 + 13 files changed, 134 insertions(+), 72 deletions(-) create mode 100644 app/views/origami/sales/add_to_existing_invoice.json.jbuilder diff --git a/app/controllers/api/orders_controller.rb b/app/controllers/api/orders_controller.rb index b42de7c5..1eba8c56 100644 --- a/app/controllers/api/orders_controller.rb +++ b/app/controllers/api/orders_controller.rb @@ -22,27 +22,19 @@ class Api::OrdersController < Api::ApiController if booking if booking.dining_facility_id.to_i == table_id.to_i - @booking = booking + if booking.booking_status == 'assign' + if booking.sale_id.nil? + @booking = booking + end + end else table = DiningFacility.find(table_id) - booking = table.get_current_booking - if booking - if booking.dining_facility_id.to_i == table_id.to_i - @booking = booking - end - end + @booking = table.get_booking end end else - puts "only table" table = DiningFacility.find(table_id) - booking = table.get_current_booking - puts booking - if booking - if booking.dining_facility_id.to_i == table_id.to_i - @booking = booking - end - end + @booking = table.get_booking end end @@ -73,54 +65,47 @@ class Api::OrdersController < Api::ApiController # check booking id is already completed. booking = Booking.find(params[:booking_id]) if booking - if booking.dining_facility_id.to_i == params[:table_id].to_i + if booking.dining_facility_id.to_i == params[:table_id].to_i && booking.booking_status != 'moved' if !booking.sale_id.nil? - if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" - @order.new_booking = true - else - @order.new_booking = false - @order.booking_id = params[:booking_id] - end + check_order_with_booking(booking) else @order.new_booking = false @order.booking_id = params[:booking_id] - puts "booking sale is null" end else - # booking.table id not equal current table - table = DiningFacility.find(params[:table_id]) - if table - booking = table.get_current_booking - if booking - if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" - @order.new_booking = true - else - @order.new_booking = false - @order.booking_id = booking.booking_id - end - end - end + check_order_with_table(params[:table_id]) end end #booking exists else - #no booking id - table = DiningFacility.find(params[:table_id]) - if table - booking = table.get_current_booking - if booking - if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" - @order.new_booking = true - else - @order.new_booking = false - @order.booking_id = booking.booking_id - end - end + check_order_with_table(params[:table_id]) end - end @status, @booking = @order.generate end + def check_order_with_table(table_id) + table = DiningFacility.find(table_id) + if table + booking = table.get_current_booking + if booking + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" + @order.new_booking = true + else + @order.new_booking = false + @order.booking_id = booking.booking_id + end + end + end + end + + def check_order_with_booking(booking) + if booking.sale.sale_status == "completed" || booking.sale.sale_status == "billed" + @order.new_booking = true + else + @order.new_booking = false + @order.booking_id = params[:booking_id] + end + end # Description # This API - allow order to add new items to existing orders, does not allow you to remove confirm items # Update customer info, Guest Info diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index 4c35a918..a8560eee 100644 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -4,7 +4,7 @@ class Origami::HomeController < BaseOrigamiController def index @tables = Table.all.active.order('status desc') @rooms = Room.all.active.order('status desc') - @complete = Sale.complete_sale + @complete = Sale.all @orders = Order.all.order('date desc') end @@ -12,13 +12,13 @@ class Origami::HomeController < BaseOrigamiController def show @tables = Table.all.active.order('status desc') @rooms = Room.all.active.order('status desc') - @complete = Sale.complete_sale + @complete = Sale.all @orders = Order.all.order('date desc') @status_order = "" @status_sale = "" @sale_array = Array.new - @dining.bookings.each do |booking| - if booking.sale_id.nil? + @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| @@ -42,7 +42,7 @@ class Origami::HomeController < BaseOrigamiController @obj_sale = sale end end - end + end end private diff --git a/app/controllers/origami/orders_controller.rb b/app/controllers/origami/orders_controller.rb index e5eb2808..cc5fac3f 100644 --- a/app/controllers/origami/orders_controller.rb +++ b/app/controllers/origami/orders_controller.rb @@ -3,7 +3,7 @@ class Origami::OrdersController < BaseOrigamiController def show @tables = Table.all.active.order('status desc') @rooms = Room.all.active.order('status desc') - @complete = Sale.complete_sale + @complete = Sale.all @orders = Order.all.order('status desc') @order = Order.find(params[:order_id]) sale_order = SaleOrder.find_by_order_id(@order.order_id) diff --git a/app/controllers/origami/rooms_controller.rb b/app/controllers/origami/rooms_controller.rb index 56030d4f..1ac7f41a 100644 --- a/app/controllers/origami/rooms_controller.rb +++ b/app/controllers/origami/rooms_controller.rb @@ -3,7 +3,7 @@ class Origami::RoomsController < BaseOrigamiController def show @tables = Table.all.active.order('status desc') @rooms = Room.all.active.order('status desc') - @complete = Sale.complete_sale + @complete = Sale.all @orders = Order.all.order('date desc') @room = DiningFacility.find(params[:room_id]) @room.bookings.each do |booking| diff --git a/app/controllers/origami/sales_controller.rb b/app/controllers/origami/sales_controller.rb index b5200114..e272c1ee 100644 --- a/app/controllers/origami/sales_controller.rb +++ b/app/controllers/origami/sales_controller.rb @@ -3,8 +3,42 @@ class Origami::SalesController < BaseOrigamiController def show @tables = Table.all.active.order('status desc') @rooms = Room.all.active.order('status desc') - @complete = Sale.complete_sale + @complete = Sale.all @orders = Order.all.order('date desc') @sale = Sale.find(params[:sale_id]) end + + def add_to_existing_invoice + dining = params[:dining_id] + sale_id = params[:sale_id] + table = DiningFacility.find(dining) + table.bookings.each do |booking| + if booking.sale_id.nil? + booking.booking_orders.each do |booking_order| + booking.booking_status = 'moved' + order = Order.find(booking_order.order_id) + order.status = 'billed' + order.order_items.each do |item| + item.order_item_status = 'billed' + end + # create sale item + saleobj = Sale.find(sale_id) + order.order_items.each do |orer_item| + saleobj.add_item (orer_item) + end + saleobj.save + order.save + booking.save + end + existing_booking = Booking.find_by_sale_id(sale_id) + booking_order = BookingOrder.where('booking_id=?',booking) + booking_order.each do |bo| + bo.booking_id = existing_booking.booking_id + bo.save + end + end + end + end + + end diff --git a/app/models/booking.rb b/app/models/booking.rb index 79fbad4a..b7c6667a 100644 --- a/app/models/booking.rb +++ b/app/models/booking.rb @@ -9,6 +9,7 @@ class Booking < ApplicationRecord belongs_to :sale, :optional => true has_many :booking_orders has_many :orders, :through => :booking_orders + scope :active, -> {where("booking_status != 'moved'")} def self.update_dining_facility(booking_arr, newd, old) booking_arr.each do |booking| diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index abf39d7a..0e10307b 100644 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -18,6 +18,17 @@ class DiningFacility < ApplicationRecord self.save end + def get_booking + booking = self.get_current_booking + if booking + if booking.dining_facility_id.to_i == self.id + if booking.booking_status == 'assign' + return booking + end + end + end + 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) diff --git a/app/models/sale_payment.rb b/app/models/sale_payment.rb index e827113c..4974c856 100644 --- a/app/models/sale_payment.rb +++ b/app/models/sale_payment.rb @@ -244,9 +244,11 @@ class SalePayment < ApplicationRecord table = DiningFacility.find(booking.dining_facility_id) bookings = table.bookings bookings.each do |tablebooking| + if tablebooking.booking_status != 'moved' if tablebooking.sale.sale_status != 'completed' status = false end + end end if status table.status = "available" diff --git a/app/views/origami/home/show.html.erb b/app/views/origami/home/show.html.erb index b6398686..8ff59b45 100644 --- a/app/views/origami/home/show.html.erb +++ b/app/views/origami/home/show.html.erb @@ -137,7 +137,6 @@ count = 0 sub_total = 0 if @status_sale == "sale" - puts @sale_array[0] @sale_array[0].sale_items.each do |sale_item| count += 1 sub_total = sub_total + sale_item.price @@ -159,8 +158,6 @@ unless @order_items.nil? count = 0 @order_items.each do |order_item | - puts @order_items.size - puts "view" count += 1 sub_total = sub_total + order_item.price @@ -212,10 +209,13 @@ Pending New Order <% + count = 0 @order_items.each do |order_item | + count += 1 %> + @@ -225,14 +225,15 @@ end %>
<%= count %> <%= order_item.item_name %> <%= order_item.qty %> <%= order_item.qty*order_item.price %>
- + <% - else + end @sale_array.each do |sale| if @sale_array.size > 1 unless sale.receipt_no == @sale_array[0].receipt_no %> - Pending New Invoice +

+ Pending Payment @@ -240,7 +241,6 @@
Receipt No - <%= sale.receipt_no %>
<% - end end end end @@ -258,17 +258,23 @@ <% if @dining.bookings.length >= 1 %> - + - <% if @status_order == 'order' %> + + <% if @status_order == 'order' && @status_sale != 'sale' %> + + <% else %> + + + <% end %> - + <% end %> @@ -319,4 +325,19 @@ $('#move').on('click',function(){ $('#back').on('click',function(){ window.location.href = '/origami/'; }) + +$('#add_invoice').on('click',function(){ + var dining_id = "<%= @dining.id %>" + var sale_id = "<%= @obj_sale.sale_id rescue "" %>" + var ajax_url = "/origami/sale/append_order"; + $.ajax({ + type: "POST", + url: ajax_url, + data: 'dining_id='+ dining_id + "&sale_id=" + sale_id, + success:function(result){ + alert("Invoice updated") + window.location.reload(); + } + }); +}) diff --git a/app/views/origami/movetable/move_dining.html.erb b/app/views/origami/movetable/move_dining.html.erb index 83419ca0..fd2e78cb 100644 --- a/app/views/origami/movetable/move_dining.html.erb +++ b/app/views/origami/movetable/move_dining.html.erb @@ -180,7 +180,7 @@ if @sale_array.size > 1 unless sale.receipt_no == @sale_array[0].receipt_no %> - Pending New Invoice + Pending Payment diff --git a/app/views/origami/payments/show.html.erb b/app/views/origami/payments/show.html.erb index fe36277b..923e3eef 100644 --- a/app/views/origami/payments/show.html.erb +++ b/app/views/origami/payments/show.html.erb @@ -24,7 +24,8 @@
Receipt No - <%= sale.receipt_no %>
- + + @@ -34,10 +35,16 @@
Items#Items QTY Price
- <% sub_total = 0 %> - <% @sale_data.sale_items.each do |sale_item| %> + <% sub_total = 0 + count = 0 + %> + <% @sale_data.sale_items.each do |sale_item| + count += 1 + %> + <% sub_total += sale_item.qty*sale_item.unit_price%> + @@ -236,7 +243,6 @@ - diff --git a/app/views/origami/sales/add_to_existing_invoice.json.jbuilder b/app/views/origami/sales/add_to_existing_invoice.json.jbuilder new file mode 100644 index 00000000..08bf292c --- /dev/null +++ b/app/views/origami/sales/add_to_existing_invoice.json.jbuilder @@ -0,0 +1 @@ +json.status true diff --git a/config/routes.rb b/config/routes.rb index dd5831ae..3da4e9a1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -80,6 +80,7 @@ Rails.application.routes.draw do get 'table/:dining_id/movetable' => "movetable#move_dining" get 'table/:dining_id/moveroom' => "moveroom#move_dining" get 'sale/:sale_id' => 'sales#show' + post 'sale/append_order' => 'sales#add_to_existing_invoice' get 'room/:room_id' => 'rooms#show' get 'order/:order_id' => "orders#show" From 0c0e44c8085f0bf1899b5550d980c70e16e6b75f Mon Sep 17 00:00:00 2001 From: Nweni Date: Thu, 22 Jun 2017 16:31:41 +0630 Subject: [PATCH 2/2] move table --- Gemfile | 2 +- Gemfile.lock | 2 ++ app/controllers/origami/home_controller.rb | 15 ++++++----- .../origami/movetable_controller.rb | 2 +- app/models/booking.rb | 25 ++++++++++++++++--- app/models/customer.rb | 12 ++++----- app/models/dining_facility.rb | 2 ++ .../api/orders/view_orders.json.jbuilder | 4 +-- 8 files changed, 44 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index 22275707..227cfa72 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ gem 'rails', '~> 5.1.0' gem 'mysql2', '>= 0.3.18', '< 0.5' #Use PosgreSQL -# gem 'pg' +gem 'pg' # redis server for cable # gem 'redis', '~> 3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 5c0f8a10..e4b905ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,7 @@ GEM nokogiri (1.8.0) mini_portile2 (~> 2.2.0) pdf-core (0.7.0) + pg (0.21.0) prawn (2.2.2) pdf-core (~> 0.7.0) ttfunk (~> 1.5) @@ -260,6 +261,7 @@ DEPENDENCIES kaminari (~> 1.0.1) listen (~> 3.0.5) mysql2 (>= 0.3.18, < 0.5) + pg prawn prawn-table puma (~> 3.0) diff --git a/app/controllers/origami/home_controller.rb b/app/controllers/origami/home_controller.rb index a8560eee..13c528ab 100644 --- a/app/controllers/origami/home_controller.rb +++ b/app/controllers/origami/home_controller.rb @@ -17,22 +17,25 @@ class Origami::HomeController < BaseOrigamiController @status_order = "" @status_sale = "" @sale_array = Array.new + @dining.bookings.active.each do |booking| if booking.sale_id.nil? && booking.booking_status != 'moved' - @order_items = Array.new + @order_items = Array.new booking.booking_orders.each do |booking_order| - order = Order.find(booking_order.order_id) - @obj_order = order - @date = order.created_at - order.order_items.each do |item| - @order_items.push(item) + if (order.status == "new") + @obj_order = order + @date = order.created_at + order.order_items.each do |item| + @order_items.push(item) + end end end @status_order = 'order' else sale = Sale.find(booking.sale_id) if sale.sale_status != "completed" + puts "enter" @sale_array.push(sale) if @status_order == 'order' @status_order = 'sale' diff --git a/app/controllers/origami/movetable_controller.rb b/app/controllers/origami/movetable_controller.rb index cc6281ce..461f8694 100644 --- a/app/controllers/origami/movetable_controller.rb +++ b/app/controllers/origami/movetable_controller.rb @@ -44,7 +44,7 @@ class Origami::MovetableController < BaseOrigamiController bookings = Booking.where('dining_facility_id=?',change_from) booking_array = Array.new bookings.each do | booking | - if booking.sale_id.nil? || booking.sale.sale_status != 'completed' + if booking.sale_id.nil? || booking.sale.sale_status != 'completed' || booking.booking_status != 'moved' booking_array.push(booking) end end diff --git a/app/models/booking.rb b/app/models/booking.rb index b7c6667a..cc4ae389 100644 --- a/app/models/booking.rb +++ b/app/models/booking.rb @@ -12,10 +12,27 @@ class Booking < ApplicationRecord scope :active, -> {where("booking_status != 'moved'")} def self.update_dining_facility(booking_arr, newd, old) - booking_arr.each do |booking| - booking.dining_facility_id = newd - booking.save - end + table = DiningFacility.find(newd) + exist = table.get_booking + if exist + # order exists + booking_arr.each do |booking| + booking.dining_facility_id = newd + booking.booking_status = 'moved' + booking.save + booking.booking_orders.each do |bo| + bo.booking_id = exist.booking_id + bo.save + end + end + else + # new table + booking_arr.each do |booking| + booking.dining_facility_id = newd + booking.save + end + end + new_dining = DiningFacility.find(newd) new_dining.make_occupied old_dining = DiningFacility.find(old) diff --git a/app/models/customer.rb b/app/models/customer.rb index 4e25c9bc..e6afca08 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -6,7 +6,7 @@ class Customer < ApplicationRecord has_many :orders has_many :sales - validates_presence_of :name, :contact_no, :email,:company,:card_no + validates_presence_of :name, :contact_no, :email,:card_no validates :contact_no, uniqueness: true validates :email, uniqueness: true validates :card_no, uniqueness: true @@ -34,7 +34,7 @@ class Customer < ApplicationRecord end return response; - + end def self.get_membership_transactions(customer) @@ -58,13 +58,13 @@ class Customer < ApplicationRecord end return response; - + end def self.search(search) if search # find(:all, :conditions => ['name LIKE ? OR contact_no LIKE ?', "%#{search}%", "%#{search}%"]) - where("name LIKE ? OR contact_no LIKE ? OR card_no LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%",) + where("name LIKE ? OR contact_no LIKE ? OR card_no LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%",) else find(:all) end @@ -80,8 +80,8 @@ class Customer < ApplicationRecord end - WALKIN = "CUS-000000000001" - TAKEAWAY = "CUS-000000000002" + WALKIN = "CUS-000000000001" + TAKEAWAY = "CUS-000000000002" private def generate_custom_id diff --git a/app/models/dining_facility.rb b/app/models/dining_facility.rb index 0e10307b..f8cb639c 100644 --- a/app/models/dining_facility.rb +++ b/app/models/dining_facility.rb @@ -20,6 +20,8 @@ class DiningFacility < ApplicationRecord def get_booking booking = self.get_current_booking + puts "is bookig?" + puts booking if booking if booking.dining_facility_id.to_i == self.id if booking.booking_status == 'assign' diff --git a/app/views/api/orders/view_orders.json.jbuilder b/app/views/api/orders/view_orders.json.jbuilder index ef927af0..b2c960af 100644 --- a/app/views/api/orders/view_orders.json.jbuilder +++ b/app/views/api/orders/view_orders.json.jbuilder @@ -23,9 +23,9 @@ if (@booking) order_items = [] @booking.booking_orders.each do |bo| order = Order.find(bo.order_id) - #if (order.status == "new") + if (order.status == "new") order_items = order_items + order.order_items - #end + end end json.order_items order_items do |item|
<%= count %> <%=sale_item.product_name%>@<%=sale_item.unit_price%>