def print_purchasing_report(request, user_id): """ View to handle user request to print the purchasing report. Returns the print template and context """ User = get_user_model() user = User.objects.get(id=user_id) purchase_order = get_orders( Order, day=None, status__in=['Purchasing']).values('purchase_order').distinct() purchase_order_id = purchase_order[0]['purchase_order'] stall, purchase_order = generate_purchasing_list(purchase_order) template = 'purchasing/print_purchasing.html' context = { 'purchase_order': purchase_order, 'purchase_order_id': purchase_order_id, 'stall': stall, 'user_id': user_id, 'generated': 'True' } return render(request, template, context)
def generate_invoice_list(request, user=None, stall_id=None): """ Service to handle generation of invoice list for all stalls or for a specific stall. Returns a list of invoices """ stall_invoice_list = list() if stall_id: stalls = Stall.objects.filter(id=stall_id) else: if user: stalls = Stall.objects.filter(name=user) if user.is_superuser: stalls = Stall.objects.all() else: stalls = Stall.objects.all() for stall in stalls: stall_invoice = dict() stall_invoice['stall'] = stall order = get_orders(Order, day=None, stall=stall, status='Invoiced') invoice = Invoice.objects.filter(stall=stall, order=order) stall_invoice['status'] = order[0].status if order else None stall_invoice['invoice'] = invoice[0] if invoice else None stall_invoice['invoice_item'] = generate_invoice_item_list(invoice=invoice) stall_invoice_list.append(stall_invoice) return stall_invoice_list
def generate_order_list(day=None, *args, **kwargs): """ Service to handle the generation of order list. Returns a list of orders """ #Get the list of orders from the stall order_list = list() order = get_orders(Order, day, *args, **kwargs) #If the stall has an order if order: for o in order: order_dict = dict() order_dict['order'] = o #Get the list of order items for an order order_dict['order_item'] = generate_order_item_list(Order_Item, order=o) order_list.append(order_dict) return order_list
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 generate_purchasing_list(purchase_order): """ Service to generate the purchasing report. Returns a list summary detailing the entire purchase order """ purchase_order_item = Purchase_Order_Item.objects.filter( purchase_order=purchase_order) stall = Stall.objects.all() fish_list = list() for poi in purchase_order_item: #First list contains the fish name, chinese name, and the sequence fish_dict = dict() fish_dict['id'] = poi.fish.id fish_dict['name'] = poi.fish.name fish_dict['chinese_name'] = poi.fish.chinese_name fish_dict['sequence'] = poi.fish.sequence values_list = list() quantity = 0 weight = 0 for s in stall: order = get_orders(Order, None, purchase_order=purchase_order, stall=s, status__in=['Purchasing']) #Get the stall id values_dict = dict() values_dict['stall'] = s.id #If an order exists for the stall, get the order details for the specific fish if order: order_item = Order_Item.objects.filter(order=order, fish=poi.fish) values_dict['order_item'] = order_item[0].id if order_item[ 0].id else None values_dict['quantity'] = order_item[0].quantity if order_item[ 0].quantity else 0 values_dict['weight'] = order_item[0].weight if order_item[ 0].weight else 0 values_dict[ 'cost'] = order_item[0].cost if order_item[0].cost else 0 #If there is no order for the stall, mark as 0. else: values_dict['order_item'] = None values_dict['quantity'] = 0 values_dict['weight'] = 0 values_dict['cost'] = 0 #Compute for total quantity and weight for totals column in the report quantity = quantity + values_dict['quantity'] weight = weight + values_dict['weight'] values_list.append(values_dict) fish_dict['values'] = values_list total_dict = dict() total_dict['quantity'] = quantity total_dict['weight'] = weight total_dict['cost'] = poi.selling_cost if poi.selling_cost else 0 fish_dict['total'] = total_dict fish_list.append(fish_dict) #Sort the list by fish sequence fish_list = sorted(fish_list, key=itemgetter('sequence')) return stall, fish_list