feat: Create new Supplier
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Pau 2022-10-17 07:25:25 +02:00
parent 012cd68c89
commit dc6c8f924d
8 changed files with 83 additions and 41 deletions

View File

@ -0,0 +1,2 @@
INSERT INTO salix.ACL (model,property,accessType,principalId)
VALUES ('Supplier','newSupplier','WRITE','administrative');

View File

@ -0,0 +1,5 @@
ALTER TABLE vn.supplier MODIFY COLUMN payMethodFk tinyint(3) unsigned DEFAULT NULL NULL;
/* ------------------------------------------------------------------------------------------- */
ALTER TABLE vn.supplier MODIFY COLUMN supplierActivityFk varchar(45) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL NULL;

View File

@ -1,28 +0,0 @@
/* eslint-disable no-console */
// eslint-disable-next-line no-unused-vars
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('new', {
description: 'returns the created item',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: 'number',
root: true
},
http: {
path: `/new`,
verb: 'POST'
}
});
Self.new = async(params, options) => {
console.log(params);
console.log(options);
};
};

View File

@ -0,0 +1,50 @@
/* eslint-disable no-console */
// eslint-disable-next-line no-unused-vars
module.exports = Self => {
Self.remoteMethod('newSupplier', {
description: 'returns the created item',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
http: {source: 'body'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/newSupplier`,
verb: 'POST'
}
});
Self.newSupplier = async(params, options) => {
const models = Self.app.models;
const myOptions = {};
params.payDemFk = 1;
console.log(params);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const supplier = await models.Supplier.create(params, myOptions);
if (tx) await tx.commit();
return {
supplier
};
} catch (e) {
console.log(e);
if (tx) await tx.rollback();
return params;
}
};
};

View File

@ -10,7 +10,7 @@ module.exports = Self => {
require('../methods/supplier/freeAgencies')(Self);
require('../methods/supplier/campaignMetricsPdf')(Self);
require('../methods/supplier/campaignMetricsEmail')(Self);
require('../methods/supplier/new')(Self);
require('../methods/supplier/newSupplier')(Self);
Self.validatesPresenceOf('name', {
message: 'The social name cannot be empty'
@ -58,6 +58,9 @@ module.exports = Self => {
}
async function tinIsValid(err, done) {
if (!this.countryFk)
return done();
const filter = {
fields: ['code'],
where: {id: this.countryFk}
@ -81,6 +84,7 @@ module.exports = Self => {
});
async function hasSupplierAccount(err, done) {
if (!this.payMethodFk) return done();
const payMethod = await Self.app.models.PayMethod.findById(this.payMethodFk);
const supplierAccount = await Self.app.models.SupplierAccount.findOne({where: {supplierFk: this.id}});
const hasIban = supplierAccount && supplierAccount.iban;
@ -93,6 +97,7 @@ module.exports = Self => {
}
Self.observe('before save', async function(ctx) {
if (ctx.isNewInstance) return;
const loopbackContext = LoopBackContext.getCurrentContext();
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
@ -102,7 +107,7 @@ module.exports = Self => {
const isPayMethodChecked = changes.isPayMethodChecked || orgData.isPayMethodChecked;
const hasChanges = orgData && changes;
const isPayMethodCheckedChanged = hasChanges
&& orgData.isPayMethodChecked != isPayMethodChecked;
&& orgData.isPayMethodChecked != isPayMethodChecked;
if (isNotFinancial && isPayMethodCheckedChanged)
throw new UserError('You can not modify is pay method checked');
@ -115,7 +120,7 @@ module.exports = Self => {
const socialName = changes.name || orgData.name;
const hasChanges = orgData && changes;
const socialNameChanged = hasChanges
&& orgData.socialName != socialName;
&& orgData.socialName != socialName;
if ((socialNameChanged) && !isAlpha(socialName))
throw new UserError('The social name has an invalid format');

View File

@ -1,6 +1,6 @@
<vn-watcher
vn-id="watcher"
url="supplier/new"
url="suppliers/newSupplier"
data="$ctrl.supplier"
insert-mode="true"
form="form">

View File

@ -5,14 +5,22 @@ import Section from 'salix/components/section';
class Controller extends Section {
constructor($element, $) {
super($element, $);
console.log($);
}
onSubmit() {
this.$.watcher.submit().then(
console.log('abc'),
json => this.$state.go('item.card.basicData', {id: json.data.id})
json => {
console.log('abc');
this.$state.go('item.card.basicData', {id: json.data.id});
console.log('ctrl --> ', this.$.$ctrl);
redirect(this.$.$ctrl);
async function redirect(ctrl) {
await new Promise(r => setTimeout(r, 100));
window.location.href = `/#!/supplier/${ctrl.supplier.supplier.id}/fiscal-data`;
}
}
);
}
}

View File

@ -31,12 +31,6 @@
"component": "vn-supplier",
"description": "Suppliers"
},
{
"url": "/create",
"state": "supplier.create",
"component": "vn-supplier-create",
"description": "New supplier"
},
{
"url": "/index?q",
"state": "supplier.index",
@ -57,6 +51,12 @@
"params": {
"supplier": "$ctrl.supplier"
}
},
{
"url": "/create",
"state": "supplier.create",
"component": "vn-supplier-create",
"description": "New supplier"
},
{
"url": "/basic-data",