2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 15:20:56 +00:00

feat: add custom UOM support

This commit is contained in:
18alantom 2022-07-08 23:21:21 +05:30 committed by Alan
parent e04d74128f
commit 014db554fa
10 changed files with 111 additions and 31 deletions

View File

@ -0,0 +1,13 @@
import { ModelNameEnum } from '../../models/types';
import { defaultUOMs } from '../../utils/defaults';
import { DatabaseManager } from '../database/manager';
import { getDefaultMetaFieldValueMap } from '../helpers';
async function execute(dm: DatabaseManager) {
for (const uom of defaultUOMs) {
const defaults = getDefaultMetaFieldValueMap();
await dm.db?.insert(ModelNameEnum.UOM, { ...uom, ...defaults });
}
}
export default { execute };

View File

@ -1,4 +1,5 @@
import { Patch } from '../database/types';
import addUOMs from './addUOMs';
import testPatch from './testPatch';
import updateSchemas from './updateSchemas';
@ -10,4 +11,9 @@ export default [
patch: updateSchemas,
priority: 100,
},
{
name: 'addUOMs',
version: '0.6.0-beta.0',
patch: addUOMs,
},
] as Patch[];

View File

@ -457,8 +457,11 @@ export class Doc extends Observable<DocValue | Doc[]> {
async loadLinks() {
this._links = {};
const inlineLinks = this.schema.fields.filter((f) => f.inline);
for (const f of inlineLinks) {
const linkFields = this.schema.fields.filter(
(f) => f.fieldtype === FieldTypeEnum.Link || f.inline
);
for (const f of linkFields) {
await this.loadLink(f.fieldname);
}
}

View File

@ -2,6 +2,7 @@ import { DocValue } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import { FiltersMap, FormulaMap, ValidationMap } from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { Invoice } from '../Invoice/Invoice';
@ -43,6 +44,26 @@ export abstract class InvoiceItem extends Doc {
(this.rate as Money).mul(this.parentdoc!.exchangeRate as number),
dependsOn: ['item', 'rate'],
},
quantity: {
formula: async () => {
if (!this.item) {
return this.quantity as number;
}
const itemDoc = await this.fyo.doc.getDoc(
ModelNameEnum.Item,
this.item as string
);
const unitDoc = itemDoc.getLink('unit');
if (unitDoc?.isWhole) {
return Math.round(this.quantity as number);
}
return this.quantity as number;
},
dependsOn: ['quantity'],
},
account: {
formula: () => {
let accountType = 'expenseAccount';

View File

@ -9,6 +9,7 @@ export enum ModelNameEnum {
Currency = 'Currency',
GetStarted = 'GetStarted',
Item = 'Item',
UOM = 'UOM',
JournalEntry = 'JournalEntry',
JournalEntryAccount = 'JournalEntryAccount',
NumberSeries = 'NumberSeries',

View File

@ -25,35 +25,10 @@
{
"fieldname": "unit",
"label": "Unit Type",
"fieldtype": "Select",
"placeholder": "Unit Type",
"default": "Unit",
"options": [
{
"value": "Unit",
"label": "Unit"
},
{
"value": "Kg",
"label": "Kg"
},
{
"value": "Gram",
"label": "Gram"
},
{
"value": "Meter",
"label": "Meter"
},
{
"value": "Hour",
"label": "Hour"
},
{
"value": "Day",
"label": "Day"
}
]
"fieldtype": "Link",
"target": "UOM",
"create": true,
"placeholder": "Unit Type"
},
{
"fieldname": "itemType",

22
schemas/app/UOM.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "UOM",
"label": "UOM",
"isSingle": false,
"naming": "manual",
"fields": [
{
"fieldname": "name",
"label": "UOM",
"fieldtype": "Data",
"placeholder": "Item Name",
"required": true
},
{
"fieldname": "isWhole",
"label": "Is Whole",
"fieldtype": "Check",
"default": false
}
],
"quickEditFields": ["isWhole"]
}

View File

@ -22,6 +22,7 @@ import SetupWizard from './app/SetupWizard.json';
import Tax from './app/Tax.json';
import TaxDetail from './app/TaxDetail.json';
import TaxSummary from './app/TaxSummary.json';
import UOM from './app/UOM.json';
import PatchRun from './core/PatchRun.json';
import SingleValue from './core/SingleValue.json';
import SystemSettings from './core/SystemSettings.json';
@ -62,6 +63,7 @@ export const appSchemas: Schema[] | SchemaStub[] = [
Party as Schema,
Address as Schema,
Item as Schema,
UOM as Schema,
Payment as Schema,
PaymentFor as Schema,

View File

@ -12,6 +12,7 @@ import { ModelNameEnum } from 'models/types';
import { initializeInstance } from 'src/initFyo';
import { createRegionalRecords } from 'src/regional';
import { getRandomString } from 'utils';
import { defaultUOMs } from 'utils/defaults';
import { getCountryCodeFromCountry, getCountryInfo } from 'utils/misc';
import { CountryInfo } from 'utils/types';
import { CreateCOA } from './createCOA';
@ -33,11 +34,21 @@ export default async function setupInstance(
await createCurrencyRecords(fyo);
await createAccountRecords(bankName, country, chartOfAccounts, fyo);
await createRegionalRecords(country, fyo);
await createDefaultEntries(fyo);
await createDefaultNumberSeries(fyo);
await completeSetup(companyName, fyo);
}
async function createDefaultEntries(fyo: Fyo) {
/**
* Create default UOM entries
*/
for (const uom of defaultUOMs) {
await checkAndCreateDoc(ModelNameEnum.UOM, uom, fyo);
}
}
async function initializeDatabase(dbPath: string, country: string, fyo: Fyo) {
const countryCode = getCountryCodeFromCountry(country);
await initializeInstance(dbPath, true, countryCode, fyo);

26
utils/defaults.ts Normal file
View File

@ -0,0 +1,26 @@
export const defaultUOMs = [
{
name: 'Unit',
isWhole: true,
},
{
name: 'Kg',
isWhole: false,
},
{
name: 'Gram',
isWhole: false,
},
{
name: 'Meter',
isWhole: false,
},
{
name: 'Hour',
isWhole: false,
},
{
name: 'Day',
isWhole: false,
},
];