feat: closed sale receipt status.

feat: approve and reject sale estimate.
feat: initial receipts, invoices, estimates and bills views.
This commit is contained in:
a.bouhuolia
2020-12-14 20:25:38 +02:00
parent 461e18f2a4
commit 27483495cb
23 changed files with 801 additions and 86 deletions

View File

@@ -2,6 +2,7 @@ import { Model, raw } from 'objection';
import moment from 'moment';
import { difference } from 'lodash';
import TenantModel from 'models/TenantModel';
import { query } from 'winston';
export default class Bill extends TenantModel {
/**
@@ -17,9 +18,49 @@ export default class Bill extends TenantModel {
static get modifiers() {
return {
/**
* Filters the bills in draft status.
*/
draft(query) {
query.where('opened_at', null);
},
/**
* Filters the opened bills.
*/
opened(query) {
query.whereNot('opened_at', null);
},
/**
* Filters the unpaid bills.
*/
unpaid(query) {
query.where('payment_amount', 0);
},
/**
* Filters the due bills.
*/
dueBills(query) {
query.where(raw('AMOUNT - PAYMENT_AMOUNT > 0'));
}
},
/**
* Filters the overdue bills.
*/
overdue(query) {
query.where('due_date', '<', moment().format('YYYY-MM-DD'));
},
/**
* Filters the partially paid bills.
*/
partiallyPaid(query) {
query.whereNot('payment_amount', 0);
query.whereNot(raw('`PAYMENT_AMOUNT` = `AMOUNT`'));
},
/**
* Filters the paid bills.
*/
paid(query) {
query.where(raw('`PAYMENT_AMOUNT` = `AMOUNT`'));
},
}
}
@@ -34,7 +75,16 @@ export default class Bill extends TenantModel {
* Virtual attributes.
*/
static get virtualAttributes() {
return ['dueAmount', 'isOpen', 'isPartiallyPaid', 'isFullyPaid', 'isPaid', 'remainingDays', 'overdueDays', 'isOverdue'];
return [
'dueAmount',
'isOpen',
'isPartiallyPaid',
'isFullyPaid',
'isPaid',
'remainingDays',
'overdueDays',
'isOverdue',
];
}
/**
@@ -181,11 +231,6 @@ export default class Bill extends TenantModel {
static get fields() {
return {
created_at: {
label: 'Created at',
column: 'created_at',
columnType: 'date',
},
vendor: {
label: 'Vendor',
column: 'vendor_id',
@@ -216,7 +261,23 @@ export default class Bill extends TenantModel {
},
status: {
label: 'Status',
column: 'status',
options: [],
query: (query, role) => {
switch(role.value) {
case 'draft':
query.modify('draft'); break;
case 'opened':
query.modify('opened'); break;
case 'unpaid':
query.modify('unpaid'); break;
case 'overdue':
query.modify('overdue'); break;
case 'partially-paid':
query.modify('partiallyPaid'); break;
case 'paid':
query.modify('paid'); break;
}
},
},
amount: {
label: 'Amount',
@@ -234,6 +295,14 @@ export default class Bill extends TenantModel {
label: 'Note',
column: 'note',
},
user: {
},
created_at: {
label: 'Created at',
column: 'created_at',
columnType: 'date',
},
}
}
}

View File

@@ -41,7 +41,6 @@ export default class PaymentReceive extends TenantModel {
query.where('contact_service', 'customer');
}
},
depositAccount: {
relation: Model.BelongsToOneRelation,
modelClass: Account.default,
@@ -50,7 +49,6 @@ export default class PaymentReceive extends TenantModel {
to: 'accounts.id',
},
},
entries: {
relation: Model.HasManyRelation,
modelClass: PaymentReceiveEntry.default,
@@ -59,7 +57,6 @@ export default class PaymentReceive extends TenantModel {
to: 'payment_receives_entries.paymentReceiveId',
},
},
transactions: {
relation: Model.HasManyRelation,
modelClass: AccountTransaction.default,
@@ -79,11 +76,58 @@ export default class PaymentReceive extends TenantModel {
*/
static get fields() {
return {
customer: {
label: 'Customer',
column: 'customer_id',
fieldType: 'options',
optionsResource: 'customers',
optionsKey: 'id',
optionsLable: 'displayName',
},
payment_date: {
label: 'Payment date',
column: 'payment_date',
columnType: 'date',
fieldType: 'date',
},
amount: {
label: 'Amount',
column: 'amount',
columnType: 'number',
fieldType: 'number',
},
reference_no: {
label: 'Reference No.',
column: 'reference_no',
columnType: 'string',
fieldType: 'text',
},
deposit_acount: {
column: 'deposit_account_id',
lable: 'Deposit account',
relation: "accounts.id",
optionsResource: "account",
},
payment_receive_no: {
label: 'Payment receive No.',
column: 'payment_receive_no',
columnType: 'string',
fieldType: 'text',
},
description: {
label: 'description',
column: 'description',
columnType: 'string',
fieldType: 'text',
},
created_at: {
label: 'Created at',
column: 'created_at',
columnType: 'date',
},
user: {
},
};
}
}

View File

@@ -2,6 +2,8 @@ import moment from 'moment';
import { Model } from 'objection';
import TenantModel from 'models/TenantModel';
import { defaultToTransform } from 'utils';
import HasItemEntries from 'services/Sales/HasItemsEntries';
import { query } from 'winston';
export default class SaleEstimate extends TenantModel {
/**
@@ -22,7 +24,13 @@ export default class SaleEstimate extends TenantModel {
* Virtual attributes.
*/
static get virtualAttributes() {
return ['isDelivered', 'isExpired', 'isConvertedToInvoice'];
return [
'isDelivered',
'isExpired',
'isConvertedToInvoice',
'isApproved',
'isRejected'
];
}
/**
@@ -53,6 +61,22 @@ export default class SaleEstimate extends TenantModel {
);
}
/**
* Detarmines whether the estimate is approved.
* @return {boolean}
*/
get isApproved() {
return !!this.approvedAt;
}
/**
* Detarmines whether the estimate is reject.
* @return {boolean}
*/
get isRejected() {
return !!this.rejectedAt;
}
/**
* Allows to mark model as resourceable to viewable and filterable.
*/
@@ -60,6 +84,50 @@ export default class SaleEstimate extends TenantModel {
return true;
}
/**
* Model modifiers.
*/
static get modifiers() {
return {
/**
* Filters the drafted estimates transactions.
*/
draft(query) {
query.where('delivered_at', null);
},
/**
* Filters the delivered estimates transactions.
*/
delivered(query) {
query.whereNot('delivered_at', null);
},
/**
* Filters the expired estimates transactions.
*/
expired(query) {
query.where('expiration_date', '<', moment().format('YYYY-MM-DD'));
},
/**
* Filters the rejected estimates transactions.
*/
rejected(query) {
query.whereNot('rejected_at', null);
},
/**
* Filters the invoiced estimates transactions.
*/
invoiced(query) {
query.whereNot('converted_to_invoice_at', null);
},
/**
* Filters the approved estimates transactions.
*/
approved(query) {
query.whereNot('approved_at', null)
},
};
}
/**
* Relationship mapping.
*/
@@ -79,7 +147,6 @@ export default class SaleEstimate extends TenantModel {
query.where('contact_service', 'customer');
}
},
entries: {
relation: Model.HasManyRelation,
modelClass: ItemEntry.default,
@@ -99,6 +166,64 @@ export default class SaleEstimate extends TenantModel {
*/
static get fields() {
return {
amount: {
label: 'Amount',
column: 'amount',
columnType: 'number',
fieldType: 'number',
},
customer: {
label: 'Customer',
column: 'customer_id',
fieldType: 'options',
optionsResource: 'customers',
optionsKey: 'id',
optionsLable: 'displayName',
},
estimate_date: {
label: 'Estimate date',
column: 'estimate_date',
columnType: 'date',
fieldType: 'date',
},
expiration_date: {
label: 'Expiration date',
column: 'expiration_date',
columnType: 'date',
fieldType: 'date',
},
note: {
label: 'Note',
column: 'note',
columnType: 'text',
fieldType: 'text',
},
terms_conditions: {
label: 'Terms & conditions',
column: 'terms_conditions',
columnType: 'text',
fieldType: 'text',
},
status: {
label: 'Status',
query: (query, role) => {
switch(role.value) {
case 'draft':
query.modify('draft'); break;
case 'delivered':
query.modify('delivered'); break;
case 'approved':
query.modify('approved'); break;
case 'rejected':
query.modify('rejected'); break;
case 'invoiced':
query.modify('invoiced');
break;
case 'expired':
query.modify('expired'); break;
}
},
},
created_at: {
label: 'Created at',
column: 'created_at',

View File

@@ -2,6 +2,7 @@ import { Model, raw } from 'objection';
import moment from 'moment';
import TenantModel from 'models/TenantModel';
import { defaultToTransform } from 'utils';
import { QueryBuilder } from 'knex';
export default class SaleInvoice extends TenantModel {
/**
@@ -29,7 +30,16 @@ export default class SaleInvoice extends TenantModel {
* Virtual attributes.
*/
static get virtualAttributes() {
return ['dueAmount', 'isDelivered', 'isOverdue', 'isPartiallyPaid', 'isFullyPaid', 'isPaid', 'remainingDays', 'overdueDays'];
return [
'dueAmount',
'isDelivered',
'isOverdue',
'isPartiallyPaid',
'isFullyPaid',
'isPaid',
'remainingDays',
'overdueDays',
];
}
/**
@@ -118,10 +128,15 @@ export default class SaleInvoice extends TenantModel {
*/
static get modifiers() {
return {
/**
* Filters the due invoices.
*/
dueInvoices(query) {
query.where(raw('BALANCE - PAYMENT_AMOUNT > 0'));
},
/**
* Filters the invoices between the given date range.
*/
filterDateRange(query, startDate, endDate, type = 'day') {
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const fromDate = moment(startDate).startOf(type).format(dateFormat);
@@ -134,16 +149,46 @@ export default class SaleInvoice extends TenantModel {
query.where('invoice_date', '<=', toDate);
}
},
/**
* Filters the invoices in draft status.
*/
draft(query) {
query.where('delivered_at', null);
},
/**
* Filters the delivered invoices.
*/
delivered(query) {
query.whereNot('delivered_at', null);
},
/**
* Filters the unpaid invoices.
*/
unpaid(query) {
query.where(raw('PAYMENT_AMOUNT = 0'));
},
/**
* Filters the overdue invoices.
*/
overdue(query) {
query.where('due_date', '<', moment().format('YYYY-MM-DD'));
},
/**
* Filters the partially invoices.
*/
partiallyPaid(query) {
query.whereNot('payment_amount', 0);
query.whereNot(raw('`PAYMENT_AMOUNT` = `BALANCE`'));
},
/**
* Filters the paid invoices.
*/
paid(query) {
query.where(raw('PAYMENT_AMOUNT = BALANCE'));
}
};
}
/**
* Due amount of the given.
*/
get dueAmount() {
return Math.max(this.balance - this.paymentAmount, 0);
}
/**
* Relationship mapping.
*/
@@ -232,11 +277,106 @@ export default class SaleInvoice extends TenantModel {
*/
static get fields() {
return {
customer: {
label: 'Customer',
column: 'customer_id',
fieldType: 'options',
optionsResource: 'customers',
optionsKey: 'id',
optionsLable: 'displayName',
},
invoice_date: {
label: 'Invoice date',
column: 'invoice_date',
columnType: 'date',
fieldType: 'date',
},
due_date: {
label: 'Due date',
column: 'due_date',
columnType: 'date',
fieldType: 'date',
},
invoice_no: {
label: 'Invoice No.',
column: 'invoice_no',
columnType: 'number',
fieldType: 'number',
},
refernece_no: {
label: 'Reference No.',
column: 'reference_no',
columnType: 'number',
fieldType: 'number',
},
invoice_message: {
label: 'Invoice message',
column: 'invoice_message',
columnType: 'text',
fieldType: 'text',
},
terms_conditions: {
label: 'Terms & conditions',
column: 'terms_conditions',
columnType: 'text',
fieldType: 'text',
},
invoice_amount: {
label: 'Invoice amount',
column: 'invoice_amount',
columnType: 'number',
fieldType: 'number',
},
payment_amount: {
label: 'Payment amount',
column: 'payment_amount',
columnType: 'number',
fieldType: 'number',
},
due_amount: {
label: 'Due amount',
column: 'due_amount',
columnType: 'number',
fieldType: 'number',
},
created_at: {
label: 'Created at',
column: 'created_at',
columnType: 'date',
},
status: {
label: 'Status',
options: [
{ key: 'draft', label: 'Draft', },
{ key: 'delivered', label: 'Delivered' },
{ key: 'unpaid', label: 'Unpaid' },
{ key: 'overdue', label: 'Overdue' },
{ key: 'partially-paid', label: 'Partially paid' },
{ key: 'paid', label: 'Paid' },
],
query: (query, role) => {
switch(role.value) {
case 'draft':
query.modify('draft');
break;
case 'delivered':
query.modify('delivered');
break;
case 'unpaid':
query.modify('unpaid');
break;
case 'overdue':
query.modify('overdue');
break;
case 'partially-paid':
query.modify('partiallyPaid');
break;
case 'paid':
query.modify('paid');
break;
}
},
}
};
}
}

View File

@@ -16,6 +16,32 @@ export default class SaleReceipt extends TenantModel {
return ['created_at', 'updated_at'];
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return [
'isClosed',
'isDraft',
];
}
/**
* Detarmine whether the sale receipt closed.
* @return {boolean}
*/
get isClosed() {
return !!this.closedAt;
}
/**
* Detarmines whether the sale receipt drafted.
* @return {boolean}
*/
get isDraft() {
return !this.closedAt;
}
/**
* Relationship mapping.
*/