2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `bs`.`ventas_contables_add`(IN vYear INT, IN vMonth INT)
|
|
|
|
BEGIN
|
2024-02-02 07:05:17 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
/**
|
|
|
|
* Reemplaza las ventas contables del último año.
|
|
|
|
* Es el origen de datos para el balance de Entradas
|
|
|
|
*
|
|
|
|
* @param vYear Año a reemplazar
|
|
|
|
* @param vMonth Mes a reemplazar
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
DECLARE TIPO_PATRIMONIAL INT DEFAULT 188;
|
|
|
|
|
2024-02-02 07:05:17 +00:00
|
|
|
DELETE FROM bs.ventas_contables
|
|
|
|
WHERE year = vYear
|
2024-08-06 05:20:30 +00:00
|
|
|
AND month = vMonth;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.ticket_list;
|
|
|
|
CREATE TEMPORARY TABLE tmp.ticket_list
|
2024-02-05 12:52:28 +00:00
|
|
|
(PRIMARY KEY (id))
|
2024-01-15 11:31:03 +00:00
|
|
|
ENGINE = MEMORY
|
2024-04-10 07:42:55 +00:00
|
|
|
SELECT t.id
|
|
|
|
FROM vn.ticket t
|
|
|
|
JOIN vn.invoiceOut io ON io.`ref` = t.refFk
|
2024-02-14 10:39:15 +00:00
|
|
|
WHERE year(io.issued) = vYear
|
2024-02-15 07:14:56 +00:00
|
|
|
AND month(io.issued) = vMonth;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
INSERT INTO bs.ventas_contables(year
|
|
|
|
, month
|
|
|
|
, venta
|
|
|
|
, grupo
|
|
|
|
, reino_id
|
|
|
|
, tipo_id
|
|
|
|
, empresa_id
|
|
|
|
, gasto)
|
2024-02-02 07:05:17 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT vYear
|
|
|
|
, vMonth
|
2024-04-24 07:53:31 +00:00
|
|
|
, round(sum(s.quantity * s.price * (100 - s.discount)/100))
|
2024-01-15 11:31:03 +00:00
|
|
|
, IF(
|
2024-04-24 07:53:31 +00:00
|
|
|
co.companyGroupFk = co2.companyGroupFk
|
2024-01-15 11:31:03 +00:00
|
|
|
,1
|
2024-04-24 07:53:31 +00:00
|
|
|
,IF(co2.companyGroupFk,2,0)
|
2024-01-15 11:31:03 +00:00
|
|
|
) as grupo
|
2024-04-24 07:53:31 +00:00
|
|
|
, it.categoryFk
|
|
|
|
, i.typeFk
|
2024-04-10 07:42:55 +00:00
|
|
|
, t.companyFk
|
2024-04-24 07:53:31 +00:00
|
|
|
, i.expenseFk
|
|
|
|
+ IF(co.companyGroupFk = co2.companyGroupFk
|
2024-01-15 11:31:03 +00:00
|
|
|
,1
|
2024-04-24 07:53:31 +00:00
|
|
|
,IF(co2.companyGroupFk,2,0)
|
2024-02-02 07:05:17 +00:00
|
|
|
) * 100000
|
2024-04-24 07:53:31 +00:00
|
|
|
+ it.categoryFk * 1000 as Gasto
|
|
|
|
FROM vn.sale s
|
|
|
|
JOIN vn.ticket t ON t.id = s.ticketFk
|
|
|
|
JOIN vn.address a on a.id = t.addressFk
|
|
|
|
JOIN vn.client c on c.id = a.clientFk
|
2024-02-05 12:52:28 +00:00
|
|
|
JOIN tmp.ticket_list tt on tt.id = t.id
|
2024-04-24 08:57:35 +00:00
|
|
|
JOIN vn.item i on s.itemFk = i.id
|
2024-04-24 07:53:31 +00:00
|
|
|
JOIN vn.company co on co.id = t.companyFk
|
|
|
|
LEFT JOIN vn.company co2 on co2.clientFk = c.id
|
|
|
|
JOIN vn.itemType it on it.id = i.typeFk
|
|
|
|
WHERE s.quantity <> 0
|
|
|
|
AND s.price <> 0
|
|
|
|
AND s.discount <> 100
|
|
|
|
AND i.typeFk <> TIPO_PATRIMONIAL
|
|
|
|
GROUP BY grupo, it.categoryFk, i.typeFk, t.companyFk, Gasto;
|
2024-02-02 07:05:17 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
INSERT INTO bs.ventas_contables(year
|
|
|
|
, month
|
|
|
|
, venta
|
|
|
|
, grupo
|
|
|
|
, reino_id
|
|
|
|
, tipo_id
|
|
|
|
, empresa_id
|
|
|
|
, gasto)
|
|
|
|
SELECT vYear
|
|
|
|
, vMonth
|
|
|
|
, sum(ts.quantity * ts.price)
|
|
|
|
, IF(
|
|
|
|
c.companyGroupFk = c2.companyGroupFk
|
|
|
|
,1
|
|
|
|
,IF(c2.companyGroupFk, 2, 0)
|
|
|
|
) as grupo
|
|
|
|
, NULL
|
|
|
|
, NULL
|
2024-02-02 07:05:17 +00:00
|
|
|
, t.companyFk
|
2024-01-15 11:31:03 +00:00
|
|
|
, 7050000000
|
2024-02-02 07:05:17 +00:00
|
|
|
FROM vn.ticketService ts
|
2024-01-15 11:31:03 +00:00
|
|
|
JOIN vn.ticket t ON ts.ticketFk = t.id
|
2024-02-02 07:05:17 +00:00
|
|
|
JOIN vn.address a on a.id = t.addressFk
|
|
|
|
JOIN vn.client cl on cl.id = a.clientFk
|
2024-04-10 07:42:55 +00:00
|
|
|
JOIN tmp.ticket_list tt on tt.id = t.id
|
2024-02-02 07:05:17 +00:00
|
|
|
JOIN vn.company c on c.id = t.companyFk
|
2024-01-15 11:31:03 +00:00
|
|
|
LEFT JOIN vn.company c2 on c2.clientFk = cl.id
|
2024-02-02 07:05:17 +00:00
|
|
|
GROUP BY grupo, t.companyFk ;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
DROP TEMPORARY TABLE tmp.ticket_list;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|