2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00
books/models/inventory/StockMovementItem.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-02-15 09:09:56 +00:00
import { t } from 'fyo';
import { DocValue } from 'fyo/core/types';
import { Doc } from 'fyo/model/doc';
import {
FiltersMap,
FormulaMap,
ReadOnlyMap,
2022-11-21 07:15:57 +00:00
RequiredMap,
ValidationMap,
} from 'fyo/model/types';
import { ValidationError } from 'fyo/utils/errors';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
2023-02-15 09:09:56 +00:00
import { safeParseFloat } from 'utils/index';
import { StockMovement } from './StockMovement';
import { MovementType } from './types';
export class StockMovementItem extends Doc {
name?: string;
item?: string;
fromLocation?: string;
toLocation?: string;
quantity?: number;
2023-02-15 09:09:56 +00:00
transferQty?: number;
stockUOM?: string;
uom?: string;
UOMConversionFactor?: number;
rate?: Money;
amount?: Money;
parentdoc?: StockMovement;
2022-11-21 07:15:57 +00:00
get isIssue() {
return this.parentdoc?.movementType === MovementType.MaterialIssue;
}
get isReceipt() {
return this.parentdoc?.movementType === MovementType.MaterialReceipt;
}
get isTransfer() {
return this.parentdoc?.movementType === MovementType.MaterialTransfer;
}
get isManufacture() {
return this.parentdoc?.movementType === MovementType.Manufacture;
}
static filters: FiltersMap = {
item: () => ({ trackItem: true }),
};
formulas: FormulaMap = {
rate: {
formula: async () => {
if (!this.item) {
return this.rate;
}
return await this.fyo.getValue(ModelNameEnum.Item, this.item, 'rate');
},
dependsOn: ['item'],
},
amount: {
formula: () => this.rate!.mul(this.quantity!),
dependsOn: ['item', 'rate', 'quantity'],
},
fromLocation: {
formula: () => {
if (this.isReceipt || this.isTransfer || this.isManufacture) {
return null;
}
2022-11-21 07:15:57 +00:00
const defaultLocation = this.fyo.singles.InventorySettings
?.defaultLocation as string | undefined;
if (defaultLocation && !this.fromLocation && this.isIssue) {
2022-11-21 07:15:57 +00:00
return defaultLocation;
}
return this.toLocation;
},
2022-11-21 07:15:57 +00:00
dependsOn: ['movementType'],
},
toLocation: {
formula: () => {
if (this.isIssue || this.isTransfer || this.isManufacture) {
return null;
}
2022-11-21 07:15:57 +00:00
const defaultLocation = this.fyo.singles.InventorySettings
?.defaultLocation as string | undefined;
if (defaultLocation && !this.toLocation && this.isReceipt) {
2022-11-21 07:15:57 +00:00
return defaultLocation;
}
return this.toLocation;
},
2022-11-21 07:15:57 +00:00
dependsOn: ['movementType'],
},
2023-02-15 09:09:56 +00:00
stockUOM: {
formula: async () => {
const { unit } = await this.fyo.db.get(
ModelNameEnum.Item,
this.item!,
'unit'
);
return unit;
},
dependsOn: ['item'],
},
UOMConversionFactor: {
formula: async () => {
const conversionFactor = await this.fyo.db.getAll(
ModelNameEnum.UOMConversionFactor,
{
fields: ['value'],
filters: { parent: this.item! },
}
);
return safeParseFloat(conversionFactor[0].value);
},
dependsOn: ['uom'],
},
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('uom');
if (unitDoc?.isWhole) {
return Math.round(this.transferQty! * this.UOMConversionFactor!);
}
return safeParseFloat(this.transferQty! * this.UOMConversionFactor!);
},
dependsOn: ['quantity', 'transferQty', 'UOMConversionFactor'],
},
};
validations: ValidationMap = {
fromLocation: (value) => {
if (!this.isManufacture) {
return;
}
if (value && this.toLocation) {
throw new ValidationError(
this.fyo.t`Only From or To can be set for Manufacture`
);
}
},
toLocation: (value) => {
if (!this.isManufacture) {
return;
}
if (value && this.fromLocation) {
throw new ValidationError(
this.fyo.t`Only From or To can be set for Manufacture`
);
}
},
2023-02-15 09:09:56 +00:00
uom: async (value: DocValue) => {
const item = await this.fyo.db.getAll(ModelNameEnum.UOMConversionFactor, {
fields: ['parent'],
filters: { uom: value as string, parent: this.item! },
});
if (item.length < 1)
throw new ValidationError(
t`UOM ${value as string} is not applicable for item ${this.item!}`
);
},
};
required: RequiredMap = {
2022-11-21 07:15:57 +00:00
fromLocation: () => this.isIssue || this.isTransfer,
toLocation: () => this.isReceipt || this.isTransfer,
};
readOnly: ReadOnlyMap = {
2022-11-21 07:15:57 +00:00
fromLocation: () => this.isReceipt,
toLocation: () => this.isIssue,
};
static createFilters: FiltersMap = {
item: () => ({ trackItem: true, itemType: 'Product' }),
};
}