fix(clientCredit): refs #6150 se arreglan los inserts en clientCredit
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Pablo Natek 2023-11-02 14:05:14 +01:00
parent d2468033ea
commit 175e73e565
6 changed files with 102 additions and 17 deletions

View File

@ -0,0 +1,77 @@
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 ;

View File

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

View File

@ -450,14 +450,14 @@ module.exports = Self => {
if (lastCredit && lastCredit.amount == 0) { if (lastCredit && lastCredit.amount == 0) {
const zeroCreditEditor = const zeroCreditEditor =
await models.ACL.checkAccessAcl(accessToken, 'Client', 'zeroCreditEditor', 'WRITE'); await models.ACL.checkAccessAcl(accessToken, 'Client', 'zeroCreditEditor', 'WRITE');
const lastCreditIsNotEditable = const lastCreditIsNotEditable =
await models.ACL.checkAccessAcl( await models.ACL.checkAccessAcl(
{req: {accessToken: {userId: lastCredit.workerFk}}}, {req: {accessToken: {userId: lastCredit.workerFk}}},
'Client', 'Client',
'zeroCreditEditor', 'zeroCreditEditor',
'WRITE' 'WRITE'
); );
if (lastCreditIsNotEditable && !zeroCreditEditor) if (lastCreditIsNotEditable && !zeroCreditEditor)
throw new UserError(`You can't change the credit set to zero from a financialBoss`); throw new UserError(`You can't change the credit set to zero from a financialBoss`);
@ -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>

View File

@ -119,7 +119,8 @@ module.exports = Self => {
Self.new = async(ctx, options) => { Self.new = async(ctx, options) => {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {userId: ctx.req.accessToken.userId}; const {userId} = ctx.req.accessToken;
const myOptions = {userId};
const args = ctx.args; const args = ctx.args;
let tx; let tx;
@ -188,6 +189,16 @@ module.exports = Self => {
myOptions myOptions
); );
const {defaultCredit} = await models.ClientConfig.findOne(myOptions);
await models.ClientCredit.create(
{
clientFk: user.id,
amount: defaultCredit,
workerFk: userId
}, myOptions
);
const address = await models.Address.create( const address = await models.Address.create(
{ {
clientFk: user.id, clientFk: user.id,