Merge pull request 'fix(clientCredit): refs #6150 se arreglan los inserts en clientCredit' (!1818) from 6150-clientCredit-update-Fix into dev
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #1818
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Pablo Natek 2023-11-07 12:29:30 +00:00
commit 3ce0957de0
5 changed files with 122 additions and 16 deletions

View File

@ -0,0 +1,109 @@
ALTER TABLE `vn`.`client` MODIFY COLUMN `credit` decimal(10,2) unsigned DEFAULT 0.00 NOT NULL;
DELETE FROM `salix`.`ACL` WHERE `model` = 'Client' AND `property` = 'create';
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`client_beforeUpdate`
BEFORE UPDATE ON `client`
FOR EACH ROW
BEGIN
DECLARE vText VARCHAR(255) DEFAULT NULL;
DECLARE vPayMethodFk INT;
SET NEW.editorFk = account.myUser_getId();
IF NOT(NEW.credit <=> OLD.credit) THEN
INSERT INTO clientCredit
SET clientFk = NEW.id,
amount = NEW.credit,
workerFk = NEW.editorFk;
END IF;
-- Comprueba que el formato de los teléfonos es válido
IF !(NEW.phone <=> OLD.phone) AND (NEW.phone <> '') THEN
CALL pbx.phone_isValid(NEW.phone);
END IF;
IF !(NEW.mobile <=> OLD.mobile) AND (NEW.mobile <> '')THEN
CALL pbx.phone_isValid(NEW.mobile);
END IF;
SELECT id INTO vPayMethodFk
FROM vn.payMethod
WHERE code = 'bankDraft';
IF NEW.payMethodFk = vPayMethodFk AND NEW.dueDay = 0 THEN
SET NEW.dueDay = 5;
END IF;
-- Avisar al comercial si ha llegado la documentación sepa/core
IF NEW.hasSepaVnl AND !OLD.hasSepaVnl THEN
SET vText = 'Sepa de VNL';
END IF;
IF NEW.hasCoreVnl AND !OLD.hasCoreVnl THEN
SET vText = 'Core de VNL';
END IF;
IF vText IS NOT NULL
THEN
INSERT INTO mail(receiver, replyTo, `subject`, body)
SELECT
CONCAT(IF(ac.id,u.name, 'jgallego'), '@verdnatura.es'),
'administracion@verdnatura.es',
CONCAT('Cliente ', NEW.id),
CONCAT('Recibida la documentación: ', vText)
FROM worker w
LEFT JOIN account.user u ON w.id = u.id AND u.active
LEFT JOIN account.account ac ON ac.id = u.id
WHERE w.id = NEW.salesPersonFk;
END IF;
IF NEW.salespersonFk IS NULL AND OLD.salespersonFk IS NOT NULL THEN
IF (SELECT COUNT(clientFk)
FROM clientProtected
WHERE clientFk = NEW.id
) > 0 THEN
CALL util.throw("HAS_CLIENT_PROTECTED");
END IF;
END IF;
IF !(NEW.salesPersonFk <=> OLD.salesPersonFk) THEN
SET NEW.lastSalesPersonFk = IFNULL(NEW.salesPersonFk, OLD.salesPersonFk);
END IF;
IF !(NEW.businessTypeFk <=> OLD.businessTypeFk) AND (NEW.businessTypeFk = 'individual' OR OLD.businessTypeFk = 'individual') THEN
SET NEW.isTaxDataChecked = 0;
END IF;
END$$
DELIMITER ;
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`client_beforeInsert`
BEFORE INSERT ON `client`
FOR EACH ROW
BEGIN
IF NEW.credit NOT NULL AND NEW.credit THEN
INSERT INTO clientCredit
SET clientFk = NEW.id,
workerFk = NEW.editorFk,
amount = NEW.credit;
END IF;
SET NEW.editorFk = account.myUser_getId();
IF (NEW.phone <> '') THEN
CALL pbx.phone_isValid(NEW.phone);
END IF;
IF (NEW.mobile <> '') THEN
CALL pbx.phone_isValid(NEW.mobile);
END IF;
SET NEW.accountingAccount = 4300000000 + NEW.id;
SET NEW.lastSalesPersonFk = NEW.salesPersonFk;
END$$
DELIMITER ;

View File

@ -17,6 +17,9 @@
}, },
"maxCreditRows": { "maxCreditRows": {
"type": "number" "type": "number"
},
"defaultCredit": {
"type": "number"
} }
} }
} }

View File

@ -483,12 +483,6 @@ module.exports = Self => {
if (userRequiredRoles <= 0) if (userRequiredRoles <= 0)
throw new UserError(`You don't have enough privileges to set this credit amount`); throw new UserError(`You don't have enough privileges to set this credit amount`);
} }
await models.ClientCredit.create({
amount: changes.credit,
clientFk: finalState.id,
workerFk: userId
}, ctx.options);
}; };
Self.changeCreditManagement = async function changeCreditManagement(ctx, finalState, changes) { Self.changeCreditManagement = async function changeCreditManagement(ctx, finalState, changes) {

View File

@ -62,13 +62,13 @@ describe('Client Model', () => {
const options = {transaction: tx}; const options = {transaction: tx};
const ctx = {options}; const ctx = {options};
// Set credit to zero by a financialBoss
const financialBoss = await models.VnUser.findOne({ const financialBoss = await models.VnUser.findOne({
where: {name: 'financialBoss'} where: {name: 'financialBoss'}
}, options); }, options);
ctx.options.accessToken = {userId: financialBoss.id}; ctx.options.accessToken = {userId: financialBoss.id};
await models.Client.changeCredit(ctx, instance, {credit: 0}); const testClient = await models.Client.findById(instance.id, options);
await testClient.updateAttributes({credit: 0}, ctx.options);
const salesAssistant = await models.VnUser.findOne({ const salesAssistant = await models.VnUser.findOne({
where: {name: 'salesAssistant'} where: {name: 'salesAssistant'}

View File

@ -212,7 +212,7 @@
<vn-td number shrink>{{::sale.quantity}}</vn-td> <vn-td number shrink>{{::sale.quantity}}</vn-td>
<vn-td vn-fetched-tags> <vn-td vn-fetched-tags>
<div> <div>
<vn-one title="{{::sale.item.name}}">{{::sale.item.name}}</vn-one> <vn-one title="{{::sale.concept}}">{{::sale.concept}}</vn-one>
<vn-one ng-if="::sale.item.subName"> <vn-one ng-if="::sale.item.subName">
<h3 title="{{::sale.item.subName}}">{{::sale.item.subName}}</h3> <h3 title="{{::sale.item.subName}}">{{::sale.item.subName}}</h3>
</vn-one> </vn-one>