Example #1
0
def create_certificate_of_origin(transaction_executor, product_id):
    product_name = get_value_from_documentid(transaction_executor,
                                             Constants.PRODUCT_TABLE_NAME,
                                             product_id, "ProductName")
    product_hs_tarriff = get_value_from_documentid(
        transaction_executor, Constants.PRODUCT_TABLE_NAME, product_id,
        "ProductHSTarriffNumber")
    manufacturer_id = get_value_from_documentid(transaction_executor,
                                                Constants.PRODUCT_TABLE_NAME,
                                                product_id, "ManufacturerId")
    manufacturer_name = get_value_from_documentid(
        transaction_executor, Constants.SCENTITY_TABLE_NAME,
        manufacturer_id[0], "ScEntityName")

    certificate_of_origin = {
        'CertificateOfOriginNumber':
        get_index_number(transaction_executor,
                         Constants.CERTIFICATE_OF_ORIGIN_TABLE_NAME,
                         "CertificateOfOriginNumber"),
        'ProductName':
        product_name[0],
        'Productid':
        product_id,
        'ProductHSTarriffNumber':
        product_hs_tarriff[0],
        'ManufacturerId':
        manufacturer_id[0],
        'ManufacturerName':
        manufacturer_name[0],
        'ManufacturerLocation':
        get_scentity_contact(transaction_executor, manufacturer_id[0],
                             "Address")[0],
        'ExportApproval': {
            "ApproverId": "",
            "isApprovedByCustoms": False
        },
        'ImportApproval': {
            "ApproverId": "",
            "isApprovedByCustoms": False
        }
    }

    certificate_of_origin_id = insert_documents(
        transaction_executor, Constants.CERTIFICATE_OF_ORIGIN_TABLE_NAME,
        convert_object_to_ion(certificate_of_origin))
    # logger.info("Certificate of Origin Created successfully!")
    return certificate_of_origin_id
Example #2
0
def create_pick_up_request(transaction_executor, requestor_sc_entity_id,
                           carrier_company_id, purchase_order_id,
                           transport_type):
    request_number = get_index_number(transaction_executor,
                                      Constants.PICK_UP_REQUESTS_TABLE,
                                      Constants.PICK_UP_REQUESTS_INDEX_NAME)
    pick_up_location = get_scentity_contact(transaction_executor,
                                            requestor_sc_entity_id, "Address")
    pick_up_request = {
        "PickUpRequestNumber": request_number,
        "PickUpLocation": pick_up_location,
        "RequestorId": requestor_sc_entity_id,
        "CarrierCompanyId": carrier_company_id,
        "PurchaseOrderId": purchase_order_id,
        "isAccepted": False,
        "TransportType": transport_type
    }

    pick_up_request_id = insert_documents(transaction_executor,
                                          Constants.PICK_UP_REQUESTS_TABLE,
                                          pick_up_request)
    # logger.info("pick_up_request was created :{}".format(pick_up_request_id))
    return pick_up_request_id
Example #3
0
def pick_up_order(transaction_executor,pick_up_request_id,truck_carrier_person_id, freight_carrier_id, export_location_id,import_location_id):
    if document_exist(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE,pick_up_request_id):
        update_document(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE,"isAccepted",pick_up_request_id,True)
        purchase_order_id = get_value_from_documentid(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE, pick_up_request_id, "PurchaseOrderId")
        purchase_order_id = purchase_order_id[0]
        container_ids = get_value_from_documentid(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id,"HighestPackagingLevelIds")
        carrier_company_id = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_ids[0][0],"CarrierCompanyId")

        actual_sc_entity_id = get_scentityid_from_personid(transaction_executor,truck_carrier_person_id)

        if carrier_company_id[0]== actual_sc_entity_id:

            location_ids = [export_location_id,import_location_id]
            scentity_type_code = list(map(lambda x: get_value_from_documentid(transaction_executor,Constants.SCENTITY_TABLE_NAME,x,"ScEntityTypeCode"),location_ids))

            # print(scentity_type_code)
            if ["3"] not in scentity_type_code[0] and ["4"] not in scentity_type_code[0] and scentity_type_code[0][0] != scentity_type_code[1][0]:
                raise Exception("import and export locations  can only be airports or sea ports")
            else:
                # logger.info("Authorized!")

                if get_document_superadmin_approval_status(transaction_executor,Constants.SCENTITY_TABLE_NAME,freight_carrier_id):
                    product_id = get_value_from_documentid(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id,"ProductId")
                    product_id = product_id[0]
                    manufacturer_id = get_value_from_documentid(transaction_executor,Constants.PRODUCT_TABLE_NAME,product_id,"ManufacturerId")
                    manufacturer_name = get_value_from_documentid(transaction_executor,Constants.SCENTITY_TABLE_NAME,manufacturer_id[0],"ScEntityName")
                    pick_up_location = get_scentity_contact(transaction_executor,manufacturer_id[0],"Address")
                    delivery_location = get_scentity_contact(transaction_executor,export_location_id,"Address")
                    # logger.info("Pickup location is : {}".format(pick_up_location))
                    airway_bill_ids = []
                    bill_of_lading_ids = []
                    lorry_reciept_ids = []
                    for container_id in container_ids[0]:
                        isPicked = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"isPicked")
                        if isPicked[0] == 0:
                            lorry_reciept_id = create_lorry_reciept(transaction_executor,actual_sc_entity_id,truck_carrier_person_id,pick_up_location[0],delivery_location[0],manufacturer_id[0],manufacturer_name[0],True)
                            lorry_reciept_ids.append(lorry_reciept_id[0])
                            update_document_in_container(transaction_executor,container_id,"LorryRecieptIds",lorry_reciept_id[0])
                            export_location_type = get_value_from_documentid(transaction_executor,Constants.SCENTITY_TABLE_NAME,export_location_id,"ScEntityTypeCode")##
                            if export_location_type == ['3']:
                                airway_bill_id = create_airway_bill(transaction_executor,manufacturer_id[0],actual_sc_entity_id, container_id, freight_carrier_id,export_location_id,import_location_id)
                                airway_bill_ids.append(airway_bill_id[0])
                                update_document_in_container(transaction_executor,container_id,"AirwayBillIds",airway_bill_id[0])
                            elif export_location_type == ['4']:
                                bill_of_lading_id = create_bill_of_lading(transaction_executor,manufacturer_id[0],actual_sc_entity_id, container_id,freight_carrier_id,export_location_id,import_location_id)
                                bill_of_lading_ids.append(bill_of_lading_id[0])
                                update_document_in_container(transaction_executor,container_id,"BillOfLadingIds",bill_of_lading_id[0])
                            
                            update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"isPicked",container_id,True)
                            
                        else:
                            raise Exception("Order Already Picked!")

                    logger.info("=====================  O R D E R ====== P I C K E D ===================")
                    return lorry_reciept_ids, airway_bill_ids, bill_of_lading_ids
                    
                else:
                    raise Exception("Carrier Company is not approved.")
        else:
            raise Exception("Person not authorized for the pickup")
    else:
        raise Exception("Check Request Id")
def deliver_product_to_distributor(transaction_executor,pick_up_request_id,pick_up_person_id):
    if document_exist(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE,pick_up_request_id):
        update_document(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE,"isAccepted",pick_up_request_id,True)
        purchase_order_id = get_value_from_documentid(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE, pick_up_request_id, "PurchaseOrderId")
        purchase_order_id = purchase_order_id[0]
        container_ids = get_value_from_documentid(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id,"HighestPackagingLevelIds")
        for container_id in container_ids[0]:   
            total_containers_ordered = len(container_ids[0]) 
            if document_exist(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id):
            #check if container_exist
                certificate_of_origin_id = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"CertificateOfOriginId")
                packing_list_id = get_value_from_documentid(transaction_executor, Constants.CONTAINER_TABLE_NAME,container_id,"PackingListId")
                import_custom_approved = (document_already_approved_by_customs(transaction_executor,"ImportApproval",Constants.CERTIFICATE_OF_ORIGIN_TABLE_NAME,certificate_of_origin_id[0]) and
                document_already_approved_by_customs(transaction_executor,"ImportApproval", Constants.PACKING_LIST_TABLE_NAME,packing_list_id[0]))

                if import_custom_approved:
                    # logger.info("Approved by Import!")
                    total_safe_containers = copy.copy(total_containers_ordered)
                    if isContainerSafe(transaction_executor,container_id):
                        actual_sc_entity_id = get_scentityid_from_personid(transaction_executor,pick_up_person_id)
                        transport_type = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"TransportType")
                        
                        #check if container is safe
                        if transport_type[0] == 1:
                            table = Constants.AIRWAY_BILL_TABLE_NAME
                            airway_bills = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"AirwayBillIds")
                            
                        elif transport_type[0] == 2:
                            table = Constants.BILL_OF_LADING_TABLE_NAME
                            bill_of_lading = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"BillOfLadingIds")


                        pick_up_scentity_id = get_value_from_documentid(transaction_executor,Constants.PICK_UP_REQUESTS_TABLE,pick_up_request_id, "CarrierCompanyId")
                        if actual_sc_entity_id == pick_up_scentity_id[0]:
                            # logger.info("Authorized!")

                            
                            if transport_type[0] == 1:
                                is_picked_up = get_sub_details(transaction_executor,table,"RecieverApproval",airway_bills[0][-1],"isApproved")
                                if is_picked_up[0] == 0:
                                    update_document(transaction_executor,Constants.AIRWAY_BILL_TABLE_NAME,"RecieverApproval.isApproved",airway_bills[0][-1],True)
                                    update_document(transaction_executor,Constants.AIRWAY_BILL_TABLE_NAME,"RecieverApproval.ApproverId",airway_bills[0][-1], pick_up_person_id)
                                    pick_up_location = get_value_from_documentid(transaction_executor,Constants.AIRWAY_BILL_TABLE_NAME,airway_bills[0][-1],"ImportAirportName")
                                    consignee_id = get_value_from_documentid(transaction_executor,Constants.AIRWAY_BILL_TABLE_NAME,airway_bills[0][-1],"SenderScEntityId")
                                else:
                                    raise Exception("container already picked up")

                            elif transport_type[0] == 2:
                                is_picked_up = get_sub_details(transaction_executor,table,"RecieverApproval",bill_of_lading[0][-1],"isApproved")
                                if is_picked_up[0] == 0:
                                    update_document(transaction_executor,Constants.BILL_OF_LADING_TABLE_NAME,"RecieverApproval.isApproved",bill_of_lading[0][-1],True)
                                    update_document(transaction_executor,Constants.BILL_OF_LADING_TABLE_NAME,"RecieverApproval.ApproverId",bill_of_lading[0][-1], pick_up_person_id)
                                    pick_up_location = get_value_from_documentid(transaction_executor,Constants.BILL_OF_LADING_TABLE_NAME,bill_of_lading[0][-1],"ImportPortName")
                                    consignee_id = get_value_from_documentid(transaction_executor,Constants.BILL_OF_LADING_TABLE_NAME,bill_of_lading[0][-1],"SenderScEntityId")
                                else:
                                    raise Exception("Container Already picked up")

                            consignee_name = get_value_from_documentid(transaction_executor,Constants.SCENTITY_TABLE_NAME,consignee_id[0],"ScEntityName")
                            delivery_location = get_scentity_contact(transaction_executor,actual_sc_entity_id[0],"Address")
                            lorry_reciepts = get_value_from_documentid(transaction_executor,Constants.CONTAINER_TABLE_NAME,container_id,"LorryRecieptIds")
                            carrier_id = get_value_from_documentid(transaction_executor,Constants.LORRY_RECEIPT_TABLE_NAME,lorry_reciepts[0][-1],"CarrierId")
                            if carrier_id[0] == pick_up_scentity_id[0]:
                                # logger.info("No request was made by buyer to pickup. Creating a new L/R to initiate import delivery.")
                                lorry_reciept_id = create_lorry_reciept(transaction_executor,actual_sc_entity_id,pick_up_person_id,pick_up_location[0],delivery_location,consignee_id,consignee_name,True)
                                update_document_in_container(transaction_executor,container_id,"LorryRecieptIds",lorry_reciept_id[0])
                            else:
                                # logger.info("Pick up request was made by new carrier assigned by buyer.")
                                update_document(transaction_executor,Constants.LORRY_RECEIPT_TABLE_NAME,"isPickedUp",lorry_reciepts[0][-1],True)
                            
                            logger.info(" ============ I M P O R T ========== P I C K U P ========S U C C E S S F U L =============")

                        else:
                            raise Exception("Not Authorized!")
                    else:
                        # raise Exception("Container Not Safe!")
                        total_safe_containers = total_safe_containers - 1
                        if total_safe_containers > 0:
                            continue
                        else:
                            raise Exception("Not Container was safe. Pick up can't be made for any container")
                         
                else:
                    raise Exception("Not approved by Customs.")
            else:
                raise Exception("Check Container Id")
             #check if pick up person is authorized
    else:
        raise Exception(" Check Request Id")
def deliver_product_to_final_entity(transaction_executor, pick_up_request_id,
                                    truck_carrier_person_id):
    if document_exist(transaction_executor, Constants.PICK_UP_REQUESTS_TABLE,
                      pick_up_request_id):
        update_document(transaction_executor, Constants.PICK_UP_REQUESTS_TABLE,
                        "isAccepted", pick_up_request_id, True)
        purchase_order_id = get_value_from_documentid(
            transaction_executor, Constants.PICK_UP_REQUESTS_TABLE,
            pick_up_request_id, "PurchaseOrderId")
        purchase_order_id = purchase_order_id[0]

        if document_exist(transaction_executor,
                          Constants.PURCHASE_ORDER_TABLE_NAME,
                          purchase_order_id):
            container_ids = get_value_from_documentid(
                transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME,
                purchase_order_id, "HighestPackagingLevelIds")

            carrier_company_id = get_value_from_documentid(
                transaction_executor, Constants.CONTAINER_TABLE_NAME,
                container_ids[0][0], "CarrierCompanyId")

            actual_sc_entity_id = get_scentityid_from_personid(
                transaction_executor, truck_carrier_person_id)

            if carrier_company_id[0] == actual_sc_entity_id:
                lorry_reciept_ids = []
                for container_id in container_ids[0]:
                    if isContainerSafe(transaction_executor, container_id):
                        isPicked = get_value_from_documentid(
                            transaction_executor,
                            Constants.CONTAINER_TABLE_NAME, container_id,
                            "isPicked")

                        if isPicked[0] == 0:
                            update_document(
                                transaction_executor,
                                Constants.PURCHASE_ORDER_TABLE_NAME,
                                "isOrderShipped", purchase_order_id, True)
                            update_document(transaction_executor,
                                            Constants.CONTAINER_TABLE_NAME,
                                            "isPicked", container_id, True)

                            consignee_id = get_sub_details(
                                transaction_executor,
                                Constants.PURCHASE_ORDER_TABLE_NAME, "Orderer",
                                purchase_order_id, "OrdererScEntityId")
                            pick_up_location = get_scentity_contact(
                                transaction_executor, consignee_id[0],
                                "Address")
                            consignee_name = get_value_from_documentid(
                                transaction_executor,
                                Constants.SCENTITY_TABLE_NAME, consignee_id[0],
                                "ScEntityName")
                            acceptor_id = get_sub_details(
                                transaction_executor,
                                Constants.PURCHASE_ORDER_TABLE_NAME,
                                "Acceptor", purchase_order_id,
                                "AcceptorScEntityId")
                            delivery_location = get_scentity_contact(
                                transaction_executor, acceptor_id[0],
                                "Address")

                            #change consignee id to person who made pick-up request
                            lorry_reciept_id = create_lorry_reciept(
                                transaction_executor, actual_sc_entity_id,
                                truck_carrier_person_id, pick_up_location[0],
                                delivery_location[0], consignee_id[0],
                                consignee_name[0], True)
                            lorry_reciept_ids.append(lorry_reciept_id[0])

                        else:
                            raise Exception("Order Already Picked")
                    else:
                        raise Exception(" Container is not Safe for Delivery!")

                logger.info(
                    " =========== L O C A L ====== T R A N S P O R T ======== I N I T I A T E D ============= "
                )
                return lorry_reciept_ids
            else:
                raise Exception(" Not Authorized! ")
        else:
            raise Exception(" Check Purchase Order Id.")
    else:
        raise Exception("Check Request Id")
Example #6
0
def request_to_change_pickup(transaction_executor, purchase_order_id,
                             buyer_person_id, new_truck_carrier_id):
    if document_exist(transaction_executor,
                      Constants.PURCHASE_ORDER_TABLE_NAME, purchase_order_id):
        orderer_id = get_orderer_id(transaction_executor, purchase_order_id)
        actual_sc_entity_id = get_scentityid_from_personid(
            transaction_executor, buyer_person_id)

        if actual_sc_entity_id == orderer_id:
            container_ids = get_value_from_documentid(
                transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME,
                purchase_order_id, "HighestPackagingLevelIds")

            for container_id in container_ids[0]:
                transport_type = get_value_from_documentid(
                    transaction_executor, Constants.CONTAINER_TABLE_NAME,
                    container_id, "TransportType")
                print(transport_type)
                if transport_type[0] == 1:
                    airway_bills = get_value_from_documentid(
                        transaction_executor, Constants.CONTAINER_TABLE_NAME,
                        container_id, "AirwayBillIds")
                    update_document(transaction_executor,
                                    Constants.AIRWAY_BILL_TABLE_NAME,
                                    "RecieverScEntityId", airway_bills[0][-1],
                                    new_truck_carrier_id)
                    pick_up_location = get_value_from_documentid(
                        transaction_executor, Constants.AIRWAY_BILL_TABLE_NAME,
                        airway_bills[0][-1], "ImportAirportName")
                else:
                    bill_of_lading = get_value_from_documentid(
                        transaction_executor, Constants.CONTAINER_TABLE_NAME,
                        container_id, "BillOfLadingIds")
                    update_document(transaction_executor,
                                    Constants.BILL_OF_LADING_TABLE_NAME,
                                    "RecieverScEntityId",
                                    bill_of_lading[0][-1],
                                    new_truck_carrier_id)
                    pick_up_location = get_value_from_documentid(
                        transaction_executor,
                        Constants.BILL_OF_LADING_TABLE_NAME,
                        bill_of_lading[0][-1], "ImportPortName")
                delivery_location = get_scentity_contact(
                    transaction_executor, actual_sc_entity_id, "Address")
                consignee_name = get_value_from_documentid(
                    transaction_executor, Constants.SCENTITY_TABLE_NAME,
                    actual_sc_entity_id, "ScEntityName")
                lorry_reciept_id = create_lorry_reciept(
                    transaction_executor, new_truck_carrier_id, "",
                    pick_up_location, delivery_location, actual_sc_entity_id,
                    consignee_name, False)
                update_document_in_container(transaction_executor,
                                             container_id, "LorryRecieptIds",
                                             lorry_reciept_id[0])
                pick_up_request_id = create_pick_up_request(
                    transaction_executor, actual_sc_entity_id,
                    new_truck_carrier_id, purchase_order_id, "3")

                # logger.info("New Request was made!")
                return pick_up_request_id[0]

        else:
            raise Exception("Not Authorized!!")
    else:
        raise Exception("Document does not exist!")