def req_already_sent(transaction_executor, person_id): # statement = "SELECT * FROM {} as j where j.SenderPersonId = ?".format(Constants.JOINING_REQUEST_TABLE_NAME) # cursor_four = transaction_executor.execute_statement(statement, person_id) # print_result(cursor_four) results = get_document_ids(transaction_executor, Constants.JOINING_REQUEST_TABLE_NAME, 'SenderPersonId', person_id) try: request_id = next(results) # logger.info("Request already sent with id : {}".format(request_id)) return True except StopIteration: # logger.info(" Request not found") return False
def create_purchase_order_input(transaction_executor, purchase_order_id, actual_sc_entity_id): product_id = get_value_from_documentid(transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, purchase_order_id, "ProductId") # number of container if order type is 1 and number of dosage if order type is 2 order_quantity = get_value_from_documentid( transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, purchase_order_id, "OrderQuantity") order_type = get_value_from_documentid(transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, purchase_order_id, "OrderType") products_per_container = get_value_from_documentid( transaction_executor, Constants.PRODUCT_TABLE_NAME, product_id[0], "ProductsPerContainer") if order_type[0] == "1": product_price = get_value_from_documentid(transaction_executor, Constants.PRODUCT_TABLE_NAME, product_id[0], "ProductPrice") elif order_type[0] == "2": inventory_table = inventory_table_already_exist( transaction_executor, actual_sc_entity_id) product_id = get_value_from_documentid( transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, purchase_order_id, "ProductId") inventory_id = next( get_document_ids(transaction_executor, inventory_table[0], "ProductId", product_id[0])) product_price = get_value_from_documentid(transaction_executor, inventory_table[0], inventory_id, "ProductPrice") purchase_order_input = { "ProductId": product_id[0], "OrderQuantity": order_quantity[0], "OrderValue": order_quantity[0] * product_price[0] * products_per_container[0] } # logger.info(purchase_order_input) return purchase_order_input
def create_req_to_join_scentity(transaction_executor, sc_entity, employee_id, person_id): request_number = get_index_number(transaction_executor, Constants.JOINING_REQUEST_TABLE_NAME, Constants.JOINING_REQUESTID_INDEX_NAME) sc_entity_identification_code = sc_entity['ScEntityIdentificationCode'] sc_entity_id = next( get_document_ids(transaction_executor, Constants.SCENTITY_TABLE_NAME, "ScEntityIdentificationCode", sc_entity_identification_code)) request = { "JoiningRequestNumber": request_number, "SenderEmployeeId": employee_id, "SenderPersonId": person_id, "ScEntityId": sc_entity_id, "isAccepted": False } print(request) result = insert_documents(transaction_executor, Constants.JOINING_REQUEST_TABLE_NAME, [request]) return result[0]
def update_current_product_inventory(transaction_executor, inventory_table_name, product_id, product_instances, case_ids, pallete_ids, container_ids, delivered_product_quantity): inventory_id = next( get_document_ids(transaction_executor, inventory_table_name, "ProductId", product_id)) insert_value_in_inventory_table(transaction_executor, inventory_table_name, "CaseIds", inventory_id, case_ids) insert_value_in_inventory_table(transaction_executor, inventory_table_name, "PalleteIds", inventory_id, pallete_ids) insert_value_in_inventory_table(transaction_executor, inventory_table_name, "ContainerIds", inventory_id, container_ids) current_inventory = get_value_from_documentid(transaction_executor, inventory_table_name, inventory_id, "CurrentInventory") updated_inventory = int(current_inventory[0]) + delivered_product_quantity update_document(transaction_executor, inventory_table_name, "CurrentInventory", inventory_id, updated_inventory)
def update_price_and_minimum_selling_amount(transaction_executor, product_id, minimum_selling_amount, new_product_price, distributor_person_id): # check if person has scentity actual_sc_entity_id = get_scentityid_from_personid(transaction_executor, distributor_person_id) if actual_sc_entity_id: # check if sc entity has inventory table and product_id inventory_table = inventory_table_already_exist( transaction_executor, actual_sc_entity_id) if inventory_table: #check product exist with distributor if product_exist_in_inventory(transaction_executor, inventory_table[0], product_id): ## minimum selling amount is in number of cases inventory_id = next( get_document_ids(transaction_executor, inventory_table[0], "ProductId", product_id)) print(inventory_id) update_document(transaction_executor, inventory_table[0], "ProductPrice", inventory_id, new_product_price) update_document(transaction_executor, inventory_table[0], "MinimumSellingAmount", inventory_id, minimum_selling_amount) logger.info( "=================== P R I C E === A N D ==== S E L L I N G ++++ A M O U N T ======= U P D A T E D ========" ) else: raise Exception("Product does not exist in inventory!") else: raise Exception("No inventory table exist") else: raise Exception("No SCentity for the person")
def register_new_Person(transaction_executor, person): """ Register a new driver in QLDB if not already registered. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type driver: dict :param driver: The driver's license to register. """ employee_id = person['EmployeeId'] if person_already_exists(transaction_executor, employee_id): result = next( get_document_ids(transaction_executor, Constants.PERSON_TABLE_NAME, 'EmployeeId', employee_id)) # logger.info('Person with employee_Id {} already exists with PersonId: {} .'.format(employee_id, result)) else: person.update({"isSuperAdmin": False}) #<<-----------uncomment after onboarding admin person.update({"isAdmin": False }) #<<----------- un comment after onboarding admin result = insert_documents(transaction_executor, Constants.PERSON_TABLE_NAME, [person]) result = result[0] # logger.info("New Person registered with person id : {}".format(result)) return result
def initiate_distributor_shipment(transaction_executor, purchase_order_id, distributor_person_id,carrier_company_id,tranport_type): # check if po exist if document_exist(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id): # check if po is accepted if get_document_superadmin_approval_status(transaction_executor, Constants.SCENTITY_TABLE_NAME, carrier_company_id): if order_already_accepted(transaction_executor,purchase_order_id): #check for authorization required_scentity_id = get_sub_details(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,"Acceptor",purchase_order_id,"AcceptorScEntityId") actual_scentity_id = get_scentityid_from_personid(transaction_executor,distributor_person_id) if required_scentity_id[0] == actual_scentity_id: # check if enough enventory for the order inventory_table = inventory_table_already_exist(transaction_executor,actual_scentity_id) product_id = get_value_from_documentid(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id,"ProductId") containers_ordered = get_value_from_documentid(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,purchase_order_id,"OrderQuantity") containers_ordered = containers_ordered[0] inventory_id = next(get_document_ids(transaction_executor,inventory_table[0],"ProductId",product_id[0])) available_unit_inventory = get_value_from_documentid(transaction_executor,inventory_table[0],inventory_id,"CurrentInventory") available_unit_inventory = int(available_unit_inventory[0]) products_per_container = get_value_from_documentid(transaction_executor,Constants.PRODUCT_TABLE_NAME,product_id[0],"ProductsPerContainer") product_units_ordered = int(containers_ordered) * int(products_per_container[0]) if available_unit_inventory >= product_units_ordered: print("=======================================") containers_in_inventory = get_value_from_documentid(transaction_executor,inventory_table[0],inventory_id,"ContainerIds") # containers_in_inventory = containers_in_inventory[0] # lis = [containers_in_inventory,[1,2]] containers_in_inventory = oneDArray(containers_in_inventory) total_containers = len(containers_in_inventory) print(containers_in_inventory) available_containers = int(available_unit_inventory/products_per_container[0]) starting_container = total_containers - (available_containers) ending_container = starting_container + int(containers_ordered) # for containers in range((1,containers_ordered+1)): # assign the first containers to the new purchase order assigned_container_ids = containers_in_inventory[starting_container:ending_container] logger.info("Assigned ids are :{}".format(assigned_container_ids)) # add purchase order to the new container for container_id in assigned_container_ids: update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"PurchaseOrderId",container_id, purchase_order_id) update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"CarrierId",container_id, carrier_company_id) update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"isDelivered",container_id, False) update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"TransportType",container_id, tranport_type) update_document(transaction_executor,Constants.CONTAINER_TABLE_NAME,"isPicked",container_id,False) update_document(transaction_executor,Constants.PURCHASE_ORDER_TABLE_NAME,"HighestPackagingLevelIds",purchase_order_id,assigned_container_ids) available_unit_inventory = available_unit_inventory - product_units_ordered update_document(transaction_executor,inventory_table[0],"CurrentInventory",inventory_id,available_unit_inventory) pick_up_request_id = create_pick_up_request(transaction_executor,actual_scentity_id,carrier_company_id, purchase_order_id, tranport_type) logger.info("===================== S H I P M E N T ======= I N I T I A T E D =======================") return pick_up_request_id[0] else: raise Exception("Not enough dosage in inventory to initiate shipment.") else: raise Exception("Person not Authorized!") else: raise Exception("Accept the Order First!") else: raise Exception(" Carrier Company is not Approved") else: raise Exception("Cannot Locate Purchase Order.")
def create_purchase_order_to_distributor(transaction_executor, purchase_order_details, distributor_id, hospital_person_id): product_id = purchase_order_details["ProductId"] number_of_containers_ordered = purchase_order_details["OrderQuantity"] if document_exist(transaction_executor, Constants.SCENTITY_TABLE_NAME, distributor_id): # check person belong to ScEntity actual_sc_entity_id = get_scentityid_from_personid( transaction_executor, hospital_person_id) if actual_sc_entity_id: manufacturer_id = get_value_from_documentid( transaction_executor, Constants.PRODUCT_TABLE_NAME, product_id, "ManufacturerId") # print(manufacturer_id) if manufacturer_id[0] != distributor_id: # scentity_type_code = get_value_from_documentid(transaction_executor,Constants.SCENTITY_TABLE_NAME,distributor_id,"ScEntityTypeCode") # logger.info("Distributor confirmed") inventory_table = inventory_table_already_exist( transaction_executor, distributor_id) if inventory_table: #check product exist with distributor if product_exist_in_inventory(transaction_executor, inventory_table[0], product_id): # check number of dosage are in muliple of cases and more than minumum amount inventory_id = next( get_document_ids(transaction_executor, inventory_table[0], "ProductId", product_id)) minimum_containers_order = get_value_from_documentid( transaction_executor, inventory_table[0], inventory_id, "MinimumSellingAmount") if number_of_containers_ordered >= minimum_containers_order[ 0] and isinstance(number_of_containers_ordered, int): purchase_order_number = get_index_number( transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, "PurchaseOrderNumber") purchase_order_details.update({"OrderType": "2"}) purchase_order_details.update( {"PurchaseOrderNumber": purchase_order_number}) purchase_order_details['Orderer'].update( {'OrdererScEntityId': actual_sc_entity_id}) purchase_order_details['Orderer'].update( {'OrdererPersonId': hospital_person_id}) purchase_order = { **purchase_order_details, "Acceptor": { "isOrderAccepted": False, "AcceptorScEntityId": distributor_id, "ApprovingPersonId": "" }, "InvoiceId": "", "HighestPackagingLevelIds": [], "HighestPackagingLevelType": "Containers" } purchase_order_id = insert_documents( transaction_executor, Constants.PURCHASE_ORDER_TABLE_NAME, convert_object_to_ion(purchase_order)) # logger.info("Order was placed sucessfully with id: {}".format(purchase_order_id)) logger.info( " ================================== O R D E R =========== P L A C E D ===============================" ) return purchase_order_id[0] else: raise Exception( "Number of dosage must be an integer and greater than {} " .format(minimum_containers_order)) else: raise Exception( "Distributor doesn't have this product.") else: raise Exception("Distributor does not have any inventory") else: raise Exception( "Order is being placed to wrong entity. Check Distributor_id" ) else: raise Exception("Check the person id!") else: raise Exception(" Check Distributor id!")
def register_new_user_with_scentity(transaction_executor, person, new_sc_entity): """ Register a new driver and a new driver's license in a single transaction. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type driver: dict :param driver: The driver's license to register. :type new_license: dict :param new_license: The driver's license to register. """ person_id = register_new_Person(transaction_executor, person) if get_scentityid_from_personid(transaction_executor, person_id): raise Exception( "Person with personId '{}' already belongs to a SC Entity".format( person_id)) else: # logger.info("Registering new Person's entity...") if lookup_scentity(transaction_executor, new_sc_entity): # send request to join a new entity # logger.info(' Entity already exist. Sending request to join it.') employee_id = person['EmployeeId'] scentity_id_code = new_sc_entity['ScEntityIdentificationCode'] scentity_id = next( get_document_ids(transaction_executor, Constants.SCENTITY_TABLE_NAME, 'ScEntityIdentificationCode', scentity_id_code)) if get_document_superadmin_approval_status( transaction_executor, Constants.SCENTITY_TABLE_NAME, scentity_id): if req_already_sent(transaction_executor, person_id) == True: raise Exception( "Please wait for your company admin to approve the request." ) else: joining_request_id = create_req_to_join_scentity( transaction_executor, new_sc_entity, employee_id, person_id) send_request_to_company(transaction_executor, joining_request_id, new_sc_entity) return person_id, joining_request_id else: raise Exception('Wait for MCG to approve this entity ... ') else: #create a new entity if req_already_sent(transaction_executor, person_id) == True: raise Exception( "Please wait for your company admin to approve the request." ) else: update_person_to_admin(transaction_executor, person_id) new_sc_entity.update({'PersonIds': [str(person_id)]}) new_sc_entity.update({'isApprovedBySuperAdmin': False}) try: result = insert_documents(transaction_executor, Constants.SCENTITY_TABLE_NAME, [new_sc_entity]) sc_entity_id = result[0] mcg_request_id = create_mcg_request( transaction_executor, sc_entity_id, person_id, 1) sc_entity_id_code = new_sc_entity[ "ScEntityIdentificationCode"] # logger.info('Registration process began for new SCEntity with ScEntityIdentificationCode : {} and ScEntityId: {}. A new request was create for super admin : {}'.format(sc_entity_id_code,sc_entity_id, mcg_request_id)) logger.info( " ================= P E R S O N ======= A N D ===== S C E N T I T Y ========= R E G I S T R A T I O N ========= I N I T I A T E D=========" ) return person_id, sc_entity_id, mcg_request_id except StopIteration: raise Exception( 'Problem occurred while inserting Scentity, please review the results.' )