Merge branch '1935-ticket_create_auto_fill' of verdnatura/salix into dev
gitea/salix/dev This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2019-12-26 08:14:22 +00:00 committed by Gitea
commit c992cbc5c6
7 changed files with 208 additions and 132 deletions

View File

@ -18,7 +18,6 @@ module.exports = Self => {
});
Self.regularizeClaim = async(ctx, params) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const resolvedState = 3;
@ -55,11 +54,10 @@ module.exports = Self => {
if (!ticketFk) {
ticketFk = await createTicket(ctx, {
clientFk: address.clientFk,
addressFk: addressFk,
warehouseFk: sale.ticket().warehouseFk,
companyFk: sale.ticket().companyFk,
userId: userId
clientId: address.clientFk,
warehouseId: sale.ticket().warehouseFk,
companyId: sale.ticket().companyFk,
addressId: addressFk
}, options);
}
@ -140,16 +138,21 @@ module.exports = Self => {
}
async function createTicket(ctx, params, options) {
let ticket = await Self.app.models.Ticket.new(ctx,
{
shipped: new Date(),
landed: new Date(),
clientFk: params.clientFk,
warehouseFk: params.warehouseFk,
companyFk: params.companyFk,
addressFk: params.addressFk,
userId: params.userId
}, options);
params.shipped = new Date();
params.landed = new Date();
params.agencyModeId = null;
params.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
return ticket.id;
}

View File

@ -11,7 +11,7 @@
</section>
<vn-data-viewer
model="model"
class="vn-w-lg">
class="vn-w-xl">
<vn-horizontal class="photo-list">
<section class="photo" ng-repeat="photo in $ctrl.photos">
<section class="image vn-shadow" on-error-src

View File

@ -29,7 +29,6 @@ module.exports = Self => {
});
Self.regularize = async(ctx, itemFk, quantity, warehouseFk) => {
const userId = ctx.req.accessToken.userId;
const models = Self.app.models;
const itemDestination = await models.ClaimDestination.findOne({
@ -56,10 +55,9 @@ module.exports = Self => {
if (!ticketFk) {
ticketFk = await createTicket(ctx, {
clientFk: itemDestination.address().clientFk,
addressFk: itemDestination.addressFk,
warehouseFk: warehouseFk,
userId: userId
clientId: itemDestination.address().clientFk,
warehouseId: warehouseFk,
addressId: itemDestination.addressFk
}, options);
}
@ -88,17 +86,22 @@ module.exports = Self => {
}
async function createTicket(ctx, params, options) {
let ticket = await Self.app.models.Ticket.new(
ctx,
{
shipped: new Date(),
landed: new Date(),
clientFk: params.clientFk,
warehouseFk: params.warehouseFk,
companyFk: params.companyFk,
addressFk: params.addressFk,
userId: params.userId
}, options);
params.shipped = new Date();
params.landed = new Date();
params.companyId = null;
params.agencyModeId = null;
params.routeId = null;
const ticket = await Self.app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyId,
params.addressId,
params.agencyModeId,
params.routeId,
options);
return ticket.id;
}

View File

@ -2,14 +2,50 @@ let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('new', {
description: 'Create a newticket and returns the new ID',
description: 'Creates a new ticket and returns the id',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: 'ClientFk, Shipped, WarehouseFk, CompanyFk, AddressFk, AgencyModeFk, RouteFk, Landed, userId',
http: {source: 'body'}
arg: 'clientId',
type: 'Number',
description: `The client id filter`,
required: true
},
{
arg: 'shipped',
type: 'Date',
description: `The shipment date filter`
},
{
arg: 'landed',
type: 'Date',
description: `The landing date filter`
},
{
arg: 'warehouseId',
type: 'Number',
description: `The warehouse id filter`,
required: true
},
{
arg: 'companyId',
type: 'Number',
description: `The company id filter`
},
{
arg: 'addressId',
type: 'Number',
description: `The address id filter`,
required: true
},
{
arg: 'agencyModeId',
type: 'Number',
description: `The agencyMode id filter`
},
{
arg: 'routeId',
type: 'Number',
description: `The route id filter`
}],
returns: {
type: 'number',
@ -21,10 +57,12 @@ module.exports = Self => {
}
});
Self.new = async(ctx, params, options) => {
let models = Self.app.models;
let address = await models.Address.findOne({
where: {id: params.addressFk},
Self.new = async(ctx, clientId, shipped, landed, warehouseId,
companyId, addressId, agencyModeId, routeId, options) => {
const myUserId = ctx.req.accessToken.userId;
const models = Self.app.models;
const address = await models.Address.findOne({
where: {id: addressId},
fields: ['id', 'clientFk'],
include: {
relation: 'client',
@ -36,18 +74,12 @@ module.exports = Self => {
}
});
// FIXME: #1953 Params should be declared with it's correct argument type
if (params.shipped)
params.shipped = new Date(params.shipped);
if (params.landed)
params.landed = new Date(params.landed);
if (!address)
throw new UserError(`This address doesn't exist`);
let agencyMode;
if (params && params.agencyModeFk)
agencyMode = await models.AgencyMode.findById(params.agencyModeFk);
if (agencyModeId)
agencyMode = await models.AgencyMode.findById(agencyModeId);
if (address.client().type().code === 'normal' && (!agencyMode || agencyMode.code != 'refund')) {
if (!address.client().isActive)
@ -65,33 +97,30 @@ module.exports = Self => {
}
try {
if (!params.shipped && params.landed) {
const shippedResult = await models.Agency.getShipped(params.landed,
address.id, params.agencyModeFk, params.warehouseFk);
params.shipped = shippedResult && shippedResult.shipped;
if (!shipped && landed) {
const shippedResult = await models.Agency.getShipped(landed,
address.id, agencyModeId, warehouseId);
shipped = shippedResult && shippedResult.shipped;
}
if (params.shipped && !params.landed) {
const landedResult = await models.Agency.getLanded(params.shipped,
address.id, params.agencyModeFk, params.warehouseFk);
params.landed = landedResult && landedResult.landed;
if (shipped && !landed) {
const landedResult = await models.Agency.getLanded(shipped,
address.id, agencyModeId, warehouseId);
landed = landedResult && landedResult.landed;
}
if (!params.userId && ctx.req && ctx.req.accessToken.userId)
params.userId = ctx.req.accessToken.userId;
query = `CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @result);
SELECT @result newTicketId;`;
let result = await Self.rawSql(query, [
params.clientFk,
params.shipped,
params.warehouseFk,
params.companyFk || 442,
params.addressFk,
params.agencyModeFk || null,
params.routeFk || null,
params.landed,
params.userId
clientId,
shipped,
warehouseId,
companyId || 442,
addressId,
agencyModeId || null,
routeId || null,
landed,
myUserId
], options);
let ticket = await models.Ticket.findById(result[1][0].newTicketId, null, options);
@ -99,7 +128,7 @@ module.exports = Self => {
let logRecord = {
originFk: cleanInstance.id,
userFk: params.userId,
userFk: myUserId,
action: 'create',
changedModel: 'Ticket',
changedModelId: cleanInstance.id,

View File

@ -15,42 +15,74 @@ describe('ticket new()', () => {
it('should throw an error if the client isnt frozen and isnt active', async() => {
let error;
let params = {addressFk: 6};
let params = {
clientId: 106,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 6
};
await app.models.Ticket.new(ctx, params)
.catch(e => {
error = e;
});
await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyFk,
params.addressId
).catch(e => {
error = e;
});
expect(error).toEqual(new UserError(`You can't create a ticket for a inactive client`));
});
it('should throw an error if the address doesnt exist', async() => {
let error;
let params = {addressFk: 'invalid address', clientFk: 104};
let params = {
clientId: 104,
shipped: today,
landed: null,
warehouseId: 1,
companyId: 442,
addressId: 'invalid address'
};
await app.models.Ticket.new(ctx, params)
.catch(response => {
expect(response.message).toEqual(`This address doesn't exist`);
error = response;
});
await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyFk,
params.addressId
).catch(response => {
expect(response.message).toEqual(`This address doesn't exist`);
error = response;
});
expect(error).toBeDefined();
});
it('should return the id of the created ticket', async() => {
let params = {
warehouseFk: 1,
clientFk: 104,
companyFk: 442,
addressFk: 4,
agencyModeFk: 1,
userId: 9,
clientId: 104,
shipped: today,
landed: today
landed: today,
warehouseId: 1,
companyId: 442,
addressId: 4,
agencyModeId: 1
};
ticket = await app.models.Ticket.new(ctx, params);
ticket = await app.models.Ticket.new(ctx,
params.clientId,
params.shipped,
params.landed,
params.warehouseId,
params.companyFk,
params.addressId,
params.agencyModeId);
let newestTicketIdInFixtures = 21;

View File

@ -1,20 +1,19 @@
<vn-autocomplete
vn-focus
<vn-autocomplete vn-focus
vn-id="client"
url="Clients"
label="Client"
search-function="{or: [{id: $search}, {name: {like: '%'+ $search +'%'}}]}"
show-field="name"
value-field="id"
ng-model="$ctrl.clientFk"
ng-model="$ctrl.clientId"
order="id">
<tpl-item>{{id}}: {{name}}</tpl-item>
</vn-autocomplete>
<vn-autocomplete
disabled="!$ctrl.clientFk"
url="{{ $ctrl.clientFk ? 'Clients/'+ $ctrl.clientFk +'/addresses' : null }}"
disabled="!$ctrl.clientId"
url="{{ $ctrl.clientId ? 'Clients/'+ $ctrl.clientId +'/addresses' : null }}"
fields="['nickname', 'street', 'city']"
ng-model="$ctrl.addressFk"
ng-model="$ctrl.addressId"
show-field="nickname"
value-field="id"
label="Address">
@ -25,18 +24,18 @@
ng-model="$ctrl.landed">
</vn-date-picker>
<vn-autocomplete
disabled="!$ctrl.warehouseFk && (!$ctrl.clientFk || !$ctrl.landed)"
ng-model="$ctrl.warehouseFk"
disabled="!$ctrl.warehouseId && (!$ctrl.clientId || !$ctrl.landed)"
ng-model="$ctrl.warehouseId"
url="Warehouses"
show-field="name"
value-field="id"
label="Warehouse">
</vn-autocomplete>
<vn-autocomplete
disabled="!$ctrl.clientFk || !$ctrl.landed || !$ctrl.warehouseFk"
disabled="!$ctrl.clientId || !$ctrl.landed || !$ctrl.warehouseId"
data="$ctrl._availableAgencies"
label="Agency"
show-field="agencyMode"
value-field="agencyModeFk"
ng-model="$ctrl.ticket.agencyModeFk">
ng-model="$ctrl.agencyModeId">
</vn-autocomplete>

View File

@ -19,16 +19,20 @@ class Controller {
this.warehouseFk = this.vnConfig.warehouseFk;
}
get ticket() {
return this._ticket;
}
set ticket(value) {
if (value)
this._ticket = value;
}
get ticket() {
return this._ticket;
get clientId() {
return this.ticket.clientFk;
}
set clientFk(value) {
set clientId(value) {
this.ticket.clientFk = value;
if (value) {
@ -36,7 +40,7 @@ class Controller {
include: {
relation: 'defaultAddress',
scope: {
fields: 'id'
fields: ['id', 'agencyModeFk']
}
},
where: {id: value}
@ -44,32 +48,21 @@ class Controller {
filter = encodeURIComponent(JSON.stringify(filter));
let query = `Clients?filter=${filter}`;
this.$http.get(query).then(res => {
if (res.data) {
let client = res.data[0];
let defaultAddress = client.defaultAddress;
this.addressFk = defaultAddress.id;
}
const [client] = res.data;
this.defaultAddress = client.defaultAddress;
this.addressId = this.defaultAddress.id;
});
} else
this.addressFk = null;
this.addressId = null;
this.getAvailableAgencies();
}
get clientFk() {
return this.ticket.clientFk;
}
set addressFk(value) {
this.ticket.addressFk = value;
this.getAvailableAgencies();
}
get addressFk() {
get addressId() {
return this.ticket.addressFk;
}
set landed(value) {
this.ticket.landed = value;
set addressId(value) {
this.ticket.addressFk = value;
this.getAvailableAgencies();
}
@ -77,12 +70,26 @@ class Controller {
return this.ticket.landed;
}
set warehouseFk(value) {
set landed(value) {
this.ticket.landed = value;
this.getAvailableAgencies();
}
get warehouseId() {
return this.ticket.warehouseFk;
}
set warehouseId(value) {
this.ticket.warehouseFk = value;
this.getAvailableAgencies();
}
get warehouseFk() {
return this.ticket.warehouseFk;
get agencyModeId() {
return this.ticket.agencyModeFk;
}
set agencyModeId(value) {
this.ticket.agencyModeFk = value;
}
getAvailableAgencies() {
@ -96,8 +103,11 @@ class Controller {
if (params.warehouseFk && params.addressFk && params.landed) {
ticket.agencyModeFk = null;
this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params})
.then(res => this._availableAgencies = res.data);
this.$http.get(`Agencies/getAgenciesWithWarehouse`, {params}).then(res => {
this._availableAgencies = res.data;
this.agencyModeId = this.defaultAddress.agencyModeFk;
});
}
}
@ -107,11 +117,11 @@ class Controller {
createTicket() {
let params = {
clientFk: this.ticket.clientFk,
clientId: this.ticket.clientFk,
landed: this.ticket.landed,
addressFk: this.ticket.addressFk,
agencyModeFk: this.ticket.agencyModeFk,
warehouseFk: this.ticket.warehouseFk,
addressId: this.ticket.addressFk,
agencyModeId: this.ticket.agencyModeFk,
warehouseId: this.ticket.warehouseFk,
};
this.$http.post(`Tickets/new`, params).then(res => {
this.vnApp.showSuccess(this.translate.instant('Data saved!'));