Пример #1
0
def purchaseorderdetails(request):
    context = {}

    po_id = request.POST['purchase_order_id']
    quotation_id = request.POST['quotation_id']
    shipping_inst = request.POST['shipping_inst']

    vendor_id = request.POST['vendor_id']
    description = request.POST['description']

    staff_id = request.user.id 
    staff = Person.objects.get(user_id = staff_id)
    quotation = Quotation.objects.get(quotation_id = quotation_id)
    vendor_info = Vendor.objects.get(vendor_id = vendor_id)

    responses = request.read()
    print(responses)
   
    q= QueryDict(responses)
    
    items_id = q.getlist('item_id')
    print(items_id)
    items_name = q.getlist('item_name')
    print(items_name)
    items_quantity = q.getlist('quantity')
    print(items_quantity)
    items_unit_price = q.getlist('unit_price')
    print(items_unit_price)
    items_total_price = q.getlist('total_price')
    print(items_total_price)


    items = list()

    i = 0
    items_length = len(items_id)
    grand_total = Decimal(0)

    while i < items_length:
        total= Decimal(items_quantity[i]) * Decimal(items_unit_price[i])
        item_table = {
            'item_name': items_name[i],
            'item_id': items_id[i],
            'quantity' : items_quantity[i],
            'unit_price': items_unit_price[i],
            'total_price': total
        }
        items.append(item_table)
        i = i + 1
        grand_total = grand_total + total
    print(items)

 

    # push the Purchase Order data to the database 
    current_time = datetime.datetime.now() 
    print(current_time)
    po_info = PurchaseOrder(purchase_order_id = po_id, 
                            shipping_instructions = shipping_inst, 
                            time_created = current_time,
                            total_price = grand_total, 
                            person_id = staff,
                            description = description,
                            vendor_id = vendor_info, 
                            quotation_id = quotation)
    po_info.save()

    # push the Purchase Order item data to the database
    purchase_order = PurchaseOrder.objects.get(purchase_order_id = po_id)
    for item in items:
        item_info = Item.objects.get(item_id = item['item_id'])
        po_item_info = PurchaseOrderItem(purchase_order_id = purchase_order, 
                                         item_id = item_info, 
                                         quantity = item['quantity'], 
                                         unit_price = item['unit_price'],
                                         total_price = item['total_price'])
        po_item_info.save()

    # update status Quotation
    q = Quotation.objects.get(quotation_id = quotation_id)
    q.status = 'APPROVED'
    q.save()

    #sending email to vendor
    x = PrettyTable()

    x.field_names = ["Item ID","Item Name","Quantity","Unit Price","Total Price"]

    for item in items:
        x.add_row([item['item_id'],item['item_name'],item['quantity'],item['unit_price'],item['total_price']])

    subject = 'PURCHASE ORDER INFORMATION: '+ po_id
    message = 'This is the Purchase Order Information: \n'+'Person In Charge: '+staff.person_name+'\n'+'Ship to:'+staff.person_address+ '\n' +'Purchase Order Number: ' + po_id + '\n'+'Quotation ID: ' + quotation.quotation_id + '\n'+'Time Issued: ' + str(current_time) + '\n'+'Vendor ID: ' + vendor_id + '\n'+'Description: ' + description + '\n'+'Shipping Instructions: ' + shipping_inst + '\n'+ str(x) +'\n'

    email_from = settings.EMAIL_HOST_USER
    recipient_list = [vendor_info.vendor_email,]
    send_mail( subject, message, email_from, recipient_list )

    # info pass to html
    context = {
            'title': 'Purchase Order Details',
            'quotation_id' : quotation_id,
            'purchase_order_id' : po_id,
            'vendor_id' : vendor_id,
            'shipping_inst' : shipping_inst,
            'rows' : items,
            'staff' : staff,
            'vendor_info' : vendor_info,
            'grand_total': grand_total,
            'time_created': current_time,
            'description' : description
        }

    return render(request,'PurchaseOrder/purchaseorderdetails.html',context)