2022-10-05 14:37:17 +00:00
|
|
|
import { Doc } from 'fyo/model/doc';
|
2022-10-06 08:50:13 +00:00
|
|
|
import {
|
|
|
|
FiltersMap,
|
|
|
|
FormulaMap,
|
2022-11-16 08:35:38 +00:00
|
|
|
ReadOnlyMap,
|
2022-11-21 07:15:57 +00:00
|
|
|
RequiredMap,
|
2023-01-31 08:06:03 +00:00
|
|
|
ValidationMap,
|
2022-10-06 08:50:13 +00:00
|
|
|
} from 'fyo/model/types';
|
2023-01-31 08:06:03 +00:00
|
|
|
import { ValidationError } from 'fyo/utils/errors';
|
2022-10-06 08:50:13 +00:00
|
|
|
import { ModelNameEnum } from 'models/types';
|
2022-10-05 14:37:17 +00:00
|
|
|
import { Money } from 'pesa';
|
2022-10-06 08:50:13 +00:00
|
|
|
import { StockMovement } from './StockMovement';
|
2022-11-16 08:35:38 +00:00
|
|
|
import { MovementType } from './types';
|
2022-10-05 14:37:17 +00:00
|
|
|
|
|
|
|
export class StockMovementItem extends Doc {
|
|
|
|
name?: string;
|
|
|
|
item?: string;
|
|
|
|
fromLocation?: string;
|
|
|
|
toLocation?: string;
|
|
|
|
quantity?: number;
|
|
|
|
rate?: Money;
|
|
|
|
amount?: Money;
|
2022-10-06 08:50:13 +00:00
|
|
|
parentdoc?: StockMovement;
|
2023-01-14 11:28:16 +00:00
|
|
|
batchNumber?: string;
|
2022-10-05 14:37:17 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-31 08:06:03 +00:00
|
|
|
get isManufacture() {
|
|
|
|
return this.parentdoc?.movementType === MovementType.Manufacture;
|
|
|
|
}
|
|
|
|
|
2022-10-05 14:37:17 +00:00
|
|
|
static filters: FiltersMap = {
|
|
|
|
item: () => ({ trackItem: true }),
|
|
|
|
};
|
2022-10-06 08:50:13 +00:00
|
|
|
|
|
|
|
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'],
|
|
|
|
},
|
2022-11-16 08:35:38 +00:00
|
|
|
fromLocation: {
|
2023-01-31 08:06:03 +00:00
|
|
|
formula: () => {
|
|
|
|
if (this.isReceipt || this.isTransfer || this.isManufacture) {
|
2022-11-16 08:35:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-11-21 07:15:57 +00:00
|
|
|
|
|
|
|
const defaultLocation = this.fyo.singles.InventorySettings
|
|
|
|
?.defaultLocation as string | undefined;
|
2023-01-31 08:06:03 +00:00
|
|
|
if (defaultLocation && !this.fromLocation && this.isIssue) {
|
2022-11-21 07:15:57 +00:00
|
|
|
return defaultLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.toLocation;
|
2022-11-16 08:35:38 +00:00
|
|
|
},
|
2022-11-21 07:15:57 +00:00
|
|
|
dependsOn: ['movementType'],
|
2022-11-16 08:35:38 +00:00
|
|
|
},
|
|
|
|
toLocation: {
|
2023-01-31 08:06:03 +00:00
|
|
|
formula: () => {
|
|
|
|
if (this.isIssue || this.isTransfer || this.isManufacture) {
|
2022-11-16 08:35:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-11-21 07:15:57 +00:00
|
|
|
|
|
|
|
const defaultLocation = this.fyo.singles.InventorySettings
|
|
|
|
?.defaultLocation as string | undefined;
|
2023-01-31 08:06:03 +00:00
|
|
|
if (defaultLocation && !this.toLocation && this.isReceipt) {
|
2022-11-21 07:15:57 +00:00
|
|
|
return defaultLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.toLocation;
|
2022-11-16 08:35:38 +00:00
|
|
|
},
|
2022-11-21 07:15:57 +00:00
|
|
|
dependsOn: ['movementType'],
|
2022-11-16 08:35:38 +00:00
|
|
|
},
|
2022-10-06 08:50:13 +00:00
|
|
|
};
|
|
|
|
|
2023-01-31 08:06:03 +00:00
|
|
|
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 Manucature`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-10-06 08:50:13 +00:00
|
|
|
required: RequiredMap = {
|
2022-11-21 07:15:57 +00:00
|
|
|
fromLocation: () => this.isIssue || this.isTransfer,
|
|
|
|
toLocation: () => this.isReceipt || this.isTransfer,
|
2022-10-06 08:50:13 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 08:35:38 +00:00
|
|
|
readOnly: ReadOnlyMap = {
|
2022-11-21 07:15:57 +00:00
|
|
|
fromLocation: () => this.isReceipt,
|
|
|
|
toLocation: () => this.isIssue,
|
2022-11-16 08:35:38 +00:00
|
|
|
};
|
|
|
|
|
2022-10-06 08:50:13 +00:00
|
|
|
static createFilters: FiltersMap = {
|
|
|
|
item: () => ({ trackItem: true, itemType: 'Product' }),
|
|
|
|
};
|
2022-10-05 14:37:17 +00:00
|
|
|
}
|