2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00
books/models/inventory/StockMovementItem.ts
18alantom a8b5884929 incr: wire up stock movement
- prevent erroneous setting of track item
- fix table UI
2022-11-07 13:28:35 +05:30

71 lines
1.8 KiB
TypeScript

import { Doc } from 'fyo/model/doc';
import {
FilterFunction,
FiltersMap,
FormulaMap,
RequiredMap
} from 'fyo/model/types';
import { ModelNameEnum } from 'models/types';
import { Money } from 'pesa';
import { QueryFilter } from 'utils/db/types';
import { StockMovement } from './StockMovement';
const locationFilter: FilterFunction = (doc: Doc) => {
const item = doc.item;
if (!doc.item) {
return {};
}
return { item } as QueryFilter;
};
export class StockMovementItem extends Doc {
name?: string;
item?: string;
fromLocation?: string;
toLocation?: string;
quantity?: number;
rate?: Money;
amount?: Money;
parentdoc?: StockMovement;
static filters: FiltersMap = {
item: () => ({ trackItem: true }),
toLocation: locationFilter,
fromLocation: locationFilter,
};
formulas: FormulaMap = {
rate: {
formula: async () => {
console.log('called', this.item);
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'],
},
};
required: RequiredMap = {
fromLocation: () =>
this.parentdoc?.movementType === 'MaterialIssue' ||
this.parentdoc?.movementType === 'MaterialTransfer',
toLocation: () =>
this.parentdoc?.movementType === 'MaterialReceipt' ||
this.parentdoc?.movementType === 'MaterialTransfer',
};
static createFilters: FiltersMap = {
item: () => ({ trackItem: true, itemType: 'Product' }),
fromLocation: (doc: Doc) => ({ item: (doc.item ?? '') as string }),
toLocation: (doc: Doc) => ({ item: (doc.item ?? '') as string }),
};
}