def post_invoice(request, user): """ View to handle invoice related POST requests """ context = dict() invoice_id = request.POST['invoice_id'] #Call save invoice service and return corresponding template and context if request.POST['save'] == 'Save': save_invoice(request, invoice_id) template = 'invoice/invoice.html' context = { 'invoices': generate_invoice_list(request, user), 'parent_template': 'invoice/list_invoice.html', 'user_id': user.id } #Return corresponding template and context elif request.POST['save'] == 'Print': stall_id = request.POST['stall_id'] template = 'invoice/invoice.html' context = { 'invoices': generate_invoice_of_stall(request, user, stall_id), 'parent_template': 'invoice/print_invoice.html', 'user_id': user.id, 'print': True } #Call close invoice service and return corresponding template and context elif request.POST['save'] == 'Close': invoices = generate_invoice_list(request, user) if check_exists_invoice_list(invoices): error = close_invoice(request, invoice_id) template = 'invoice/invoice.html' context = { 'invoices': generate_invoice_list(request, user), 'parent_template': 'invoice/list_invoice.html', 'user_id': user.id, 'error': error } else: error = 'No invoices generated for this purchase order.' template = 'purchasing/show_summary.html' orders_list = generate_stall_orders_list(request, user.id, status__in=['New','Submitted', 'Purchasing']) context = { 'purchase_order': orders_list, 'user_id': user.id, 'error': error, 'generated': check_exists_order_list(orders_list) } return template, context
def generate_purchase_order(request, user_id): """ Service to handle the generation of purchase order. """ stall_orders = generate_stall_orders_list(request, user_id) purchase_order = Purchase_Order.objects.create() for stall in stall_orders: if stall['order_list']: for order in stall['order_list']: order_id = order['order'].id order_obj = Order.objects.filter(id=order_id, status='Submitted') for item in order['order_item']: name = item['name'].split('(')[0].rstrip() fish = Fish.objects.get(name=name) quantity = item['value'] purchase_order_item = Purchase_Order_Item.objects.filter( purchase_order=purchase_order, fish=fish) if purchase_order_item: order_item = get_or_none(Order_Item, 'filter', order=order_obj, fish=fish) if order_item: if order_item[0].status == 'Submitted': current_quantity = purchase_order_item[ 0].quantity new_quantity = current_quantity + quantity purchase_order_item.update( quantity=new_quantity) order_item.update(status='Purchasing') else: order_item = get_or_none(Order_Item, 'filter', order=order_obj, fish=fish) if order_item: if order_item[0].status == 'Submitted': purchase_order_item = Purchase_Order_Item.objects.create( purchase_order=purchase_order, fish=fish, quantity=quantity) order_item.update(status='Purchasing') order_obj.update(purchase_order=purchase_order, status='Purchasing')
def order_summary(request, user_id): """ View to handle user requests for order summary """ template = 'order/order_summary.html' context = { 'purchase_order': generate_stall_orders_list(request, user_id, status__in=[ 'New', 'Submitted', 'Purchasing', 'Invoiced', 'Closed', 'Cancelled' ]), 'user_id': user_id, 'summary': False } return render(request, template, context)
def get_invoice(request, user): """ View to handle invoice related GET requests """ stall_id = request.GET['stall_id'] if request.GET else None stall_id = None if stall_id == 'All' else stall_id invoices = generate_invoice_of_stall(request, user, stall_id) if stall_id else generate_invoice_list(request, user) #If there exists an open invoice, return list of invoices if check_exists_invoice_list(invoices): template = 'invoice/invoice.html' context = { 'invoices': invoices, 'parent_template': 'invoice/list_invoice.html', 'user_id': user.id } else: error = 'No invoices generated for this purchase order.' #If there does not exist any open invoice and user is a superuser, return list of orders if user.is_superuser: template = 'purchasing/show_summary.html' orders_list = generate_stall_orders_list(request, user.id, status__in=['New','Submitted', 'Purchasing']) context = { 'purchase_order': orders_list, 'user_id': user.id, 'error': error, 'generated': check_exists_order_list(orders_list) } #If there does not exist any open invoice and user is not a superuser, return error page in invoice page else: template = 'invoice/invoice.html' context = { 'invoices': invoices, 'parent_template': 'invoice/list_invoice.html', 'user_id': user.id, 'error': error } return template, context
def get_purchase_order(request, user_id): """ View to handle purchase order related user GET requests """ User = get_user_model() user = User.objects.get(id=user_id) request_type = request.path.split('/')[3] purchase_order = get_orders( Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() if purchase_order: #Return purchase order summary report if request_type == 'summary': orders_list = generate_stall_orders_list( request, user_id, status__in=['New', 'Submitted', 'Purchasing']) error = None if check_exists_order_list( orders_list) else 'No orders have been created.' template = 'purchasing/show_summary.html' context = { 'purchase_order': orders_list, 'purchase_order_id': purchase_order[0]['purchase_order'], 'user_id': user_id, 'generated': True, 'error': error } elif request_type == 'stall': stall_name = request.GET['stall_name'] if request.GET else None if stall_name: #Return purchase order report by stall for all stalls if stall_name == 'All': template = 'purchasing/list_stall.html' context = { 'purchase_order': generate_po_by_stall(purchase_order), 'user_id': user_id, 'generated': True } #Return purchase order report by stall for specific stall else: stall = Stall.objects.get(description=stall_name) template = 'purchasing/get_stall.html' context = { 'purchase_order': generate_po_by_stall(purchase_order, stall), 'user_id': user_id, 'generated': True } #Return purchase order report by stall for all stalls else: template = 'purchasing/list_stall.html' context = { 'purchase_order': generate_po_by_stall(purchase_order), 'user_id': user_id, 'generated': True } #Return purchase order report by fish elif request_type == 'fish': template = 'purchasing/list_fish.html' context = { 'purchase_order': generate_po_by_fish(purchase_order), 'user_id': user_id, 'generated': True } elif request_type == 'add': template = 'purchasing/list_fish.html' context = { 'purchase_order': generate_po_by_fish(purchase_order), 'user_id': user_id, 'generated': True } #Show purchase order summary if there is no purchase orders. Reflect error message if there are no open orders. else: orders_list = generate_stall_orders_list( request, user_id, status__in=['New', 'Submitted', 'Purchasing']) error = None if check_exists_order_list( orders_list) else 'No orders have been created.' template = 'purchasing/show_summary.html' context = { 'purchase_order': orders_list, 'user_id': user_id, 'generated': False, 'error': error } return template, context
def save(request, user_id, request_type, purchase_order): """ Method to handle the save purchase order requests """ #Call save purchase order by fish service if request_type == 'fish': template = 'purchasing/list_fish.html' purchase_order_item_id = request.POST['purchase_order_item_id'] weight = request.POST['weight'] cost = request.POST['cost'] save_purchased_fish(purchase_order, purchase_order_item_id, weight, cost) purchase_order = generate_po_by_fish(purchase_order) #Call save purchase order by stall service elif request_type == 'stall': stall_name = request.POST[ 'stall_name'] if 'stall_name' in request.POST else None order_item_id = request.POST['order_item_id'] weight = request.POST['weight'] cost = request.POST['cost'] save_stall(purchase_order, order_item_id, weight, cost) if stall_name: #Return template and context for purchase order for all stalls if stall_name == 'All': template = 'purchasing/list_stall.html' purchase_order = generate_po_by_stall(purchase_order) #Return template and context for purchase order for specific stalls else: stall = Stall.objects.get(description=stall_name) template = 'purchasing/get_stall.html' purchase_order = generate_po_by_stall(purchase_order, stall) #Return template and context for purchase order for all stalls else: template = 'purchasing/list_stall.html' purchase_order = generate_po_by_stall(purchase_order) #Call save purchase order using the purchasing report elif request_type == 'purchasing': purchase_order = get_orders( Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() fish_id = request.path.split('/')[4] save_distributed_fish(request, purchase_order[0]['purchase_order'], fish_id) stall, purchase_order = generate_purchasing_list(purchase_order) template = 'purchasing/purchasing.html' context = {'status': 'success'} return template, context #Return purchase order summary by default else: template = 'purchasing/summary.html' purchase_order = generate_stall_orders_list(request, user_id, 'today') context = { 'purchase_order': purchase_order, 'user_id': user_id, 'generated': True } return template, context
def post_purchase_order(request, user_id, fish_id=None): """ View to handle purchase order related user POST requests """ User = get_user_model() user = User.objects.get(id=user_id) #Call generate purchase order service and return corresponding template and context if request.POST['save'] == 'Generate Purchase Order': generate_purchase_order(request, user_id) template = 'purchasing/show_summary.html' context = { 'purchase_order': generate_stall_orders_list( request, user_id, status__in=['New', 'Submitted', 'Purchasing']), 'user_id': user_id, 'generated': True } #Call generate invoice service and return corresponding template and context elif request.POST['save'] == 'Generate Invoice': #Code below is remove as generate invoice will be done by order instead of by bulk #purchase_order = get_orders(Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() order_id = request.POST['order_id'] stall_id = request.POST['stall_id'] generate_invoice(order_id=order_id) template = 'invoice/invoice.html' context = { 'invoices': generate_invoice_list(request, stall_id=stall_id), 'parent_template': 'invoice/list_invoice.html', 'user_id': user.id } #Call save purchase order method and return corresponding template and context elif request.POST['save'] == 'Save': request_type = request.path.split('/')[3] purchase_order = get_orders( Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() template, context = save(request, user_id, request_type, purchase_order) #Call save purchase order method and return corresponding template and context. Request Type should be 'Add' elif request.POST['save'] == 'Add': request_type = request.path.split('/')[3] purchase_order = get_orders( Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() template, context = save(request, user_id, request_type, purchase_order) #Show purchase order summary if there is no purchase orders else: template = 'purchasing/show_summary.html' context = { 'purchase_order': generate_stall_orders_list( request, user_id, status__in=['New', 'Submitted', 'Purchasing']), 'user_id': user_id, 'generated': False } return template, context
def get_order(request, user_id, isAll, order_id=None, stall_id=None): """ View to handle order related user GET requests """ User = get_user_model() user = User.objects.get(id=user_id) isSuperuser = request.user.is_superuser if isSuperuser: #Return specific order view if order_id: order = generate_order_list(id=order_id)[0] template = 'order/get_order.html' context = { 'stall': order['order'].stall, 'order': order, 'user_id': user_id } else: #Return list of orders for the specific stall view if stall_id: template = 'order/order_summary.html' context = { 'purchase_order': generate_order_list_for_stall(request, stall_id, status__in=[ 'New', 'Submitted', 'Purchasing', 'Invoiced', 'Closed', 'Cancelled' ]), 'user_id': user_id, 'summary': True } #Return list of orders for all stall view else: stall_orders = generate_stall_orders_list( request, user, status__in=['New', 'Submitted', 'Purchasing']) template = 'order/list_order_all.html' context = { 'stall_orders': stall_orders, 'fish': Fish.objects.all().order_by('sequence'), 'user_id': user_id } else: stall = Stall.objects.get(name=user) #Return list of orders for stalls assigned to the same user view if isAll: orders = generate_order_list(stall=stall) template = 'order/list_order_by_stall.html' context = {'stall': stall, 'orders': orders, 'user_id': user_id} else: order = generate_order_list(stall=stall, id=order_id)[0] #Return specific order view if order: template = 'order/get_order.html' context = {'stall': stall, 'order': order, 'user_id': user_id} #Return new order view else: template = 'order/new_order.html' context = { 'stall': stall, 'fish': Fish.objects.all().order_by('sequence'), 'user_id': user_id } return template, context