2
0
mirror of https://github.com/frappe/books.git synced 2024-11-13 00:46:28 +00:00
books/models/baseModels/SalesInvoiceItem/SalesInvoiceItem.js

121 lines
2.9 KiB
JavaScript
Raw Normal View History

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