DROP PROCEDURE IF EXISTS `get_seller_orders`;
DELIMITER ;;
CREATE PROCEDURE `get_seller_orders` (
    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 = 'get_seller_orders: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    SELECT O.id, O.status, OS.description, OP.product_id, P.name, OP.quantity, OP.amount, 'get_seller_orders' AS 'proc' FROM orders O
    JOIN order_products OP
    ON O.id = OP.order_id AND O.seller_id = @user_id
    JOIN order_status_t OS
    ON O.status = OS.id
    JOIN products P
    ON OP.product_id = P.id;
END ;;
DELIMITER ;
