Skip to content

Commit 8ae6ae0

Browse files
committed
incr: add default location
1 parent 5d73284 commit 8ae6ae0

File tree

9 files changed

+139
-67
lines changed

9 files changed

+139
-67
lines changed

backend/patches/addUOMs.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
import { ModelNameEnum } from '../../models/types';
2-
import { defaultUOMs } from '../../utils/defaults';
32
import { DatabaseManager } from '../database/manager';
43
import { getDefaultMetaFieldValueMap } from '../helpers';
54

5+
const defaultUOMs = [
6+
{
7+
name: `Unit`,
8+
isWhole: true,
9+
},
10+
{
11+
name: `Kg`,
12+
isWhole: false,
13+
},
14+
{
15+
name: `Gram`,
16+
isWhole: false,
17+
},
18+
{
19+
name: `Meter`,
20+
isWhole: false,
21+
},
22+
{
23+
name: `Hour`,
24+
isWhole: false,
25+
},
26+
{
27+
name: `Day`,
28+
isWhole: false,
29+
},
30+
];
31+
632
async function execute(dm: DatabaseManager) {
733
for (const uom of defaultUOMs) {
834
const defaults = getDefaultMetaFieldValueMap();

models/inventory/InventorySettings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AccountTypeEnum } from 'models/baseModels/Account/types';
44
import { ValuationMethod } from './types';
55

66
export class InventorySettings extends Doc {
7+
defaultLocation?: string;
78
stockInHand?: string;
89
valuationMethod?: ValuationMethod;
910
stockReceivedButNotBilled?: string;

models/inventory/StockMovementItem.ts

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import {
33
FiltersMap,
44
FormulaMap,
55
ReadOnlyMap,
6-
RequiredMap
6+
RequiredMap,
77
} from 'fyo/model/types';
88
import { ModelNameEnum } from 'models/types';
99
import { Money } from 'pesa';
10-
import { locationFilter } from './helpers';
1110
import { StockMovement } from './StockMovement';
1211
import { MovementType } from './types';
1312

@@ -21,10 +20,20 @@ export class StockMovementItem extends Doc {
2120
amount?: Money;
2221
parentdoc?: StockMovement;
2322

23+
get isIssue() {
24+
return this.parentdoc?.movementType === MovementType.MaterialIssue;
25+
}
26+
27+
get isReceipt() {
28+
return this.parentdoc?.movementType === MovementType.MaterialReceipt;
29+
}
30+
31+
get isTransfer() {
32+
return this.parentdoc?.movementType === MovementType.MaterialTransfer;
33+
}
34+
2435
static filters: FiltersMap = {
2536
item: () => ({ trackItem: true }),
26-
toLocation: locationFilter,
27-
fromLocation: locationFilter,
2837
};
2938

3039
formulas: FormulaMap = {
@@ -43,40 +52,50 @@ export class StockMovementItem extends Doc {
4352
dependsOn: ['item', 'rate', 'quantity'],
4453
},
4554
fromLocation: {
46-
formula: () => {
47-
if (this.parentdoc?.movementType === MovementType.MaterialReceipt) {
55+
formula: (fn) => {
56+
if (this.isReceipt || this.isTransfer) {
4857
return null;
4958
}
59+
60+
const defaultLocation = this.fyo.singles.InventorySettings
61+
?.defaultLocation as string | undefined;
62+
if (defaultLocation && !this.location && this.isIssue) {
63+
return defaultLocation;
64+
}
65+
66+
return this.toLocation;
5067
},
68+
dependsOn: ['movementType'],
5169
},
5270
toLocation: {
53-
formula: () => {
54-
if (this.parentdoc?.movementType === MovementType.MaterialIssue) {
71+
formula: (fn) => {
72+
if (this.isIssue || this.isTransfer) {
5573
return null;
5674
}
75+
76+
const defaultLocation = this.fyo.singles.InventorySettings
77+
?.defaultLocation as string | undefined;
78+
if (defaultLocation && !this.location && this.isReceipt) {
79+
return defaultLocation;
80+
}
81+
82+
return this.toLocation;
5783
},
84+
dependsOn: ['movementType'],
5885
},
5986
};
6087

6188
required: RequiredMap = {
62-
fromLocation: () =>
63-
this.parentdoc?.movementType === 'MaterialIssue' ||
64-
this.parentdoc?.movementType === 'MaterialTransfer',
65-
toLocation: () =>
66-
this.parentdoc?.movementType === 'MaterialReceipt' ||
67-
this.parentdoc?.movementType === 'MaterialTransfer',
89+
fromLocation: () => this.isIssue || this.isTransfer,
90+
toLocation: () => this.isReceipt || this.isTransfer,
6891
};
6992

7093
readOnly: ReadOnlyMap = {
71-
fromLocation: () =>
72-
this.parentdoc?.movementType === MovementType.MaterialReceipt,
73-
toLocation: () =>
74-
this.parentdoc?.movementType === MovementType.MaterialIssue,
94+
fromLocation: () => this.isReceipt,
95+
toLocation: () => this.isIssue,
7596
};
7697

7798
static createFilters: FiltersMap = {
7899
item: () => ({ trackItem: true, itemType: 'Product' }),
79-
fromLocation: (doc: Doc) => ({ item: (doc.item ?? '') as string }),
80-
toLocation: (doc: Doc) => ({ item: (doc.item ?? '') as string }),
81100
};
82101
}

models/inventory/StockTransferItem.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Doc } from 'fyo/model/doc';
22
import { FiltersMap, FormulaMap } from 'fyo/model/types';
33
import { ModelNameEnum } from 'models/types';
44
import { Money } from 'pesa';
5-
import { locationFilter } from './helpers';
65

76
export class StockTransferItem extends Doc {
87
item?: string;
@@ -94,6 +93,20 @@ export class StockTransferItem extends Doc {
9493
},
9594
dependsOn: ['item'],
9695
},
96+
location: {
97+
formula: () => {
98+
if (this.location) {
99+
return;
100+
}
101+
102+
const defaultLocation = this.fyo.singles.InventorySettings
103+
?.defaultLocation as string | undefined;
104+
105+
if (defaultLocation && !this.location) {
106+
return defaultLocation;
107+
}
108+
},
109+
},
97110
};
98111

99112
static filters: FiltersMap = {
@@ -105,6 +118,5 @@ export class StockTransferItem extends Doc {
105118

106119
return { for: ['not in', [itemNotFor]], trackItem: true };
107120
},
108-
location: locationFilter,
109121
};
110122
}

models/inventory/helpers.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1 @@
1-
import { Doc } from "fyo/model/doc";
2-
import { FilterFunction } from "fyo/model/types";
3-
import { QueryFilter } from "utils/db/types";
41

5-
export const locationFilter: FilterFunction = (doc: Doc) => {
6-
const item = doc.item;
7-
if (!doc.item) {
8-
return { item: null };
9-
}
10-
11-
return { item: ['in', [null, item]] } as QueryFilter;
12-
};

schemas/app/inventory/InventorySettings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
"default": "FIFO",
2222
"required": true
2323
},
24+
{
25+
"fieldname": "defaultLocation",
26+
"label": "Default Location",
27+
"fieldtype": "Link",
28+
"target": "Location",
29+
"create": true
30+
},
2431
{
2532
"fieldname": "stockInHand",
2633
"label": "Stock In Hand Acc.",

schemas/app/inventory/Location.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
"required": true
1313
},
1414
{
15-
"fieldname": "item",
16-
"label": "Item",
15+
"fieldname": "address",
16+
"label": "Address",
1717
"fieldtype": "Link",
18-
"target": "Item"
18+
"target": "Address",
19+
"placeholder": "Click to create",
20+
"inline": true
1921
}
2022
],
21-
"quickEditFields": ["item"]
23+
"quickEditFields": ["item", "address"]
2224
}

src/setup/setupInstance.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
DEFAULT_LOCALE,
88
DEFAULT_SERIES_START,
99
} from 'fyo/utils/consts';
10-
import { ValueError } from 'fyo/utils/errors';
1110
import {
1211
AccountRootTypeEnum,
1312
AccountTypeEnum,
@@ -23,7 +22,7 @@ import {
2322
setCurrencySymbols,
2423
} from 'src/utils/initialization';
2524
import { getRandomString } from 'utils';
26-
import { defaultUOMs } from 'utils/defaults';
25+
import { getDefaultLocations, getDefaultUOMs } from 'utils/defaults';
2726
import { getCountryCodeFromCountry, getCountryInfo } from 'utils/misc';
2827
import { CountryInfo } from 'utils/types';
2928
import { CreateCOA } from './createCOA';
@@ -62,9 +61,13 @@ async function createDefaultEntries(fyo: Fyo) {
6261
/**
6362
* Create default UOM entries
6463
*/
65-
for (const uom of defaultUOMs) {
64+
for (const uom of getDefaultUOMs(fyo)) {
6665
await checkAndCreateDoc(ModelNameEnum.UOM, uom, fyo);
6766
}
67+
68+
for (const loc of getDefaultLocations(fyo)) {
69+
await checkAndCreateDoc(ModelNameEnum.Location, loc, fyo);
70+
}
6871
}
6972

7073
async function initializeDatabase(dbPath: string, country: string, fyo: Fyo) {
@@ -368,5 +371,10 @@ async function updateInventorySettings(fyo: Fyo) {
368371
inventorySettings.set(settingName, accounts[0].name);
369372
}
370373

374+
const location = fyo.t`Stores`;
375+
if (await fyo.db.exists(ModelNameEnum.Location, location)) {
376+
inventorySettings.set('defaultLocation', location);
377+
}
378+
371379
await inventorySettings.sync();
372380
}

utils/defaults.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
export const defaultUOMs = [
2-
{
3-
name: 'Unit',
4-
isWhole: true,
5-
},
6-
{
7-
name: 'Kg',
8-
isWhole: false,
9-
},
10-
{
11-
name: 'Gram',
12-
isWhole: false,
13-
},
14-
{
15-
name: 'Meter',
16-
isWhole: false,
17-
},
18-
{
19-
name: 'Hour',
20-
isWhole: false,
21-
},
22-
{
23-
name: 'Day',
24-
isWhole: false,
25-
},
26-
];
1+
import { Fyo } from 'fyo';
2+
3+
export function getDefaultUOMs(fyo: Fyo) {
4+
return [
5+
{
6+
name: fyo.t`Unit`,
7+
isWhole: true,
8+
},
9+
{
10+
name: fyo.t`Kg`,
11+
isWhole: false,
12+
},
13+
{
14+
name: fyo.t`Gram`,
15+
isWhole: false,
16+
},
17+
{
18+
name: fyo.t`Meter`,
19+
isWhole: false,
20+
},
21+
{
22+
name: fyo.t`Hour`,
23+
isWhole: false,
24+
},
25+
{
26+
name: fyo.t`Day`,
27+
isWhole: false,
28+
},
29+
];
30+
}
31+
32+
export function getDefaultLocations(fyo: Fyo) {
33+
return [{ name: fyo.t`Stores` }];
34+
}

0 commit comments

Comments
 (0)