2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`payment_beforeInsert`
|
|
|
|
BEFORE INSERT ON `payment`
|
|
|
|
FOR EACH ROW
|
|
|
|
BEGIN
|
2024-05-16 16:48:59 +00:00
|
|
|
DECLARE vBankAccount DOUBLE;
|
|
|
|
DECLARE vSupplierAccount DOUBLE;
|
|
|
|
DECLARE vNewBookEntry INT;
|
|
|
|
DECLARE vIsCash BOOLEAN;
|
|
|
|
DECLARE vIsSupplierActive BOOLEAN;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT (at2.code = 'cash') INTO vIsCash
|
|
|
|
FROM accounting a
|
|
|
|
JOIN accountingType at2 ON at2.id = a.accountingTypeFk
|
|
|
|
WHERE a.id = NEW.bankFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-28 12:50:58 +00:00
|
|
|
IF vIsCash THEN
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT account INTO vBankAccount
|
|
|
|
FROM accounting
|
|
|
|
WHERE id = NEW.bankFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT account INTO vSupplierAccount
|
|
|
|
FROM supplier
|
|
|
|
WHERE id = NEW.supplierFk;
|
|
|
|
|
2024-06-03 18:16:27 +00:00
|
|
|
CALL ledger_next(YEAR(NEW.received), vNewBookEntry);
|
2024-05-16 16:48:59 +00:00
|
|
|
|
|
|
|
INSERT INTO XDiario (
|
|
|
|
ASIEN,
|
|
|
|
FECHA,
|
|
|
|
SUBCTA,
|
|
|
|
CONTRA,
|
|
|
|
CONCEPTO,
|
|
|
|
EURODEBE,
|
|
|
|
EUROHABER,
|
|
|
|
empresa_id)
|
|
|
|
SELECT vNewBookEntry,
|
|
|
|
NEW.received,
|
|
|
|
SUBCTA,
|
|
|
|
CONTRA,
|
|
|
|
NEW.concept,
|
|
|
|
EURODEBE,
|
|
|
|
EUROHABER,
|
|
|
|
NEW.companyFk
|
|
|
|
FROM (SELECT vBankAccount SUBCTA,
|
|
|
|
vSupplierAccount CONTRA,
|
|
|
|
0 EURODEBE,
|
|
|
|
NEW.amount + NEW.bankingFees EUROHABER
|
|
|
|
UNION ALL
|
|
|
|
SELECT vSupplierAccount SUBCTA,
|
|
|
|
vBankAccount CONTRA,
|
|
|
|
NEW.amount EURODEBE,
|
|
|
|
0 EUROHABER) gf;
|
|
|
|
|
|
|
|
IF NEW.bankingFees <> 0 THEN
|
|
|
|
INSERT INTO XDiario (
|
|
|
|
ASIEN,
|
|
|
|
FECHA,
|
|
|
|
SUBCTA,
|
|
|
|
CONTRA,
|
|
|
|
CONCEPTO,
|
|
|
|
EURODEBE,
|
|
|
|
EUROHABER,
|
|
|
|
empresa_id)
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT vNewBookEntry,
|
|
|
|
NEW.received,
|
2024-05-16 16:48:59 +00:00
|
|
|
IF(c.id = 1,6260000002,
|
|
|
|
IF(CEE = 1,6260000003,6260000004)),
|
|
|
|
vBankAccount,
|
2024-01-15 11:31:03 +00:00
|
|
|
NEW.concept,
|
2024-05-16 16:48:59 +00:00
|
|
|
NEW.bankingFees,
|
|
|
|
0,
|
2024-01-15 11:31:03 +00:00
|
|
|
NEW.companyFk
|
2024-05-16 16:48:59 +00:00
|
|
|
FROM supplier s
|
|
|
|
JOIN country c ON s.countryFk = c.id
|
|
|
|
WHERE s.id = NEW.supplierFk;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SET NEW.dueDated = IFNULL(NEW.dueDated, NEW.received);
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
SELECT isActive INTO vIsSupplierActive
|
|
|
|
FROM supplier
|
|
|
|
WHERE id = NEW.supplierFk;
|
|
|
|
|
|
|
|
IF vIsSupplierActive = FALSE THEN
|
|
|
|
CALL util.throw('SUPPLIER_INACTIVE');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
IF ISNULL(NEW.workerFk) THEN
|
|
|
|
SET NEW.workerFk = account.myUser_getId();
|
|
|
|
END IF;
|
|
|
|
END$$
|
2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER ;
|