Exemplo n.º 1
0
    def on_post(self, req, resp, login_id, session,order_id):
        # Authenticate login id and session availability.
        # try:
        #     if (MemcacheFunctions.IsSessionValid(login_id,session) == False):
        #         resp.status = falcon.HTTP_401
        #         Err = {"Reason": "Invalid Login Credentials or Session is Expired"}
        #         result_json = json.dumps(Err)
        #         resp.body = result_json
        #         return
        # except ValueError as err:
        #     raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        # except Exception as err:
        #     raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)

        # checks whether the user is_on_boarded, in order to create an order
        try:
            # Connecting the database
            database_connection = get_db_connection()
            cursor = database_connection.cursor()
            # Query to display data from this tbl with given loginID parameter
            cursor.execute("select is_on_boarded from logins where login_id = '"+login_id+"'")
            row = cursor.fetchone()
            if(row['is_on_boarded'] == 0):
                resp.status = falcon.HTTP_204
                Err = {"Reason": "Please complete your profile"}
                result_json = json.dumps(Err)
                resp.body = (result_json)
                return
        except ValueError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except pymysql.IntegrityError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except Exception as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        finally:
            cursor.close()
            database_connection.close()

        # If the user is valid and is_on_boarded, proceeds to create an order
        try:
            raw_json = req.stream.read(req.content_length or 0).decode('utf-8')
            bookings = json.loads(raw_json, object_pairs_hook=OrderedDict, encoding='utf-8')
            # cart_id = req.get_param('cart_id')
            cart_id = order_id
            # Connecting the database
            database_connection = get_db_connection()
            cursor = database_connection.cursor()
            # Query to display merchant from this tbl w given loginID parameter
            # cursor.execute("SELECT merchant_id\
            #                 FROM logins\
            #                 WHERE login_id = "+login_id+"")
            # merchant_id_dict = cursor.fetchone()
            # merchant_id = merchant_id_dict['merchant_id']
            # Query to dispay data from this tbl with given quotationID JSON parameter
            # cursor.execute("SELECT p1.country_code as port_from,p2.country_code as port_to\
            #                 FROM ship_quotations as q\
            #                 INNER JOIN ports as p1 on p1.port_id = q.port_id_from\
            #                 INNER JOIN ports as p2 on p2.port_id = q.port_id_to\
            #                 WHERE q.quotation_id = '"+bookings['quotation_id']+"'")
            # port_to_from_dict = cursor.fetchone()
            # port_from = port_to_from_dict['port_from']
            # port_to = port_to_from_dict['port_to']
            # Query to display configVal from this tbl w configKey criteria
            cursor.execute("select config_value from config where config_key = 'logistic-booking-fee'")
            row = cursor.fetchone()
            booking_fee = row['config_value']
            # Query to display configVal from this tbl w configKey criteria
            cursor.execute("select config_value from config where config_key = 'logistic-booking-fee-description'")
            row = cursor.fetchone()
            fee_description = row['config_value']

            cursor.execute("select config_value from config where config_key = 'platform-tax_type'")
            row = cursor.fetchone()
            tax_type = row['config_value']

            cursor.execute("select config_value from config where config_key = 'platform-tax_rate'")
            row = cursor.fetchone()
            tax_rate = row['config_value']

            # Query to display cartID from this tbl w given loginID parameter
            # cursor.execute("select cart_id from carts where login_id = "+login_id+"")
            # row = cursor.fetchone()
            # if cursor.rowcount is 1:
            #     cart_id = row['cart_id']
            # else:
            #     # Query to create new cart
            #     cursor.execute("insert into carts(login_id) values(%s)",login_id)
            #     database_connection.commit()
            #     cart_id = cursor.lastrowid
            todayDate = datetime.today()
            database_connection.begin()
            cursor.execute("insert into carts_item(cart_id,transaction_description,transaction_fee,platform_id,status) values (%s,%s,%s,%s,%s)",(cart_id,fee_description,booking_fee,'1','UNPAID'))
            cart_item_id = cursor.lastrowid
            cursor.execute("update carts as c1 \
                            set c1.amount = c1.amount+ "+str(booking_fee)+"\
                            ,c1.tax_type = '"+str(tax_type)+"'\
                            ,c1.tax_rate = "+str(tax_rate)+"\
                            ,c1.tax_amount = (c1.amount*(c1.tax_rate/100))\
                            ,c1.total_amount = c1.amount + c1.tax_amount\
                            where c1.cart_id = "+str(cart_id)+"")
            order_id_new = Tools.GetOrderID(bookings['quotation_id'],cursor,database_connection)
            price_without_tax = float(bookings['booking_price']) - float(bookings['booking_tax_amount'])
            arguments =     (order_id_new,
                            cart_item_id,
                            cart_id,
                            login_id,
                            bookings['quotation_id'],
                            bookings['hs_code_id']['hs_code'] if type(bookings['hs_code_id']) is OrderedDict else bookings['hs_code_id'],
                            'ORDERBOOKED',
                            bookings['container_id'],
                            bookings['consignor_company_name'],
                            bookings['consignor_contact_person'],
                            bookings['consignor_contact_number'],
                            bookings['consignor_email'],
                            bookings['consignor_delivery_address'],
                            bookings['consignor_billing_address'],
                            bookings['consignor_shipper'],
                            bookings['four_digit_hs_code'],
                            bookings['halal_status'],
                            bookings['cargo_description'],
                            bookings['consignor_merchandise_value'],
                            bookings['consignor_commercial_value'],
                            bookings['consignee_merchandise_value'],	
                            bookings['consignee_commercial_value'],
                            bookings['consignee_company_name'],
                            bookings['consignee_contact_person'],
                            bookings['consignee_contact_number'],
                            bookings['consignee_email'],
                            bookings['consignee_delivery_address'],
                            bookings['consignee_billing_address'],
                            bookings['consignee_shipper'],
                            bookings['cbm'],
                            todayDate,
                            todayDate,
                            bookings['booking_price_total'],
                            bookings['booking_price_ba'],
                            bookings['booking_price_bd'],
                            bookings['booking_tax_rate'],
                            bookings['booking_tax_amount'],
                            bookings['booking_tax_type'],
                            bookings['booking_price'],
                            price_without_tax,
                            bookings['booking_tax_rate'],
                            bookings['booking_tax_amount'],
                            bookings['booking_tax_type'],
                            bookings['quantity'],
                            bookings['tracking_number'],
                            bookings['logistic_freight_provider'],
                            bookings['packing_details'],
                            bookings['customBroker'],
                            bookings['billing_of_loading'],
                            'UNPAID',
                            booking_fee,
                            fee_description,
                            bookings['weight'],
                            tax_type,
                            tax_rate)
            
            # Query to create new data in cartItems
            cursor.execute("insert into ship_temp_orders(temp_order_id ,\
                                                        cart_item_id,\
                                                        cart_id ,\
                                                        login_id,\
                                                        quotation_id,\
                                                        hs_code_id,\
                                                        cargo_status_code,\
                                                        container_id,\
                                                        consignor_company_name,\
                                                        consignor_contact_person,\
                                                        consignor_contact_number,\
                                                        consignor_email,\
                                                        consignor_delivery_address,\
                                                        consignor_billing_address,\
                                                        consignor_shipper,\
                                                        four_digit_hs_code,\
                                                        halal_status,\
                                                        cargo_description,\
                                                        consignor_merchandise_value,\
                                                        consignor_commercial_value,\
                                                        consignee_merchandise_value,\
                                                        consignee_commercial_value,\
                                                        consignee_company_name,\
                                                        consignee_contact_person,\
                                                        consignee_contact_number,\
                                                        consignee_email,\
                                                        consignee_delivery_address,\
                                                        consignee_billing_address,\
                                                        consignee_shipper,\
                                                        cbm,\
                                                        booking_date,\
                                                        confirmation_date,\
                                                        booking_price_a,\
                                                        booking_price_ba,\
                                                        booking_price_bd,\
                                                        booking_price_tax,\
                                                        booking_price_tax_amount,\
                                                        booking_price_tax_type,\
                                                        booking_price_total,\
                                                        booking_price_total_wo_tax,\
                                                        closing_price_tax,\
                                                        closing_price_tax_amount,\
                                                        closing_price_tax_type,\
                                                        quantity,\
                                                        tracking_number,\
                                                        logistic_provider,\
                                                        packing_details,\
                                                        custom_agent_id,\
                                                        bill_of_loading,\
                                                        booking_fee_payment_status,\
                                                        transaction_fee,\
                                                        transaction_description,\
                                                        weight,\
                                                        booking_fee_tax_type,\
                                                        booking_fee_tax_rate)\
            values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",arguments)
            database_connection.commit()
            if cursor.rowcount > 0 :
                resp.status = falcon.HTTP_200
                message = {"status":order_id_new}
                result_json = json.dumps(message)
                resp.body = result_json
            else:
                resp.status = falcon.HTTP_406
                message = {"Status": "Order is failed to be created"}
                result_json = json.dumps(message)
                resp.body = result_json
        except ValueError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except pymysql.IntegrityError as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        except Exception as err:
            raise falcon.HTTPError(falcon.HTTP_400, traceback.print_exc(file=sys.stdout) , err.args)
        finally:
            database_connection.rollback()
            cursor.close()
            database_connection.close()