1111)
1212from django .utils .translation import gettext_lazy as _
1313
14- from django_ledger .io import GROUP_EXPENSES , GROUP_INCOME
14+ from django_ledger .io import GROUP_EXPENSES , GROUP_INCOME , GROUP_TRANSFERS , GROUP_DEBT_PAYMENT
1515from django_ledger .models import (
1616 StagedTransactionModel ,
1717 ImportJobModel ,
@@ -24,9 +24,7 @@ class ImportJobModelCreateForm(ModelForm):
2424 def __init__ (self , entity_model : EntityModel , * args , ** kwargs ):
2525 super ().__init__ (* args , ** kwargs )
2626 self .ENTITY_MODEL : EntityModel = entity_model
27- self .fields [
28- 'bank_account_model'
29- ].queryset = self .ENTITY_MODEL .bankaccountmodel_set .all ().active ()
27+ self .fields ['bank_account_model' ].queryset = self .ENTITY_MODEL .bankaccountmodel_set .all ().active ()
3028
3129 ofx_file = forms .FileField (
3230 label = 'Select File...' ,
@@ -54,19 +52,15 @@ class Meta:
5452 ),
5553 }
5654 help_texts = {
57- 'bank_account_model' : _ (
58- 'Select the bank account to import transactions from.'
59- ),
55+ 'bank_account_model' : _ ('Select the bank account to import transactions from.' ),
6056 }
6157
6258
6359class ImportJobModelUpdateForm (ModelForm ):
6460 class Meta :
6561 model = ImportJobModel
6662 fields = ['description' ]
67- widgets = {
68- 'description' : TextInput (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES })
69- }
63+ widgets = {'description' : TextInput (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES })}
7064
7165
7266class StagedTransactionModelForm (ModelForm ):
@@ -75,59 +69,49 @@ class StagedTransactionModelForm(ModelForm):
7569
7670 def __init__ (self , base_formset_instance , * args , ** kwargs ):
7771 super ().__init__ (* args , ** kwargs )
78- self .BASE_FORMSET_INSTANCE : 'BaseStagedTransactionModelFormSet' = (
79- base_formset_instance
80- )
72+ self .BASE_FORMSET_INSTANCE : 'BaseStagedTransactionModelFormSet' = base_formset_instance
8173 self .VENDOR_CHOICES = self .BASE_FORMSET_INSTANCE .VENDOR_CHOICES
8274 self .VENDOR_MAP = self .BASE_FORMSET_INSTANCE .VENDOR_MAP
8375
8476 self .CUSTOMER_CHOICES = self .BASE_FORMSET_INSTANCE .CUSTOMER_CHOICES
8577 self .CUSTOMER_MAP = self .BASE_FORMSET_INSTANCE .CUSTOMER_MAP
8678
87- self .EXPENSE_ACCOUNT_CHOICES = (
88- self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_EXPENSES_CHOICES
89- )
90- self .SALES_ACCOUNT_CHOICES = (
91- self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_SALES_CHOICES
92- )
93- self .SHOW_VENDOR_FIELD : bool = False
94- self .SHOW_CUSTOMER_FIELD : bool = False
79+ self .EXPENSE_ACCOUNT_CHOICES = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_EXPENSES_CHOICES
80+ self .SALES_ACCOUNT_CHOICES = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_SALES_CHOICES
81+ self .TRANSFER_ACCOUNT_CHOICES = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_TRANSFERS_CHOICES
82+ self .CC_PAYMENT_ACCOUNT_CHOICES = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_TRANSFERS_CC_PAYMENT
9583
9684 staged_tx_model : StagedTransactionModel = getattr (self , 'instance' , None )
9785
98- if staged_tx_model .can_migrate_receipt ():
99- if staged_tx_model .is_expense ():
100- self .fields ['customer_model' ].widget = forms .HiddenInput ()
101- self .fields ['vendor_model' ].choices = self .VENDOR_CHOICES
102- self .fields ['account_model' ].choices = self .EXPENSE_ACCOUNT_CHOICES
103- self .SHOW_VENDOR_FIELD = True
104- elif staged_tx_model .is_sales ():
105- self .fields ['vendor_model' ].widget = forms .HiddenInput ()
106- self .fields ['customer_model' ].choices = self .CUSTOMER_CHOICES
107- self .fields ['account_model' ].choices = self .SALES_ACCOUNT_CHOICES
108- self .SHOW_CUSTOMER_FIELD = True
109- else :
110- self .fields ['customer_model' ].widget = forms .HiddenInput ()
111- self .fields ['vendor_model' ].widget = forms .HiddenInput ()
112- # avoids multiple DB queries rendering the formset...
113- self .fields [
114- 'account_model'
115- ].choices = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_CHOICES
86+ self .fields ['vendor_model' ].choices = self .VENDOR_CHOICES
87+ self .fields ['customer_model' ].choices = self .CUSTOMER_CHOICES
88+ self .fields ['account_model' ].choices = self .BASE_FORMSET_INSTANCE .ACCOUNT_MODEL_CHOICES
11689
11790 # avoids multiple DB queries rendering the formset...
118- self .fields [
119- 'unit_model'
120- ].choices = self .BASE_FORMSET_INSTANCE .UNIT_MODEL_CHOICES
91+ self .fields ['unit_model' ].choices = self .BASE_FORMSET_INSTANCE .UNIT_MODEL_CHOICES
12192
12293 if staged_tx_model :
123- if not staged_tx_model .is_children ():
94+ if staged_tx_model .is_sales ():
95+ self .fields ['account_model' ].choices = self .SALES_ACCOUNT_CHOICES
96+
97+ if staged_tx_model .is_expense ():
98+ self .fields ['account_model' ].choices = self .EXPENSE_ACCOUNT_CHOICES
99+
100+ if staged_tx_model .is_transfer ():
101+ self .fields ['account_model' ].choices = self .TRANSFER_ACCOUNT_CHOICES
102+
103+ if staged_tx_model .is_debt_payment ():
104+ self .fields ['account_model' ].choices = self .CC_PAYMENT_ACCOUNT_CHOICES
105+
106+ if not staged_tx_model .can_have_amount_split ():
124107 self .fields ['amount_split' ].widget = HiddenInput ()
125108 self .fields ['amount_split' ].disabled = True
126- else :
109+
110+ if not staged_tx_model .can_have_bundle_split ():
127111 self .fields ['bundle_split' ].widget = HiddenInput ()
128112 self .fields ['bundle_split' ].disabled = True
129113
130- if staged_tx_model .has_children ():
114+ if not staged_tx_model .can_have_receipt ():
131115 self .fields ['receipt_type' ].widget = HiddenInput ()
132116 self .fields ['receipt_type' ].disabled = True
133117
@@ -139,29 +123,26 @@ def __init__(self, base_formset_instance, *args, **kwargs):
139123 self .fields ['unit_model' ].widget = HiddenInput ()
140124 self .fields ['unit_model' ].disabled = True
141125
142- # receipt-related field visibility: not allowed for children rows
143- if staged_tx_model .is_children ():
144- for f in [
145- 'receipt_type' ,
146- 'vendor_model' ,
147- 'customer_model' ,
148- 'bundle_split' ,
149- ]:
150- self .fields [f ].widget = HiddenInput ()
151- self .fields [f ].disabled = True
152-
153- if staged_tx_model .is_single ():
154- self .fields ['bundle_split' ].widget = HiddenInput ()
155- self .fields ['bundle_split' ].disabled = True
126+ if not staged_tx_model .can_have_vendor ():
127+ self .fields ['vendor_model' ].widget = HiddenInput ()
128+ self .fields ['vendor_model' ].disabled = True
156129
157- if not staged_tx_model .can_migrate ():
130+ if not staged_tx_model .can_have_customer ():
131+ self .fields ['customer_model' ].widget = HiddenInput ()
132+ self .fields ['customer_model' ].disabled = True
133+
134+ if not staged_tx_model .can_import ():
158135 self .fields ['tx_import' ].widget = HiddenInput ()
159136 self .fields ['tx_import' ].disabled = True
160137
161138 if not staged_tx_model .can_split ():
162139 self .fields ['tx_split' ].widget = HiddenInput ()
163140 self .fields ['tx_split' ].disabled = True
164141
142+ if not staged_tx_model .can_unbundle ():
143+ # self.fields['bundle_split'].widget = HiddenInput()
144+ self .fields ['bundle_split' ].disabled = True
145+
165146 def clean_account_model (self ):
166147 staged_txs_model : StagedTransactionModel = self .instance
167148 if staged_txs_model .has_children ():
@@ -186,20 +167,6 @@ def clean(self):
186167 if self .cleaned_data ['tx_import' ] and self .cleaned_data ['tx_split' ]:
187168 raise ValidationError (message = _ ('Cannot import and split at the same time' ))
188169
189- staged_txs_model : StagedTransactionModel = self .instance
190- if all (
191- [
192- staged_txs_model .has_children (),
193- staged_txs_model .has_receipt (),
194- not staged_txs_model .bundle_split ,
195- ]
196- ):
197- raise ValidationError (
198- message = _ (
199- 'Receipt transactions cannot be split into multiple receipts.'
200- )
201- )
202-
203170 class Meta :
204171 model = StagedTransactionModel
205172 fields = [
@@ -223,18 +190,10 @@ class Meta:
223190 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' ,
224191 }
225192 ),
226- 'amount_split' : NumberInput (
227- attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }
228- ),
229- 'vendor_model' : Select (
230- attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }
231- ),
232- 'customer_model' : Select (
233- attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }
234- ),
235- 'receipt_type' : Select (
236- attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }
237- ),
193+ 'amount_split' : NumberInput (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }),
194+ 'vendor_model' : Select (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }),
195+ 'customer_model' : Select (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }),
196+ 'receipt_type' : Select (attrs = {'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES + ' is-small' }),
238197 }
239198
240199
@@ -250,52 +209,46 @@ def __init__(
250209
251210 # validates that the job import model belongs to the entity model...
252211 if import_job_model .entity_uuid != entity_model .uuid :
253- raise ValidationError (
254- message = _ ('Import job does not belong to this entity' )
255- )
212+ raise ValidationError (message = _ ('Import job does not belong to this entity' ))
256213
257214 self .ENTITY_MODEL = entity_model
258215 self .IMPORT_JOB_MODEL : ImportJobModel = import_job_model
259216
260217 self .queryset = (
261- self .IMPORT_JOB_MODEL .stagedtransactionmodel_set .all ().is_pending ()
218+ StagedTransactionModel .objects .for_entity (entity_model = entity_model )
219+ .for_import_job (import_job_model = import_job_model )
220+ .is_pending ()
262221 )
263222
264- self .MAPPED_ACCOUNT_MODEL = (
265- self .IMPORT_JOB_MODEL .bank_account_model .account_model
266- )
223+ self .MAPPED_ACCOUNT_MODEL = self .IMPORT_JOB_MODEL .bank_account_model .account_model
267224
268225 self .account_model_qs = (
269- entity_model .get_coa_accounts ()
270- .available ()
271- .exclude (uuid__exact = self .MAPPED_ACCOUNT_MODEL .uuid )
226+ entity_model .get_coa_accounts ().available ().exclude (uuid__exact = self .MAPPED_ACCOUNT_MODEL .uuid )
272227 )
273- self .ACCOUNT_MODEL_CHOICES = [(None , '----' )] + [
274- (a .uuid , a ) for a in self .account_model_qs
275- ]
228+ self .ACCOUNT_MODEL_CHOICES = [(None , '----' )] + [(a .uuid , a ) for a in self .account_model_qs ]
276229 self .ACCOUNT_MODEL_EXPENSES_CHOICES = [(None , '----' )] + [
277230 (a .uuid , a ) for a in self .account_model_qs if a .role in GROUP_EXPENSES
278231 ]
279232 self .ACCOUNT_MODEL_SALES_CHOICES = [(None , '----' )] + [
280233 (a .uuid , a ) for a in self .account_model_qs if a .role in GROUP_INCOME
281234 ]
282235
283- self .unit_model_qs = entity_model .entityunitmodel_set .all ()
284- self .UNIT_MODEL_CHOICES = [(None , '----' )] + [
285- (u .uuid , u ) for i , u in enumerate (self .unit_model_qs )
236+ self .ACCOUNT_MODEL_TRANSFERS_CHOICES = [(None , '----' )] + [
237+ (a .uuid , a ) for a in self .account_model_qs if a .role in GROUP_TRANSFERS
238+ ]
239+
240+ self .ACCOUNT_MODEL_TRANSFERS_CC_PAYMENT = [(None , '----' )] + [
241+ (a .uuid , a ) for a in self .account_model_qs if a .role in GROUP_DEBT_PAYMENT
286242 ]
287243
244+ self .unit_model_qs = entity_model .entityunitmodel_set .all ()
245+ self .UNIT_MODEL_CHOICES = [(None , '----' )] + [(u .uuid , u ) for i , u in enumerate (self .unit_model_qs )]
246+
288247 self .VENDOR_MODEL_QS = entity_model .vendormodel_set .visible ()
289- len (self .VENDOR_MODEL_QS )
290248 self .CUSTOMER_MODEL_QS = entity_model .customermodel_set .visible ()
291- len (self .CUSTOMER_MODEL_QS )
292249
293- self .VENDOR_CHOICES = [(None , '-----' )] + [
294- (str (v .uuid ), v ) for v in self .VENDOR_MODEL_QS
295- ]
296- self .CUSTOMER_CHOICES = [(None , '-----' )] + [
297- (str (c .uuid ), c ) for c in self .CUSTOMER_MODEL_QS
298- ]
250+ self .VENDOR_CHOICES = [(None , '-----' )] + [(str (v .uuid ), v ) for v in self .VENDOR_MODEL_QS ]
251+ self .CUSTOMER_CHOICES = [(None , '-----' )] + [(str (c .uuid ), c ) for c in self .CUSTOMER_MODEL_QS ]
299252
300253 self .VENDOR_MAP = dict (self .VENDOR_CHOICES )
301254 self .CUSTOMER_MAP = dict (self .CUSTOMER_CHOICES )
0 commit comments