1

So basically I am in the project for creating a kiosk application for a canteen.

In general, this is the flow of the kiosk application that I want:

  1. The user selects the food/drinks.
  2. The user enters his table number.
  3. The kiosk checks the stock of the user's foods/drinks. If it is available, proceed otherwise cancel it.
  4. The kiosk books the items for preventing other kiosk application ordering that items since the items are limited (for example only 50 units). The booking will expire at a certain time. (For example, the user forgets to bring his debit card so he will leave the kiosk).
  5. The user makes payment.
  6. The kiosk POST the order to the web server. The order will not be posted to the web server before the payment is made successfully (There is a chance that it will be failed).
  7. Order is done. The kiosk print the receipt.

This is my current model:

Menu Item

class Menu(models.Model):
    image = models.ImageField(upload_to=path_and_rename)
    name = models.CharField(max_length=100)
    price = models.IntegerField()
    category = models.IntegerField()  //Food or Drink
    availability = models.BooleanField(default=False)
    qty = models.IntegerField(default=100) //It will reach 0 when sold out.
    sellerID = models.ForeignKey(Seller, on_delete=models.PROTECT)

Order

class Order(models.Model):
    cardID = models.IntegerField()
    amount = models.IntegerField()
    time = models.DateTimeField(auto_now_add=True)

Order Detail

class OrderDetail(models.Model):
    orderID = models.ForeignKey(Order, on_delete=models.PROTECT)
    menuID = models.ForeignKey(Menu, on_delete=models.PROTECT)
    price = models.IntegerField()
    qty = models.IntegerField()
    tableNumber = models.IntegerField()
    done = models.BooleanField(default=False)
    finishTime = models.DateTimeField(null=True, blank=True)
    sellerID = models.ForeignKey(Seller, on_delete=models.PROTECT)

For full of the code of the webserver is here at my github: here.

Why I need a booking system? Because before making a payment, the user must be sure that he will get the order (not out of stock). In the canteen, there is more than one kiosk machine. Don't let someone's food/drink stock be taken by the other on another machine.

Therefore, I have no idea to build the Booking part. I have no idea how to make the booking will expire at a certain time. If not, are there any other methods to prevent the problem that I have mentioned in the previous paragraph?

I think it is similar to the airline ticket booking system. For example, there is only a single ticket left. John books that flight then John will be given a certain time to make the payment. When John has made the payment, John will get the ticket. But when there is another person, let's say Alex. Alex try to book that flight, but since it is already been booked by John, Alex can't make it. (In Alex's screen, that ticket has been sold).

1 Answer 1

1

You could approach this problem by setting the "availability" Boolean field of your menu items to false once you've created and order including the item (or by adding a new Boolean field for this purpose, e.g. "reserved"). The workflow would be, for example:

  • create an order;
  • set item availability to false;
  • run a separate thread to monitor if the order has been completed;
  • complete the order (or not);
  • when the reservation thread time expires and the order hasn't been completed, set the the menu item's availability value to true so that it can be ordered again;

Sample views.py:

import threading
import time

def order_check(order_detail):
    time.sleep(1000)  # waiting time before checking the order;
    if not order_detail.done:
        order_detail.menu.availability = True
        order_detail.menu.save()

def reserve_order_view(request, order_detail_pk):
    order_detail = OrderDetail.objects.get(pk=order_detail_pk)
    order_detail.menu.availability = False
    order_detail.menu.save()
    process_order_thread = threading.Thread(target=order_check, args=(order_detail, ))
    process_order_thread.start()  # start your monitoring thread and continue;

def complete_order_view(request, order_detail_pk):
    order_detail = OrderDetail.objects.get(pk=order_detail_pk)
    order_detail.done = True
    order_detail.save()
    # ... etc...

EDIT: reserving based on quantity

In this case, you can reserve menu items based on their quantity number with a slight adjustment in the methods:

def order_check(order_detail, item_qty_ordered):
    time.sleep(1000)  # waiting time before checking the order;
    if not order_detail.done:
        order_detail.menu.qty += item_qty_ordered # the order wasn't completed, so add back the menu item quantity;
        order_detail.menu.save()

def reserve_order_view(request, order_detail_pk, item_qty_ordered=1):
    order_detail = OrderDetail.objects.get(pk=order_detail_pk)
    order_detail.menu.qty -= item_qty_ordered # the user is ordering some quantity of the menu item;
    order_detail.menu.save()
    process_order_thread = threading.Thread(target=order_check, kwargs={'order_detail_pk': order_detail_pk, 'item_qty_ordered': item_qty_ordered})
    process_order_thread.start()  # start your monitoring thread and continue;

This is just to illustrate the workflow. In your methods, you should also check and prevent orders of menu items that aren't available (qty=0).

6
  • Thank you for your answer. Therefore, this approach will block a menu item, right? For example, There is a Hotdog with qty=2. There are Alex and John as the user. So basically if Alex orders a hotdog (qty=1) then John can't order the hotdog (qty=1) before Alex has finished his order and payment? Actually, I just want to make a system that Alex and John can order a single hotdog. When they made the payment process, other people trying to order the hotdog will be rejected because of the hotdog qty is 0 (being reserved by Alex and John). May 15, 2019 at 4:06
  • That makes sense. I've updated my answer to consider reserving menu items based on quantity. Let me know if it works for you. May 15, 2019 at 7:27
  • okay i get your idea. so we never delete the data of failed booking (no payment is made) ? BTW, for the order detail I will do POST request it as bulk(list), so how do I modify your code) ? May 16, 2019 at 3:40
  • You can delete the order detail it if you want, for example in the order_check method after the time has expired and the order hasn't been completed (that is only if you don't want to have a history of past orders). As for your POST request, please clarify what data you're including in the request and what arguments your view method receives so that I can edit my code. May 16, 2019 at 3:52
  • 1
    @ShalahuddinAl-Ayyubbi your POST data is a list of dictionaries that contain all the data you need, so you can just loop over it and query the order details, e.g. for data_dict in request.data: order_detail = OrderDetail.objects.get(id=int(data_dict['orderID'])). Anything beyond that is already explained in my answer. May 17, 2019 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.