DROP TRIGGER IF EXISTS `BeneficiaryScripts_au`;
COMMIT;

DELIMITER //

CREATE TRIGGER `BeneficiaryScripts_au` AFTER UPDATE ON `BeneficiaryScripts` FOR EACH ROW

BEGIN

  DECLARE dtEndDate datetime;

  IF old.status = 'IMPORTING' AND new.status = 'READY' THEN

    UPDATE `BeneficiaryScriptLines`
    SET `status` = 'PENDING',
      `updatedAt` = NOW()
    WHERE `beneficiaryScriptId` = new.ID;

  ELSEIF old.status = 'IMPORTING' AND new.status = 'INCOMPLETE' THEN

    UPDATE `BeneficiaryScriptLines`
    SET `status` = 'INCOMPLETE',
      `updatedAt` = NOW()
    WHERE `beneficiaryScriptId` = new.ID;
    
  END IF;

END
//

COMMIT //

DELIMITER ;
