DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`packaging_calculate`(isPackageReturnable TINYINT(1), packagingReturnFk INT(11), base DECIMAL(10,2), price DECIMAL(10,2), upload VARCHAR(255)) RETURNS decimal(10,2) DETERMINISTIC BEGIN DECLARE vAmount INT DEFAULT NULL; DECLARE vValue DECIMAL(10,2); IF isPackageReturnable THEN SELECT cb.freightPackagingFull INTO vAmount FROM returnBuckets cb WHERE cb.id = packagingReturnFk; SET vValue = IF (vAmount IS NULL, IFNULL(base,0), vAmount / IFNULL(upload, 0) + IFNULL(base, 0)); ELSE SET vValue = IFNULL(price, 0) + IFNULL(base, 0); END IF; RETURN vValue; END$$ DELIMITER ;