DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`stockBuyed_add`( vDated DATE ) BEGIN /** * Inserta el volumen de compra por comprador * en stockBuyed de acuerdo con la fecha. * * @param vDated Fecha de compra */ CREATE OR REPLACE TEMPORARY TABLE tStockBuyed (INDEX (userFk)) ENGINE = MEMORY SELECT requested, reserved, userFk FROM stockBuyed WHERE dated = vDated; DELETE FROM stockBuyed WHERE dated = vDated; CALL item_calculateStock(vDated); INSERT INTO stockBuyed(userFk, buyed, `dated`, description) SELECT it.workerFk, SUM((ti.quantity / b.packing) * buy_getVolume(b.id)) / vc.palletM3 / 1000000, vDated, u.name FROM itemType it JOIN item i ON i.typeFk = it.id LEFT JOIN tmp.item ti ON ti.itemFk = i.id JOIN itemCategory ic ON ic.id = it.categoryFk JOIN warehouse wh ON wh.code = 'VNH' JOIN tmp.buyUltimate bu ON bu.itemFk = i.id AND bu.warehouseFk = wh.id JOIN buy b ON b.id = bu.buyFk JOIN volumeConfig vc JOIN account.`user` u ON u.id = it.workerFk JOIN workerDepartment wd ON wd.workerFk = u.id JOIN department d ON d.id = wd.departmentFk WHERE ic.display AND d.code IN ('shopping', 'logistic', 'franceTeam') GROUP BY it.workerFk; INSERT INTO stockBuyed(buyed, dated, description) SELECT SUM(ic.cm3 * ito.quantity / vc.palletM3 / 1000000), vDated, IF(c.code = 'ES', p.name, c.name) destiny FROM itemTicketOut ito JOIN ticket t ON t.id = ito.ticketFk JOIN `address` a ON a.id = t.addressFk JOIN province p ON p.id = a.provinceFk JOIN country c ON c.id = p.countryFk JOIN warehouse wh ON wh.id = t.warehouseFk JOIN itemCost ic ON ic.itemFk = ito.itemFk AND ic.warehouseFk = t.warehouseFk JOIN volumeConfig vc WHERE ito.shipped BETWEEN vDated AND util.dayend(vDated) AND wh.code = 'VNH' GROUP BY destiny; UPDATE stockBuyed s JOIN tStockBuyed ts ON ts.userFk = s.userFk SET s.requested = ts.requested, s.reserved = ts.reserved WHERE s.dated = vDated; DROP TEMPORARY TABLE tmp.buyUltimate, tmp.item, tStockBuyed; END$$ DELIMITER ;