diff --git a/modules/invoiceIn/back/methods/invoice-in/getSerial.js b/modules/invoiceIn/back/methods/invoice-in/getSerial.js index 8635a0be7..a6c5ad00e 100644 --- a/modules/invoiceIn/back/methods/invoice-in/getSerial.js +++ b/modules/invoiceIn/back/methods/invoice-in/getSerial.js @@ -1,12 +1,17 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + module.exports = Self => { Self.remoteMethod('getSerial', { description: 'Return invoiceIn serial', accessType: 'READ', - accepts: { - arg: 'issued', - type: 'date', + accepts: [{ + arg: 'daysAgo', + type: 'number', required: true - }, + }, { + arg: 'serial', + type: 'string' + }], returns: { type: 'object', root: true @@ -17,17 +22,25 @@ module.exports = Self => { } }); - Self.getSerial = async(issued, options) => { - const myOptions = {}; + Self.getSerial = async(daysAgo, serial) => { + const conn = Self.dataSource.connector; + const stmt = []; - if (typeof options == 'object') - Object.assign(myOptions, options); + const issued = Date.vnNew(); + issued.setDate(issued.getDate() - daysAgo); - const result = await Self.rawSql(` + stmt.push(new ParameterizedSQL(` SELECT i.serial, SUM(IF(i.isBooked, 0,1)) pending, COUNT(*) total FROM vn.invoiceIn i - WHERE i.issued >= ? - GROUP BY i.serial`, [issued]); + WHERE i.issued >= ? `, [issued])); + + if (serial) + stmt.push(new ParameterizedSQL(`AND i.serial LIKE ? `, [serial])); + + stmt.push(`GROUP BY i.serial`); + + const sql = ParameterizedSQL.join(stmt); + const result = await conn.executeStmt(sql); return result; }; diff --git a/modules/invoiceIn/back/methods/invoice-in/specs/getSerial.spec.js b/modules/invoiceIn/back/methods/invoice-in/specs/getSerial.spec.js new file mode 100644 index 000000000..d07fef196 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/specs/getSerial.spec.js @@ -0,0 +1,21 @@ +const models = require('vn-loopback/server/server').models; + +describe('invoiceIn getSerial()', () => { + it('should check that returns without serial param', async() => { + const result = await models.InvoiceIn.getSerial(45); + + expect(result.length).toBeGreaterThan(0); + }); + + it('should check that returns with serial param', async() => { + const result = await models.InvoiceIn.getSerial(45, 'R'); + + expect(result.length).toBeGreaterThan(0); + }); + + it('should check that returns with non exist serial param', async() => { + const result = await models.InvoiceIn.getSerial(45, 'Mock serial'); + + expect(result.length).toEqual(0); + }); +}); diff --git a/modules/invoiceIn/front/locale/es.yml b/modules/invoiceIn/front/locale/es.yml index f2f77b690..a2d658519 100644 --- a/modules/invoiceIn/front/locale/es.yml +++ b/modules/invoiceIn/front/locale/es.yml @@ -23,3 +23,4 @@ Total stems: Total tallos Show agricultural receipt as PDF: Ver recibo agrícola como PDF Send agricultural receipt as PDF: Enviar recibo agrícola como PDF New InvoiceIn: Nueva Factura +Days ago: Últimos días diff --git a/modules/invoiceIn/front/serial-search-panel/index.html b/modules/invoiceIn/front/serial-search-panel/index.html index 467f7439c..c412e783c 100644 --- a/modules/invoiceIn/front/serial-search-panel/index.html +++ b/modules/invoiceIn/front/serial-search-panel/index.html @@ -1,26 +1,28 @@ - - + ng-keydown="$ctrl.onKeyPress($event)" + required="true" + min="0"> +
- Id/Name: {{$ctrl.filter.search}} + {{$ctrl.$t('Serial')}}: {{$ctrl.filter.serial}}
diff --git a/modules/invoiceIn/front/serial-search-panel/index.js b/modules/invoiceIn/front/serial-search-panel/index.js index 334e4cf01..b11911ee3 100644 --- a/modules/invoiceIn/front/serial-search-panel/index.js +++ b/modules/invoiceIn/front/serial-search-panel/index.js @@ -5,16 +5,16 @@ import './style.scss'; class Controller extends SearchPanel { constructor($element, $) { super($element, $); + this.filter = {}; const filter = { fields: ['daysAgo'] }; - this.$http.get('InvoiceInConfigs', {filter}); - } - - $onInit() { - this.filter = { - tags: [] - }; + this.$http.get('InvoiceInConfigs', {filter}).then(res => { + if (res.data) { + this.invoiceInConfig = res.data[0]; + this.addFilters(); + } + }); } removeItemFilter(param) { @@ -28,6 +28,9 @@ class Controller extends SearchPanel { } addFilters() { + if (!this.filter.daysAgo) + this.filter.daysAgo = this.invoiceInConfig.daysAgo; + return this.model.addFilter({}, this.filter); } } diff --git a/modules/invoiceIn/front/serial-search-panel/index.spec.js b/modules/invoiceIn/front/serial-search-panel/index.spec.js new file mode 100644 index 000000000..b5228e126 --- /dev/null +++ b/modules/invoiceIn/front/serial-search-panel/index.spec.js @@ -0,0 +1,43 @@ +import './index.js'; + +describe('InvoiceIn', () => { + describe('Component serial-search-panel', () => { + let controller; + let $scope; + + beforeEach(ngModule('invoiceIn')); + + beforeEach(inject(($componentController, $rootScope) => { + $scope = $rootScope.$new(); + const $element = angular.element(''); + controller = $componentController('vnInvoiceInSerialSearchPanel', {$element, $scope}); + controller.model = { + addFilter: jest.fn(), + }; + controller.invoiceInConfig = { + daysAgo: 45, + }; + })); + + describe('addFilters()', () => { + it('should add default daysAgo if it is not already set', () => { + controller.filter = { + serial: 'R', + }; + controller.addFilters(); + + expect(controller.filter.daysAgo).toEqual(controller.invoiceInConfig.daysAgo); + }); + + it('should not add default daysAgo if it is already set', () => { + controller.filter = { + daysAgo: 1, + serial: 'R', + }; + controller.addFilters(); + + expect(controller.filter.daysAgo).toEqual(1); + }); + }); + }); +}); diff --git a/modules/invoiceIn/front/serial/index.html b/modules/invoiceIn/front/serial/index.html index f8ca4bbbb..e381e7293 100644 --- a/modules/invoiceIn/front/serial/index.html +++ b/modules/invoiceIn/front/serial/index.html @@ -1,9 +1,7 @@ + limit="20"> @@ -30,7 +28,7 @@ {{::invoiceIn.total}} diff --git a/modules/invoiceIn/front/serial/index.js b/modules/invoiceIn/front/serial/index.js index 622087748..9d27e4e8f 100644 --- a/modules/invoiceIn/front/serial/index.js +++ b/modules/invoiceIn/front/serial/index.js @@ -6,24 +6,10 @@ export default class Controller extends Section { super($element, $); } - exprBuilder(param, value) { - switch (param) { - case 'issued': - return {'ii.issued': { - between: this.dateRange(value)} - }; - case 'serial': - return {[`ii.${param}`]: value}; - } - } - - dateRange(value) { - const minHour = new Date(value); - minHour.setHours(0, 0, 0, 0); - const maxHour = new Date(value); - maxHour.setHours(23, 59, 59, 59); - - return [minHour, maxHour]; + goToIndex(daysAgo) { + const issued = Date.vnNew(); + issued.setDate(issued.getDate() - daysAgo); + this.$state.go('invoiceIn.index', {q: `{"isBooked": true, "from": ${issued.getTime()}}`}); } }