33from applications .iwdModuleV2 .models import *
44from applications .ps1 .models import *
55from decimal import Decimal
6+ import json
67class WorkOrderFormSerializer (serializers .ModelSerializer ):
78 class Meta :
89 model = WorkOrder
@@ -145,70 +146,31 @@ class Meta:
145146# model = LetterOfIntentDetails
146147# fields = '__all__'
147148
148- class ItemSerializer (serializers .ModelSerializer ):
149+ class ItemsSerializer (serializers .ModelSerializer ):
149150 class Meta :
150151 model = Item
151- fields = ['id' , 'name' , 'description' , 'unit' , 'price_per_unit' , 'quantity' , 'docs' , 'total_price' ]
152+ fields = ['name' , 'description' , 'unit' , 'price_per_unit' , 'quantity' , 'docs' , 'total_price' , 'id' ]
153+
154+
152155class CreateProposalSerializer (serializers .ModelSerializer ):
153- items = ItemSerializer (many = True )
156+ items = ItemsSerializer (many = True , write_only = True ) # Keep the many=True option
157+
154158 class Meta :
155159 model = Proposal
156160 fields = '__all__'
157161
158162 def create (self , validated_data ):
159- items_data = validated_data . pop ( 'items' , [])
160- proposal = Proposal . objects . create ( ** validated_data )
163+ # Pop the 'items' from the validated data
164+ items_data = validated_data . pop ( 'items' , [] )
161165
162- total_budget = Decimal (0 )
163- items = []
164-
165- for item_data in items_data :
166- try :
167- quantity = item_data ['quantity' ]
168- price_per_unit = item_data ['price_per_unit' ]
169- total_price = quantity * price_per_unit
170- total_budget += total_price
171- items .append (Item (proposal = proposal , total_price = total_price , ** item_data ))
172-
173- except (ValueError , TypeError ) as e :
174- print (f"Error processing item: { item_data } , Error: { e } " )
175- continue
176-
177- if items :
178- Item .objects .bulk_create (items )
166+ # Create the proposal instance with the validated data
167+ proposal = Proposal .objects .create (** validated_data )
179168
180- proposal .proposal_budget = total_budget
181169 proposal .save ()
182170
183171 return proposal
184172
185173class ProposalSerializer (serializers .ModelSerializer ):
186-
187174 class Meta :
188175 model = Proposal
189- fields = '__all__'
190-
191- def create (self , validated_data ):
192- items = ItemSerializer (many = True )
193- items_data = validated_data .pop ('items' , [])
194- proposal = Proposal .objects .create (** validated_data )
195-
196- total_budget = Decimal (0 )
197- items = []
198-
199- for item_data in items_data :
200- try :
201- quantity = item_data ['quantity' ]
202- price_per_unit = item_data ['price_per_unit' ]
203- total_price = quantity * price_per_unit
204- total_budget += total_price
205- items .append (Item (proposal = proposal , total_price = total_price , ** item_data ))
206- except (ValueError , TypeError ) as e :
207- print (f"Error processing item: { item_data } , Error: { e } " )
208- if items :
209- Item .objects .bulk_create (items )
210-
211- proposal .proposal_budget = total_budget
212- proposal .save ()
213-
214- return proposal
176+ fields = '__all__'
0 commit comments