2
0
mirror of https://github.com/frappe/books.git synced 2025-01-26 16:48:28 +00:00
books/models/baseModels/PurchaseInvoiceItem/PurchaseInvoiceItem.js

110 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-02-16 11:49:16 +05:30
import { t } from 'frappe';
export default {
name: 'PurchaseInvoiceItem',
2019-07-19 18:54:31 +05:30
doctype: 'DocType',
isChild: 1,
keywordFields: [],
tableFields: ['item', 'tax', 'quantity', 'rate', 'amount'],
2019-07-19 18:54:31 +05:30
fields: [
{
fieldname: 'item',
2022-02-16 11:49:16 +05:30
label: t`Item`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Link',
target: 'Item',
required: 1,
getFilters(_, doc) {
2021-12-11 00:47:28 +05:30
let items = doc.parentdoc.items.map((d) => d.item).filter(Boolean);
2022-03-10 12:23:28 +05:30
const baseFilter = { for: ['not in', ['sales']] };
if (items.length <= 0) {
return baseFilter;
}
2022-03-10 12:23:28 +05:30
return {
name: ['not in', items],
...baseFilter,
};
2021-12-11 00:47:28 +05:30
},
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'description',
2022-02-16 11:49:16 +05:30
label: t`Description`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Text',
formula: (row, doc) => doc.getFrom('Item', row.item, 'description'),
2021-12-11 00:47:28 +05:30
hidden: 1,
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'quantity',
2022-02-16 11:49:16 +05:30
label: t`Quantity`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Float',
required: 1,
default: 1,
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'rate',
2022-02-16 11:49:16 +05:30
label: t`Rate`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Currency',
required: 1,
formula: async (row, doc) => {
const baseRate =
(await doc.getFrom('Item', row.item, 'rate')) || frappe.pesa(0);
return baseRate.div(doc.exchangeRate);
},
2021-12-11 00:47:28 +05:30
getCurrency: (row, doc) => doc.currency,
2022-02-28 12:46:33 +05:30
validate(value) {
if (value.gte(0)) {
return;
}
throw new frappe.errors.ValidationError(
frappe.t`Rate (${frappe.format(
value,
'Currency'
)}) cannot be less zero.`
);
},
},
{
fieldname: 'baseRate',
2022-02-16 11:49:16 +05:30
label: t`Rate (Company Currency)`,
fieldtype: 'Currency',
formula: (row, doc) => row.rate.mul(doc.exchangeRate),
2021-12-11 00:47:28 +05:30
readOnly: 1,
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'account',
2022-02-16 11:49:16 +05:30
label: t`Account`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Link',
target: 'Account',
required: 1,
2022-02-28 17:36:41 +05:30
readOnly: 1,
2021-12-11 00:47:28 +05:30
formula: (row, doc) => doc.getFrom('Item', row.item, 'expenseAccount'),
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'tax',
2022-02-16 11:49:16 +05:30
label: t`Tax`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Link',
target: 'Tax',
formula: (row, doc) => {
if (row.tax) return row.tax;
return doc.getFrom('Item', row.item, 'tax');
2021-12-11 00:47:28 +05:30
},
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'amount',
2022-02-16 11:49:16 +05:30
label: t`Amount`,
2019-07-19 18:54:31 +05:30
fieldtype: 'Currency',
readOnly: 1,
formula: (row) => row.rate.mul(row.quantity),
2021-12-11 00:47:28 +05:30
getCurrency: (row, doc) => doc.currency,
2019-07-19 18:54:31 +05:30
},
{
fieldname: 'baseAmount',
2022-02-16 11:49:16 +05:30
label: t`Amount (Company Currency)`,
fieldtype: 'Currency',
2019-07-19 18:54:31 +05:30
readOnly: 1,
formula: (row, doc) => row.amount.mul(doc.exchangeRate),
2021-12-11 00:47:28 +05:30
},
],
2019-07-19 18:54:31 +05:30
};