DROP PROCEDURE IF EXISTS `create_order`;
DELIMITER ;;
CREATE PROCEDURE `create_order` (
    IN p_seller_id INT,
    IN p_order_ship_fee INT,
    IN p_mode_of_payment INT,
    IN p_delivery_location VARCHAR(100),
    IN p_session_id CHAR(64)
)
proc:BEGIN
    IF (SELECT COUNT(session_id) FROM sessions WHERE session_id = LOWER(p_session_id)) = 0 OR p_session_id IS NULL THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'create_order: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    INSERT INTO orders
    (`buyer_id`, `seller_id`, `ship_fee`, `mode_of_payment`, `delivery_location`, `status`)
    VALUES
    (@user_id, p_seller_id, p_order_ship_fee, p_mode_of_payment, p_delivery_location, 1);
    SELECT 0 AS 'status', LAST_INSERT_ID() AS 'order_id', 'create_order' AS 'proc';
END ;;
DELIMITER ;
