Untitled

mail@pastecode.io avatar
unknown
python
3 years ago
4.0 kB
2
Indexable
Never
from jinja2 import FileSystemLoader,Environment
import os 
import pdfkit

@router.get('/memberships/invoice/')
@access_required(["user"])                                     # admin accessible only
async def get_membership_invoice(request: Request,payload=None, user_type=None):
    try:
        
        mobile = payload.get("mobile")
        user_id = payload.get("user_id")
        membership_id = payload.get("membership_id")
        print(membership_id)
        auth_object = mydb.auth.find_one({"_id":ObjectId(user_id)})
        
        membership_object = mydb.membership.find_one({"_id":ObjectId(membership_id)})
        price = membership_object.get("price")
        price_excluding_tax = membership_object.get("price_excluding_tax")
        sap_item_code = membership_object.get("sap_item_code")
        membership_address_object = mydb.membership_address.find_one({"membership_id":membership_id,"user_id":user_id})
        warehouse_code = membership_address_object.get("warehouse_code")
        collection_object = mydb.collection_item.find_one({"number":sap_item_code})
        tax = collection_object.get("tax").get("rate")
        warehouse_array = collection_object.get("warehouses")    
        total = (price-price_excluding_tax)/2
        
        # mrp = 0
        # for warehouse_object in warehouse_array:
        #     if warehouse_object.get("code") == warehouse_code:
        #         mrp = warehouse_object.get("MRP")
        #         break
        print("11111111111111111",collection_object.get("hsn"))
        item_details={
            "s_no":1,
            "name":collection_object.get("name"),
            "hsn":collection_object.get("hsn",23456),
            "quantity_fulfilled":1,
            "mrp":999,
            "unit_cost" : "{:0.2f}".format(price_excluding_tax),
            "payable_value" : "{:0.2f}".format(price_excluding_tax),
            "tax" : tax,
            "SGST" : "{:0.2f}".format(round(total,2)),
            "CGST" : "{:0.2f}".format(round(total,2)),
            "total_value" : "{:0.2f}".format(price),
            "warehouse_code":warehouse_code
        }
        address = membership_address_object.get("house_no")\
            +","+membership_address_object.get("society")\
            +","+membership_address_object.get("state")\
            +","+membership_address_object.get("city")\
            +","+membership_address_object.get("block")
        customer_details = {
            "name":payload.get("name"),
            "mobile": mobile,
            "address":address,
            "payment_mode":auth_object.get("membership")[0].get("payment_mode"),
            "membership_created_at":auth_object.get("membership")[0].get("created_at").strftime("%a %b %d %l:%M%p"),
            "order_id": "DEERIKA"+str(mobile)
        }
        
        env = Environment(
                        loader=FileSystemLoader(
                        os.path.abspath(
                        os.path.dirname(__file__)) + '/templates', encoding='utf8'))
        temp = env.get_template('invoice.html')
        html = temp.render(picklists = item_details, customer = customer_details)
        print("--html file rendered--")
        filename = "Invoice_{}.pdf" .format("xyz")
        with open('html_op.html','w') as f:
            f.write(html)
        pdfkit.from_file("html_op.html", filename)
        # aws_access_key = config("aws_access_key")
        # aws_secret_key = config("aws_secret_key")
        # s3 = boto3.resource(
        #     service_name = "s3",
        #     region_name = "ap-south-1",
        #     aws_access_key_id = aws_access_key,
        #     aws_secret_access_key = aws_secret_key
        #     )
        # mimetype = 'application/pdf'
        # s3.Bucket(BUCKET_NAME).upload_file(filename, filename, ExtraArgs={"ContentType": mimetype})
        print("--File uploaded successfully--")
    except Exception as e:
        return {
            "message":"Success",
            "success":True
        }