diff --git a/db/routines/sage/procedures/invoiceIn_add.sql b/db/routines/sage/procedures/invoiceIn_add.sql index 0898d6810..ba7a0c6c0 100644 --- a/db/routines/sage/procedures/invoiceIn_add.sql +++ b/db/routines/sage/procedures/invoiceIn_add.sql @@ -4,10 +4,10 @@ BEGIN /** * Traslada la info de contabilidad relacionada con las facturas recibidas * - * @vInvoiceInFk Factura recibida - * @vXDiarioFk Id tabla XDiario + * @param vInvoiceInFk Factura recibida + * @param vXDiarioFk Id tabla XDiario */ - DECLARE vInvoiceInOriginalFk INT; + DECLARE vInvoiceInOriginalFk INT; DECLARE vDone BOOL DEFAULT FALSE; DECLARE vBase DOUBLE; DECLARE vVat DOUBLE; @@ -204,9 +204,9 @@ BEGIN WHERE correctingFk = vInvoiceInFk; IF vInvoiceInOriginalFk THEN - UPDATE movContaIVA mci - JOIN vn.invoiceInRefund iir ON iir.invoiceInRefundFk = vInvoiceInFk + JOIN vn.invoiceInCorrection iic ON iic.correctingFk = vInvoiceInFk + JOIN vn.siiTypeInvoiceOut st ON st.id = iic.siiTypeInvoiceOutFk JOIN (SELECT issued, SUM(sub.taxableBase) taxableBase, SUM(ROUND((sub.taxableBase * sub.PorcentajeIva) / 100 , 2)) vat @@ -215,20 +215,20 @@ BEGIN ti.PorcentajeIva FROM vn.invoiceIn i JOIN vn.invoiceInTax iit ON iit.invoiceInFk = i.id - JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk + JOIN TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk WHERE i.id = vInvoiceInOriginalFk GROUP BY ti.CodigoIva)sub )invoiceInOriginal JOIN ClavesOperacion co ON co.Descripcion = 'Factura rectificativa' - SET mci.TipoRectificativa = iir.refundCategoryFk, - mci.ClaseAbonoRectificativas = iir.refundType, + SET mci.TipoRectificativa = iic.cplusRectificationTypeFk, + mci.ClaseAbonoRectificativas = REGEXP_REPLACE(st.`code`, '[^0-9]', ''), mci.FechaFacturaOriginal = invoiceInOriginal.issued, mci.FechaOperacion = invoiceInOriginal.issued, mci.BaseImponibleOriginal = invoiceInOriginal.taxableBase, mci.CuotaIvaOriginal = invoiceInOriginal.vat, mci.ClaveOperacionFactura = co.ClaveOperacionFactura_ WHERE mci.id = vXDiarioFk; - END IF; END$$ +$$ DELIMITER ; \ No newline at end of file diff --git a/db/routines/sage/procedures/invoiceOut_add.sql b/db/routines/sage/procedures/invoiceOut_add.sql index 95d6a56dd..f9c6f6b87 100644 --- a/db/routines/sage/procedures/invoiceOut_add.sql +++ b/db/routines/sage/procedures/invoiceOut_add.sql @@ -169,6 +169,7 @@ BEGIN UPDATE movContaIVA mci JOIN vn.invoiceOut i ON i.id = vInvoiceOutCorrectedFk JOIN vn.invoiceCorrection ic ON ic.correctedFk = vInvoiceOutCorrectedFk + JOIN vn.siiTypeInvoiceOut st ON st.id = ic.siiTypeInvoiceOutFk JOIN (SELECT SUM(IF(IFNULL(e.vatFk, TRUE), iot.taxableBase, 0)) taxableBase, SUM(IF(IFNULL(e.vatFk, TRUE), iot.vat, 0)) vat, SUM(IF(IFNULL(e.vatFk, TRUE), 0, iot.vat)) equ @@ -177,8 +178,8 @@ BEGIN WHERE iot.invoiceOutFk = vInvoiceOutCorrectedFk ) tax JOIN ClavesOperacion co ON co.Descripcion = 'Factura rectificativa' - SET mci.TipoRectificativa = 2, - mci.ClaseAbonoRectificativas = 1, + SET mci.TipoRectificativa = ic.cplusRectificationTypeFk, + mci.ClaseAbonoRectificativas = REGEXP_REPLACE(st.`code`, '[^0-9]', ''), mci.FechaFacturaOriginal = i.issued, mci.FechaOperacion = i.issued, mci.BaseImponibleOriginal = tax.taxableBase, diff --git a/db/routines/vn/procedures/invoiceIn_booking.sql b/db/routines/vn/procedures/invoiceIn_booking.sql index 36189afcc..3d1ca402a 100644 --- a/db/routines/vn/procedures/invoiceIn_booking.sql +++ b/db/routines/vn/procedures/invoiceIn_booking.sql @@ -43,7 +43,7 @@ BEGIN ii.cplusTaxBreakFk, ii.cplusSubjectOpFk, ii.siiTypeInvoiceInFk, - ii.cplusRectificationTypeFk, + ic.cplusRectificationTypeFk, ii.booked, IFNULL(a.isUeeMember, c.isUeeMember) isUeeMember, (c.id = cc.id) isSameCountry, @@ -66,6 +66,7 @@ BEGIN e.name expenseName FROM invoiceIn ii JOIN supplier s ON s.id = ii.supplierFk + LEFT JOIN invoiceInCorrection ic ON ic.correctingFk = ii.id LEFT JOIN province p ON p.id = s.provinceFk LEFT JOIN autonomy a ON a.id = p.autonomyFk JOIN country c ON c.id = s.countryFk diff --git a/db/versions/11266-goldenChrysanthemum/00-firstScript.sql b/db/versions/11266-goldenChrysanthemum/00-firstScript.sql new file mode 100644 index 000000000..37143c99b --- /dev/null +++ b/db/versions/11266-goldenChrysanthemum/00-firstScript.sql @@ -0,0 +1,47 @@ + USE vn; + + DROP TRIGGER IF EXISTS invoiceIn_beforeUpdate; + + UPDATE vn.invoiceIn + SET cplusRectificationTypeFk = NULL + WHERE cplusRectificationTypeFk = 1; + + DELETE IGNORE FROM vn.cplusRectificationType WHERE id = 1; + + UPDATE vn.cplusRectificationType + SET id = 1 + WHERE id = 3; + +DELIMITER $$ +CREATE OR REPLACE DEFINER=`vn`@`localhost` TRIGGER `vn`.`invoiceIn_beforeUpdate` + BEFORE UPDATE ON `invoiceIn` + FOR EACH ROW +BEGIN + DECLARE vWithholdingSageFk INT; + + IF NOT (NEW.supplierRef <=> OLD.supplierRef) AND NOT util.checkPrintableChars(NEW.supplierRef) THEN + CALL util.throw('The invoiceIn reference contains invalid characters'); + END IF; + + SET NEW.editorFk = account.myUser_getId(); + + IF (SELECT COUNT(*) FROM vn.invoiceIn + WHERE supplierRef = NEW.supplierRef + AND supplierFk = NEW.supplierFk + AND YEAR(issued) = YEAR(NEW.issued) + AND id <> NEW.id + ) THEN + CALL util.throw('reference duplicated'); + END IF; + + IF NEW.supplierFk != OLD.supplierFk THEN + CALL supplier_checkIsActive(NEW.supplierFk); + SELECT withholdingSageFk INTO vWithholdingSageFk + FROM vn.supplier + WHERE id = NEW.supplierFk; + SET NEW.withholdingSageFk = vWithholdingSageFk; + END IF; + +END$$ +DELIMITER ; +