2022-04-19 05:59:36 +00:00
|
|
|
import { Fyo } from 'fyo';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-04-14 05:24:11 +00:00
|
|
|
import {
|
|
|
|
Action,
|
|
|
|
FiltersMap,
|
|
|
|
FormulaMap,
|
|
|
|
ListViewSettings,
|
2022-04-22 11:02:03 +00:00
|
|
|
ValidationMap,
|
2022-04-19 05:59:36 +00:00
|
|
|
} from 'fyo/model/types';
|
2022-04-22 11:02:03 +00:00
|
|
|
import {
|
|
|
|
validateEmail,
|
|
|
|
validatePhoneNumber,
|
|
|
|
} from 'fyo/model/validationFunction';
|
2022-04-14 05:24:11 +00:00
|
|
|
import { PartyRole } from './types';
|
|
|
|
|
|
|
|
export class Party extends Doc {
|
2022-04-14 06:43:58 +00:00
|
|
|
async updateOutstandingAmount() {
|
2022-04-14 05:24:11 +00:00
|
|
|
const role = this.role as PartyRole;
|
|
|
|
switch (role) {
|
|
|
|
case 'Customer':
|
|
|
|
await this._updateOutstandingAmount('SalesInvoice');
|
|
|
|
break;
|
|
|
|
case 'Supplier':
|
|
|
|
await this._updateOutstandingAmount('PurchaseInvoice');
|
|
|
|
break;
|
|
|
|
case 'Both':
|
|
|
|
await this._updateOutstandingAmount('SalesInvoice');
|
|
|
|
await this._updateOutstandingAmount('PurchaseInvoice');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateOutstandingAmount(
|
|
|
|
schemaName: 'SalesInvoice' | 'PurchaseInvoice'
|
|
|
|
) {
|
|
|
|
const outstandingAmounts = (
|
2022-04-19 05:59:36 +00:00
|
|
|
await this.fyo.db.getAllRaw(schemaName, {
|
2022-04-14 05:24:11 +00:00
|
|
|
fields: ['outstandingAmount', 'party'],
|
|
|
|
filters: { submitted: true },
|
|
|
|
})
|
|
|
|
).filter(({ party }) => party === this.name);
|
|
|
|
|
|
|
|
const totalOutstanding = outstandingAmounts
|
2022-04-18 11:29:20 +00:00
|
|
|
.map(({ outstandingAmount }) =>
|
2022-04-19 05:59:36 +00:00
|
|
|
this.fyo.pesa(outstandingAmount as number)
|
2022-04-18 11:29:20 +00:00
|
|
|
)
|
2022-04-19 05:59:36 +00:00
|
|
|
.reduce((a, b) => a.add(b), this.fyo.pesa(0));
|
2022-04-14 05:24:11 +00:00
|
|
|
|
|
|
|
await this.set('outstandingAmount', totalOutstanding);
|
2022-04-24 06:48:44 +00:00
|
|
|
await this.sync();
|
2022-04-14 05:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
formulas: FormulaMap = {
|
2022-04-29 13:00:24 +00:00
|
|
|
defaultAccount: {
|
|
|
|
formula: async () => {
|
|
|
|
const role = this.role as PartyRole;
|
|
|
|
if (role === 'Both') {
|
|
|
|
return '';
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-29 13:00:24 +00:00
|
|
|
let accountName = 'Debtors';
|
|
|
|
if (role === 'Supplier') {
|
|
|
|
accountName = 'Creditors';
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-29 13:00:24 +00:00
|
|
|
const accountExists = await this.fyo.db.exists('Account', accountName);
|
|
|
|
return accountExists ? accountName : '';
|
|
|
|
},
|
|
|
|
dependsOn: ['role'],
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-29 13:00:24 +00:00
|
|
|
currency: {
|
2022-05-02 10:15:16 +00:00
|
|
|
formula: async () => this.fyo.singles.SystemSettings!.currency as string,
|
2022-04-29 13:00:24 +00:00
|
|
|
},
|
|
|
|
address: {
|
|
|
|
formula: async () => {
|
|
|
|
const address = this.address as string | undefined;
|
|
|
|
if (address) {
|
2022-05-02 10:15:16 +00:00
|
|
|
return (await this.fyo.getValue(
|
|
|
|
'Address',
|
|
|
|
address,
|
|
|
|
'addressDisplay'
|
|
|
|
)) as string;
|
2022-04-29 13:00:24 +00:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-04-22 11:02:03 +00:00
|
|
|
validations: ValidationMap = {
|
|
|
|
email: validateEmail,
|
|
|
|
phone: validatePhoneNumber,
|
|
|
|
};
|
|
|
|
|
2022-04-14 05:24:11 +00:00
|
|
|
static filters: FiltersMap = {
|
|
|
|
defaultAccount: (doc: Doc) => {
|
|
|
|
const role = doc.role as PartyRole;
|
|
|
|
if (role === 'Both') {
|
2022-04-14 06:43:58 +00:00
|
|
|
return {
|
|
|
|
isGroup: false,
|
|
|
|
accountType: ['in', ['Payable', 'Receivable']],
|
|
|
|
};
|
2022-04-14 05:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
isGroup: false,
|
|
|
|
accountType: role === 'Customer' ? 'Receivable' : 'Payable',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-04-18 11:29:20 +00:00
|
|
|
static getListViewSettings(): ListViewSettings {
|
|
|
|
return {
|
2022-05-02 10:56:47 +00:00
|
|
|
columns: ['name', 'email', 'phone', 'outstandingAmount'],
|
2022-04-18 11:29:20 +00:00
|
|
|
};
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-04-19 05:59:36 +00:00
|
|
|
static getActions(fyo: Fyo): Action[] {
|
2022-04-18 11:29:20 +00:00
|
|
|
return [
|
|
|
|
{
|
2022-04-28 18:57:10 +00:00
|
|
|
label: fyo.t`Create Purchase`,
|
2022-04-18 11:29:20 +00:00
|
|
|
condition: (doc: Doc) =>
|
2022-04-26 10:12:33 +00:00
|
|
|
!doc.notInserted && (doc.role as PartyRole) !== 'Customer',
|
2022-04-18 11:29:20 +00:00
|
|
|
action: async (partyDoc, router) => {
|
2022-05-02 05:31:11 +00:00
|
|
|
const doc = await fyo.doc.getNewDoc('PurchaseInvoice', {
|
|
|
|
party: partyDoc.name,
|
|
|
|
account: partyDoc.defaultAccount as string,
|
|
|
|
});
|
2022-04-18 11:29:20 +00:00
|
|
|
router.push({
|
|
|
|
path: `/edit/PurchaseInvoice/${doc.name}`,
|
|
|
|
query: {
|
2022-05-01 04:45:47 +00:00
|
|
|
schemaName: 'PurchaseInvoice',
|
2022-04-18 11:29:20 +00:00
|
|
|
values: {
|
|
|
|
// @ts-ignore
|
|
|
|
party: partyDoc.name!,
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
});
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
{
|
2022-04-28 18:57:10 +00:00
|
|
|
label: fyo.t`View Purchases`,
|
2022-04-18 11:29:20 +00:00
|
|
|
condition: (doc: Doc) =>
|
2022-04-26 10:12:33 +00:00
|
|
|
!doc.notInserted && (doc.role as PartyRole) !== 'Customer',
|
2022-04-18 11:29:20 +00:00
|
|
|
action: async (partyDoc, router) => {
|
2022-05-02 05:31:11 +00:00
|
|
|
router.push(`/list/PurchaseInvoice/party/${partyDoc.name}`);
|
2022-04-18 11:29:20 +00:00
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
{
|
2022-04-28 18:57:10 +00:00
|
|
|
label: fyo.t`Create Sale`,
|
2022-04-18 11:29:20 +00:00
|
|
|
condition: (doc: Doc) =>
|
2022-04-26 10:12:33 +00:00
|
|
|
!doc.notInserted && (doc.role as PartyRole) !== 'Supplier',
|
2022-04-18 11:29:20 +00:00
|
|
|
action: async (partyDoc, router) => {
|
2022-05-02 05:31:11 +00:00
|
|
|
const doc = await fyo.doc.getNewDoc('SalesInvoice', {
|
|
|
|
party: partyDoc.name,
|
|
|
|
account: partyDoc.defaultAccount as string,
|
|
|
|
});
|
2022-04-18 11:29:20 +00:00
|
|
|
router.push({
|
|
|
|
path: `/edit/SalesInvoice/${doc.name}`,
|
|
|
|
query: {
|
2022-05-01 04:45:47 +00:00
|
|
|
schemaName: 'SalesInvoice',
|
2022-04-18 11:29:20 +00:00
|
|
|
values: {
|
|
|
|
// @ts-ignore
|
|
|
|
party: partyDoc.name!,
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
});
|
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
{
|
2022-04-28 18:57:10 +00:00
|
|
|
label: fyo.t`View Sales`,
|
2022-04-18 11:29:20 +00:00
|
|
|
condition: (doc: Doc) =>
|
2022-04-26 10:12:33 +00:00
|
|
|
!doc.notInserted && (doc.role as PartyRole) !== 'Supplier',
|
2022-04-18 11:29:20 +00:00
|
|
|
action: async (partyDoc, router) => {
|
2022-05-02 05:31:11 +00:00
|
|
|
router.push(`/list/SalesInvoice/party/${partyDoc.name}`);
|
2022-04-18 11:29:20 +00:00
|
|
|
},
|
2022-04-14 05:24:11 +00:00
|
|
|
},
|
2022-04-18 11:29:20 +00:00
|
|
|
];
|
|
|
|
}
|
2022-04-14 05:24:11 +00:00
|
|
|
}
|