11from django .forms import ModelForm , TextInput , Select , HiddenInput
22
3- from django_ledger .io .roles import INCOME_SALES
3+ from django_ledger .io .roles import GROUP_INCOME , GROUP_EXPENSES
44from django_ledger .models import AccountModel , ItemModel , UnitOfMeasureModel
55from django_ledger .settings import DJANGO_LEDGER_FORM_INPUT_CLASSES
66
@@ -33,30 +33,24 @@ class UnitOfMeasureModelUpdateForm(UnitOfMeasureModelCreateForm):
3333 pass
3434
3535
36- class ProductOrServiceCreateForm (ModelForm ):
37- # PRODUCT_ACCOUNT_ROLES: list = [COGS, ASSET_CA_INVENTORY, INCOME_SALES]
36+ class ProductOrServiceUpdateForm (ModelForm ):
3837
3938 def __init__ (self , entity_slug : str , user_model , * args , ** kwargs ):
4039 self .ENTITY_SLUG = entity_slug
4140 self .USER_MODEL = user_model
4241 super ().__init__ (* args , ** kwargs )
4342
4443 accounts_qs = AccountModel .on_coa .with_roles (
45- roles = [ INCOME_SALES ] ,
44+ roles = GROUP_INCOME ,
4645 entity_slug = self .ENTITY_SLUG ,
4746 user_model = self .USER_MODEL )
47+ self .fields ['earnings_account' ].queryset = accounts_qs .filter (role__in = GROUP_INCOME )
4848
4949 uom_qs = UnitOfMeasureModel .objects .for_entity (
5050 entity_slug = self .ENTITY_SLUG ,
5151 user_model = self .USER_MODEL
5252 )
53-
54- self .fields ['earnings_account' ].queryset = accounts_qs .filter (role__iexact = INCOME_SALES )
5553 self .fields ['uom' ].queryset = uom_qs
56- self .fields ['is_product_or_service' ].initial = True
57-
58- def clean_is_product_or_service (self ):
59- return True
6054
6155 class Meta :
6256 model = ItemModel
@@ -68,7 +62,53 @@ class Meta:
6862 'uom' ,
6963 'default_amount' ,
7064 'earnings_account' ,
71- 'is_product_or_service' ,
65+ ]
66+ widgets = {
67+ 'name' : TextInput (attrs = {
68+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
69+ }),
70+ 'uom' : Select (attrs = {
71+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
72+ }),
73+ 'earnings_account' : Select (attrs = {
74+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
75+ }),
76+ 'sku' : TextInput (attrs = {
77+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
78+ }),
79+ 'upc' : TextInput (attrs = {
80+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
81+ }),
82+ 'item_id' : TextInput (attrs = {
83+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
84+ }),
85+ 'default_amount' : TextInput (attrs = {
86+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
87+ }),
88+ }
89+
90+
91+ class ProductOrServiceCreateForm (ProductOrServiceUpdateForm ):
92+
93+ def __init__ (self , entity_slug : str , user_model , * args , ** kwargs ):
94+ super ().__init__ (entity_slug = entity_slug , user_model = user_model , * args , ** kwargs )
95+ uom_qs = UnitOfMeasureModel .objects .for_entity_active (
96+ entity_slug = self .ENTITY_SLUG ,
97+ user_model = self .USER_MODEL
98+ )
99+ self .fields ['uom' ].queryset = uom_qs
100+ self .fields ['is_product_or_service' ].initial = True
101+
102+ class Meta (ProductOrServiceUpdateForm .Meta ):
103+ fields = [
104+ 'name' ,
105+ 'sku' ,
106+ 'upc' ,
107+ 'item_id' ,
108+ 'uom' ,
109+ 'default_amount' ,
110+ 'earnings_account' ,
111+ 'is_product_or_service'
72112 ]
73113 widgets = {
74114 'name' : TextInput (attrs = {
@@ -98,5 +138,63 @@ class Meta:
98138 }
99139
100140
101- class ProductOrServiceUpdateForm (ProductOrServiceCreateForm ):
102- pass
141+ class ExpenseItemUpdateForm (ModelForm ):
142+ def __init__ (self , entity_slug : str , user_model , * args , ** kwargs ):
143+ self .ENTITY_SLUG = entity_slug
144+ self .USER_MODEL = user_model
145+ super ().__init__ (* args , ** kwargs )
146+
147+ accounts_qs = AccountModel .on_coa .with_roles (
148+ roles = GROUP_EXPENSES ,
149+ entity_slug = self .ENTITY_SLUG ,
150+ user_model = self .USER_MODEL )
151+ self .fields ['expense_account' ].queryset = accounts_qs .filter (role__in = GROUP_EXPENSES )
152+
153+ uom_qs = UnitOfMeasureModel .objects .for_entity (
154+ entity_slug = self .ENTITY_SLUG ,
155+ user_model = self .USER_MODEL
156+ )
157+ self .fields ['uom' ].queryset = uom_qs
158+
159+ class Meta :
160+ model = ItemModel
161+ fields = [
162+ 'name' ,
163+ 'uom' ,
164+ 'default_amount' ,
165+ 'expense_account' ,
166+ ]
167+ widgets = {
168+ 'name' : TextInput (attrs = {
169+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
170+ }),
171+ 'uom' : Select (attrs = {
172+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
173+ }),
174+ 'expense_account' : Select (attrs = {
175+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
176+ }),
177+ 'sku' : TextInput (attrs = {
178+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
179+ }),
180+ 'upc' : TextInput (attrs = {
181+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
182+ }),
183+ 'item_id' : TextInput (attrs = {
184+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
185+ }),
186+ 'default_amount' : TextInput (attrs = {
187+ 'class' : DJANGO_LEDGER_FORM_INPUT_CLASSES
188+ }),
189+ }
190+
191+
192+ class ExpenseItemCreateForm (ExpenseItemUpdateForm ):
193+
194+ def __init__ (self , entity_slug : str , user_model , * args , ** kwargs ):
195+ super ().__init__ (entity_slug = entity_slug , user_model = user_model , * args , ** kwargs )
196+ uom_qs = UnitOfMeasureModel .objects .for_entity_active (
197+ entity_slug = self .ENTITY_SLUG ,
198+ user_model = self .USER_MODEL
199+ )
200+ self .fields ['uom' ].queryset = uom_qs
0 commit comments