parent
11a402e18c
commit
9e550e746a
|
@ -3,19 +3,19 @@
|
|||
<div style="max-width: 40em; margin: 0 auto;">
|
||||
<vn-card>
|
||||
<vn-horizontal pad-medium>
|
||||
<vn-searchbar
|
||||
vn-auto
|
||||
<vn-searchbar vn-auto
|
||||
index="index"
|
||||
on-search="index.accept()"
|
||||
on-search="$ctrl.search(index)"
|
||||
advanced="true"
|
||||
search="$ctrl.model.search"
|
||||
popover="vn-client-search-panel">
|
||||
</vn-searchbar>
|
||||
</vn-horizontal>
|
||||
</vn-card>
|
||||
<vn-card margin-medium-top>
|
||||
<vn-item-client ng-repeat="client in index.model" title="View client" client="client"></vn-item-client>
|
||||
<vn-item-client ng-repeat="client in index.model.instances" title="View client" client="client"></vn-item-client>
|
||||
</vn-card>
|
||||
<vn-paging index="index"></vn-paging>
|
||||
<vn-paging index="index" total="index.model.count"></vn-paging>
|
||||
</div>
|
||||
<a ui-sref="create" fixed-bottom-right>
|
||||
<vn-float-button icon="person_add"></vn-float-button>
|
||||
|
|
|
@ -2,6 +2,16 @@ import {module} from '../module';
|
|||
import './style.css';
|
||||
import './item-client';
|
||||
|
||||
module.component('vnClientIndex', {
|
||||
template: require('./index.html')
|
||||
});
|
||||
class Controller {
|
||||
search(index) {
|
||||
index.filter.search = this.model.search;
|
||||
index.accept();
|
||||
}
|
||||
}
|
||||
|
||||
export const NAME = 'vnClientIndex';
|
||||
export const COMPONENT = {
|
||||
template: require('./index.html'),
|
||||
controller: Controller
|
||||
};
|
||||
module.component(NAME, COMPONENT);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<form ng-submit="$ctrl.onSubmit()">
|
||||
<vn-horizontal>
|
||||
<vn-textfield vn-one label="Search" model="$ctrl.model.search"></vn-textfield>
|
||||
<vn-textfield vn-one label="Search" model="$ctrl.search"></vn-textfield>
|
||||
<vn-icon
|
||||
ng-if="$ctrl.advanced"
|
||||
ng-click="$ctrl.onClick($event)"
|
||||
|
|
|
@ -22,9 +22,10 @@ export default class Controller {
|
|||
}
|
||||
onChildSubmit(filter) {
|
||||
this.vnPopover.hide();
|
||||
this.index.filter = filter;
|
||||
Object.assign(this.index.filter, filter);
|
||||
this.onSubmit();
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
if (this.onSearch)
|
||||
this.onSearch();
|
||||
|
@ -39,6 +40,7 @@ ngModule.component('vnSearchbar', {
|
|||
template: require('./searchbar.html'),
|
||||
bindings: {
|
||||
index: '<',
|
||||
search: '=',
|
||||
onSearch: '&',
|
||||
advanced: '=',
|
||||
popover: '@'
|
||||
|
|
|
@ -5,6 +5,8 @@ module.exports = function(Client) {
|
|||
require('../scopes/client/card.js')(Client);
|
||||
require('../scopes/client/activate.js')(Client);
|
||||
require('../scopes/client/addresses.js')(Client);
|
||||
require('../scopes/client/filter.js')(Client);
|
||||
require('../scopes/client/before-save.js')(Client);
|
||||
|
||||
// Validations
|
||||
|
||||
|
@ -58,41 +60,4 @@ module.exports = function(Client) {
|
|||
});
|
||||
}
|
||||
|
||||
// Hooks
|
||||
|
||||
Client.observe('before save', function(ctx, next) {
|
||||
if (ctx.instance) {
|
||||
if (!ctx.instance.dueDay)
|
||||
ctx.instance.dueDay = 5;
|
||||
next();
|
||||
} else {
|
||||
Client.findById(ctx.where.id, function(err, instance) {
|
||||
if (instance
|
||||
&& instance.payMethodFk != ctx.data.payMethodFk
|
||||
&& instance.dueDay == ctx.data.dueDay)
|
||||
ctx.data.dueDay = 5;
|
||||
next();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Basic filter
|
||||
|
||||
Client.installMethod('filter', filterClients);
|
||||
function filterClients(p) {
|
||||
return {
|
||||
where: {
|
||||
id: p.id,
|
||||
name: {regexp: p.name},
|
||||
cif: p.cif,
|
||||
socialName: {regexp: p.socialName},
|
||||
city: {regexp: p.city},
|
||||
postcode: p.postcode,
|
||||
email: {regexp: p.email},
|
||||
phone: p.phone
|
||||
},
|
||||
skip: (p.page - 1) * p.size,
|
||||
limit: p.size
|
||||
};
|
||||
}
|
||||
};
|
|
@ -54,13 +54,41 @@ module.exports = function(self) {
|
|||
}
|
||||
});
|
||||
|
||||
this.filter = (params, cb) => {
|
||||
let filter = removeEmpty(filterCb(params));
|
||||
this.find(filter, function(err, instances) {
|
||||
if(!err)
|
||||
cb(null, instances);
|
||||
})
|
||||
};
|
||||
this[methodName] = (params, cb) => {
|
||||
let filter = removeEmpty(filterCb(params));
|
||||
var response = {}
|
||||
|
||||
function returnValues(){
|
||||
if(response.instances !== undefined && response.count !== undefined)
|
||||
cb(null, response);
|
||||
}
|
||||
|
||||
function error(){
|
||||
cb(null, response);
|
||||
}
|
||||
|
||||
this.find(filter, function(err, instances) {
|
||||
if(!err){
|
||||
response.instances = instances;
|
||||
returnValues();
|
||||
}
|
||||
else{
|
||||
error();
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
this.count(filter.where, function(err, totalCount){
|
||||
if(!err){
|
||||
response.count = totalCount;
|
||||
returnValues();
|
||||
}
|
||||
else{
|
||||
error();
|
||||
}
|
||||
|
||||
})
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
module.exports = function(Client){
|
||||
Client.observe('before save', function(ctx, next) {
|
||||
if (ctx.instance) {
|
||||
if (!ctx.instance.dueDay)
|
||||
ctx.instance.dueDay = 5;
|
||||
next();
|
||||
} else {
|
||||
Client.findById(ctx.where.id, function(err, instance) {
|
||||
if (instance
|
||||
&& instance.payMethodFk != ctx.data.payMethodFk
|
||||
&& instance.dueDay == ctx.data.dueDay)
|
||||
ctx.data.dueDay = 5;
|
||||
next();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
module.exports = function(Client){
|
||||
Client.installMethod('filter', filterClients);
|
||||
function filterClients(p) {
|
||||
if(p.search && p.search !== "")
|
||||
return searchWhere(p);
|
||||
return andWhere(p);
|
||||
}
|
||||
|
||||
function searchWhere(p){
|
||||
return {
|
||||
where: {
|
||||
or:[
|
||||
{id: p.search,},
|
||||
{name: {regexp: p.search}}
|
||||
]
|
||||
|
||||
},
|
||||
skip: (p.page - 1) * p.size,
|
||||
limit: p.size
|
||||
}
|
||||
}
|
||||
|
||||
function andWhere(p){
|
||||
return {
|
||||
where: {
|
||||
id: p.id,
|
||||
name: {regexp: p.name},
|
||||
cif: p.cif,
|
||||
socialName: {regexp: p.socialName},
|
||||
city: {regexp: p.city},
|
||||
postcode: p.postcode,
|
||||
email: {regexp: p.email},
|
||||
phone: p.phone
|
||||
},
|
||||
skip: (p.page - 1) * p.size,
|
||||
limit: p.size
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue