The application allows the customer to place new orders and accept orders by the contractor as well as generate reservations, i.e. manage delivery time slots to central warehouses and logistics centers.
The source code for the application is available here.
You can try the app here.
In this post, I will introduce the new_booking(id) function which handles the booking for a specific order (contract). It takes the contract identification number as a parameter, i.e.
@bp.route('/booking/<int:id>', methods=['GET', 'POST'])
@login_required
def new_booking(id):
Then, in the new_booking () function, I create a booking form and get the booking object for a given contract, i.e.
form = BookingForm()
result = Booking.query.filter_by(contract_id=id).first()
Next, I set the contract object for the booking, i.e.
current_contract = Contract.query.get(id) # Returns contract for current booking
In the following lines I get a list of orders that were generated for a specific warehouse on the day of delivery, i.e.
# Filtering by one column gives a list of tuple(s) so I converted it to a list of values
contracts = [ids[0] for ids in Contract.query.with_entities(Contract.id).filter_by(
date_of_delivery=current_contract.date_of_delivery).filter_by(
warehouse=current_contract.warehouse).all()]
Then I validate if the form has been submitted. Depending on whether the reservation for a given contract has already been created, I update the data in the database based on the data from the form. If the booking object does not exist, I create a new booking object and save it to the database, i.e.
if form.validate_on_submit():
if result:
result.booking_time = form.booking_time.data
result.driver_full_name = form.driver_full_name.data
result.driver_phone_number = form.driver_phone_number.data
result.truck_reg_number = form.truck_reg_number.data
db.session.commit()
else:
booking = Booking(booking_time=form.booking_time.data,
contract_id = id,
driver_full_name=form.driver_full_name.data,
driver_phone_number=form.driver_phone_number.data,
truck_reg_number=form.truck_reg_number.data)
db.session.add(booking)
db.session.commit()
Next, I change the order status to accepted and redirect to the function displaying all orders for a given contractor, i.e.
contract = Contract.query.get(id)
contract.status = 'accepted'
db.session.commit()
page = session.get('page')
per_page = session.get('per_page')
return redirect(url_for('contracts.contracts', page=page, per_page=per_page))
If the page is loaded using the GET method, then depending on whether the booking is already present in the system or is being created, the data is completed in the form, i.e. if the booking is available and we open it, e.g. to edit the data, then the form will be completed with the previous one entered data. However, if the reservation is just being created, the form will contain a modification, the dates of which cannot be selected, because they have already been selected by other suppliers, i.e.
if result is not None:
reserved_booking_time = [times[0] for times in
Booking.query.with_entities(Booking.booking_time).filter(
Booking.contract_id.in_(contracts)).all() if times[0]!=result.booking_time]
form.booking_time.data = result.booking_time
form.driver_full_name.data = result.driver_full_name
form.driver_phone_number.data = result.driver_phone_number
form.truck_reg_number.data = result.truck_reg_number
else:
reserved_booking_time = [times[0] for times in
Booking.query.with_entities(Booking.booking_time).filter(
Booking.contract_id.in_(contracts)).all()]
return render_template('booking.html', form=form, reserved_booking_time=reserved_booking_time)
The complete code for the new_booking() function is available here.
A contract accepted in this way can only be edited by the customer. However, if the contract has already been accepted and the booking has been made, the change of the contract changes the status of the order from accepted back to open and requires the contractor to accept the changes again (but the entered data are present and the reservation is does not require data to be repeated).
The contractor may also download a pdf of the booking if the contract is accepted (for open contract the option to download pdf is inactive).
