2022-04-28 12:37:07 +00:00
|
|
|
import { Fyo, t } from 'fyo';
|
2022-04-24 06:48:44 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-06-14 09:10:46 +00:00
|
|
|
import { Action, ColumnConfig, DocStatus, RenderData } from 'fyo/model/types';
|
2022-04-19 06:05:39 +00:00
|
|
|
import { DateTime } from 'luxon';
|
2022-05-23 05:30:54 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-11-07 07:15:38 +00:00
|
|
|
import { safeParseFloat } from 'utils/index';
|
2022-04-14 05:24:11 +00:00
|
|
|
import { Router } from 'vue-router';
|
2022-05-14 08:46:05 +00:00
|
|
|
import {
|
|
|
|
AccountRootType,
|
2022-09-29 09:41:05 +00:00
|
|
|
AccountRootTypeEnum
|
2022-05-14 08:46:05 +00:00
|
|
|
} from './baseModels/Account/types';
|
2022-10-12 09:29:43 +00:00
|
|
|
import {
|
|
|
|
Defaults,
|
|
|
|
numberSeriesDefaultsMap
|
|
|
|
} from './baseModels/Defaults/Defaults';
|
2022-05-02 15:17:14 +00:00
|
|
|
import { InvoiceStatus, ModelNameEnum } from './types';
|
2022-04-14 05:24:11 +00:00
|
|
|
|
2022-05-02 15:17:14 +00:00
|
|
|
export function getInvoiceActions(
|
|
|
|
schemaName: ModelNameEnum.PurchaseInvoice | ModelNameEnum.SalesInvoice,
|
|
|
|
fyo: Fyo
|
|
|
|
): Action[] {
|
2022-04-14 08:01:33 +00:00
|
|
|
return [
|
|
|
|
{
|
2022-04-19 05:59:36 +00:00
|
|
|
label: fyo.t`Make Payment`,
|
2022-04-14 08:01:33 +00:00
|
|
|
condition: (doc: Doc) =>
|
2022-05-03 13:13:47 +00:00
|
|
|
doc.isSubmitted && !(doc.outstandingAmount as Money).isZero(),
|
2022-04-14 08:01:33 +00:00
|
|
|
action: async function makePayment(doc: Doc) {
|
2022-05-11 10:44:31 +00:00
|
|
|
const payment = fyo.doc.getNewDoc('Payment');
|
2022-04-26 10:12:33 +00:00
|
|
|
payment.once('afterSync', async () => {
|
2022-04-14 08:01:33 +00:00
|
|
|
await payment.submit();
|
|
|
|
});
|
2022-05-03 13:13:47 +00:00
|
|
|
|
2022-04-14 08:01:33 +00:00
|
|
|
const isSales = schemaName === 'SalesInvoice';
|
2022-05-03 13:13:47 +00:00
|
|
|
const party = doc.party as string;
|
2022-04-14 08:01:33 +00:00
|
|
|
const paymentType = isSales ? 'Receive' : 'Pay';
|
|
|
|
const hideAccountField = isSales ? 'account' : 'paymentAccount';
|
|
|
|
|
2022-04-20 06:38:47 +00:00
|
|
|
const { openQuickEdit } = await import('src/utils/ui');
|
2022-04-14 08:01:33 +00:00
|
|
|
await openQuickEdit({
|
|
|
|
schemaName: 'Payment',
|
|
|
|
name: payment.name as string,
|
2022-10-11 11:26:04 +00:00
|
|
|
hideFields: ['party', 'paymentType', 'for'],
|
2022-04-14 08:01:33 +00:00
|
|
|
defaults: {
|
|
|
|
party,
|
|
|
|
[hideAccountField]: doc.account,
|
|
|
|
date: new Date().toISOString().slice(0, 10),
|
|
|
|
paymentType,
|
|
|
|
for: [
|
|
|
|
{
|
2022-05-01 04:45:47 +00:00
|
|
|
referenceType: doc.schemaName,
|
2022-04-14 08:01:33 +00:00
|
|
|
referenceName: doc.name,
|
|
|
|
amount: doc.outstandingAmount,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2022-04-19 05:59:36 +00:00
|
|
|
getLedgerLinkAction(fyo),
|
2022-04-14 08:01:33 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:13:47 +00:00
|
|
|
export function getLedgerLinkAction(fyo: Fyo): Action {
|
|
|
|
return {
|
|
|
|
label: fyo.t`Ledger Entries`,
|
|
|
|
condition: (doc: Doc) => doc.isSubmitted,
|
|
|
|
action: async (doc: Doc, router: Router) => {
|
|
|
|
router.push({
|
|
|
|
name: 'Report',
|
|
|
|
params: {
|
2022-05-27 07:15:07 +00:00
|
|
|
reportClassName: 'GeneralLedger',
|
|
|
|
defaultFilters: JSON.stringify({
|
2022-05-03 13:13:47 +00:00
|
|
|
referenceType: doc.schemaName,
|
|
|
|
referenceName: doc.name,
|
2022-05-27 07:15:07 +00:00
|
|
|
}),
|
2022-05-03 13:13:47 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-28 12:37:07 +00:00
|
|
|
export function getTransactionStatusColumn(): ColumnConfig {
|
2022-06-14 09:10:46 +00:00
|
|
|
const statusMap = getStatusMap();
|
2022-04-14 08:01:33 +00:00
|
|
|
|
|
|
|
return {
|
2022-04-28 12:37:07 +00:00
|
|
|
label: t`Status`,
|
2022-04-14 08:01:33 +00:00
|
|
|
fieldname: 'status',
|
|
|
|
fieldtype: 'Select',
|
2022-06-14 09:10:46 +00:00
|
|
|
render(doc) {
|
|
|
|
const status = getDocStatus(doc) as InvoiceStatus;
|
2022-04-14 08:01:33 +00:00
|
|
|
const color = statusColor[status];
|
|
|
|
const label = statusMap[status];
|
2022-06-14 09:10:46 +00:00
|
|
|
|
2022-04-14 08:01:33 +00:00
|
|
|
return {
|
|
|
|
template: `<Badge class="text-xs" color="${color}">${label}</Badge>`,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-14 09:10:46 +00:00
|
|
|
export const statusColor: Record<
|
|
|
|
DocStatus | InvoiceStatus,
|
|
|
|
string | undefined
|
|
|
|
> = {
|
|
|
|
'': 'gray',
|
2022-04-14 08:01:33 +00:00
|
|
|
Draft: 'gray',
|
|
|
|
Unpaid: 'orange',
|
|
|
|
Paid: 'green',
|
2022-06-14 09:10:46 +00:00
|
|
|
Saved: 'gray',
|
|
|
|
NotSaved: 'gray',
|
|
|
|
Submitted: 'green',
|
2022-04-14 08:01:33 +00:00
|
|
|
Cancelled: 'red',
|
|
|
|
};
|
|
|
|
|
2022-06-14 09:10:46 +00:00
|
|
|
export function getStatusMap(): Record<DocStatus | InvoiceStatus, string> {
|
|
|
|
return {
|
|
|
|
'': '',
|
|
|
|
Draft: t`Draft`,
|
|
|
|
Unpaid: t`Unpaid`,
|
|
|
|
Paid: t`Paid`,
|
|
|
|
Saved: t`Saved`,
|
|
|
|
NotSaved: t`Not Saved`,
|
|
|
|
Submitted: t`Submitted`,
|
|
|
|
Cancelled: t`Cancelled`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDocStatus(
|
|
|
|
doc?: RenderData | Doc
|
|
|
|
): DocStatus | InvoiceStatus {
|
|
|
|
if (!doc) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.notInserted) {
|
|
|
|
return 'Draft';
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 09:10:46 +00:00
|
|
|
if (doc.dirty) {
|
|
|
|
return 'NotSaved';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!doc.schema?.isSubmittable) {
|
|
|
|
return 'Saved';
|
|
|
|
}
|
|
|
|
|
|
|
|
return getSubmittableDocStatus(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSubmittableDocStatus(doc: RenderData | Doc) {
|
|
|
|
if (
|
|
|
|
[ModelNameEnum.SalesInvoice, ModelNameEnum.PurchaseInvoice].includes(
|
|
|
|
doc.schema.name as ModelNameEnum
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return getInvoiceStatus(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!doc.submitted && !doc.cancelled) {
|
|
|
|
return 'Submitted';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!!doc.submitted && !!doc.cancelled) {
|
|
|
|
return 'Cancelled';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Saved';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInvoiceStatus(doc: RenderData | Doc): InvoiceStatus {
|
|
|
|
if (
|
|
|
|
doc.submitted &&
|
|
|
|
!doc.cancelled &&
|
|
|
|
(doc.outstandingAmount as Money).isZero()
|
|
|
|
) {
|
|
|
|
return 'Paid';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
doc.submitted &&
|
|
|
|
!doc.cancelled &&
|
|
|
|
(doc.outstandingAmount as Money).isPositive()
|
|
|
|
) {
|
|
|
|
return 'Unpaid';
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (doc.cancelled) {
|
2022-06-14 09:10:46 +00:00
|
|
|
return 'Cancelled';
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
2022-06-14 09:10:46 +00:00
|
|
|
|
|
|
|
return 'Saved';
|
2022-04-14 08:01:33 +00:00
|
|
|
}
|
2022-04-19 06:05:39 +00:00
|
|
|
|
|
|
|
export async function getExchangeRate({
|
|
|
|
fromCurrency,
|
|
|
|
toCurrency,
|
|
|
|
date,
|
|
|
|
}: {
|
|
|
|
fromCurrency: string;
|
|
|
|
toCurrency: string;
|
|
|
|
date?: string;
|
|
|
|
}) {
|
2022-09-29 09:41:05 +00:00
|
|
|
if (!fetch) {
|
|
|
|
return 1;
|
2022-04-19 06:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 09:41:05 +00:00
|
|
|
if (!date) {
|
|
|
|
date = DateTime.local().toISODate();
|
2022-04-19 06:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const cacheKey = `currencyExchangeRate:${date}:${fromCurrency}:${toCurrency}`;
|
|
|
|
|
|
|
|
let exchangeRate = 0;
|
|
|
|
if (localStorage) {
|
2022-11-07 07:15:38 +00:00
|
|
|
exchangeRate = safeParseFloat(
|
2022-04-19 06:05:39 +00:00
|
|
|
localStorage.getItem(cacheKey as string) as string
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-29 09:41:05 +00:00
|
|
|
if (exchangeRate && exchangeRate !== 1) {
|
|
|
|
return exchangeRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await fetch(
|
|
|
|
`https://api.vatcomply.com/rates?date=${date}&base=${fromCurrency}&symbols=${toCurrency}`
|
|
|
|
);
|
|
|
|
const data = await res.json();
|
|
|
|
exchangeRate = data.rates[toCurrency];
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
exchangeRate ??= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localStorage) {
|
|
|
|
localStorage.setItem(cacheKey, String(exchangeRate));
|
2022-04-19 06:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return exchangeRate;
|
|
|
|
}
|
2022-05-14 08:46:05 +00:00
|
|
|
|
|
|
|
export function isCredit(rootType: AccountRootType) {
|
|
|
|
switch (rootType) {
|
|
|
|
case AccountRootTypeEnum.Asset:
|
|
|
|
return false;
|
|
|
|
case AccountRootTypeEnum.Liability:
|
|
|
|
return true;
|
|
|
|
case AccountRootTypeEnum.Equity:
|
|
|
|
return true;
|
|
|
|
case AccountRootTypeEnum.Expense:
|
|
|
|
return false;
|
|
|
|
case AccountRootTypeEnum.Income:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 09:29:43 +00:00
|
|
|
|
|
|
|
export function getNumberSeries(schemaName: string, fyo: Fyo) {
|
|
|
|
const numberSeriesKey = numberSeriesDefaultsMap[schemaName];
|
|
|
|
if (!numberSeriesKey) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaults = fyo.singles.Defaults as Defaults | undefined;
|
|
|
|
const field = fyo.getField(schemaName, 'numberSeries');
|
|
|
|
const value = defaults?.[numberSeriesKey] as string | undefined;
|
|
|
|
return value ?? (field?.default as string | undefined);
|
|
|
|
}
|