refs #3126 procReplace
This commit is contained in:
parent
a854fff94f
commit
104d11ac3c
|
@ -1,669 +0,0 @@
|
|||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travelVolume`(vTravelFk INT)
|
||||
BEGIN
|
||||
|
||||
SELECT w1.name AS ORI,
|
||||
w2.name AS DES,
|
||||
tr.shipped shipment,
|
||||
tr.landed landing,
|
||||
a.name Agencia,
|
||||
s.name Proveedor,
|
||||
e.id Id_Entrada,
|
||||
e.invoiceNumber Referencia,
|
||||
CAST(ROUND(SUM(GREATEST(b.stickers ,b.quantity /b.packing ) *
|
||||
vn.item_getVolume(b.itemFk ,b.packagingFk)) / vc.trolleyM3 / 1000000 ,1) AS DECIMAL(10,2)) AS CC,
|
||||
CAST(ROUND(SUM(GREATEST(b.stickers ,b.quantity /b.packing ) *
|
||||
vn.item_getVolume(b.itemFk ,b.packagingFk)) / vc.palletM3 / 1000000,1) AS DECIMAL(10,2)) AS espais
|
||||
FROM vn.buy b
|
||||
JOIN vn.entry e ON e.id = b.entryFk
|
||||
JOIN vn.supplier s ON s.id = e.supplierFk
|
||||
JOIN vn.travel tr ON tr.id = e.travelFk
|
||||
JOIN vn.agencyMode a ON a.id = tr.agencyModeFk
|
||||
JOIN vn.warehouse w1 ON w1.id = tr.warehouseInFk
|
||||
JOIN vn.warehouse w2 ON w2.id = tr.warehouseOutFk
|
||||
JOIN vn.volumeConfig vc
|
||||
JOIN vn.item i ON i.id = b.itemFk
|
||||
JOIN vn.itemType it ON it.id = i.typeFk
|
||||
WHERE tr.id = vTravelFk;
|
||||
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travelVolume_get`(vFromDated DATE, vToDated DATE, vWarehouseFk INT)
|
||||
BEGIN
|
||||
SELECT tr.landed Fecha,
|
||||
a.name Agencia,
|
||||
count(DISTINCT e.id) numEntradas,
|
||||
FLOOR(sum(item_getVolume(b.itemFk, b.packagingFk) * b.stickers / 1000000 )) AS m3
|
||||
FROM vn.travel tr
|
||||
JOIN vn.agencyMode a ON a.id = tr.agencyModeFk
|
||||
JOIN vn.entry e ON e.travelFk = tr.id
|
||||
JOIN vn.buy b ON b.entryFk = e.id
|
||||
WHERE tr.landed BETWEEN vFromDated AND vToDated
|
||||
AND e.isRaid = FALSE
|
||||
AND tr.warehouseInFk = vWarehouseFk
|
||||
GROUP BY tr.landed , a.name ;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_getEntriesMissingPackage`(vSelf INT)
|
||||
BEGIN
|
||||
DECLARE vpackageOrPackingNull INT;
|
||||
DECLARE vTravelFk INT;
|
||||
|
||||
SELECT travelfk INTO vTravelFk
|
||||
FROM entry
|
||||
WHERE id = vSelf;
|
||||
|
||||
SELECT e.id entryFk
|
||||
FROM travel t
|
||||
JOIN entry e ON e.travelFk = t.id
|
||||
JOIN buy b ON b.entryFk = e.id
|
||||
WHERE t.id = vTravelFk
|
||||
AND (b.packing IS NULL OR b.packagingFk IS NULL);
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticketBoxesView`(IN vTicketFk INT)
|
||||
BEGIN
|
||||
|
||||
SELECT s.id,
|
||||
s.itemFk,
|
||||
s.concept,
|
||||
floor(s.quantity / b.packing) as Cajas,
|
||||
b.packing,
|
||||
s.isPicked,
|
||||
i.size
|
||||
FROM ticket t
|
||||
JOIN sale s ON s.ticketFk = t.id
|
||||
JOIN item i ON i.id = s.itemFk
|
||||
JOIN cache.last_buy lb on lb.warehouse_id = t.warehouseFk AND lb.item_id = s.itemFk
|
||||
JOIN buy b on b.id = lb.buy_id
|
||||
JOIN packaging p on p.id = b.packagingFk
|
||||
WHERE s.quantity >= b.packing
|
||||
AND t.id = vTicketFk
|
||||
AND p.isBox
|
||||
GROUP BY s.itemFk;
|
||||
|
||||
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`stockBuyedByWorker`(
|
||||
vDate DATE,
|
||||
vWorker INT)
|
||||
BEGIN
|
||||
/**
|
||||
* Calculates the space reserved by buyers of the same container
|
||||
*
|
||||
* @param vdate date of container delivery
|
||||
* @param vWorker buyer reserving space in the container
|
||||
*/
|
||||
DECLARE vVolume DECIMAL(10, 2);
|
||||
DECLARE vWarehouseFk INT;
|
||||
DECLARE vCompressionRatio DECIMAL(1, 1);
|
||||
|
||||
CALL stockTraslation(vDate);
|
||||
|
||||
SELECT warehouseFk, conversionCoefficient INTO vWarehouseFk, vCompressionRatio
|
||||
FROM auctionConfig;
|
||||
|
||||
SELECT volume INTO vVolume
|
||||
FROM vn.packaging WHERE id = 'cc';
|
||||
|
||||
SELECT b.entryFk Id_Entrada,
|
||||
i.id Id_Article,
|
||||
i.name Article,
|
||||
ti.amount Cantidad,
|
||||
(vCompressionRatio * (ti.amount / b.packing) * vn.buy_getVolume(b.id))
|
||||
/ vVolume buyed,
|
||||
b.packagingFk id_cubo,
|
||||
b.packing
|
||||
FROM tmp.item ti
|
||||
JOIN item i ON i.id = ti.item_id
|
||||
JOIN itemType it ON i.typeFk = it.id
|
||||
JOIN itemCategory ic ON ic.id = it.categoryFk
|
||||
JOIN worker w ON w.id = it.workerFk
|
||||
JOIN tmp.buyUltimate bu ON bu.itemFk = i.id
|
||||
AND bu.warehouseFk = vWarehouseFk
|
||||
JOIN buy b ON b.id = bu.buyFk
|
||||
WHERE ic.display AND w.id = vWorker;
|
||||
|
||||
DROP TEMPORARY TABLE
|
||||
tmp.buyUltimate,
|
||||
tmp.item;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelvingMakeFromDate`(IN `vShelvingFk` VARCHAR(8), IN `vBarcode` VARCHAR(22), IN `vQuantity` INT, IN `vPackagingFk` VARCHAR(10), IN `vGrouping` INT, IN `vPacking` INT, IN `vWarehouseFk` INT, `vCreated` VARCHAR(22))
|
||||
BEGIN
|
||||
|
||||
DECLARE vItemFk INT;
|
||||
|
||||
SELECT vn.barcodeToItem(vBarcode) INTO vItemFk;
|
||||
|
||||
SELECT itemFk INTO vItemFk
|
||||
FROM vn.buy b
|
||||
WHERE b.id = vItemFk;
|
||||
|
||||
IF (SELECT COUNT(*) FROM vn.shelving WHERE code = vShelvingFk COLLATE utf8_unicode_ci) = 0 THEN
|
||||
|
||||
INSERT IGNORE INTO vn.parking(`code`) VALUES(vShelvingFk);
|
||||
INSERT INTO vn.shelving(`code`, parkingFk)
|
||||
SELECT vShelvingFk, id
|
||||
FROM vn.parking
|
||||
WHERE `code` = vShelvingFk COLLATE utf8_unicode_ci;
|
||||
|
||||
END IF;
|
||||
|
||||
IF (SELECT COUNT(*) FROM vn.itemShelving
|
||||
WHERE shelvingFk COLLATE utf8_unicode_ci = vShelvingFk
|
||||
AND itemFk = vItemFk
|
||||
AND packing = vPacking) = 1 THEN
|
||||
|
||||
UPDATE vn.itemShelving
|
||||
SET visible = visible+vQuantity,
|
||||
created = vCreated
|
||||
WHERE shelvingFk COLLATE utf8_unicode_ci = vShelvingFk
|
||||
AND itemFk = vItemFk
|
||||
AND packing = vPacking;
|
||||
|
||||
ELSE
|
||||
CALL cache.last_buy_refresh(FALSE);
|
||||
INSERT INTO itemShelving( itemFk,
|
||||
shelvingFk,
|
||||
visible,
|
||||
created,
|
||||
`grouping`,
|
||||
packing,
|
||||
packagingFk)
|
||||
SELECT vItemFk,
|
||||
vShelvingFk,
|
||||
vQuantity,
|
||||
vCreated,
|
||||
IF(vGrouping = 0, IFNULL(b.packing, vPacking), vGrouping) `grouping`,
|
||||
IF(vPacking = 0, b.packing, vPacking) packing,
|
||||
IF(vPackagingFk = '', b.packagingFk, vPackagingFk) packaging
|
||||
FROM vn.item i
|
||||
LEFT JOIN cache.last_buy lb ON i.id = lb.item_id AND lb.warehouse_id = vWarehouseFk
|
||||
LEFT JOIN vn.buy b ON b.id = lb.buy_id
|
||||
WHERE i.id = vItemFk;
|
||||
END IF;
|
||||
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemShelving_add`(IN vShelvingFk VARCHAR(8), IN vBarcode VARCHAR(22), IN vQuantity INT, IN vPackagingFk VARCHAR(10), IN vGrouping INT, IN vPacking INT, IN vWarehouseFk INT)
|
||||
BEGIN
|
||||
|
||||
|
||||
/**
|
||||
* Añade registro o lo actualiza si ya existe.
|
||||
*
|
||||
* @param vShelvingFk matrícula del carro
|
||||
* @param vBarcode el id del registro
|
||||
* @param vQuantity indica la cantidad del producto
|
||||
* @param vPackagingFk el packaging del producto en itemShelving, NULL para coger el de la ultima compra
|
||||
* @param vGrouping el grouping del producto en itemShelving, NULL para coger el de la ultima compra
|
||||
* @param vPacking el packing del producto, NULL para coger el de la ultima compra
|
||||
* @param vWarehouseFk indica el sector
|
||||
*
|
||||
**/
|
||||
|
||||
DECLARE vItemFk INT;
|
||||
|
||||
SELECT barcodeToItem(vBarcode) INTO vItemFk;
|
||||
|
||||
IF (SELECT COUNT(*) FROM shelving WHERE code = vShelvingFk COLLATE utf8_unicode_ci) = 0 THEN
|
||||
|
||||
INSERT IGNORE INTO parking(code) VALUES(vShelvingFk);
|
||||
INSERT INTO shelving(code, parkingFk)
|
||||
SELECT vShelvingFk, id
|
||||
FROM parking
|
||||
WHERE `code` = vShelvingFk COLLATE utf8_unicode_ci;
|
||||
|
||||
END IF;
|
||||
|
||||
IF (SELECT COUNT(*) FROM itemShelving
|
||||
WHERE shelvingFk COLLATE utf8_unicode_ci = vShelvingFk
|
||||
AND itemFk = vItemFk
|
||||
AND packing = vPacking) = 1 THEN
|
||||
|
||||
UPDATE itemShelving
|
||||
SET visible = visible+vQuantity
|
||||
WHERE shelvingFk COLLATE utf8_unicode_ci = vShelvingFk AND itemFk = vItemFk AND packing = vPacking;
|
||||
|
||||
ELSE
|
||||
CALL cache.last_buy_refresh(FALSE);
|
||||
INSERT INTO itemShelving( itemFk,
|
||||
shelvingFk,
|
||||
visible,
|
||||
grouping,
|
||||
packing,
|
||||
packagingFk)
|
||||
|
||||
SELECT vItemFk,
|
||||
vShelvingFk,
|
||||
vQuantity,
|
||||
IFNULL(vGrouping, b.grouping),
|
||||
IFNULL(vPacking, b.packing),
|
||||
IFNULL(vPackagingFk, b.packagingFk)
|
||||
FROM item i
|
||||
LEFT JOIN cache.last_buy lb ON i.id = lb.item_id AND lb.warehouse_id = vWarehouseFk
|
||||
LEFT JOIN buy b ON b.id = lb.buy_id
|
||||
WHERE i.id = vItemFk;
|
||||
END IF;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`itemFreight_Show`(vItemFk INT, vWarehouseFk INT)
|
||||
BEGIN
|
||||
|
||||
SELECT cm3 Volumen_Entrada,
|
||||
cm3delivery Volumen_Salida,
|
||||
p.volume Volumen_del_embalaje,
|
||||
p.width Ancho_del_embalaje,
|
||||
p.`depth` Largo_del_embalaje,
|
||||
b.packagingFk ,
|
||||
IFNULL(p.height, i.`size`) + 10 Altura,
|
||||
b.packing Packing_Entrada,
|
||||
i.packingOut Packing_Salida,
|
||||
i.id itemFk,
|
||||
b.id buyFk,
|
||||
b.entryFk,
|
||||
w.name warehouseFk
|
||||
FROM vn.itemCost ic
|
||||
JOIN vn.item i ON i.id = ic.itemFk
|
||||
LEFT JOIN cache.last_buy lb ON lb.item_id = ic.itemFk AND lb.warehouse_id = ic.warehouseFk
|
||||
LEFT JOIN vn.buy b ON b.id = lb.buy_id
|
||||
LEFT JOIN vn.packaging p ON p.id = b.packagingFk
|
||||
LEFT JOIN vn.warehouse w ON w.id = ic.warehouseFk
|
||||
WHERE ic.itemFk = vItemFk
|
||||
AND ic.warehouseFk = vWarehouseFk;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`inventoryMake`(vDate DATE, vWh INT)
|
||||
proc: BEGIN
|
||||
/**
|
||||
* Recalcula los inventarios de todos los almacenes, si vWh = 0
|
||||
*
|
||||
* @param vDate Fecha de los nuevos inventarios
|
||||
* @param vWh almacen al cual hacer el inventario
|
||||
*/
|
||||
|
||||
DECLARE vDone BOOL;
|
||||
DECLARE vEntryFk INT;
|
||||
DECLARE vTravelFk INT;
|
||||
DECLARE vDateLastInventory DATE;
|
||||
DECLARE vDateYesterday DATETIME DEFAULT vDate - INTERVAL 1 SECOND;
|
||||
DECLARE vWarehouseOutFkInventory INT;
|
||||
DECLARE vInventorySupplierFk INT;
|
||||
DECLARE vAgencyModeFkInventory INT;
|
||||
|
||||
DECLARE cWarehouses CURSOR FOR
|
||||
SELECT id
|
||||
FROM warehouse
|
||||
WHERE isInventory
|
||||
AND vWh IN (0,id);
|
||||
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
||||
|
||||
OPEN cWarehouses;
|
||||
SET @isModeInventory := TRUE;
|
||||
l: LOOP
|
||||
|
||||
SET vDone = FALSE;
|
||||
FETCH cWarehouses INTO vWh;
|
||||
|
||||
IF vDone THEN
|
||||
LEAVE l;
|
||||
END IF;
|
||||
|
||||
SELECT w.id INTO vWarehouseOutFkInventory
|
||||
FROM warehouse w
|
||||
WHERE w.code = 'inv';
|
||||
|
||||
SELECT inventorySupplierFk INTO vInventorySupplierFk
|
||||
FROM entryConfig;
|
||||
|
||||
SELECT am.id INTO vAgencyModeFkInventory
|
||||
FROM agencyMode am
|
||||
where code = 'inv';
|
||||
|
||||
SELECT MAX(landed) INTO vDateLastInventory
|
||||
FROM travel tr
|
||||
JOIN entry e ON e.travelFk = tr.id
|
||||
JOIN buy b ON b.entryFk = e.id
|
||||
WHERE warehouseOutFk = vWarehouseOutFkInventory
|
||||
AND landed < vDate
|
||||
AND e.supplierFk = vInventorySupplierFk
|
||||
AND warehouseInFk = vWh
|
||||
AND NOT isRaid;
|
||||
|
||||
IF vDateLastInventory IS NULL THEN
|
||||
SELECT inventoried INTO vDateLastInventory FROM config;
|
||||
END IF;
|
||||
|
||||
-- Generamos travel, si no existe.
|
||||
SET vTravelFK = 0;
|
||||
|
||||
SELECT id INTO vTravelFk
|
||||
FROM travel
|
||||
WHERE warehouseOutFk = vWarehouseOutFkInventory
|
||||
AND warehouseInFk = vWh
|
||||
AND landed = vDate
|
||||
AND agencyModeFk = vAgencyModeFkInventory
|
||||
AND ref = 'inventario'
|
||||
LIMIT 1;
|
||||
|
||||
IF NOT vTravelFK THEN
|
||||
|
||||
INSERT INTO travel SET
|
||||
warehouseOutFk = vWarehouseOutFkInventory,
|
||||
warehouseInFk = vWh,
|
||||
shipped = vDate,
|
||||
landed = vDate,
|
||||
agencyModeFk = vAgencyModeFkInventory,
|
||||
ref = 'inventario',
|
||||
isDelivered = TRUE,
|
||||
isReceived = TRUE;
|
||||
|
||||
SELECT LAST_INSERT_ID() INTO vTravelFk;
|
||||
|
||||
END IF;
|
||||
|
||||
-- Generamos entrada si no existe, o la vaciamos.
|
||||
SET vEntryFk = 0;
|
||||
|
||||
SELECT id INTO vEntryFk
|
||||
FROM entry
|
||||
WHERE supplierFk = vInventorySupplierFk
|
||||
AND travelFk = vTravelFk;
|
||||
|
||||
IF NOT vEntryFk THEN
|
||||
|
||||
INSERT INTO entry SET
|
||||
supplierFk = vInventorySupplierFk,
|
||||
isConfirmed = TRUE,
|
||||
isOrdered = TRUE,
|
||||
travelFk = vTravelFk;
|
||||
|
||||
SELECT LAST_INSERT_ID() INTO vEntryFk;
|
||||
|
||||
ELSE
|
||||
|
||||
DELETE FROM buy WHERE entryFk = vEntryFk;
|
||||
|
||||
END IF;
|
||||
|
||||
-- Preparamos tabla auxilar
|
||||
CREATE OR REPLACE TEMPORARY TABLE tmp.inventory (
|
||||
itemFk INT(11) NOT NULL PRIMARY KEY,
|
||||
quantity int(11) DEFAULT '0',
|
||||
buyingValue decimal(10,3) DEFAULT '0.000',
|
||||
freightValue decimal(10,3) DEFAULT '0.000',
|
||||
packing int(11) DEFAULT '0',
|
||||
`grouping` smallint(5) unsigned NOT NULL DEFAULT '1',
|
||||
groupingMode tinyint(4) NOT NULL DEFAULT 0 ,
|
||||
comissionValue decimal(10,3) DEFAULT '0.000',
|
||||
packageValue decimal(10,3) DEFAULT '0.000',
|
||||
packageFk varchar(10) COLLATE utf8_unicode_ci DEFAULT '--',
|
||||
price1 decimal(10,2) DEFAULT '0.00',
|
||||
price2 decimal(10,2) DEFAULT '0.00',
|
||||
price3 decimal(10,2) DEFAULT '0.00',
|
||||
minPrice decimal(10,2) DEFAULT '0.00',
|
||||
producer varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
|
||||
INDEX (itemFK)) ENGINE = MEMORY;
|
||||
|
||||
-- Compras
|
||||
INSERT INTO tmp.inventory(itemFk,quantity)
|
||||
SELECT b.itemFk, SUM(b.quantity)
|
||||
FROM buy b
|
||||
JOIN entry e ON e.id = b.entryFk
|
||||
JOIN travel tr ON tr.id = e.travelFk
|
||||
WHERE tr.warehouseInFk = vWh
|
||||
AND tr.landed BETWEEN vDateLastInventory
|
||||
AND vDateYesterday
|
||||
AND NOT isRaid
|
||||
GROUP BY b.itemFk;
|
||||
SELECT vDateLastInventory , vDateYesterday;
|
||||
|
||||
-- Traslados
|
||||
INSERT INTO tmp.inventory(itemFk, quantity)
|
||||
SELECT itemFk, quantityOut
|
||||
FROM (
|
||||
SELECT b.itemFk,- SUM(b.quantity) quantityOut
|
||||
FROM buy b
|
||||
JOIN entry e ON e.id = b.entryFk
|
||||
JOIN travel tr ON tr.id = e.travelFk
|
||||
WHERE tr.warehouseOutFk = vWh
|
||||
AND tr.shipped BETWEEN vDateLastInventory
|
||||
AND vDateYesterday
|
||||
AND NOT isRaid
|
||||
GROUP BY b.itemFk
|
||||
) sub
|
||||
ON DUPLICATE KEY UPDATE quantity = IFNULL(quantity, 0) + sub.quantityOut;
|
||||
|
||||
-- Ventas
|
||||
INSERT INTO tmp.inventory(itemFk,quantity)
|
||||
SELECT itemFk, saleOut
|
||||
FROM (
|
||||
SELECT s.itemFk, - SUM(s.quantity) saleOut
|
||||
FROM sale s
|
||||
JOIN ticket t ON t.id = s.ticketFk
|
||||
WHERE t.warehouseFk = vWh
|
||||
AND t.shipped BETWEEN vDateLastInventory AND vDateYesterday
|
||||
GROUP BY s.itemFk
|
||||
) sub
|
||||
ON DUPLICATE KEY UPDATE quantity = IFNULL(quantity,0) + sub.saleOut;
|
||||
|
||||
-- Actualiza valores de la ultima compra
|
||||
UPDATE tmp.inventory inv
|
||||
JOIN cache.last_buy lb ON lb.item_id = inv.itemFk AND lb.warehouse_id = vWh
|
||||
JOIN buy b ON b.id = lb.buy_id
|
||||
JOIN item i ON i.id = b.itemFk
|
||||
LEFT JOIN producer p ON p.id = i.producerFk
|
||||
SET inv.buyingValue = b.buyingValue,
|
||||
inv.freightValue = b.freightValue,
|
||||
inv.packing = b.packing,
|
||||
inv.`grouping`= b.`grouping`,
|
||||
inv.groupingMode = b.groupingMode,
|
||||
inv.comissionValue = b.comissionValue,
|
||||
inv.packageValue = b.packageValue,
|
||||
inv.packageFk = b.packagingFk,
|
||||
inv.price1 = b.price1,
|
||||
inv.price2 = b.price2,
|
||||
inv.price3 = b.price3,
|
||||
inv.minPrice = b.minPrice,
|
||||
inv.producer = p.name;
|
||||
|
||||
INSERT INTO buy( itemFk,
|
||||
quantity,
|
||||
buyingValue,
|
||||
freightValue,
|
||||
packing,
|
||||
`grouping`,
|
||||
groupingMode,
|
||||
comissionValue,
|
||||
packageValue,
|
||||
packagingFk,
|
||||
price1,
|
||||
price2,
|
||||
price3,
|
||||
minPrice,
|
||||
entryFk)
|
||||
SELECT itemFk,
|
||||
GREATEST(quantity, 0),
|
||||
buyingValue,
|
||||
freightValue,
|
||||
packing,
|
||||
`grouping`,
|
||||
groupingMode,
|
||||
comissionValue,
|
||||
packageValue,
|
||||
packagingFk,
|
||||
price1,
|
||||
price2,
|
||||
price3,
|
||||
minPrice,
|
||||
vEntryFk
|
||||
FROM tmp.inventory;
|
||||
|
||||
SELECT vWh, COUNT(*), util.VN_NOW() FROM tmp.inventory;
|
||||
|
||||
-- Actualizamos el campo lastUsed de item
|
||||
UPDATE item i
|
||||
JOIN tmp.inventory i2 ON i2.itemFk = i.id
|
||||
SET i.lastUsed = NOW()
|
||||
WHERE i2.quantity;
|
||||
|
||||
-- DROP TEMPORARY TABLE tmp.inventory;
|
||||
|
||||
END LOOP;
|
||||
|
||||
CLOSE cWarehouses;
|
||||
|
||||
UPDATE config SET inventoried = vDate;
|
||||
SET @isModeInventory := FALSE;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.entryToDelete;
|
||||
CREATE TEMPORARY TABLE tmp.entryToDelete
|
||||
(INDEX(entryId) USING BTREE) ENGINE = MEMORY
|
||||
SELECT e.id as entryId,
|
||||
t.id as travelId
|
||||
FROM travel t
|
||||
JOIN `entry` e ON e.travelFk = t.id
|
||||
WHERE e.supplierFk = vInventorySupplierFk
|
||||
AND t.shipped <= util.VN_CURDATE() - INTERVAL 12 DAY
|
||||
AND (DAY(t.shipped) <> 1 OR shipped < util.VN_CURDATE() - INTERVAL 12 DAY);
|
||||
|
||||
DELETE e
|
||||
FROM `entry` e
|
||||
JOIN tmp.entryToDelete tmp ON tmp.entryId = e.id;
|
||||
|
||||
DELETE IGNORE t
|
||||
FROM travel t
|
||||
JOIN tmp.entryToDelete tmp ON tmp.travelId = t.id;
|
||||
END$$
|
||||
DELIMITER ;
|
||||
|
||||
DELIMITER $$
|
||||
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`inventory_repair`()
|
||||
BEGIN
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.lastEntry;
|
||||
CREATE TEMPORARY TABLE tmp.lastEntry
|
||||
(PRIMARY KEY (buyFk))
|
||||
SELECT
|
||||
i.id AS itemFk,
|
||||
w.id AS warehouseFk,
|
||||
w.name AS warehouse,
|
||||
tr.landed,
|
||||
b.id AS buyFk,
|
||||
b.entryFk,
|
||||
b.isIgnored,
|
||||
b.price2,
|
||||
b.price3,
|
||||
b.stickers,
|
||||
b.packing,
|
||||
b.grouping,
|
||||
b.groupingMode,
|
||||
b.weight,
|
||||
i.stems,
|
||||
b.quantity,
|
||||
b.buyingValue,
|
||||
b.packagingFk ,
|
||||
s.id AS supplierFk,
|
||||
s.name AS supplier
|
||||
FROM itemType it
|
||||
RIGHT JOIN (entry e
|
||||
LEFT JOIN supplier s ON s.id = e.supplierFk
|
||||
RIGHT JOIN buy b ON b.entryFk = e.id
|
||||
LEFT JOIN item i ON i.id = b.itemFk
|
||||
LEFT JOIN ink ON ink.id = i.inkFk
|
||||
LEFT JOIN travel tr ON tr.id = e.travelFk
|
||||
LEFT JOIN warehouse w ON w.id = tr.warehouseInFk
|
||||
LEFT JOIN origin o ON o.id = i.originFk
|
||||
) ON it.id = i.typeFk
|
||||
LEFT JOIN edi.ekt ek ON b.ektFk = ek.id
|
||||
WHERE (b.packagingFk = "--" OR b.price2 = 0 OR b.packing = 0 OR b.buyingValue = 0) AND tr.landed > util.firstDayOfMonth(TIMESTAMPADD(MONTH,-1,util.VN_CURDATE())) AND s.name = 'INVENTARIO';
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.lastEntryOk;
|
||||
CREATE TEMPORARY TABLE tmp.lastEntryOk
|
||||
(PRIMARY KEY (buyFk))
|
||||
SELECT
|
||||
i.id AS itemFk,
|
||||
w.id AS warehouseFk,
|
||||
w.name AS warehouse,
|
||||
tr.landed,
|
||||
b.id AS buyFk,
|
||||
b.entryFk,
|
||||
b.isIgnored,
|
||||
b.price2,
|
||||
b.price3,
|
||||
b.stickers,
|
||||
b.packing,
|
||||
b.grouping,
|
||||
b.groupingMode,
|
||||
b.weight,
|
||||
i.stems,
|
||||
b.quantity,
|
||||
b.buyingValue,
|
||||
b.packagingFk,
|
||||
s.id AS supplierFk,
|
||||
s.name AS supplier
|
||||
FROM itemType it
|
||||
RIGHT JOIN (entry e
|
||||
LEFT JOIN supplier s ON s.id = e.supplierFk
|
||||
RIGHT JOIN buy b ON b.entryFk = e.id
|
||||
LEFT JOIN item i ON i.id = b.itemFk
|
||||
LEFT JOIN ink ON ink.id = i.inkFk
|
||||
LEFT JOIN travel tr ON tr.id = e.travelFk
|
||||
LEFT JOIN warehouse w ON w.id = tr.warehouseInFk
|
||||
LEFT JOIN origin o ON o.id = i.originFk
|
||||
) ON it.id = i.typeFk
|
||||
LEFT JOIN edi.ekt ek ON b.ektFk = ek.id
|
||||
WHERE b.packagingFk != "--" AND b.price2 != 0 AND b.packing != 0 AND b.buyingValue > 0 AND tr.landed > util.firstDayOfMonth(TIMESTAMPADD(MONTH,-2,util.VN_CURDATE()))
|
||||
ORDER BY tr.landed DESC;
|
||||
|
||||
DROP TEMPORARY TABLE IF EXISTS tmp.lastEntryOkGroup;
|
||||
CREATE TEMPORARY TABLE tmp.lastEntryOkGroup
|
||||
(INDEX (warehouseFk,itemFk))
|
||||
SELECT *
|
||||
FROM tmp.lastEntryOk tmp
|
||||
GROUP BY tmp.itemFk,tmp.warehouseFk;
|
||||
|
||||
UPDATE buy b
|
||||
JOIN tmp.lastEntry lt ON lt.buyFk = b.id
|
||||
JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
|
||||
SET b.packagingFk = eo.packagingFk WHERE b.packagingFk = "--";
|
||||
|
||||
UPDATE buy b
|
||||
JOIN tmp.lastEntry lt ON lt.buyFk = b.id
|
||||
JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
|
||||
SET b.price2 = eo.price2 WHERE b.price2 = 0 ;
|
||||
|
||||
UPDATE buy b
|
||||
JOIN tmp.lastEntry lt ON lt.buyFk = b.id
|
||||
JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
|
||||
SET b.packing = eo.packing WHERE b.packing = 0;
|
||||
|
||||
UPDATE buy b
|
||||
JOIN tmp.lastEntry lt ON lt.buyFk = b.id
|
||||
JOIN tmp.lastEntryOkGroup eo ON eo.itemFk = lt.itemFk AND eo.warehouseFk = lt.warehouseFk
|
||||
SET b.buyingValue = eo.buyingValue WHERE b.buyingValue = 0;
|
||||
|
||||
DROP TEMPORARY TABLE tmp.lastEntry;
|
||||
DROP TEMPORARY TABLE tmp.lastEntryOk;
|
||||
DROP TEMPORARY TABLE tmp.lastEntryOkGroup;
|
||||
END$$
|
||||
DELIMITER ;
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue