mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
feat: seed customers and vendors views.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Model } from 'objection';
|
||||
import TenantModel from 'models/TenantModel';
|
||||
import PaginationQueryBuilder from './Pagination';
|
||||
import QueryParser from 'lib/LogicEvaluation/QueryParser';
|
||||
|
||||
class CustomerQueryBuilder extends PaginationQueryBuilder {
|
||||
constructor(...args) {
|
||||
@@ -50,6 +51,45 @@ export default class Customer extends TenantModel {
|
||||
return this.openingBalance + this.balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
/**
|
||||
* Filters the active customers.
|
||||
*/
|
||||
active(query) {
|
||||
query.where('active', 1);
|
||||
},
|
||||
/**
|
||||
* Filters the inactive customers.
|
||||
*/
|
||||
inactive(query) {
|
||||
query.where('active', 0);
|
||||
},
|
||||
/**
|
||||
* Filters the customers that have overdue invoices.
|
||||
*/
|
||||
overdue(query) {
|
||||
query.select(
|
||||
'*',
|
||||
Customer
|
||||
.relatedQuery('overDueInvoices', query.knex())
|
||||
.count()
|
||||
.as('countOverdue'),
|
||||
);
|
||||
query.having('countOverdue', '>', 0);
|
||||
},
|
||||
/**
|
||||
* Filters the unpaid customers.
|
||||
*/
|
||||
unpaid(query) {
|
||||
query.whereRaw('`BALANCE` + `OPENING_BALANCE` <> 0');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
@@ -65,11 +105,48 @@ export default class Customer extends TenantModel {
|
||||
to: 'sales_invoices.customerId',
|
||||
},
|
||||
},
|
||||
|
||||
overDueInvoices: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: SaleInvoice.default,
|
||||
join: {
|
||||
from: 'contacts.id',
|
||||
to: 'sales_invoices.customerId',
|
||||
},
|
||||
filter: (query) => {
|
||||
query.modify('overdue');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static get fields() {
|
||||
return {
|
||||
status: {
|
||||
label: 'Status',
|
||||
options: [
|
||||
{ key: 'active', label: 'Active' },
|
||||
{ key: 'inactive', label: 'Inactive' },
|
||||
{ key: 'overdue', label: 'Overdue' },
|
||||
{ key: 'unpaid', label: 'Unpaid' },
|
||||
],
|
||||
query: (query, role) => {
|
||||
switch(role.value) {
|
||||
case 'active':
|
||||
query.modify('active');
|
||||
break;
|
||||
case 'inactive':
|
||||
query.modify('inactive');
|
||||
break;
|
||||
case 'overdue':
|
||||
query.modify('overdue');
|
||||
break;
|
||||
case 'unpaid':
|
||||
query.modify('unpaid');
|
||||
break;
|
||||
}
|
||||
},
|
||||
},
|
||||
created_at: {
|
||||
column: 'created_at',
|
||||
}
|
||||
|
||||
@@ -50,6 +50,45 @@ export default class Vendor extends TenantModel {
|
||||
return this.openingBalance + this.balance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
/**
|
||||
* Filters the active customers.
|
||||
*/
|
||||
active(query) {
|
||||
query.where('active', 1);
|
||||
},
|
||||
/**
|
||||
* Filters the inactive customers.
|
||||
*/
|
||||
inactive(query) {
|
||||
query.where('active', 0);
|
||||
},
|
||||
/**
|
||||
* Filters the vendors that have overdue invoices.
|
||||
*/
|
||||
overdue(query) {
|
||||
query.select(
|
||||
'*',
|
||||
Vendor
|
||||
.relatedQuery('overdueBills', query.knex())
|
||||
.count()
|
||||
.as('countOverdue'),
|
||||
);
|
||||
query.having('countOverdue', '>', 0);
|
||||
},
|
||||
/**
|
||||
* Filters the unpaid customers.
|
||||
*/
|
||||
unpaid(query) {
|
||||
query.whereRaw('`BALANCE` + `OPENING_BALANCE` <> 0');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
@@ -64,12 +103,50 @@ export default class Vendor extends TenantModel {
|
||||
from: 'contacts.id',
|
||||
to: 'bills.vendorId',
|
||||
},
|
||||
},
|
||||
overdueBills: {
|
||||
relation: Model.HasManyRelation,
|
||||
modelClass: Bill.default,
|
||||
join: {
|
||||
from: 'contacts.id',
|
||||
to: 'bills.vendorId',
|
||||
},
|
||||
filter: (query) => {
|
||||
query.modify('overdue');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static get fields() {
|
||||
return {
|
||||
status: {
|
||||
label: 'Status',
|
||||
options: [
|
||||
{ key: 'active', label: 'Active' },
|
||||
{ key: 'inactive', label: 'Inactive' },
|
||||
{ key: 'overdue', label: 'Overdue' },
|
||||
{ key: 'unpaid', label: 'Unpaid' },
|
||||
],
|
||||
query: (query, role) => {
|
||||
console.log(role);
|
||||
|
||||
switch(role.value) {
|
||||
case 'active':
|
||||
query.modify('active');
|
||||
break;
|
||||
case 'inactive':
|
||||
query.modify('inactive');
|
||||
break;
|
||||
case 'overdue':
|
||||
query.modify('overdue');
|
||||
break;
|
||||
case 'unpaid':
|
||||
query.modify('unpaid');
|
||||
break;
|
||||
}
|
||||
},
|
||||
},
|
||||
created_at: {
|
||||
column: 'created_at',
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user