@@ -72,7 +72,7 @@ def WorkOrderReceiptCreate(self, **params):
72
72
wo_id = params ["workOrderId" ]
73
73
input_json_str = params ["raw" ]
74
74
input_value = json .loads (input_json_str )
75
-
75
+ # Check if work order id exists
76
76
wo_request = self .kv_helper .get ("wo-requests" , wo_id )
77
77
if wo_request is None :
78
78
raise JSONRPCDispatchException (
@@ -82,12 +82,22 @@ def WorkOrderReceiptCreate(self, **params):
82
82
)
83
83
else :
84
84
wo_receipt = self .kv_helper .get ("wo-receipts" , wo_id )
85
- if wo_receipt is None :
85
+ wo_receipt = json .loads (wo_receipt )
86
+ if wo_receipt and "workOrderId" not in wo_receipt ["params" ]:
86
87
status , err_msg = \
87
88
self .__validate_work_order_receipt_create_req (
88
89
input_value , wo_request )
89
90
if status is True :
90
- self .kv_helper .set ("wo-receipts" , wo_id , input_json_str )
91
+ # If receiptUpdates doesn't exists then
92
+ # create an entry with empty array
93
+ if "receiptUpdates" not in wo_receipt ["params" ]:
94
+ input_value ["params" ]["receiptUpdates" ] = []
95
+ else :
96
+ input_value ["params" ]["receiptUpdates" ] = \
97
+ wo_receipt ["receiptUpdates" ]
98
+ self .kv_helper .set ("wo-receipts" , wo_id , json .dumps (
99
+ input_value
100
+ ))
91
101
raise JSONRPCDispatchException (
92
102
JRPCErrorCodes .SUCCESS ,
93
103
"Receipt created successfully"
@@ -186,11 +196,14 @@ def WorkOrderReceiptUpdate(self, **params):
186
196
status , err_msg = self .__validate_work_order_receipt_update_req (
187
197
input_value )
188
198
if status is True :
199
+ value = json .loads (value )
189
200
# Load previous updates to receipt
190
- updates_to_receipt = \
191
- self .kv_helper .get ("wo-receipt-updates" , wo_id )
201
+ if "receiptUpdates" in value ["params" ]:
202
+ updates_to_receipt = value ["params" ]["receiptUpdates" ]
203
+ else :
204
+ updates_to_receipt = []
192
205
# If it is first update to receipt
193
- if updates_to_receipt is None :
206
+ if len ( updates_to_receipt ) == 0 :
194
207
updated_receipt = []
195
208
else :
196
209
updated_receipt = json .loads (updates_to_receipt )
@@ -220,8 +233,9 @@ def WorkOrderReceiptUpdate(self, **params):
220
233
" is not allowed"
221
234
)
222
235
updated_receipt .append (input_value )
223
- self .kv_helper .set ("wo-receipt-updates" , wo_id ,
224
- json .dumps (updated_receipt ))
236
+ input_value ["receiptUpdates" ] = updated_receipt
237
+ self .kv_helper .set ("wo-receipts" , wo_id ,
238
+ json .dumps (input_value ))
225
239
raise JSONRPCDispatchException (
226
240
JRPCErrorCodes .SUCCESS ,
227
241
"Receipt updated successfully"
@@ -377,15 +391,19 @@ def WorkOrderReceiptRetrieve(self, **params):
377
391
value = self .kv_helper .get ("wo-receipts" , wo_id )
378
392
if value :
379
393
receipt = json .loads (value )
380
- receipt_updates = self .kv_helper .get ("wo-receipt-updates" , wo_id )
381
- if receipt_updates is None :
394
+ if "receiptUpdates" in receipt ["params" ]:
395
+ receipt_updates = receipt ["params" ]["receiptUpdates" ]
396
+ else :
397
+ receipt_updates = []
398
+ if len (receipt_updates ) == 0 :
399
+ # If there is no updates to receipt
400
+ # then current status is same as create status
382
401
receipt ["params" ]["receiptCurrentStatus" ] = \
383
402
receipt ["params" ]["receiptCreateStatus" ]
384
403
else :
385
- receipt_updates_json = json .loads (receipt_updates )
386
- # Get the recent update to receipt
387
- last_receipt = receipt_updates_json [len (receipt_updates_json )
388
- - 1 ]
404
+ # Get the latest update to receipt
405
+ last_receipt = receipt_updates [len (receipt_updates )
406
+ - 1 ]
389
407
receipt ["params" ]["receiptCurrentStatus" ] = \
390
408
last_receipt ["updateType" ]
391
409
return receipt ["params" ]
@@ -420,10 +438,12 @@ def WorkOrderReceiptUpdateRetrieve(self, **params):
420
438
# starts from 1
421
439
update_index = input_params ["updateIndex" ]
422
440
# Load list of updates to the receipt
423
- receipt_updates = self .kv_helper .get ("wo-receipt-updates " , wo_id )
441
+ receipt_entry = self .kv_helper .get ("wo-receipts " , wo_id )
424
442
425
- if receipt_updates :
426
- receipt_updates_json = json .loads (receipt_updates )
443
+ if receipt_entry :
444
+ receipt_entry_json = json .loads (receipt_entry )
445
+ receipt_updates_json = \
446
+ receipt_entry_json ["params" ]["receiptUpdates" ]
427
447
total_updates = len (receipt_updates_json )
428
448
if update_index <= 0 :
429
449
raise JSONRPCDispatchException (
0 commit comments