From 7c17250919a3b78c354da6f3290d29e1d7944863 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 16 Dec 2021 15:19:56 +0100 Subject: [PATCH] trigger payment_beforeInsert sql --- .../10400-christmas/payment_beforeInsert.sql | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 db/changes/10400-christmas/payment_beforeInsert.sql diff --git a/db/changes/10400-christmas/payment_beforeInsert.sql b/db/changes/10400-christmas/payment_beforeInsert.sql new file mode 100644 index 000000000..537bf0c54 --- /dev/null +++ b/db/changes/10400-christmas/payment_beforeInsert.sql @@ -0,0 +1,61 @@ +DROP TRIGGER IF EXISTS vn.payment_beforeInsert; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`%` TRIGGER `payment_beforeInsert` BEFORE INSERT ON `payment` FOR EACH ROW +-- Edit trigger body code below this line. Do not edit lines above this one +BEGIN + DECLARE cuenta_banco,cuenta_proveedor DOUBLE; + DECLARE vNewBookEntry INT; + DECLARE bolCASH BOOLEAN; + DECLARE isSupplierActive BOOLEAN; + DECLARE vIsPayMethodChecked BOOLEAN; + + -- PAK 10/02/15 No se asientan los pagos directamente, salvo en el caso de las cajas de CASH + SELECT (at2.code = 'cash') INTO bolCASH FROM vn.bank b JOIN vn.accountingType at2 ON at2.id = b.cash WHERE b.id = NEW.bankFk ; + + IF bolCASH THEN + + SELECT account INTO cuenta_banco + FROM bank + WHERE id = NEW.bankFk; + + SELECT account INTO cuenta_proveedor + FROM supplier + WHERE id = NEW.supplierFk; + + CALL vn.ledger_next(vNewBookEntry); + + 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 cuenta_banco SUBCTA,cuenta_proveedor CONTRA,0 EURODEBE, NEW.amount + NEW.bankingFees EUROHABER + UNION ALL + SELECT cuenta_proveedor SUBCTA, cuenta_banco CONTRA,NEW.amount EURODEBE, 0 EUROHABER + ) gf; + + IF NEW.bankingFees <> 0 THEN + INSERT INTO XDiario (ASIEN,FECHA,SUBCTA,CONTRA,CONCEPTO,EURODEBE,EUROHABER,empresa_id) + SELECT vNewBookEntry,NEW.received,IF(c.id = 1,6260000002,IF(CEE = 1,6260000003,6260000004)),cuenta_banco,NEW.concept,NEW.bankingFees,0,NEW.companyFk + FROM supplier s + JOIN country c ON s.countryFk = c.id + WHERE s.id = NEW.supplierFk; + END IF; + END IF; + + SET NEW.dueDated = IFNULL(NEW.dueDated, NEW.received); + + SELECT isActive, isPayMethodChecked INTO vIsSupplierActive, vIsPayMethodChecked + FROM supplier + WHERE id = NEW.supplierFk; + + IF isSupplierActive = FALSE THEN + CALL util.throw('SUPPLIER_INACTIVE'); + END IF; + + IF vIsPayMethodChecked = FALSE THEN + CALL util.throw('SUPPLIER_DOES_NOT_HAVE_PAYMETHODCHECKED'); + END IF; + + END$$ +DELIMITER ;