feat: sorting columns.

This commit is contained in:
elforjani3
2021-03-03 20:07:33 +02:00
parent 31f864bc4e
commit 5f92196317
15 changed files with 305 additions and 269 deletions

View File

@@ -44,17 +44,17 @@ export const useManualJournalsColumns = () => {
className: 'journal_type', className: 'journal_type',
}, },
{ {
id: 'publish', id: 'status',
Header: formatMessage({ id: 'publish' }), Header: formatMessage({ id: 'publish' }),
accessor: (row) => StatusAccessor(row), accessor: (row) => StatusAccessor(row),
width: 95, width: 95,
className: 'publish', className: 'status',
}, },
{ {
id: 'note', id: 'note',
Header: formatMessage({ id: 'note' }), Header: formatMessage({ id: 'note' }),
accessor: NoteAccessor, accessor: NoteAccessor,
disableSorting: true, disableSortBy: true,
width: 85, width: 85,
className: 'note', className: 'note',
}, },

View File

@@ -139,28 +139,28 @@ export function useExpensesTableColumns() {
className: 'payment_date', className: 'payment_date',
}, },
{ {
id: 'total_amount', id: 'amount',
Header: formatMessage({ id: 'full_amount' }), Header: formatMessage({ id: 'full_amount' }),
accessor: TotalAmountAccessor, accessor: TotalAmountAccessor,
className: 'total_amount', className: 'amount',
width: 150, width: 150,
}, },
{ {
id: 'payment_account_id', id: 'payment_account',
Header: formatMessage({ id: 'payment_account' }), Header: formatMessage({ id: 'payment_account' }),
accessor: 'payment_account.name', accessor: 'payment_account.name',
className: 'payment_account', className: 'payment_account',
width: 150, width: 150,
}, },
{ {
id: 'expense_account_id', id: 'expense_account',
Header: formatMessage({ id: 'expense_account' }), Header: formatMessage({ id: 'expense_account' }),
accessor: ExpenseAccountAccessor, accessor: ExpenseAccountAccessor,
width: 160, width: 160,
className: 'expense_account', className: 'expense_account',
}, },
{ {
id: 'publish', id: 'published',
Header: formatMessage({ id: 'publish' }), Header: formatMessage({ id: 'publish' }),
accessor: PublishAccessor, accessor: PublishAccessor,
width: 100, width: 100,

View File

@@ -158,7 +158,7 @@ export const useInventoryAdjustmentsColumns = () => {
width: 100, width: 100,
}, },
{ {
id: 'publish', id: 'published_at',
Header: formatMessage({ id: 'status' }), Header: formatMessage({ id: 'status' }),
accessor: PublishAccessor, accessor: PublishAccessor,
width: 95, width: 95,

View File

@@ -124,7 +124,7 @@ export function ItemsActionMenuList({
/> />
</Menu> </Menu>
); );
}; }
export const ItemsActionsTableCell = (props) => { export const ItemsActionsTableCell = (props) => {
return ( return (
@@ -137,7 +137,6 @@ export const ItemsActionsTableCell = (props) => {
); );
}; };
/** /**
* Retrieve all items table columns. * Retrieve all items table columns.
*/ */
@@ -147,30 +146,35 @@ export const useItemsTableColumns = () => {
return React.useMemo( return React.useMemo(
() => [ () => [
{ {
id: 'name',
Header: formatMessage({ id: 'item_name' }), Header: formatMessage({ id: 'item_name' }),
accessor: 'name', accessor: 'name',
className: 'name', className: 'name',
width: 180, width: 180,
}, },
{ {
id:'code',
Header: formatMessage({ id: 'item_code' }), Header: formatMessage({ id: 'item_code' }),
accessor: 'code', accessor: 'code',
className: 'code', className: 'code',
width: 120, width: 120,
}, },
{ {
id: 'type',
Header: formatMessage({ id: 'item_type' }), Header: formatMessage({ id: 'item_type' }),
accessor: ItemTypeAccessor, accessor: ItemTypeAccessor,
className: 'item_type', className: 'item_type',
width: 120, width: 120,
}, },
{ {
id: 'category',
Header: formatMessage({ id: 'category' }), Header: formatMessage({ id: 'category' }),
accessor: 'category.name', accessor: 'category.name',
className: 'category', className: 'category',
width: 150, width: 150,
}, },
{ {
id:'sell_price',
Header: formatMessage({ id: 'sell_price' }), Header: formatMessage({ id: 'sell_price' }),
Cell: SellPriceCell, Cell: SellPriceCell,
accessor: 'sell_price', accessor: 'sell_price',
@@ -178,6 +182,7 @@ export const useItemsTableColumns = () => {
width: 150, width: 150,
}, },
{ {
id:'cost_price',
Header: formatMessage({ id: 'cost_price' }), Header: formatMessage({ id: 'cost_price' }),
Cell: CostPriceCell, Cell: CostPriceCell,
accessor: 'cost_price', accessor: 'cost_price',
@@ -185,6 +190,7 @@ export const useItemsTableColumns = () => {
width: 150, width: 150,
}, },
{ {
id:'quantity_on_hand',
Header: formatMessage({ id: 'quantity_on_hand' }), Header: formatMessage({ id: 'quantity_on_hand' }),
accessor: 'quantity_on_hand', accessor: 'quantity_on_hand',
Cell: QuantityOnHandCell, Cell: QuantityOnHandCell,
@@ -199,4 +205,4 @@ export const useItemsTableColumns = () => {
], ],
[formatMessage], [formatMessage],
); );
} };

View File

@@ -112,11 +112,11 @@ export function useBillsTableColumns() {
className: 'bill_date', className: 'bill_date',
}, },
{ {
id: 'vendor_id', id: 'vendor',
Header: formatMessage({ id: 'vendor_name' }), Header: formatMessage({ id: 'vendor_name' }),
accessor: 'vendor.display_name', accessor: 'vendor.display_name',
width: 140, width: 140,
className: 'vendor_id', className: 'vendor',
}, },
{ {
id: 'bill_number', id: 'bill_number',

View File

@@ -83,7 +83,7 @@ export function usePaymentMadesTableColumns() {
className: 'payment_date', className: 'payment_date',
}, },
{ {
id: 'vendor_id', id: 'vendor',
Header: formatMessage({ id: 'vendor_name' }), Header: formatMessage({ id: 'vendor_name' }),
accessor: 'vendor.display_name', accessor: 'vendor.display_name',
width: 140, width: 140,
@@ -98,7 +98,7 @@ export function usePaymentMadesTableColumns() {
className: 'payment_number', className: 'payment_number',
}, },
{ {
id: 'payment_account_id', id: 'payment_account',
Header: formatMessage({ id: 'payment_account' }), Header: formatMessage({ id: 'payment_account' }),
accessor: 'payment_account.name', accessor: 'payment_account.name',
width: 140, width: 140,
@@ -112,7 +112,7 @@ export function usePaymentMadesTableColumns() {
className: 'amount', className: 'amount',
}, },
{ {
id: 'reference', id: 'reference_no',
Header: formatMessage({ id: 'reference' }), Header: formatMessage({ id: 'reference' }),
accessor: 'reference', accessor: 'reference',
width: 140, width: 140,

View File

@@ -153,7 +153,7 @@ export function useEstiamtesTableColumns() {
className: 'estimate_date', className: 'estimate_date',
}, },
{ {
id: 'customer_id', id: 'customer',
Header: formatMessage({ id: 'customer_name' }), Header: formatMessage({ id: 'customer_name' }),
accessor: 'customer.display_name', accessor: 'customer.display_name',
width: 140, width: 140,
@@ -190,11 +190,11 @@ export function useEstiamtesTableColumns() {
className: 'status', className: 'status',
}, },
{ {
id: 'reference', id: 'reference_no',
Header: formatMessage({ id: 'reference_no' }), Header: formatMessage({ id: 'reference_no' }),
accessor: 'reference', accessor: 'reference_no',
width: 140, width: 90,
className: 'reference', className: 'reference_no',
}, },
{ {
id: 'actions', id: 'actions',

View File

@@ -159,7 +159,7 @@ export function useInvoicesTableColumns() {
className: 'invoice_date', className: 'invoice_date',
}, },
{ {
id: 'customer_id', id: 'customer',
Header: formatMessage({ id: 'customer_name' }), Header: formatMessage({ id: 'customer_name' }),
accessor: 'customer.display_name', accessor: 'customer.display_name',
width: 180, width: 180,

View File

@@ -93,7 +93,7 @@ export function usePaymentReceivesColumns() {
className: 'payment_date', className: 'payment_date',
}, },
{ {
id: 'customer_id', id: 'customer',
Header: formatMessage({ id: 'customer_name' }), Header: formatMessage({ id: 'customer_name' }),
accessor: 'customer.display_name', accessor: 'customer.display_name',
width: 160, width: 160,
@@ -122,7 +122,7 @@ export function usePaymentReceivesColumns() {
className: 'reference_no', className: 'reference_no',
}, },
{ {
id: 'deposit_account_id', id: 'deposit_account',
Header: formatMessage({ id: 'deposit_account' }), Header: formatMessage({ id: 'deposit_account' }),
accessor: 'deposit_account.name', accessor: 'deposit_account.name',
width: 140, width: 140,

View File

@@ -104,7 +104,7 @@ export function useReceiptsTableColumns() {
className: 'receipt_date', className: 'receipt_date',
}, },
{ {
id: 'customer_id', id: 'customer',
Header: formatMessage({ id: 'customer_name' }), Header: formatMessage({ id: 'customer_name' }),
accessor: 'customer.display_name', accessor: 'customer.display_name',
width: 140, width: 140,
@@ -119,7 +119,7 @@ export function useReceiptsTableColumns() {
className: 'receipt_number', className: 'receipt_number',
}, },
{ {
id: 'deposit_account_id', id: 'deposit_account',
Header: formatMessage({ id: 'deposit_account' }), Header: formatMessage({ id: 'deposit_account' }),
accessor: 'deposit_account.name', accessor: 'deposit_account.name',
width: 140, width: 140,

View File

@@ -1,19 +1,19 @@
import { Model } from 'objection'; import { Model } from "objection";
import TenantModel from 'models/TenantModel'; import TenantModel from "models/TenantModel";
export default class BillPayment extends TenantModel { export default class BillPayment extends TenantModel {
/** /**
* Table name * Table name
*/ */
static get tableName() { static get tableName() {
return 'bills_payments'; return "bills_payments";
} }
/** /**
* Timestamps columns. * Timestamps columns.
*/ */
get timestamps() { get timestamps() {
return ['createdAt', 'updatedAt']; return ["createdAt", "updatedAt"];
} }
static get resourceable() { static get resourceable() {
@@ -24,18 +24,18 @@ export default class BillPayment extends TenantModel {
* Relationship mapping. * Relationship mapping.
*/ */
static get relationMappings() { static get relationMappings() {
const BillPaymentEntry = require('models/BillPaymentEntry'); const BillPaymentEntry = require("models/BillPaymentEntry");
const AccountTransaction = require('models/AccountTransaction'); const AccountTransaction = require("models/AccountTransaction");
const Contact = require('models/Contact'); const Contact = require("models/Contact");
const Account = require('models/Account'); const Account = require("models/Account");
return { return {
entries: { entries: {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: BillPaymentEntry.default, modelClass: BillPaymentEntry.default,
join: { join: {
from: 'bills_payments.id', from: "bills_payments.id",
to: 'bills_payments_entries.billPaymentId', to: "bills_payments_entries.billPaymentId",
}, },
}, },
@@ -43,20 +43,20 @@ export default class BillPayment extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Contact.default, modelClass: Contact.default,
join: { join: {
from: 'bills_payments.vendorId', from: "bills_payments.vendorId",
to: 'contacts.id', to: "contacts.id",
}, },
filter(query) { filter(query) {
query.where('contact_service', 'vendor'); query.where("contact_service", "vendor");
} },
}, },
paymentAccount: { paymentAccount: {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Account.default, modelClass: Account.default,
join: { join: {
from: 'bills_payments.paymentAccountId', from: "bills_payments.paymentAccountId",
to: 'accounts.id', to: "accounts.id",
}, },
}, },
@@ -64,11 +64,11 @@ export default class BillPayment extends TenantModel {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: AccountTransaction.default, modelClass: AccountTransaction.default,
join: { join: {
from: 'bills_payments.id', from: "bills_payments.id",
to: 'accounts_transactions.referenceId' to: "accounts_transactions.referenceId",
}, },
filter(builder) { filter(builder) {
builder.where('reference_type', 'BillPayment'); builder.where("reference_type", "BillPayment");
}, },
}, },
}; };
@@ -80,16 +80,22 @@ export default class BillPayment extends TenantModel {
static get fields() { static get fields() {
return { return {
vendor: { vendor: {
lable: "Vendor name", label: "Vendor name",
column: 'vendor_id', column: "vendor_id",
relation: 'contacts.id', relation: "contacts.id",
relationColumn: 'contacts.display_name', relationColumn: "contacts.display_name",
}, },
amount: { amount: {
label: "Amount", label: "Amount",
column: "amount", column: "amount",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
},
due_amount: {
label: "Due amount",
column: "due_amount",
columnType: "number",
fieldType: "number",
}, },
payment_account: { payment_account: {
label: "Payment account", label: "Payment account",
@@ -97,40 +103,40 @@ export default class BillPayment extends TenantModel {
relation: "accounts.id", relation: "accounts.id",
relationColumn: "accounts.name", relationColumn: "accounts.name",
fieldType: 'options', fieldType: "options",
optionsResource: 'Account', optionsResource: "Account",
optionsKey: 'id', optionsKey: "id",
optionsLabel: 'name', optionsLabel: "name",
}, },
payment_number: { payment_number: {
label: "Payment number", label: "Payment number",
column: "payment_number", column: "payment_number",
columnType: 'string', columnType: "string",
fieldType: 'text', fieldType: "text",
}, },
payment_date: { payment_date: {
label: "Payment date", label: "Payment date",
column: "payment_date", column: "payment_date",
columnType: 'date', columnType: "date",
fieldType: 'date', fieldType: "date",
}, },
reference_no: { reference_no: {
label: "Reference No.", label: "Reference No.",
column: "reference", column: "reference",
columnType: 'string', columnType: "string",
fieldType: 'text', fieldType: "text",
}, },
description: { description: {
label: "Description", label: "Description",
column: "description", column: "description",
columnType: 'string', columnType: "string",
fieldType: 'text', fieldType: "text",
}, },
created_at: { created_at: {
label: 'Created at', label: "Created at",
column: 'created_at', column: "created_at",
columnType: 'date', columnType: "date",
}, },
} };
} }
} }

View File

@@ -1,22 +1,20 @@
import { Model } from 'objection'; import { Model } from "objection";
import TenantModel from 'models/TenantModel'; import TenantModel from "models/TenantModel";
import { import { buildFilterQuery } from "lib/ViewRolesBuilder";
buildFilterQuery,
} from 'lib/ViewRolesBuilder';
export default class Item extends TenantModel { export default class Item extends TenantModel {
/** /**
* Table name * Table name
*/ */
static get tableName() { static get tableName() {
return 'items'; return "items";
} }
/** /**
* Model timestamps. * Model timestamps.
*/ */
get timestamps() { get timestamps() {
return ['createdAt', 'updatedAt']; return ["createdAt", "updatedAt"];
} }
/** /**
@@ -44,9 +42,9 @@ export default class Item extends TenantModel {
* Relationship mapping. * Relationship mapping.
*/ */
static get relationMappings() { static get relationMappings() {
const Media = require('models/Media'); const Media = require("models/Media");
const Account = require('models/Account'); const Account = require("models/Account");
const ItemCategory = require('models/ItemCategory'); const ItemCategory = require("models/ItemCategory");
return { return {
/** /**
@@ -56,8 +54,8 @@ export default class Item extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: ItemCategory.default, modelClass: ItemCategory.default,
join: { join: {
from: 'items.categoryId', from: "items.categoryId",
to: 'items_categories.id', to: "items_categories.id",
}, },
}, },
@@ -65,8 +63,8 @@ export default class Item extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Account.default, modelClass: Account.default,
join: { join: {
from: 'items.costAccountId', from: "items.costAccountId",
to: 'accounts.id', to: "accounts.id",
}, },
}, },
@@ -74,8 +72,8 @@ export default class Item extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Account.default, modelClass: Account.default,
join: { join: {
from: 'items.sellAccountId', from: "items.sellAccountId",
to: 'accounts.id', to: "accounts.id",
}, },
}, },
@@ -83,8 +81,8 @@ export default class Item extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Account.default, modelClass: Account.default,
join: { join: {
from: 'items.inventoryAccountId', from: "items.inventoryAccountId",
to: 'accounts.id', to: "accounts.id",
}, },
}, },
@@ -92,13 +90,13 @@ export default class Item extends TenantModel {
relation: Model.ManyToManyRelation, relation: Model.ManyToManyRelation,
modelClass: Media.default, modelClass: Media.default,
join: { join: {
from: 'items.id', from: "items.id",
through: { through: {
from: 'media_links.model_id', from: "media_links.model_id",
to: 'media_links.media_id', to: "media_links.media_id",
}, },
to: 'media.id', to: "media.id",
} },
}, },
}; };
} }
@@ -109,72 +107,76 @@ export default class Item extends TenantModel {
static get fields() { static get fields() {
return { return {
type: { type: {
label: 'Type', label: "Type",
column: 'type', column: "type",
}, },
name: { name: {
label: 'Name', label: "Name",
column: 'name', column: "name",
},
code: {
label: "Code",
column: "code",
}, },
sellable: { sellable: {
label: 'Sellable', label: "Sellable",
column: 'sellable', column: "sellable",
}, },
purchasable: { purchasable: {
label: 'Purchasable', label: "Purchasable",
column: 'purchasable', column: "purchasable",
}, },
sell_price: { sell_price: {
label: 'Sell price', label: "Sell price",
column: 'sell_price' column: "sell_price",
}, },
cost_price: { cost_price: {
label: 'Cost price', label: "Cost price",
column: 'cost_price', column: "cost_price",
}, },
currency_code: { currency_code: {
label: 'Currency', label: "Currency",
column: 'currency_code', column: "currency_code",
}, },
cost_account: { cost_account: {
label: 'Cost account', label: "Cost account",
column: 'cost_account_id', column: "cost_account_id",
relation: 'accounts.id', relation: "accounts.id",
relationColumn: 'accounts.name', relationColumn: "accounts.name",
}, },
sell_account: { sell_account: {
label: 'Sell account', label: "Sell account",
column: 'sell_account_id', column: "sell_account_id",
relation: 'accounts.id', relation: "accounts.id",
relationColumn: 'accounts.name', relationColumn: "accounts.name",
}, },
inventory_account: { inventory_account: {
label: "Inventory account", label: "Inventory account",
column: 'inventory_account_id', column: "inventory_account_id",
relation: 'accounts.id', relation: "accounts.id",
relationColumn: 'accounts.name', relationColumn: "accounts.name",
}, },
sell_description: { sell_description: {
label: "Sell description", label: "Sell description",
column: 'sell_description', column: "sell_description",
}, },
purchase_description: { purchase_description: {
label: "Purchase description", label: "Purchase description",
column: 'purchase_description', column: "purchase_description",
}, },
quantity_on_hand: { quantity_on_hand: {
label: "Quantity on hand", label: "Quantity on hand",
column: 'quantity_on_hand', column: "quantity_on_hand",
}, },
note: { note: {
label: 'Note', label: "Note",
column: 'note', column: "note",
}, },
category: { category: {
label: "Category", label: "Category",
column: 'category_id', column: "category_id",
relation: 'items_categories.id', relation: "items_categories.id",
relationColumn: 'items_categories.name', relationColumn: "items_categories.name",
}, },
// user: { // user: {
// label: 'User', // label: 'User',
@@ -183,10 +185,10 @@ export default class Item extends TenantModel {
// relationColumn: 'users.', // relationColumn: 'users.',
// }, // },
created_at: { created_at: {
label: 'Created at', label: "Created at",
column: 'created_at', column: "created_at",
columnType: 'date', columnType: "date",
fieldType: 'date', fieldType: "date",
}, },
}; };
} }

View File

@@ -172,6 +172,12 @@ export default class SaleEstimate extends TenantModel {
columnType: 'number', columnType: 'number',
fieldType: 'number', fieldType: 'number',
}, },
estimate_number: {
label: 'Estimate number',
column: 'estimate_number',
columnType: 'text',
fieldType: 'text',
},
customer: { customer: {
label: 'Customer', label: 'Customer',
column: 'customer_id', column: 'customer_id',
@@ -195,6 +201,12 @@ export default class SaleEstimate extends TenantModel {
columnType: 'date', columnType: 'date',
fieldType: 'date', fieldType: 'date',
}, },
reference_no: {
label: "Reference No.",
column: "reference_no",
columnType: "number",
fieldType: "number",
},
note: { note: {
label: 'Note', label: 'Note',
column: 'note', column: 'note',

View File

@@ -1,23 +1,23 @@
import { Model, raw } from 'objection'; import { Model, raw } from "objection";
import moment from 'moment'; import moment from "moment";
import TenantModel from 'models/TenantModel'; import TenantModel from "models/TenantModel";
import { defaultToTransform } from 'utils'; import { defaultToTransform } from "utils";
import { QueryBuilder } from 'knex'; import { QueryBuilder } from "knex";
import { query } from 'winston'; import { query } from "winston";
export default class SaleInvoice extends TenantModel { export default class SaleInvoice extends TenantModel {
/** /**
* Table name * Table name
*/ */
static get tableName() { static get tableName() {
return 'sales_invoices'; return "sales_invoices";
} }
/** /**
* Timestamps columns. * Timestamps columns.
*/ */
get timestamps() { get timestamps() {
return ['created_at', 'updated_at']; return ["created_at", "updated_at"];
} }
/** /**
@@ -25,14 +25,14 @@ export default class SaleInvoice extends TenantModel {
*/ */
static get virtualAttributes() { static get virtualAttributes() {
return [ return [
'dueAmount', "dueAmount",
'isDelivered', "isDelivered",
'isOverdue', "isOverdue",
'isPartiallyPaid', "isPartiallyPaid",
'isFullyPaid', "isFullyPaid",
'isPaid', "isPaid",
'remainingDays', "remainingDays",
'overdueDays', "overdueDays",
]; ];
} }
@@ -91,12 +91,14 @@ export default class SaleInvoice extends TenantModel {
*/ */
get remainingDays() { get remainingDays() {
// Can't continue in case due date not defined. // Can't continue in case due date not defined.
if (!this.dueDate) { return null; } if (!this.dueDate) {
return null;
}
const date = moment(); const date = moment();
const dueDate = moment(this.dueDate); const dueDate = moment(this.dueDate);
return Math.max(dueDate.diff(date, 'days'), 0); return Math.max(dueDate.diff(date, "days"), 0);
} }
/** /**
@@ -115,14 +117,16 @@ export default class SaleInvoice extends TenantModel {
* *
* @param {*} asDate * @param {*} asDate
*/ */
getOverdueDays(asDate = moment().format('YYYY-MM-DD')) { getOverdueDays(asDate = moment().format("YYYY-MM-DD")) {
// Can't continue in case due date not defined. // Can't continue in case due date not defined.
if (!this.dueDate) { return null; } if (!this.dueDate) {
return null;
}
const date = moment(asDate); const date = moment(asDate);
const dueDate = moment(this.dueDate); const dueDate = moment(this.dueDate);
return Math.max(date.diff(dueDate, 'days'), 0); return Math.max(date.diff(dueDate, "days"), 0);
} }
/** /**
@@ -134,71 +138,71 @@ export default class SaleInvoice extends TenantModel {
* Filters the due invoices. * Filters the due invoices.
*/ */
dueInvoices(query) { dueInvoices(query) {
query.where(raw('BALANCE - PAYMENT_AMOUNT > 0')); query.where(raw("BALANCE - PAYMENT_AMOUNT > 0"));
}, },
/** /**
* Filters the invoices between the given date range. * Filters the invoices between the given date range.
*/ */
filterDateRange(query, startDate, endDate, type = 'day') { filterDateRange(query, startDate, endDate, type = "day") {
const dateFormat = 'YYYY-MM-DD HH:mm:ss'; const dateFormat = "YYYY-MM-DD HH:mm:ss";
const fromDate = moment(startDate).startOf(type).format(dateFormat); const fromDate = moment(startDate).startOf(type).format(dateFormat);
const toDate = moment(endDate).endOf(type).format(dateFormat); const toDate = moment(endDate).endOf(type).format(dateFormat);
if (startDate) { if (startDate) {
query.where('invoice_date', '>=', fromDate); query.where("invoice_date", ">=", fromDate);
} }
if (endDate) { if (endDate) {
query.where('invoice_date', '<=', toDate); query.where("invoice_date", "<=", toDate);
} }
}, },
/** /**
* Filters the invoices in draft status. * Filters the invoices in draft status.
*/ */
draft(query) { draft(query) {
query.where('delivered_at', null); query.where("delivered_at", null);
}, },
/** /**
* Filters the delivered invoices. * Filters the delivered invoices.
*/ */
delivered(query) { delivered(query) {
query.whereNot('delivered_at', null); query.whereNot("delivered_at", null);
}, },
/** /**
* Filters the unpaid invoices. * Filters the unpaid invoices.
*/ */
unpaid(query) { unpaid(query) {
query.where(raw('PAYMENT_AMOUNT = 0')); query.where(raw("PAYMENT_AMOUNT = 0"));
}, },
/** /**
* Filters the overdue invoices. * Filters the overdue invoices.
*/ */
overdue(query, asDate = moment().format('YYYY-MM-DD')) { overdue(query, asDate = moment().format("YYYY-MM-DD")) {
query.where('due_date', '<', asDate); query.where("due_date", "<", asDate);
}, },
/** /**
* Filters the not overdue invoices. * Filters the not overdue invoices.
*/ */
notOverdue(query, asDate = moment().format('YYYY-MM-DD')) { notOverdue(query, asDate = moment().format("YYYY-MM-DD")) {
query.where('due_date', '>=', asDate); query.where("due_date", ">=", asDate);
}, },
/** /**
* Filters the partially invoices. * Filters the partially invoices.
*/ */
partiallyPaid(query) { partiallyPaid(query) {
query.whereNot('payment_amount', 0); query.whereNot("payment_amount", 0);
query.whereNot(raw('`PAYMENT_AMOUNT` = `BALANCE`')); query.whereNot(raw("`PAYMENT_AMOUNT` = `BALANCE`"));
}, },
/** /**
* Filters the paid invoices. * Filters the paid invoices.
*/ */
paid(query) { paid(query) {
query.where(raw('PAYMENT_AMOUNT = BALANCE')); query.where(raw("PAYMENT_AMOUNT = BALANCE"));
}, },
/** /**
* Filters the sale invoices from the given date. * Filters the sale invoices from the given date.
*/ */
fromDate(query, fromDate) { fromDate(query, fromDate) {
query.where('invoice_date', '<=', fromDate) query.where("invoice_date", "<=", fromDate);
}, },
/** /**
* Sort the sale invoices by full-payment invoices. * Sort the sale invoices by full-payment invoices.
@@ -210,8 +214,8 @@ export default class SaleInvoice extends TenantModel {
* Sort the sale invoices by the due amount. * Sort the sale invoices by the due amount.
*/ */
sortByDueAmount(query, order) { sortByDueAmount(query, order) {
query.orderByRaw(`BALANCE - PAYMENT_AMOUNT ${order}`) query.orderByRaw(`BALANCE - PAYMENT_AMOUNT ${order}`);
} },
}; };
} }
@@ -219,22 +223,22 @@ export default class SaleInvoice extends TenantModel {
* Relationship mapping. * Relationship mapping.
*/ */
static get relationMappings() { static get relationMappings() {
const AccountTransaction = require('models/AccountTransaction'); const AccountTransaction = require("models/AccountTransaction");
const ItemEntry = require('models/ItemEntry'); const ItemEntry = require("models/ItemEntry");
const Contact = require('models/Contact'); const Contact = require("models/Contact");
const InventoryCostLotTracker = require('models/InventoryCostLotTracker'); const InventoryCostLotTracker = require("models/InventoryCostLotTracker");
const PaymentReceiveEntry = require('models/PaymentReceiveEntry'); const PaymentReceiveEntry = require("models/PaymentReceiveEntry");
return { return {
entries: { entries: {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: ItemEntry.default, modelClass: ItemEntry.default,
join: { join: {
from: 'sales_invoices.id', from: "sales_invoices.id",
to: 'items_entries.referenceId', to: "items_entries.referenceId",
}, },
filter(builder) { filter(builder) {
builder.where('reference_type', 'SaleInvoice'); builder.where("reference_type", "SaleInvoice");
}, },
}, },
@@ -242,23 +246,23 @@ export default class SaleInvoice extends TenantModel {
relation: Model.BelongsToOneRelation, relation: Model.BelongsToOneRelation,
modelClass: Contact.default, modelClass: Contact.default,
join: { join: {
from: 'sales_invoices.customerId', from: "sales_invoices.customerId",
to: 'contacts.id', to: "contacts.id",
}, },
filter(query) { filter(query) {
query.where('contact_service', 'Customer'); query.where("contact_service", "Customer");
} },
}, },
transactions: { transactions: {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: AccountTransaction.default, modelClass: AccountTransaction.default,
join: { join: {
from: 'sales_invoices.id', from: "sales_invoices.id",
to: 'accounts_transactions.referenceId' to: "accounts_transactions.referenceId",
}, },
filter(builder) { filter(builder) {
builder.where('reference_type', 'SaleInvoice'); builder.where("reference_type", "SaleInvoice");
}, },
}, },
@@ -266,11 +270,11 @@ export default class SaleInvoice extends TenantModel {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: InventoryCostLotTracker.default, modelClass: InventoryCostLotTracker.default,
join: { join: {
from: 'sales_invoices.id', from: "sales_invoices.id",
to: 'inventory_cost_lot_tracker.transactionId' to: "inventory_cost_lot_tracker.transactionId",
}, },
filter(builder) { filter(builder) {
builder.where('transaction_type', 'SaleInvoice'); builder.where("transaction_type", "SaleInvoice");
}, },
}, },
@@ -278,8 +282,8 @@ export default class SaleInvoice extends TenantModel {
relation: Model.HasManyRelation, relation: Model.HasManyRelation,
modelClass: PaymentReceiveEntry.default, modelClass: PaymentReceiveEntry.default,
join: { join: {
from: 'sales_invoices.id', from: "sales_invoices.id",
to: 'payment_receives_entries.invoice_id', to: "payment_receives_entries.invoice_id",
}, },
}, },
}; };
@@ -291,11 +295,11 @@ export default class SaleInvoice extends TenantModel {
* @param {Numeric} amount * @param {Numeric} amount
*/ */
static async changePaymentAmount(invoiceId, amount) { static async changePaymentAmount(invoiceId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement'; const changeMethod = amount > 0 ? "increment" : "decrement";
await this.query() await this.query()
.where('id', invoiceId) .where("id", invoiceId)
[changeMethod]('payment_amount', Math.abs(amount)); [changeMethod]("payment_amount", Math.abs(amount));
} }
/** /**
@@ -304,114 +308,120 @@ export default class SaleInvoice extends TenantModel {
static get fields() { static get fields() {
return { return {
customer: { customer: {
label: 'Customer', label: "Customer",
column: 'customer_id', column: "customer_id",
relation: 'contacts.id', relation: "contacts.id",
relationColumn: 'contacts.displayName', relationColumn: "contacts.displayName",
fieldType: 'options', fieldType: "options",
optionsResource: 'customers', optionsResource: "customers",
optionsKey: 'id', optionsKey: "id",
optionsLable: 'displayName', optionsLable: "displayName",
}, },
invoice_date: { invoice_date: {
label: 'Invoice date', label: "Invoice date",
column: 'invoice_date', column: "invoice_date",
columnType: 'date', columnType: "date",
fieldType: 'date', fieldType: "date",
}, },
due_date: { due_date: {
label: 'Due date', label: "Due date",
column: 'due_date', column: "due_date",
columnType: 'date', columnType: "date",
fieldType: 'date', fieldType: "date",
}, },
invoice_no: { invoice_no: {
label: 'Invoice No.', label: "Invoice No.",
column: 'invoice_no', column: "invoice_no",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
}, },
refernece_no: { reference_no: {
label: 'Reference No.', label: "Reference No.",
column: 'reference_no', column: "reference_no",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
}, },
invoice_message: { invoice_message: {
label: 'Invoice message', label: "Invoice message",
column: 'invoice_message', column: "invoice_message",
columnType: 'text', columnType: "text",
fieldType: 'text', fieldType: "text",
}, },
terms_conditions: { terms_conditions: {
label: 'Terms & conditions', label: "Terms & conditions",
column: 'terms_conditions', column: "terms_conditions",
columnType: 'text', columnType: "text",
fieldType: 'text', fieldType: "text",
}, },
invoice_amount: { invoice_amount: {
label: 'Invoice amount', label: "Invoice amount",
column: 'invoice_amount', column: "invoice_amount",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
}, },
payment_amount: { payment_amount: {
label: 'Payment amount', label: "Payment amount",
column: 'payment_amount', column: "payment_amount",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
},
balance: {
label: "Balance",
column: "balance",
columnType: "number",
fieldType: "number",
}, },
due_amount: { due_amount: {
label: 'Due amount', label: "Due amount",
column: 'due_amount', column: "due_amount",
columnType: 'number', columnType: "number",
fieldType: 'number', fieldType: "number",
sortQuery(query, role) { sortQuery(query, role) {
query.modify('sortByDueAmount', role.order); query.modify("sortByDueAmount", role.order);
} },
}, },
created_at: { created_at: {
label: 'Created at', label: "Created at",
column: 'created_at', column: "created_at",
columnType: 'date', columnType: "date",
}, },
status: { status: {
label: 'Status', label: "Status",
options: [ options: [
{ key: 'draft', label: 'Draft', }, { key: "draft", label: "Draft" },
{ key: 'delivered', label: 'Delivered' }, { key: "delivered", label: "Delivered" },
{ key: 'unpaid', label: 'Unpaid' }, { key: "unpaid", label: "Unpaid" },
{ key: 'overdue', label: 'Overdue' }, { key: "overdue", label: "Overdue" },
{ key: 'partially-paid', label: 'Partially paid' }, { key: "partially-paid", label: "Partially paid" },
{ key: 'paid', label: 'Paid' }, { key: "paid", label: "Paid" },
], ],
query: (query, role) => { query: (query, role) => {
switch(role.value) { switch (role.value) {
case 'draft': case "draft":
query.modify('draft'); query.modify("draft");
break; break;
case 'delivered': case "delivered":
query.modify('delivered'); query.modify("delivered");
break; break;
case 'unpaid': case "unpaid":
query.modify('unpaid'); query.modify("unpaid");
break; break;
case 'overdue': case "overdue":
query.modify('overdue'); query.modify("overdue");
break; break;
case 'partially-paid': case "partially-paid":
query.modify('partiallyPaid'); query.modify("partiallyPaid");
break; break;
case 'paid': case "paid":
query.modify('paid'); query.modify("paid");
break; break;
} }
}, },
sortQuery(query, role) { sortQuery(query, role) {
query.modify('sortByStatus', role.order); query.modify("sortByStatus", role.order);
} },
} },
}; };
} }
} }

View File

@@ -133,7 +133,7 @@ export default class SaleReceipt extends TenantModel {
}, },
deposit_account: { deposit_account: {
column: 'deposit_account_id', column: 'deposit_account_id',
lable: 'Deposit account', label: 'Deposit account',
relation: "accounts.id", relation: "accounts.id",
optionsResource: "account", optionsResource: "account",
}, },