This commit is contained in:
Vicent Llopis 2022-03-03 14:42:12 +01:00
parent 4811254ccb
commit 2f87ac8c96
9 changed files with 247 additions and 63 deletions

View File

@ -1,6 +1,6 @@
module.exports = Self => {
Self.remoteMethod('createInvoiceIn', {
description: 'create a invoce in from a agency terms',
description: 'create a invoce in from one or more agency terms',
accessType: 'WRITE',
accepts: [{
arg: 'rows',
@ -12,7 +12,7 @@ module.exports = Self => {
arg: 'dms',
type: ['object'],
required: true,
description: 'The dms files'
description: 'the dms file'
}],
returns: {
},

View File

@ -0,0 +1,61 @@
const models = require('vn-loopback/server/server').models;
describe('AgencyTerm createInvoiceIn()', () => {
const rows = [
{
routeFk: 2,
supplierFk: 1,
created: '2022-03-02T23:00:00.000Z',
totalPrice: 165
}
];
const dms = [
{
id: 7,
file: '7.pdf',
contentType: 'application/pdf',
reference: '1',
description: 'Plants SL',
hasFile: false,
companyFk: 442,
dmsTypeFk: 1,
warehouseFk: 1,
workerFk: 9
}
];
const userId = 1;
const activeCtx = {
accessToken: {userId: userId},
};
const ctx = {req: activeCtx};
it('should make a global invoicing', async() => {
const tx = await models.AgencyTerm.beginTransaction({});
const options = {transaction: tx};
try {
const invoiceInId = 10;
const invoiceInDueDayId = 11;
const invoiceInTaxId = 12;
const oldInvoiceIn = await models.AgencyTerm.findById(invoiceInId, null, options);
const oldInvoiceInDueDay = await models.AgencyTerm.findById(invoiceInDueDayId, null, options);
const oldInvoiceInTax = await models.AgencyTerm.findById(invoiceInTaxId, null, options);
const [newInvoiceIn] = await models.Ticket.rawSql('SELECT MAX(id) id FROM invoiceIn', null, options);
// const [newInvoiceInDueDay] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInDueDay', null, options);
// const [newInvoiceInTax] = await models.AgencyTerm.rawSql('SELECT MAX(id) id FROM invoiceInTax', null, options);
await models.AgencyTerm.createInvoiceIn(rows, dms, options);
// expect(newInvoiceIn.id).toEqual(oldInvoiceIn.id + 1);
// expect(newInvoiceInDueDay.id).toEqual(oldInvoiceInDueDay.id + 1);
// expect(newInvoiceInTax.id).toEqual(oldInvoiceInTax.id + 1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -0,0 +1,26 @@
const models = require('vn-loopback/server/server').models;
xdescribe('AgencyTerm filter()', () => {
const authUserId = 9;
it('should all return the tickets matching the filter', async() => {
const tx = await models.AgencyTerm.beginTransaction({});
try {
const options = {transaction: tx};
const filter = {};
const ctx = {req: {accessToken: {userId: authUserId}}, args: {filter: filter}};
const agencyTerms = await models.AgencyTerm.filter(ctx, null, options);
const firstAgencyTerm = agencyTerms[0];
expect(firstAgencyTerm.routeFk).toEqual(1);
expect(agencyTerms.length).toEqual(3);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -10,7 +10,6 @@ class Controller extends Section {
hasFile: false,
hasFileAttached: false
};
this.addedDms;
}
get route() {
@ -83,7 +82,6 @@ class Controller extends Section {
this.$http(options).then(res => {
if (res) {
let addedDms = res.data;
this.vnApp.showSuccess(this.$t('Data saved!'));
this.$.watcher.updateOriginalData();
const params = {
@ -94,7 +92,7 @@ class Controller extends Section {
this.$http.post('AgencyTerms/createInvoiceIn', params)
.then(() => {
this.$state.go('route.agencyTerm.index');
this.vnApp.showSuccess(this.$t('InvoiceIn created'));
this.vnApp.showSuccess(this.$t('Data saved!'));
});
}
});

View File

@ -1,64 +1,53 @@
import './index';
import watcher from 'core/mocks/watcher.js';
describe('Client', () => {
describe('Component vnClientDmsCreate', () => {
describe('AgencyTerm', () => {
describe('Component vnAgencyTermCreateInvoiceIn', () => {
let controller;
let $scope;
let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('client'));
beforeEach(ngModule('route'));
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
const $element = angular.element('<vn-client-create></vn-client-create>');
controller = $componentController('vnClientDmsCreate', {$element, $scope});
controller._client = {id: 1101, name: 'Bruce wayne'};
const $element = angular.element('<vn-agency-term-create-invoice-in></vn-agency-term-create-invoice-in>');
controller = $componentController('vnAgencyTermCreateInvoiceIn', {$element});
controller._route = {
id: 1
};
}));
describe('client() setter', () => {
it('should set the client data and then call setDefaultParams() and getAllowedContentTypes()', () => {
describe('$onChanges()', () => {
it('should update the params data when $params.q is defined', () => {
controller.$params = {q: '{"supplierName": "Plants SL","rows": null}'};
const params = {q: '{"supplierName": "Plants SL", "rows": null}'};
const json = JSON.parse(params.q);
controller.$onChanges();
expect(controller.params).toEqual(json);
});
});
describe('route() setter', () => {
it('should set the ticket data and then call setDefaultParams() and getAllowedContentTypes()', () => {
jest.spyOn(controller, 'setDefaultParams');
jest.spyOn(controller, 'getAllowedContentTypes');
controller.client = {
id: 15,
name: 'Bruce wayne'
controller.route = {
id: 1
};
expect(controller.client).toBeDefined();
expect(controller.route).toBeDefined();
expect(controller.setDefaultParams).toHaveBeenCalledWith();
expect(controller.getAllowedContentTypes).toHaveBeenCalledWith();
});
});
describe('setDefaultParams()', () => {
it('should perform a GET query and define the dms property on controller', () => {
const params = {filter: {
where: {code: 'paymentsLaw'}
}};
let serializedParams = $httpParamSerializer(params);
$httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 12, code: 'paymentsLaw'});
controller.setDefaultParams();
$httpBackend.flush();
expect(controller.dms).toBeDefined();
expect(controller.dms.reference).toEqual(1101);
expect(controller.dms.dmsTypeId).toEqual(12);
});
});
describe('onFileChange()', () => {
it('should set dms hasFileAttached property to true if has any files', () => {
const files = [{id: 1, name: 'MyFile'}];
controller.onFileChange(files);
$scope.$apply();
expect(controller.dms.hasFileAttached).toBeTruthy();
});
});
describe('getAllowedContentTypes()', () => {
it('should make an HTTP GET request to get the allowed content types', () => {
const expectedResponse = ['image/png', 'image/jpg'];
@ -70,5 +59,49 @@ describe('Client', () => {
expect(controller.allowedContentTypes).toEqual('image/png, image/jpg');
});
});
describe('setDefaultParams()', () => {
it('should perform a GET query and define the dms property on controller', () => {
const params2 = {filter: {
where: {code: 'invoiceIn'}
}};
let serializedParams = $httpParamSerializer(params2);
$httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 1, code: 'invoiceIn'});
controller.params = {supplierName: 'Plants SL'};
controller.setDefaultParams();
$httpBackend.flush();
expect(controller.dms).toBeDefined();
expect(controller.dms.dmsTypeId).toEqual(1);
});
});
describe('onSubmit()', () => {
it('should make an HTTP POST request to save the form data', () => {
controller.$.watcher = watcher;
jest.spyOn(controller.$.watcher, 'updateOriginalData');
const files = [{id: 1, name: 'MyFile'}];
controller.dms = {files};
const serializedParams = $httpParamSerializer(controller.dms);
const query = `dms/uploadFile?${serializedParams}`;
controller.params = {rows: null};
$httpBackend.expect('POST', query).respond({});
$httpBackend.expect('POST', 'AgencyTerms/createInvoiceIn').respond({});
controller.onSubmit();
$httpBackend.flush();
});
});
describe('onFileChange()', () => {
it('should set dms hasFileAttached property to true if has any files', () => {
const files = [{id: 1, name: 'MyFile'}];
controller.onFileChange(files);
$scope.$apply();
expect(controller.dms.hasFileAttached).toBeTruthy();
});
});
});
});

View File

@ -99,10 +99,11 @@ class Controller extends Section {
createInvoiceIn() {
const rowsToCreateInvoiceIn = [];
const supplier = this.checked[0].supplierFk;
const supplierFk = this.checked[0].supplierFk;
for (let agencyTerm of this.checked) {
if (supplier == agencyTerm.supplierFk) {
let hasSameSupplier = supplierFk == agencyTerm.supplierFk;
if (hasSameSupplier) {
rowsToCreateInvoiceIn.push({
routeFk: agencyTerm.routeFk,
supplierFk: agencyTerm.supplierFk,

View File

@ -1,29 +1,93 @@
import './index.js';
import crudModel from 'core/mocks/crud-model';
describe('Item', () => {
describe('Component vnItemIndex', () => {
describe('AgencyTerm', () => {
describe('Component vnAgencyTermIndex', () => {
let controller;
let $httpBackend;
let $scope;
let $window;
beforeEach(ngModule('item'));
beforeEach(ngModule('route'));
beforeEach(inject(($componentController, _$httpBackend_, $rootScope) => {
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
const $element = angular.element('<vn-item-index></vn-item-index>');
controller = $componentController('vnItemIndex', {$element, $scope});
beforeEach(inject(($componentController, _$window_) => {
$window = _$window_;
const $element = angular.element('<vn-agency-term-index></vn-agency-term-index>');
controller = $componentController('vnAgencyTermIndex', {$element});
controller.$.model = crudModel;
controller.$.model.data = [
{supplierFk: 1, totalPrice: null},
{supplierFk: 1},
{supplierFk: 2}
];
}));
describe('onCloneAccept()', () => {
it('should perform a post query and then call go() then update itemSelected in the controller', () => {
describe('checked() getter', () => {
it('should return the checked lines', () => {
const data = controller.$.model.data;
data[0].checked = true;
const checkedRows = controller.checked;
const firstCheckedRow = checkedRows[0];
expect(firstCheckedRow.supplierFk).toEqual(1);
});
});
describe('totalCheked() getter', () => {
it('should return the total checked lines', () => {
const data = controller.$.model.data;
data[0].checked = true;
const checkedRows = controller.totalChecked;
expect(checkedRows).toEqual(1);
});
});
describe('preview()', () => {
it('should show the dialog summary', () => {
controller.$.summary = {show: () => {}};
jest.spyOn(controller.$.summary, 'show');
let event = new MouseEvent('click', {
view: $window,
bubbles: true,
cancelable: true
});
const route = {id: 1};
controller.preview(event, route);
expect(controller.$.summary.show).toHaveBeenCalledWith();
});
});
describe('createInvoiceIn()', () => {
it('should throw an error if are checked more than one autonomous', () => {
jest.spyOn(controller.vnApp, 'showError');
const data = controller.$.model.data;
data[0].checked = true;
data[2].checked = true;
controller.createInvoiceIn();
expect(controller.vnApp.showError).toHaveBeenCalled();
});
it('should call the function go() on $state to go to the file managment', () => {
jest.spyOn(controller.$state, 'go');
const data = controller.$.model.data;
data[0].checked = true;
$httpBackend.expectRoute('POST', `Items/:id/clone`).respond({id: 99});
controller.onCloneAccept(1);
$httpBackend.flush();
controller.createInvoiceIn();
expect(controller.$state.go).toHaveBeenCalledWith('item.card.tags', {id: 99});
delete data[0].checked;
const params = JSON.stringify({
supplierName: data[0].supplierName,
rows: [data[0]]
});
expect(controller.$state.go).toHaveBeenCalledWith('route.agencyTerm.createInvoiceIn', {q: params});
});
});
});

View File

@ -2,4 +2,5 @@ picture: Foto
Buy requests: Peticiones de compra
Agency route: Agencia ruta
Agency Agreement: Agencia acuerdo
Autonomous: Autónomo
Autonomous: Autónomos
Two autonomous cannot be counted at the same time: Dos autonónomos no pueden ser contabilizados al mismo tiempo

View File

@ -53,7 +53,7 @@
"url": "/createInvoiceIn?q",
"state": "route.agencyTerm.createInvoiceIn",
"component": "vn-agency-term-create-invoice-in",
"description": "Create invoiceIn",
"description": "File management",
"params": {
"route": "$ctrl.route"
},