CREATE FUNCTION `calculateEbayPrice`(
    price DECIMAL(10,2),
    corePrice DECIMAL(10,2),
    weight DECIMAL(10,2),
    sumPrice DECIMAL(10,2),
    numberOfPieces INT
) RETURNS DECIMAL(10,2) DETERMINISTIC
BEGIN
    DECLARE productCost DECIMAL(10,2);
    DECLARE finalPrice DECIMAL(10,2);

    SET productCost = IF(numberOfPieces > 1, (price + corePrice) * numberOfPieces, price + corePrice);
    SET finalPrice = 0;

    IF (productCost > 0 AND productCost <= 0.99) THEN
        SET finalPrice = 9.95;
    ELSEIF (productCost >= 1 AND productCost <= 1.99) THEN
        SET finalPrice = 12.85;
    ELSEIF (productCost >= 2 AND productCost <= 49.99) THEN
        IF (productCost <= 2.99) THEN
            SET finalPrice = productCost * 7;
        ELSEIF (productCost <= 3.99) THEN
            SET finalPrice = productCost * 4.7;
        ELSEIF (productCost <= 5.99) THEN
            SET finalPrice = productCost * 4.3;
        ELSEIF (productCost <= 7.99) THEN
            SET finalPrice = productCost * 3.5;
        ELSEIF (productCost <= 9.99) THEN
            SET finalPrice = productCost * 3.2;
        ELSEIF (productCost <= 12.99) THEN
            SET finalPrice = productCost * 2.6;
        ELSEIF (productCost <= 14.99) THEN
            SET finalPrice = productCost * 2.45;
        ELSEIF (productCost <= 17.99) THEN
            SET finalPrice = productCost * 2.3;
        ELSEIF (productCost <= 21.99) THEN
            SET finalPrice = productCost * 2.25;
        ELSEIF (productCost <= 24.99) THEN
            SET finalPrice = productCost * 2.15;
        ELSEIF (productCost <= 29.99) THEN
            SET finalPrice = productCost * 1.95;
        ELSEIF (productCost <= 39.99) THEN
            SET finalPrice = productCost * 1.75;
        ELSEIF (productCost <= 49.99) THEN
            SET finalPrice = productCost * 1.7;
        END IF;
        
        IF (weight <= 1.99) THEN
            SET finalPrice = finalPrice + 10;
        ELSEIF (weight <= 2.99) THEN
            SET finalPrice = finalPrice + 15;
        ELSEIF (weight <= 4.99) THEN
            SET finalPrice = finalPrice + 20;
        ELSEIF (weight >= 5) THEN
            SET finalPrice = finalPrice + 25;
        END IF;
        
    ELSEIF (productCost >= 50) THEN
        IF (productCost <= 64.99) THEN
            SET finalPrice = productCost * 1.55;
        ELSEIF (productCost <= 79.99) THEN
            SET finalPrice = productCost * 1.50;
        ELSEIF (productCost <= 99.99) THEN
            SET finalPrice = productCost * 1.45;
        ELSEIF (productCost <= 199.99) THEN
            SET finalPrice = productCost * 1.40;
        ELSEIF (productCost <= 299.99) THEN
            SET finalPrice = productCost * 1.30;
        ELSEIF (productCost >= 300) THEN
            SET finalPrice = productCost * 1.25;
        END IF;
        
        IF (weight <= 1.99) THEN
            SET finalPrice = finalPrice + 5;
        ELSEIF (weight <= 2.99) THEN
            SET finalPrice = finalPrice + 10;
        ELSEIF (weight <= 4.99) THEN
            SET finalPrice = finalPrice + 15;
        ELSEIF (weight >= 5) THEN
            SET finalPrice = finalPrice + 20;
        END IF;
    END IF;

    SET finalPrice = finalPrice + sumPrice;
    SET finalPrice = CEILING(finalPrice * 20) / 20;

    RETURN finalPrice;
END;
