3
3
from enum import Enum
4
4
5
5
from flet import (
6
- ButtonStyle ,
7
6
Card ,
8
7
Column ,
9
8
Container ,
10
- ElevatedButton ,
11
9
Icon ,
12
10
IconButton ,
13
11
ListTile ,
26
24
from core import utils , views
27
25
from core .abstractions import DialogHandler , TView , TViewParams
28
26
from core .intent_result import IntentResult
29
- from core .models import (
30
- get_cycle_from_value ,
31
- get_cycle_values_as_list ,
32
- get_time_unit_from_value ,
33
- get_time_unit_values_as_list ,
34
- )
35
27
from res import colors , dimens , fonts , res_utils
36
28
37
29
from tuttle .model import Client , Contract , CONTRACT_DEFAULT_VAT_RATE
@@ -223,52 +215,7 @@ def __init__(
223
215
self .clients_map = {}
224
216
self .contacts_map = {}
225
217
self .available_currencies = []
226
- self .title = ""
227
218
self .client = None
228
- self .rate = ""
229
- self .currency = ""
230
- self .vat_rate = ""
231
- self .time_unit : TimeUnit = None
232
- self .unit_pw = ""
233
- self .volume = ""
234
- self .term_of_payment = ""
235
- self .billing_cycle : Cycle = None
236
-
237
- def on_title_changed (self , e ):
238
- """Called when the title of the contract is changed"""
239
- self .title = e .control .value
240
-
241
- def on_rate_changed (self , e ):
242
- """Called when the rate of the contract is changed"""
243
- self .rate = e .control .value
244
-
245
- def on_currency_changed (self , e ):
246
- """Called when the currency of the contract is changed"""
247
- self .currency = e .control .value
248
-
249
- def on_volume_changed (self , e ):
250
- """Called when the volume of the contract is changed"""
251
- self .volume = e .control .value
252
-
253
- def on_term_of_payment_changed (self , e ):
254
- """Called when the term of payment of the contract is changed"""
255
- self .term_of_payment = e .control .value
256
-
257
- def on_upw_changed (self , e ):
258
- """Called when the unit pw of the contract is changed"""
259
- self .unit_pw = e .control .value
260
-
261
- def on_vat_rate_changed (self , e ):
262
- """Called when the vat rate of the contract is changed"""
263
- self .vat_rate = e .control .value
264
-
265
- def on_unit_selected (self , e ):
266
- """Called when the unit of the contract is changed""" ""
267
- self .time_unit = get_time_unit_from_value (e .control .value )
268
-
269
- def on_billing_cycle_selected (self , e ):
270
- """Called when the billing cycle of the contract is changed"""
271
- self .billing_cycle = get_cycle_from_value (e .control .value )
272
219
273
220
def clear_ui_field_errors (self , e ):
274
221
"""Clears all the errors in the ui form fields"""
@@ -311,7 +258,7 @@ def load_contract_for_update(self):
311
258
if not result .was_intent_successful or not result .data :
312
259
self .show_snack (result .error_msg , is_error = True )
313
260
self .old_contract_if_editing = result .data
314
- self .display_with_contract_info ()
261
+ self .display_contract_info ()
315
262
316
263
def load_currencies (self ):
317
264
"""Loads the available currencies into a dropdown"""
@@ -348,16 +295,14 @@ def get_client_dropdown_item(self, client_id):
348
295
if client_id not in self .clients_map :
349
296
return ""
350
297
# prefix client name with a key {client_id}
351
- return f"# { client_id } { self .clients_map [client_id ].name } "
298
+ return f"{ client_id } . { self .clients_map [client_id ].name } "
352
299
353
300
def on_client_selected (self , e ):
354
301
# parse selected value to extract id
355
302
selected = e .control .value
356
303
_id = ""
357
304
for c in selected :
358
- if c == "#" :
359
- continue
360
- if c == " " :
305
+ if c == "." :
361
306
break
362
307
_id = _id + c
363
308
@@ -403,9 +348,9 @@ def on_client_set_from_pop_up(self, client):
403
348
self .show_snack (result .error_msg , True )
404
349
self .update_self ()
405
350
406
- def display_with_contract_info (self ):
351
+ def display_contract_info (self ):
407
352
"""initialize form fields with data from old contract"""
408
- self .title_ui_field .value = self .title = self . old_contract_if_editing .title
353
+ self .title_ui_field .value = self .old_contract_if_editing .title
409
354
signature_date = self .old_contract_if_editing .signature_date
410
355
self .signature_date_ui_field .set_date (signature_date )
411
356
start_date = self .old_contract_if_editing .start_date
@@ -417,41 +362,81 @@ def display_with_contract_info(self):
417
362
self .clients_ui_field .update_value (
418
363
self .get_client_dropdown_item (self .client .id )
419
364
)
420
- self .rate_ui_field .value = self .rate = self .old_contract_if_editing .rate
421
- self .currency = self .old_contract_if_editing .currency
422
- self .currency_ui_field .update_value (self .currency )
423
- self .vat_rate_ui_field .value = (
424
- self .vat_rate
425
- ) = self .old_contract_if_editing .VAT_rate
426
-
427
- self .time_unit = self .old_contract_if_editing .unit
428
- if self .time_unit :
429
- self .units_ui_field .update_value (self .time_unit .value )
430
- self .unit_PW_ui_field .value = (
431
- self .unit_pw
432
- ) = self .old_contract_if_editing .units_per_workday
433
- self .volume_ui_field .value = self .volume = self .old_contract_if_editing .volume
365
+ self .rate_ui_field .value = self .old_contract_if_editing .rate
366
+ self .currency_ui_field .update_value (self .old_contract_if_editing .currency )
367
+ self .vat_rate_ui_field .value = self .old_contract_if_editing .VAT_rate
368
+ if self .old_contract_if_editing .unit :
369
+ self .time_unit_field .update_value (self .old_contract_if_editing .unit .name )
370
+ self .unit_PW_ui_field .value = self .old_contract_if_editing .units_per_workday
371
+ self .volume_ui_field .value = self .old_contract_if_editing .volume
434
372
self .term_of_payment_ui_field .value = (
435
- self .term_of_payment
436
- ) = self .old_contract_if_editing .term_of_payment
437
- self .billing_cycle = self .old_contract_if_editing .billing_cycle
438
- if self .billing_cycle :
439
- self .billing_cycle_ui_field .update_value (self .billing_cycle .value )
373
+ self .old_contract_if_editing .term_of_payment
374
+ )
375
+ if self .old_contract_if_editing .billing_cycle :
376
+ self .billing_cycle_ui_field .update_value (
377
+ self .old_contract_if_editing .billing_cycle .name
378
+ )
440
379
self .form_title_ui_field .value = "Edit Contract"
441
380
self .submit_btn .text = "Save changes"
442
381
443
382
def on_save (self , e ):
444
383
"""Called when the edit / save button is clicked"""
445
- if not self .title :
384
+ # get data from form fields
385
+ title = self .title_ui_field .value
386
+ rate = self .rate_ui_field .value
387
+ vat_rate = self .vat_rate_ui_field .value
388
+ unit_pw = self .unit_PW_ui_field .value
389
+ volume = self .volume_ui_field .value
390
+ term_of_payment = self .term_of_payment_ui_field .value
391
+ currency = self .currency_ui_field .value
392
+ time_unit_str = self .time_unit_field .value
393
+ try :
394
+ time_unit = TimeUnit [time_unit_str ]
395
+ except KeyError :
396
+ time_unit = None
397
+
398
+ billing_cycle_str = self .billing_cycle_ui_field .value
399
+ try :
400
+ billing_cycle = Cycle [billing_cycle_str ]
401
+ except KeyError :
402
+ billing_cycle = None
403
+
404
+ # check for missing fields
405
+ if not title :
446
406
self .title_ui_field .error_text = "Contract title is required"
447
407
self .update_self ()
448
408
return # error occurred, stop here
449
409
410
+ if not currency :
411
+ self .currency_ui_field .update_error_txt ("Please specify the currency" )
412
+ self .update_self ()
413
+ return
414
+
415
+ if not rate :
416
+ self .rate_ui_field .error_text = "Rate of enumeration is required"
417
+ self .update_self ()
418
+ return
419
+
420
+ if not time_unit :
421
+ self .time_unit_field .update_error_txt ("Unit of time tracked is required" )
422
+ self .update_self ()
423
+ return
424
+
425
+ if not unit_pw :
426
+ self .unit_PW_ui_field .error_text = "Units per workday is required"
427
+ self .update_self ()
428
+ return
429
+
450
430
if self .client is None :
451
431
self .clients_ui_field .update_error_txt ("Please select a client" )
452
432
self .update_self ()
453
433
return # error occurred, stop here
454
434
435
+ if not billing_cycle :
436
+ self .billing_cycle_ui_field .update_error_txt ("Billing cycle is required" )
437
+ self .update_self ()
438
+ return
439
+
455
440
signatureDate = self .signature_date_ui_field .get_date ()
456
441
if signatureDate is None :
457
442
self .show_snack ("Please specify the signature date" , True )
@@ -473,24 +458,26 @@ def on_save(self, e):
473
458
)
474
459
return # error occurred, stop here
475
460
476
- if not self .vat_rate :
477
- self .vat_rate = CONTRACT_DEFAULT_VAT_RATE
461
+ vat_rate = self .vat_rate_ui_field .value
462
+ if not vat_rate :
463
+ vat_rate = CONTRACT_DEFAULT_VAT_RATE
478
464
479
465
self .toggle_progress (is_on_going_action = True )
466
+
480
467
result : IntentResult = self .intent .save_contract (
481
- title = self . title ,
468
+ title = title ,
482
469
signature_date = signatureDate ,
483
470
start_date = startDate ,
484
471
end_date = endDate ,
485
472
client = self .client ,
486
- rate = self . rate ,
487
- currency = self . currency ,
488
- VAT_rate = self . vat_rate ,
489
- unit = self . time_unit ,
490
- units_per_workday = self . unit_pw ,
491
- volume = self . volume ,
492
- term_of_payment = self . term_of_payment ,
493
- billing_cycle = self . billing_cycle ,
473
+ rate = rate ,
474
+ currency = currency ,
475
+ VAT_rate = vat_rate ,
476
+ unit = time_unit ,
477
+ units_per_workday = unit_pw ,
478
+ volume = volume ,
479
+ term_of_payment = term_of_payment ,
480
+ billing_cycle = billing_cycle ,
494
481
contract = self .old_contract_if_editing ,
495
482
)
496
483
success_msg = (
@@ -511,47 +498,40 @@ def build(self):
511
498
self .title_ui_field = views .TTextField (
512
499
label = "Title" ,
513
500
hint = "Short description of the contract." ,
514
- on_change = self .on_title_changed ,
515
501
on_focus = self .clear_ui_field_errors ,
516
502
)
517
503
self .rate_ui_field = views .TTextField (
518
504
label = "Rate" ,
519
505
hint = "Rate of remuneration" ,
520
- on_change = self .on_rate_changed ,
521
506
on_focus = self .clear_ui_field_errors ,
522
507
keyboard_type = utils .KEYBOARD_NUMBER ,
523
508
)
524
509
self .currency_ui_field = views .TDropDown (
525
510
label = "Currency" ,
526
511
hint = "Payment currency" ,
527
- on_change = self .on_currency_changed ,
528
512
items = self .available_currencies ,
529
513
)
530
514
self .vat_rate_ui_field = views .TTextField (
531
515
label = "VAT rate" ,
532
516
hint = f"VAT rate applied to the contractual rate. default is { CONTRACT_DEFAULT_VAT_RATE } " ,
533
- on_change = self .on_vat_rate_changed ,
534
517
on_focus = self .clear_ui_field_errors ,
535
518
keyboard_type = utils .KEYBOARD_NUMBER ,
536
519
)
537
520
self .unit_PW_ui_field = views .TTextField (
538
521
label = "Units per workday" ,
539
522
hint = "How many units (e.g. hours) constitute a whole work day?" ,
540
- on_change = self .on_upw_changed ,
541
523
on_focus = self .clear_ui_field_errors ,
542
524
keyboard_type = utils .KEYBOARD_NUMBER ,
543
525
)
544
526
self .volume_ui_field = views .TTextField (
545
527
label = "Volume (optional)" ,
546
528
hint = "Number of time units agreed on" ,
547
- on_change = self .on_volume_changed ,
548
529
on_focus = self .clear_ui_field_errors ,
549
530
keyboard_type = utils .KEYBOARD_NUMBER ,
550
531
)
551
532
self .term_of_payment_ui_field = views .TTextField (
552
533
label = "Term of payment (optional)" ,
553
534
hint = "How many days after receipt of invoice this invoice is due." ,
554
- on_change = self .on_term_of_payment_changed ,
555
535
on_focus = self .clear_ui_field_errors ,
556
536
keyboard_type = utils .KEYBOARD_NUMBER ,
557
537
)
@@ -560,15 +540,13 @@ def build(self):
560
540
on_change = self .on_client_selected ,
561
541
items = self .get_clients_names_as_list (),
562
542
)
563
- self .units_ui_field = views .TDropDown (
543
+ self .time_unit_field = views .TDropDown (
564
544
label = "Unit of time tracked." ,
565
- on_change = self .on_unit_selected ,
566
- items = get_time_unit_values_as_list (),
545
+ items = [str (t ) for t in TimeUnit ],
567
546
)
568
547
self .billing_cycle_ui_field = views .TDropDown (
569
548
label = "Billing Cycle" ,
570
- on_change = self .on_billing_cycle_selected ,
571
- items = get_cycle_values_as_list (),
549
+ items = [str (c ) for c in Cycle ],
572
550
)
573
551
self .signature_date_ui_field = views .DateSelector (label = "Signed on" )
574
552
self .start_date_ui_field = views .DateSelector (label = "Valid from" )
@@ -594,7 +572,7 @@ def build(self):
594
572
self .currency_ui_field ,
595
573
self .rate_ui_field ,
596
574
self .term_of_payment_ui_field ,
597
- self .units_ui_field ,
575
+ self .time_unit_field ,
598
576
self .unit_PW_ui_field ,
599
577
self .vat_rate_ui_field ,
600
578
self .volume_ui_field ,
@@ -806,6 +784,9 @@ def display_contract_data(self):
806
784
_status = self .contract .get_status (default = "" )
807
785
if _status :
808
786
self .status_control .value = f"Status { _status } "
787
+ self .status_control .visible = True
788
+ else :
789
+ self .status_control .visible = False
809
790
self .billing_cycle_control .value = (
810
791
self .contract .billing_cycle .value if self .contract .billing_cycle else ""
811
792
)
0 commit comments