Merge branch 'dev' into refs-#6665-Clean-Views-vn2008-(1/9)
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Ivan Mas 2024-02-06 08:51:57 +00:00
commit c3912dd098
22 changed files with 592 additions and 117 deletions

View File

@ -13,7 +13,7 @@ RUN apt-get update \
graphicsmagick \ graphicsmagick \
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \ && apt-get install -y --no-install-recommends nodejs \
&& curl -fsSL https://get.pnpm.io/install.sh | env PNPM_VERSION=8.15.1 sh - && corepack enable pnpm
# Puppeteer # Puppeteer

View File

@ -20,16 +20,16 @@ BEGIN
c.movil, c.movil,
c.POBLACION poblacion, c.POBLACION poblacion,
p.`name` provincia, p.`name` provincia,
vn2008.red(f.futur) futur, ROUND(f.futur, 2) futur,
c.Credito credito, c.Credito credito,
pm.`name` forma_pago, pm.`name` forma_pago,
vn2008.red(c365 / 12) consumo_medio365, ROUND(c365 / 12, 2) consumo_medio365,
vn2008.red(c365) consumo365, ROUND(c365, 2) consumo365,
vn2008.red(CmLy.peso) peso_mes_año_pasado, ROUND(CmLy.peso, 2) peso_mes_año_pasado,
vn2008.red(CmLy.peso * 1.19) objetivo, ROUND(CmLy.peso * 1.19, 2) objetivo,
tr.CodigoTrabajador, tr.CodigoTrabajador,
vn2008.red(mes_actual.consumo) consumoMes, ROUND(mes_actual.consumo, 2) consumoMes,
vn2008.red(IFNULL(mes_actual.consumo, 0) - IFNULL(CmLy.peso * 1.19, 0)) como_lo_llevo, ROUND(IFNULL(mes_actual.consumo, 0) - IFNULL(CmLy.peso * 1.19, 0), 2) como_lo_llevo,
DATE(LastTicket) ultimo_ticket, DATE(LastTicket) ultimo_ticket,
dead.muerto, dead.muerto,
g.Greuge, g.Greuge,

View File

@ -5,7 +5,8 @@ proc: BEGIN
DECLARE vReserveDate DATETIME; DECLARE vReserveDate DATETIME;
DECLARE vParams CHAR(100); DECLARE vParams CHAR(100);
DECLARE vInventoryDate DATE; DECLARE vInventoryDate DATE;
DECLARE vIsLogifloraDay BOOLEAN; DECLARE vLifeScope DATE;
DECLARE vWarehouseFkInventory INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN BEGIN
@ -26,43 +27,42 @@ proc: BEGIN
LEAVE proc; LEAVE proc;
END IF; END IF;
-- Invoca al procedimiento que genera el stock virtual de Logiflora, si coincide con la peticion de refresco del disponible
IF vn.isLogifloraDay(vDated, vWarehouse) THEN
-- CALL edi.floramondo_offerRefresh;
SET vIsLogifloraDay = TRUE;
ELSE
SET vIsLogifloraDay = FALSE;
END IF;
-- Calcula algunos parámetros necesarios -- Calcula algunos parámetros necesarios
SET vStartDate = TIMESTAMP(vDated, '00:00:00'); SET vStartDate = TIMESTAMP(vDated, '00:00:00');
SELECT inventoried INTO vInventoryDate FROM vn.config; SELECT inventoried INTO vInventoryDate FROM vn.config;
SELECT DATE_SUB(vStartDate, INTERVAL MAX(life) DAY) INTO vLifeScope FROM vn.itemType;
SELECT SUBTIME(util.VN_NOW(), reserveTime) INTO vReserveDate SELECT SUBTIME(util.VN_NOW(), reserveTime) INTO vReserveDate
FROM hedera.orderConfig; FROM hedera.orderConfig;
SELECT w.id INTO vWarehouseFkInventory
FROM vn.warehouse w
WHERE w.code = 'inv';
-- Calcula el ultimo dia de vida para cada producto -- Calcula el ultimo dia de vida para cada producto
DROP TEMPORARY TABLE IF EXISTS itemRange; DROP TEMPORARY TABLE IF EXISTS itemRange;
CREATE TEMPORARY TABLE itemRange CREATE TEMPORARY TABLE itemRange
(PRIMARY KEY (itemFk)) (PRIMARY KEY (itemFk))
ENGINE = MEMORY ENGINE = MEMORY
SELECT c.itemFk, SELECT i.id itemFk,
IF(it.life IS NULL, util.dayEnd(DATE_ADD(c.maxLanded, INTERVAL it.life DAY)) ended, it.life
NULL, FROM vn.item i
TIMESTAMP(TIMESTAMPADD(DAY, it.life, c.landing), '23:59:59')) ended LEFT JOIN (
FROM ( SELECT b.itemFk, MAX(t.landed) maxLanded
SELECT b.itemFk, MAX(t.landed) landing
FROM vn.buy b FROM vn.buy b
JOIN vn.entry e ON b.entryFk = e.id JOIN vn.entry e ON b.entryFk = e.id
JOIN vn.travel t ON t.id = e.travelFk JOIN vn.travel t ON t.id = e.travelFk
JOIN vn.warehouse w ON w.id = t.warehouseInFk JOIN vn.warehouse w ON w.id = t.warehouseInFk
WHERE t.landed BETWEEN vInventoryDate AND vStartDate JOIN vn.item i ON i.id = b.itemFk
JOIN vn.itemType it ON it.id = i.typeFk
WHERE t.landed BETWEEN vLifeScope AND vStartDate
AND t.warehouseInFk = vWarehouse AND t.warehouseInFk = vWarehouse
AND t.warehouseOutFk <> vWarehouseFkInventory
AND it.life
AND NOT e.isExcludedFromAvailable AND NOT e.isExcludedFromAvailable
GROUP BY b.itemFk GROUP BY b.itemFk
) c ) c ON i.id = c.itemFk
JOIN vn.item i ON i.id = c.itemFk
JOIN vn.itemType it ON it.id = i.typeFk JOIN vn.itemType it ON it.id = i.typeFk
HAVING ended >= vStartDate OR ended IS NULL; HAVING ended >= vStartDate OR life IS NULL;
-- Calcula el ATP -- Calcula el ATP
DELETE FROM available WHERE calc_id = vCalc; DELETE FROM available WHERE calc_id = vCalc;
@ -86,7 +86,7 @@ proc: BEGIN
WHERE i.landed >= vStartDate WHERE i.landed >= vStartDate
AND (ir.ended IS NULL OR i.landed <= ir.ended) AND (ir.ended IS NULL OR i.landed <= ir.ended)
AND i.warehouseInFk = vWarehouse AND i.warehouseInFk = vWarehouse
AND (ISNULL(wf.entryFk) OR vIsLogifloraDay) AND ISNULL(wf.entryFk)
UNION ALL UNION ALL
SELECT i.itemFk, i.shipped, i.quantity SELECT i.itemFk, i.shipped, i.quantity
FROM vn.itemEntryOut i FROM vn.itemEntryOut i

View File

@ -0,0 +1,234 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `sage`.`invoiceIn_add`(vInvoiceInFk INT, vXDiarioFk INT)
BEGIN
/**
* Traslada la info de contabilidad relacionada con las facturas recibidas
*
* @vInvoiceInFk Factura recibida
* @vXDiarioFk Id tabla XDiario
*/
DECLARE vInvoiceInOriginalFk INT;
DECLARE vDone BOOL DEFAULT FALSE;
DECLARE vBase DOUBLE;
DECLARE vVat DOUBLE;
DECLARE vRate DOUBLE;
DECLARE vTransactionCode INT;
DECLARE vCounter INT DEFAULT 0;
DECLARE vTransactionCodeOld INT;
DECLARE vTaxCode INT;
DECLARE vTaxCodeOld INT;
DECLARE vOperationCode VARCHAR(1);
DECLARE vIsIntracommunity BOOL DEFAULT FALSE;
DECLARE vSerialDua VARCHAR(1) DEFAULT 'D';
DECLARE vInvoiceTypeReceived VARCHAR(1);
DECLARE vInvoiceTypeInformative VARCHAR(1);
DECLARE vIsInformativeExportation BOOL DEFAULT FALSE;
DECLARE vCursor CURSOR FOR
SELECT it.taxableBase,
CAST((( it.taxableBase / 100) * t.PorcentajeIva) AS DECIMAL (10,2)),
t.PorcentajeIva,
it.transactionTypeSageFk,
it.taxTypeSageFk,
tty.isIntracommunity,
tt.ClaveOperacionDefecto
FROM vn.invoiceIn i
JOIN vn.invoiceInTax it ON it.InvoiceInFk = i.id
JOIN TiposIva t ON t.CodigoIva = it.taxTypeSageFk
JOIN taxType tty ON tty.id = t.CodigoIva
JOIN TiposTransacciones tt ON tt.CodigoTransaccion = it.transactionTypeSageFk
LEFT JOIN vn.dua d ON d.id = vInvoiceInFk
WHERE i.id = vInvoiceInFk
AND d.id IS NULL;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
DELETE FROM movContaIVA
WHERE id = vXDiarioFk;
SELECT codeSage INTO vInvoiceTypeReceived
FROM invoiceType WHERE code ='received';
SELECT codeSage INTO vInvoiceTypeInformative
FROM invoiceType WHERE code ='informative';
INSERT INTO movContaIVA(id, LibreA1)
VALUES (vXDiarioFk, vInvoiceInFk);
OPEN vCursor;
l: LOOP
FETCH vCursor INTO vBase,
vVat,
vRate,
vTransactionCode,
vTaxCode,
vIsIntracommunity,
vOperationCode;
IF vDone THEN
LEAVE l;
END IF;
SET vTransactionCodeOld = vTransactionCode;
SET vTaxCodeOld = vTaxCode;
IF vOperationCode IS NOT NULL THEN
UPDATE movContaIVA
SET ClaveOperacionFactura = vOperationCode
WHERE id = vXDiarioFk;
END IF;
SET vCounter = vCounter + 1;
CASE vCounter
WHEN 1 THEN
UPDATE movContaIVA
SET BaseIva1 = vBase,
PorIva1 = vRate,
CuotaIva1 = vVat,
CodigoTransaccion1 = vTransactionCode,
CodigoIva1 = vTaxCode
WHERE id = vXDiarioFk;
WHEN 2 THEN
UPDATE movContaIVA
SET BaseIva2 = vBase,
PorIva2 = vRate,
CuotaIva2 = vVat,
CodigoTransaccion2 = vTransactionCode,
CodigoIva2 = vTaxCode
WHERE id = vXDiarioFk;
WHEN 3 THEN
UPDATE movContaIVA
SET BaseIva3 = vBase,
PorIva3 = vRate,
CuotaIva3 = vVat,
CodigoTransaccion3 = vTransactionCode,
CodigoIva3 = vTaxCode
WHERE id = vXDiarioFk;
WHEN 4 THEN
UPDATE movContaIVA
SET BaseIva4 = vBase,
PorIva4 = vRate,
CuotaIva4 = vVat,
CodigoTransaccion4 = vTransactionCode,
CodigoIva4 = vTaxCode
WHERE id = vXDiarioFk;
ELSE
SELECT vXDiarioFk INTO vXDiarioFk;
END CASE;
IF vIsIntracommunity THEN
UPDATE movContaIVA
SET Intracomunitaria = TRUE
WHERE id = vXDiarioFk;
END IF;
SET vTransactionCodeOld = vTransactionCode;
SET vTaxCodeOld = vTaxCode;
END LOOP;
CLOSE vCursor;
SELECT d.ASIEN AND x.ASIEN IS NULL INTO vIsInformativeExportation
FROM vn.dua d
LEFT JOIN vn.XDiario x ON x.ASIEN = d.ASIEN
AND x.SERIE = vSerialDua COLLATE utf8mb3_unicode_ci
WHERE d.ASIEN = (
SELECT ASIEN
FROM vn.XDiario
WHERE id = vXDiarioFk)
LIMIT 1;
UPDATE movContaIVA mci
JOIN tmp.invoiceIn ii ON ii.id = vInvoiceInFk
JOIN vn.XDiario x ON x.id = mci.id
LEFT JOIN tmp.invoiceDua id ON id.id = mci.id
JOIN vn.supplier s ON s.id = ii.supplierFk
JOIN Naciones n ON n.countryFk = s.countryFk
SET mci.CodigoDivisa = ii.currencyFk,
mci.Año = YEAR(ii.issued),
mci.Serie = ii.serial,
mci.Factura = ii.id,
mci.FechaFactura = ii.issued,
mci.ImporteFactura = IFNULL(mci.BaseIva1, 0) + IFNULL(mci.CuotaIva1, 0) +
IFNULL(mci.BaseIva2, 0) + IFNULL(mci.CuotaIva2, 0) +
IFNULL(mci.BaseIva3, 0) + IFNULL(mci.CuotaIva3, 0) +
IFNULL(mci.BaseIva4, 0) + IFNULL(mci.CuotaIva4, 0),
mci.TipoFactura = IF(id.id,
IF( ii.serial = vSerialDua COLLATE utf8mb3_unicode_ci, vInvoiceTypeReceived, vInvoiceTypeInformative),
IF(vIsInformativeExportation,vInvoiceTypeInformative, vInvoiceTypeReceived)),
mci.CodigoCuentaFactura = x.SUBCTA,
mci.CifDni = IF(LEFT(TRIM(s.nif), 2) = n.SiglaNacion, SUBSTRING(TRIM(s.nif), 3), s.nif),
mci.Nombre = s.name,
mci.SiglaNacion = n.SiglaNacion,
mci.EjercicioFactura = YEAR(ii.issued),
mci.FechaOperacion = ii.issued,
mci.MantenerAsiento = TRUE,
mci.SuFacturaNo = ii.supplierRef,
mci.IvaDeducible1 = IF(id.id, FALSE, IF(IFNULL(mci.BaseIva1, FALSE) = FALSE, FALSE, ii.isVatDeductible)),
mci.IvaDeducible2 = IF(id.id, FALSE, IF(IFNULL(mci.BaseIva2, FALSE) = FALSE, FALSE, ii.isVatDeductible)),
mci.IvaDeducible3 = IF(id.id, FALSE, IF(IFNULL(mci.BaseIva3, FALSE) = FALSE, FALSE, ii.isVatDeductible)),
mci.IvaDeducible4 = IF(id.id, FALSE, IF(IFNULL(mci.BaseIva4, FALSE) = FALSE, FALSE, ii.isVatDeductible)),
mci.FechaFacturaOriginal = x.FECHA_EX
WHERE mci.id = vXDiarioFk;
-- RETENCIONES
UPDATE movContaIVA mci
JOIN vn.invoiceIn ii ON ii.id = vInvoiceInFk
JOIN vn.XDiario x ON x.id = mci.id
JOIN vn.supplier s ON s.id = supplierFk
JOIN vn.invoiceInTax iit ON iit.invoiceInFk = ii.id
JOIN vn.expense e ON e.id = iit.expenseFk
JOIN TiposRetencion t ON t.CodigoRetencion = ii.withholdingSageFk
LEFT JOIN tmp.invoiceDua id ON id.id = mci.id
JOIN (SELECT SUM(x2.BASEEURO) taxableBase, SUM(x2.EURODEBE) taxBase
FROM vn.XDiario x1
JOIN vn.XDiario x2 ON x1.ASIEN = x2.ASIEN
WHERE x2.BASEEURO <> 0
AND x1.id = vXDiarioFk
)sub
JOIN ClavesOperacion co ON co.Descripcion = 'Arrendamiento de locales de negocio'
SET mci.CodigoRetencion = t.CodigoRetencion,
mci.ClaveOperacionFactura = IF( t.Retencion = 'ARRENDAMIENTO Y SUBARRENDAMIENTO', co.ClaveOperacionFactura_, mci.ClaveOperacionFactura),
mci.BaseRetencion = IF (t.Retencion = 'ACTIVIDADES AGRICOLAS O GANADERAS', sub.taxableBase + sub.taxBase, sub.taxableBase),
mci.PorRetencion = t.PorcentajeRetencion,
mci.ImporteRetencion = iit.taxableBase * - 1
WHERE mci.id = vXDiarioFk
AND e.name = 'Retenciones'
AND id.id IS NULL;
SELECT correctedFk INTO vInvoiceInOriginalFk
FROM vn.invoiceInCorrection
WHERE correctingFk = vInvoiceInFk;
IF vInvoiceInOriginalFk THEN
UPDATE movContaIVA mci
JOIN vn.invoiceInRefund iir ON iir.invoiceInRefundFk = vInvoiceInFk
JOIN (SELECT issued,
SUM(sub.taxableBase) taxableBase,
SUM(ROUND((sub.taxableBase * sub.PorcentajeIva) / 100 , 2)) vat
FROM(SELECT issued,
SUM(iit.taxableBase) taxableBase,
ti.PorcentajeIva
FROM vn.invoiceIn i
JOIN vn.invoiceInTax iit ON iit.invoiceInFk = i.id
JOIN sage.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,
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 ;

View File

@ -1,7 +1,13 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingRadar`(vSectorFk INT) CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingRadar`(
vSectorFk INT
)
proc:BEGIN proc:BEGIN
/**
* Calcula la información detallada respecto un sector.
*
* @param vSectorFk Id de sector
*/
DECLARE vCalcVisibleFk INT; DECLARE vCalcVisibleFk INT;
DECLARE vCalcAvailableFk INT; DECLARE vCalcAvailableFk INT;
DECLARE hasFatherSector BOOLEAN; DECLARE hasFatherSector BOOLEAN;

View File

@ -1,7 +1,7 @@
CREATE OR REPLACE DEFINER=`root`@`localhost` CREATE OR REPLACE DEFINER=`root`@`localhost`
SQL SECURITY DEFINER SQL SECURITY DEFINER
VIEW `vn`.`itemShelvingAvailable` VIEW `vn`.`itemShelvingAvailable`
AS SELECT `s`.`id` AS `saleFk`, AS SELECT `s`.`id` `saleFk`,
`tst`.`updated` AS `Modificado`, `tst`.`updated` AS `Modificado`,
`s`.`ticketFk` AS `ticketFk`, `s`.`ticketFk` AS `ticketFk`,
0 AS `isPicked`, 0 AS `isPicked`,
@ -38,7 +38,7 @@ FROM (
) )
JOIN `vn`.`agencyMode` `am` ON(`am`.`id` = `t`.`agencyModeFk`) JOIN `vn`.`agencyMode` `am` ON(`am`.`id` = `t`.`agencyModeFk`)
) )
JOIN `vn`.`ticketStateToday` `tst` ON(`tst`.`ticket` = `t`.`id`) JOIN `vn`.`ticketStateToday` `tst` ON(`tst`.`ticketFk` = `t`.`id`)
) )
JOIN `vn`.`state` `st` ON(`st`.`id` = `tst`.`state`) JOIN `vn`.`state` `st` ON(`st`.`id` = `tst`.`state`)
) )

View File

@ -1,14 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn2008`.`red`(intCANTIDAD DOUBLE)
RETURNS double
DETERMINISTIC
BEGIN
DECLARE n DOUBLE;
SET n = SIGN(intCANTIDAD) * TRUNCATE( (ABS(intCANTIDAD) * 100) + 0.5001 ,0) /100 ;
RETURN n;
END$$
DELIMITER ;

View File

@ -0,0 +1 @@
REVOKE EXECUTE ON FUNCTION vn2008.red FROM hrBoss, salesPerson;

View File

@ -0,0 +1,4 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('InvoiceIn', 'corrective', 'WRITE', 'ALLOW', 'ROLE', 'administrative'),
('InvoiceInCorrection', '*', '*', 'ALLOW', 'ROLE', 'administrative');

View File

@ -0,0 +1,17 @@
CREATE TABLE IF NOT EXISTS `vn`.`invoiceInCorrection` (
`correctingFk` mediumint(8) UNSIGNED NOT NULL COMMENT 'Factura rectificativa',
`correctedFk` mediumint(8) UNSIGNED NOT NULL COMMENT 'Factura rectificada',
`cplusRectificationTypeFk` int(10) UNSIGNED NOT NULL,
`siiTypeInvoiceOutFk` int(10) UNSIGNED NOT NULL,
`invoiceCorrectionTypeFk` int(11) NOT NULL DEFAULT 3,
PRIMARY KEY (`correctingFk`),
KEY `invoiceInCorrection_correctedFk` (`correctedFk`),
KEY `invoiceInCorrection_cplusRectificationTypeFk` (`cplusRectificationTypeFk`),
KEY `invoiceInCorrection_siiTypeInvoiceOut` (`siiTypeInvoiceOutFk`),
KEY `invoiceInCorrection_invoiceCorrectionTypeFk` (`invoiceCorrectionTypeFk`),
CONSTRAINT `invoiceInCorrection_correctedFk` FOREIGN KEY (`correctedFk`) REFERENCES `invoiceIn` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_correctingFk` FOREIGN KEY (`correctingFk`) REFERENCES `invoiceIn` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_siiTypeInvoiceOut` FOREIGN KEY (`siiTypeInvoiceOutFk`) REFERENCES `siiTypeInvoiceOut` (`id`) ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_invoiceCorrectionTypeFk` FOREIGN KEY (`invoiceCorrectionTypeFk`) REFERENCES `invoiceCorrectionType` (`id`) ON UPDATE CASCADE,
CONSTRAINT `invoiceInCorrection_cplusRectificationTypeFk` FOREIGN KEY (`cplusRectificationTypeFk`) REFERENCES `cplusRectificationType` (`id`) ON UPDATE CASCADE
);

View File

@ -2,13 +2,17 @@ module.exports = Self => {
Self.remoteMethodCtx('clone', { Self.remoteMethodCtx('clone', {
description: 'Clone the invoiceIn and as many invoiceInTax and invoiceInDueDay referencing it', description: 'Clone the invoiceIn and as many invoiceInTax and invoiceInDueDay referencing it',
accessType: 'WRITE', accessType: 'WRITE',
accepts: { accepts: [{
arg: 'id', arg: 'id',
type: 'number', type: 'number',
required: true, required: true,
description: 'The invoiceIn id', description: 'The invoiceIn id',
http: {source: 'path'} http: {source: 'path'}
}, }, {
arg: 'isRectification',
type: 'boolean',
description: 'Clone quantities in negative and clone Intrastat'
}],
returns: { returns: {
type: 'object', type: 'object',
root: true root: true
@ -19,7 +23,7 @@ module.exports = Self => {
} }
}); });
Self.clone = async(ctx, id, options) => { Self.clone = async(ctx, id, isRectification, options) => {
const models = Self.app.models; const models = Self.app.models;
let tx; let tx;
const myOptions = {}; const myOptions = {};
@ -45,14 +49,28 @@ module.exports = Self => {
'isVatDeductible', 'isVatDeductible',
'withholdingSageFk', 'withholdingSageFk',
'deductibleExpenseFk', 'deductibleExpenseFk',
] ],
include: [
{
relation: 'invoiceInTax',
},
{
relation: 'invoiceInDueDay',
},
{
relation: 'invoiceInIntrastat'
}
],
}, myOptions); }, myOptions);
const sourceInvoiceInTax = await models.InvoiceInTax.find({where: {invoiceInFk: id}}, myOptions); const invoiceInTax = sourceInvoiceIn.invoiceInTax();
const sourceInvoiceInDueDay = await models.InvoiceInDueDay.find({where: {invoiceInFk: id}}, myOptions); const invoiceInDueDay = sourceInvoiceIn.invoiceInDueDay();
const invoiceInIntrastat = sourceInvoiceIn.invoiceInIntrastat();
const issued = new Date(sourceInvoiceIn.issued); const issued = new Date(sourceInvoiceIn.issued);
issued.setMonth(issued.getMonth() + 1); issued.setMonth(issued.getMonth() + 1);
const clonedRef = sourceInvoiceIn.supplierRef + '(2)'; const totalCorrections = await models.InvoiceInCorrection.count({correctedFk: id}, myOptions);
const clonedRef = sourceInvoiceIn.supplierRef + `(${totalCorrections + 2})`;
const clone = await models.InvoiceIn.create({ const clone = await models.InvoiceIn.create({
serial: sourceInvoiceIn.serial, serial: sourceInvoiceIn.serial,
@ -68,18 +86,19 @@ module.exports = Self => {
const promises = []; const promises = [];
for (let tax of sourceInvoiceInTax) { for (let tax of invoiceInTax) {
promises.push(models.InvoiceInTax.create({ promises.push(models.InvoiceInTax.create({
invoiceInFk: clone.id, invoiceInFk: clone.id,
taxableBase: tax.taxableBase, taxableBase: isRectification ? -tax.taxableBase : tax.taxableBase,
expenseFk: tax.expenseFk, expenseFk: tax.expenseFk,
foreignValue: tax.foreignValue, foreignValue: isRectification ? -tax.foreignValue : tax.foreignValue,
taxTypeSageFk: tax.taxTypeSageFk, taxTypeSageFk: tax.taxTypeSageFk,
transactionTypeSageFk: tax.transactionTypeSageFk transactionTypeSageFk: tax.transactionTypeSageFk
}, myOptions)); }, myOptions));
} }
for (let dueDay of sourceInvoiceInDueDay) { if (!isRectification) {
for (let dueDay of invoiceInDueDay) {
const dueDated = dueDay.dueDated; const dueDated = dueDay.dueDated;
dueDated.setMonth(dueDated.getMonth() + 1); dueDated.setMonth(dueDated.getMonth() + 1);
@ -91,7 +110,20 @@ module.exports = Self => {
foreignValue: dueDated.foreignValue, foreignValue: dueDated.foreignValue,
}, myOptions)); }, myOptions));
} }
} else {
for (let intrastat of invoiceInIntrastat) {
promises.push(models.InvoiceInIntrastat.create({
invoiceInFk: clone.id,
net: -intrastat.net,
intrastatFk: intrastat.intrastatFk,
amount: -intrastat.amount,
stems: -intrastat.stems,
country: intrastat.countryFk,
dated: Date.vnNew(),
statisticalValue: intrastat.statisticalValue
}, myOptions));
}
}
await Promise.all(promises); await Promise.all(promises);
if (tx) await tx.commit(); if (tx) await tx.commit();

View File

@ -0,0 +1,58 @@
module.exports = Self => {
Self.remoteMethodCtx('corrective', {
description: 'Creates a rectificated invoice in',
accessType: 'WRITE',
accepts: [{
arg: 'id',
type: 'number'
}, {
arg: 'invoiceReason',
type: 'number',
}, {
arg: 'invoiceType',
type: 'number',
}, {
arg: 'invoiceClass',
type: 'number'
}],
returns: {
type: 'number',
root: true
},
http: {
path: '/corrective',
verb: 'POST'
}
});
Self.corrective = async(ctx, id, invoiceReason, invoiceType, invoiceClass, options) => {
const models = Self.app.models;
const myOptions = {};
let tx;
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const clone = await Self.clone(ctx, id, true, myOptions);
await models.InvoiceInCorrection.create({
correctingFk: clone.id,
correctedFk: id,
cplusRectificationTypeFk: invoiceType,
siiTypeInvoiceOutFk: invoiceClass,
invoiceCorrectionTypeFk: invoiceReason
}, myOptions);
if (tx) await tx.commit();
return clone.id;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
};

View File

@ -1,4 +1,3 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
const buildFilter = require('vn-loopback/util/filter').buildFilter; const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters; const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
@ -80,6 +79,11 @@ module.exports = Self => {
type: 'boolean', type: 'boolean',
description: 'Whether the invoice is booked or not', description: 'Whether the invoice is booked or not',
}, },
{
arg: 'correctedFk',
type: 'number',
description: 'The corrected invoice',
}
], ],
returns: { returns: {
type: ['object'], type: ['object'],
@ -93,6 +97,7 @@ module.exports = Self => {
Self.filter = async(ctx, filter, options) => { Self.filter = async(ctx, filter, options) => {
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
const models = Self.app.models;
const args = ctx.args; const args = ctx.args;
const myOptions = {}; const myOptions = {};
@ -105,6 +110,14 @@ module.exports = Self => {
dateTo.setHours(23, 59, 0, 0); dateTo.setHours(23, 59, 0, 0);
} }
let correctings;
if (args.correctedFk) {
correctings = await models.InvoiceInCorrection.find({
fields: ['correctingFk'],
where: {correctedFk: args.correctedFk}
});
}
const where = buildFilter(ctx.args, (param, value) => { const where = buildFilter(ctx.args, (param, value) => {
switch (param) { switch (param) {
case 'search': case 'search':
@ -128,6 +141,8 @@ module.exports = Self => {
return {[`ii.${param}`]: value}; return {[`ii.${param}`]: value};
case 'awbCode': case 'awbCode':
return {'sub.code': value}; return {'sub.code': value};
case 'correctedFk':
return {'ii.id': {inq: correctings.map(x => x.correctingFk)}};
} }
}); });

View File

@ -2,50 +2,75 @@ const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
describe('invoiceIn clone()', () => { describe('invoiceIn clone()', () => {
beforeAll(async() => { let ctx;
const activeCtx = { let options;
accessToken: {userId: 9}, let tx;
http: {
beforeEach(async() => {
ctx = {
req: { req: {
accessToken: {userId: 1},
headers: {origin: 'http://localhost'} headers: {origin: 'http://localhost'}
} },
} args: {}
}; };
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: ctx.req
}); });
options = {transaction: tx};
tx = await models.Sale.beginTransaction({});
options.transaction = tx;
});
afterEach(async() => {
await tx.rollback();
}); });
it('should return the cloned invoiceIn and also clone invoiceInDueDays and invoiceInTaxes if there are any referencing the invoiceIn', async() => { it('should return the cloned invoiceIn and also clone invoiceInDueDays and invoiceInTaxes if there are any referencing the invoiceIn', async() => {
const userId = 1; const clone = await models.InvoiceIn.clone(ctx, 1, false, options);
const ctx = {
req: {
accessToken: {userId: userId},
headers: {origin: 'http://localhost:5000'},
}
};
const tx = await models.InvoiceIn.beginTransaction({});
const options = {transaction: tx};
try {
const clone = await models.InvoiceIn.clone(ctx, 1, options);
expect(clone.supplierRef).toEqual('1234(2)'); expect(clone.supplierRef).toEqual('1234(2)');
const invoiceIn = await models.InvoiceIn.findOne({
const invoiceInTaxes = await models.InvoiceInTax.find({where: {invoiceInFk: clone.id}}, options); include: [
{
expect(invoiceInTaxes.length).toEqual(2); relation: 'invoiceInTax',
},
const invoiceInDueDays = await models.InvoiceInDueDay.find({where: {invoiceInFk: clone.id}}, options); {
relation: 'invoiceInDueDay',
expect(invoiceInDueDays.length).toEqual(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
} }
], where: {
id: clone.id
}
}, options);
const invoiceInTax = invoiceIn.invoiceInTax();
const invoiceInDueDay = invoiceIn.invoiceInDueDay();
expect(invoiceInTax.length).toEqual(2);
expect(invoiceInDueDay.length).toEqual(2);
});
it('should return the cloned invoiceIn and also clone invoiceInIntrastat and invoiceInTaxes if it is rectificative', async() => {
const clone = await models.InvoiceIn.clone(ctx, 1, true, options);
expect(clone.supplierRef).toEqual('1234(2)');
const invoiceIn = await models.InvoiceIn.findOne({
include: [
{
relation: 'invoiceInTax',
},
{
relation: 'invoiceInIntrastat',
}
], where: {
id: clone.id
}
}, options);
const invoiceInTax = invoiceIn.invoiceInTax();
const invoiceInIntrastat = invoiceIn.invoiceInIntrastat();
expect(invoiceInTax.length).toEqual(2);
expect(invoiceInIntrastat.length).toEqual(2);
}); });
}); });

View File

@ -0,0 +1,49 @@
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('invoiceIn corrective()', () => {
let ctx;
let options;
let tx;
beforeEach(async() => {
ctx = {
req: {
accessToken: {userId: 9},
headers: {origin: 'http://localhost'}
},
args: {}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: ctx.req
});
options = {transaction: tx};
tx = await models.Sale.beginTransaction({});
options.transaction = tx;
});
afterEach(async() => {
await tx.rollback();
});
it('La función corrective debería devolver un id cuando se ejecuta correctamente', async() => {
const originalId = 1;
const invoiceReason = 3;
const invoiceType = 2;
const invoiceClass = 1;
const cloneId = await models.InvoiceIn.corrective(ctx,
originalId, invoiceReason, invoiceType, invoiceClass, options);
expect(cloneId).toBeDefined();
const correction = await models.InvoiceInCorrection.findOne({
where: {correctedFk: originalId, correctingFk: cloneId}
}, options);
expect(correction.cplusRectificationTypeFk).toEqual(invoiceType);
expect(correction.siiTypeInvoiceOutFk).toEqual(invoiceClass);
expect(correction.invoiceCorrectionTypeFk).toEqual(invoiceReason);
});
});

View File

@ -5,6 +5,9 @@
"InvoiceInConfig": { "InvoiceInConfig": {
"dataSource": "vn" "dataSource": "vn"
}, },
"InvoiceInCorrection": {
"dataSource": "vn"
},
"InvoiceInDueDay": { "InvoiceInDueDay": {
"dataSource": "vn" "dataSource": "vn"
}, },

View File

@ -0,0 +1,38 @@
{
"name": "InvoiceInCorrection",
"base": "VnModel",
"options": {
"mysql": {
"table": "invoiceInCorrection"
}
},
"properties": {
"correctingFk": {
"id": true,
"type": "number"
}
},
"relations": {
"invoiceIn": {
"type": "belongsTo",
"model": "InvoiceIn",
"foreignKey": "correctedFk"
},
"cplusRectificationType": {
"type": "belongsTo",
"model": "CplusRectificationType",
"foreignKey": "cplusRectificationTypeFk"
},
"invoiceCorrectionType": {
"type": "belongsTo",
"model": "InvoiceCorrectionType",
"foreignKey": "invoiceCorrectionTypeFk"
},
"siiTypeInvoiceOut": {
"type": "belongsTo",
"model": "SiiTypeInvoiceOut",
"foreignKey": "siiTypeInvoiceOutFk"
}
}
}

View File

@ -9,6 +9,8 @@ module.exports = Self => {
require('../methods/invoice-in/invoiceInPdf')(Self); require('../methods/invoice-in/invoiceInPdf')(Self);
require('../methods/invoice-in/invoiceInEmail')(Self); require('../methods/invoice-in/invoiceInEmail')(Self);
require('../methods/invoice-in/getSerial')(Self); require('../methods/invoice-in/getSerial')(Self);
require('../methods/invoice-in/corrective')(Self);
Self.rewriteDbError(function(err) { Self.rewriteDbError(function(err) {
if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn')) if (err.code === 'ER_ROW_IS_REFERENCED_2' && err.sqlMessage.includes('vehicleInvoiceIn'))
return new UserError(`This invoice has a linked vehicle.`); return new UserError(`This invoice has a linked vehicle.`);

View File

@ -103,6 +103,11 @@
"type": "belongsTo", "type": "belongsTo",
"model": "Dms", "model": "Dms",
"foreignKey": "dmsFk" "foreignKey": "dmsFk"
},
"invoiceInCorrection": {
"type": "hasOne",
"model": "InvoiceInCorrection",
"foreignKey": "correctedFk"
} }
} }
} }

View File

@ -17,6 +17,9 @@
}, },
"description": { "description": {
"type": "string" "type": "string"
},
"code": {
"type": "string"
} }
} }
} }

View File

@ -15,7 +15,7 @@ SELECT pack.packages,
LEFT JOIN vn.province p ON p.id = c.provinceFk LEFT JOIN vn.province p ON p.id = c.provinceFk
JOIN vn.ticket t ON t.refFk = io.ref JOIN vn.ticket t ON t.refFk = io.ref
JOIN vn.address a ON a.id = t.addressFk JOIN vn.address a ON a.id = t.addressFk
LEFT JOIN vn.incoterms ic ON ic.code = a.incotermsFk JOIN vn.incoterms ic ON ic.code = a.incotermsFk
LEFT JOIN vn.customsAgent ca ON ca.id = a.customsAgentFk LEFT JOIN vn.customsAgent ca ON ca.id = a.customsAgentFk
JOIN vn.sale s ON s.ticketFk = t.id JOIN vn.sale s ON s.ticketFk = t.id
JOIN ( JOIN (

View File

@ -1,8 +1,5 @@
SELECT IF(incotermsFk IS NULL, FALSE, TRUE) AS hasIncoterms SELECT COUNT(*) AS hasIncoterms
FROM ticket t FROM invoiceOut io
JOIN invoiceOut io ON io.ref = t.refFk JOIN vn.invoiceOutSerial ios ON ios.code = io.serial
JOIN client c ON c.id = t.clientFk AND ios.taxAreaFk = 'WORLD'
JOIN address a ON a.id = t.addressFk WHERE io.ref = ?
WHERE t.refFk = ?
AND IF(c.hasToinvoiceByAddress = FALSE, c.defaultAddressFk, TRUE)
LIMIT 1