diff --git a/app/controllers/origami/split_bill_controller.rb b/app/controllers/origami/split_bill_controller.rb index 96caf298..e1432951 100644 --- a/app/controllers/origami/split_bill_controller.rb +++ b/app/controllers/origami/split_bill_controller.rb @@ -6,21 +6,31 @@ class Origami::SplitBillController < BaseOrigamiController @table = DiningFacility.find(dining_id) @booking = @table.get_booking @order_items = Array.new + @sale_data = Array.new + + table_bookings = Booking.where("dining_facility_id = #{dining_id} and sale_id IS NOT NULL") + if !table_bookings.nil? + table_bookings.each do |table_booking| + @sale_data.push(table_booking.sale) + end + end + if @booking - @booking.booking_orders.each do |booking_order| - @order = Order.find(booking_order.order_id) - if (@order.status == "new") - @order.order_items.each do |item| - @order_items.push(item) - end - end - end + @booking.booking_orders.each do |booking_order| + @order = Order.find(booking_order.order_id) + if (@order.status == "new") + @order.order_items.each do |item| + @order_items.push(item) + end + end + end else @booking = nil end end def create + order_ids = params[:order_ids] order_items = JSON.parse(params[:order_items]) # byebug status = false @@ -59,36 +69,65 @@ class Origami::SplitBillController < BaseOrigamiController :checkin_at => Time.now.utc, :checkin_by => current_user.name, :booking_status => "assign" }) - customer = Customer.find(params[:customer_id]) - order_type = "dine_in" - if !customer.nil? - if customer.customer_type == "Takeaway" - order_type = "takeaway" - elsif customer.customer_type == "Delivery" - order_type = "delivery" + order_item_count = 0 + order_id = nil + + order_items.each do |order_item| + order_item_count = 0 + order = Order.find(order_item["order_id"]) + if order.order_items.count == 1 + order_id = order.id + break + else + order_item_count += 1 end end - # begin - order = Order.new - order.date = Time.now.utc - order.source = "cashier" - order.order_type = order_type - order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile - order.item_count = order_items.count - order.status = "new" - order.table_id = params[:dining_id] # this is dining facilities's id - order.waiters = current_user.name - order.employee_name = current_user.name - order.guest_info = nil - order.save - BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id}) + if !order_id.nil? + BookingOrder.find_by_order_id(order_id).delete - order_items.each do |order_item| - orderItem = OrderItem.find(order_item["id"]) - orderItem.order_id = order.order_id - orderItem.save - end + BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_id}) + order_items.each do |order_item| + orderItem = OrderItem.find(order_item["id"]) + orderItem.order_id = order_id + orderItem.save! + end + else + if order_ids.count == 1 && order_item_count == 1 + BookingOrder.create({:booking_id => booking.booking_id, :order_id => order_ids[0]}) + else + customer = Customer.find(params[:customer_id]) + order_type = "dine_in" + if !customer.nil? + if customer.customer_type == "Takeaway" + order_type = "takeaway" + elsif customer.customer_type == "Delivery" + order_type = "delivery" + end + end + + # begin + order = Order.new + order.source = "cashier" + order.order_type = order_type + order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile + order.item_count = order_items.count + order.status = "new" + order.table_id = params[:dining_id] # this is dining facilities's id + order.waiters = current_user.name + order.employee_name = current_user.name + order.guest_info = nil + order.save! + + BookingOrder.create({:booking_id => booking.booking_id, :order_id => order.order_id}) + + order_items.each do |order_item| + orderItem = OrderItem.find(order_item["id"]) + orderItem.order_id = order.order_id + orderItem.save! + end + end + end sale = Sale.new status, sale_id = sale.generate_invoice_from_booking(booking.booking_id, current_user, get_cashier_by_terminal) diff --git a/app/views/origami/orders/show.html.erb b/app/views/origami/orders/show.html.erb index 70e1ba9d..6e8163ed 100755 --- a/app/views/origami/orders/show.html.erb +++ b/app/views/origami/orders/show.html.erb @@ -194,16 +194,14 @@
-
+
-
+
- -

Order No: <%=@order.order_id rescue ' '%>

-
-
-

Date: <%=@order.created_at.strftime("%d/%m/%Y - %I:%M %p") rescue '-'%>

+ <% if !@order.nil? %> + + <% end %> +

Order Lists

@@ -40,6 +39,7 @@ %> > + <%=order_item.item_name%> @@ -69,16 +69,42 @@
-
+
+
+
+
+

Receipt Lists

+
+
+
+ <% if !@sale_data.nil? %> + + + <% @sale_data.each do |sale_data| %> + > + + + <% end %> + +
Receipt No. - <%= sale_data.receipt_no %>
+ <% end %> +
+
+ +
+
+
+ +
-
+
@@ -142,7 +168,7 @@ var order_items = get_selected_order_items();// Selected Order Items var cnt_items = parseInt(cnt_order_item) - parseInt(order_items.length); if (order_items.length > 0){ - if(cnt_items > 0){ + // if(cnt_items > 0){ swal({ title: "Alert", text: "Are you sure, you want to Split?", @@ -156,14 +182,21 @@ splitBillProcess(cnt_items); } }); - } - else{ - splitBillProcess(cnt_items); - } + // } + // else{ + // splitBillProcess(cnt_items); + // } }else{ swal("Opps","Please select at least on item!","warning"); } }); + + //selected receipt row click + $('.receipt_row').on('click',function(){ + var dining_id = $("#table_id").text(); + var sale_id = $(this).attr('id').substr(0,16); + window.location = '/origami/table/' + dining_id + "/table_invoice/" + sale_id; + }); }); /* function for split bill process */ @@ -177,23 +210,32 @@ function splitBillProcess(cnt_items){ if(cnt_items == 0){ booking_id = json_booking.booking_id; } - + + var order_ids = []; + if(order_items!=undefined && order_items!=null){ + $.each(order_items,function(key,value){ + if($.inArray(value.order_id,order_ids) == -1){ + order_ids.push(value.order_id); + } + }); + } + var ajax_url = "/origami/split_bills"; $.ajax({ type: "POST", url: ajax_url, dataType: 'JSON', - data: {'dining_id' : dining_id, 'type': type, 'customer_id' : customer_id, 'booking_id' : booking_id, 'order_items' : JSON.stringify(order_items)}, + data: {'dining_id' : dining_id, 'type': type, 'customer_id' : customer_id, 'booking_id' : booking_id, 'order_ids' : order_ids, 'order_items' : JSON.stringify(order_items)}, success: function (result) { if (!result.status) { swal("Information!", result.error_message); } else { - if (cnt_items > 0){ + // if (cnt_items > 0){ window.location.reload(); - }else{ - backToOrigami(); - } + // }else{ + // backToOrigami(); + // } } } }); @@ -216,6 +258,7 @@ function get_selected_order_items(){ $('.item-row.selected-item').each(function(i){ var order_item = {}; order_item.id = $(this).attr('id').substr(0,16); + order_item.order_id = $(this).find('#item-order-id').text().trim(); order_item.name = $(this).find('#item-name-price').text().split('@')[0]; order_item.account_id = $(this).find('#item-account-type').text(); order_item.price = $(this).find('#item-total-price').text();