2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-05-16 16:48:59 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ledger_doCompensation`(
|
|
|
|
vDated DATE,
|
|
|
|
vCompensationAccount VARCHAR(10),
|
|
|
|
vBankFk VARCHAR(10),
|
|
|
|
vConcept VARCHAR(255),
|
|
|
|
vAmount DECIMAL(10,2),
|
|
|
|
vCompanyFk INT,
|
|
|
|
vOriginalAccount VARCHAR(10)
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Compensa un pago o un recibo insertando en contabilidad
|
|
|
|
*
|
|
|
|
* @param vDated fecha en la cual se anota
|
|
|
|
* @param vCompensationAccount cuenta contable contra la que se compensa
|
|
|
|
* @param vBankFk banco de la compensacion
|
|
|
|
* @param vConcept descripcion
|
|
|
|
* @param vAmount cantidad que se compensa
|
2024-05-16 16:48:59 +00:00
|
|
|
* @param vCompanyFk empresa
|
2024-01-15 11:31:03 +00:00
|
|
|
* @param vOriginalAccount cuenta contable desde la cual se compensa
|
|
|
|
*
|
2024-05-16 16:48:59 +00:00
|
|
|
*/
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vNewBookEntry INT;
|
2024-05-16 16:48:59 +00:00
|
|
|
DECLARE vIsClientCompensation INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vClientFk INT;
|
2024-05-16 16:48:59 +00:00
|
|
|
DECLARE vSupplierFk INT;
|
|
|
|
DECLARE vIsOriginalAClient BOOL;
|
|
|
|
DECLARE vPayMethodCompensation INT;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
CALL ledger_next(YEAR(vDated), vNewBookEntry);
|
|
|
|
|
|
|
|
SELECT COUNT(id) INTO vIsOriginalAClient
|
|
|
|
FROM client
|
|
|
|
WHERE accountingAccount LIKE vOriginalAccount COLLATE utf8_general_ci;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SELECT id, COUNT(id) INTO vClientFk, vIsClientCompensation
|
|
|
|
FROM client
|
|
|
|
WHERE accountingAccount LIKE vCompensationAccount COLLATE utf8_general_ci;
|
2024-05-16 16:48:59 +00:00
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
SET @vAmount1:= 0.0;
|
|
|
|
SET @vAmount2:= 0.0;
|
|
|
|
|
2024-05-16 16:48:59 +00:00
|
|
|
INSERT INTO XDiario (ASIEN, FECHA, SUBCTA, CONTRA, CONCEPTO, EURODEBE, EUROHABER, empresa_id)
|
2024-01-15 11:31:03 +00:00
|
|
|
VALUES ( vNewBookEntry,
|
|
|
|
vDated,
|
|
|
|
vOriginalAccount,
|
|
|
|
vCompensationAccount,
|
|
|
|
vConcept,
|
|
|
|
@vAmount1:= IF(
|
|
|
|
(vIsOriginalAClient AND vAmount > 0) OR
|
|
|
|
(NOT vIsOriginalAClient AND vAmount < 0),
|
|
|
|
0,
|
|
|
|
ABS(vAmount)
|
|
|
|
),
|
|
|
|
@vAmount2:= IF(@vAmount1,
|
|
|
|
0,
|
|
|
|
ABS(vAmount)
|
|
|
|
),
|
|
|
|
vCompanyFk
|
|
|
|
),
|
2024-05-16 16:48:59 +00:00
|
|
|
( vNewBookEntry,
|
|
|
|
vDated,
|
|
|
|
vCompensationAccount,
|
|
|
|
vOriginalAccount,
|
|
|
|
vConcept,
|
|
|
|
@vAmount2,
|
2024-01-15 11:31:03 +00:00
|
|
|
@vAmount1,
|
2024-05-16 16:48:59 +00:00
|
|
|
vCompanyFk);
|
|
|
|
|
|
|
|
IF vIsClientCompensation THEN
|
|
|
|
IF vIsOriginalAClient THEN
|
|
|
|
SET vAmount = -vAmount;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
INSERT INTO receipt
|
|
|
|
SET invoiceFk = vConcept,
|
|
|
|
amountPaid = vAmount,
|
|
|
|
payed = vDated,
|
|
|
|
bankFk = vBankFk,
|
|
|
|
companyFk = vCompanyFk,
|
|
|
|
clientFk = vClientFk,
|
|
|
|
isConciliate = TRUE;
|
|
|
|
ELSE
|
|
|
|
IF NOT vIsOriginalAClient THEN
|
|
|
|
SET vAmount = -vAmount;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT id INTO vSupplierFk
|
|
|
|
FROM supplier
|
|
|
|
WHERE `account` LIKE vCompensationAccount COLLATE utf8_general_ci;
|
|
|
|
|
|
|
|
SELECT id INTO vPayMethodCompensation
|
|
|
|
FROM payMethod
|
|
|
|
WHERE `code` = 'compensation';
|
|
|
|
|
|
|
|
INSERT INTO payment
|
|
|
|
SET received = vDated,
|
|
|
|
dueDated = vDated,
|
|
|
|
supplierFk = vSupplierFk,
|
|
|
|
amount = vAmount,
|
|
|
|
bankFk = vBankFk,
|
|
|
|
payMethodFk = vPayMethodCompensation,
|
|
|
|
concept = vConcept,
|
|
|
|
companyFk = vCompanyFk,
|
|
|
|
isConciliated = TRUE;
|
|
|
|
END IF;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|