@@ -3,11 +3,10 @@ import {
33 FiltersMap ,
44 FormulaMap ,
55 ReadOnlyMap ,
6- RequiredMap
6+ RequiredMap ,
77} from 'fyo/model/types' ;
88import { ModelNameEnum } from 'models/types' ;
99import { Money } from 'pesa' ;
10- import { locationFilter } from './helpers' ;
1110import { StockMovement } from './StockMovement' ;
1211import { 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}
0 commit comments