ScanOrderScanModelViewController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // ScanOrderScanModelViewController.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/8/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanOrderScanModelViewController.h"
  9. #import "RAConvertor.h"
  10. #import "RAUtils.h"
  11. #import "AppDelegate.h"
  12. #define NUMBERS @"0123456789\n"
  13. @interface ScanOrderScanModelViewController ()
  14. @end
  15. @implementation ScanOrderScanModelViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.qtyEdit.delegate = self;
  19. // Do any additional setup after loading the view.
  20. }
  21. - (void)viewWillAppear:(BOOL)animated
  22. {
  23. [super viewWillAppear:animated];
  24. NSMutableDictionary* jitem =(NSMutableDictionary*) self.scan_val;
  25. // jitem[@"product_id"]= item[0];
  26. // NSString* model =item[1];
  27. // NSString* description =item[2];
  28. // jitem[@"model"] =model;
  29. // jitem[@"description"] = [NSString stringWithFormat:@"%@ \r\n%@",model,description];
  30. // jitem[@"unit_price"] =@( [item[3] doubleValue]);
  31. // jitem[@"stockUom"] = @([item[4] intValue]);
  32. // jitem[@"count"] = @([item[4] intValue]);
  33. // jitem[@"subtotal_price"] = @([item[4] intValue]* [item[3] doubleValue]);
  34. //
  35. // jitem[@"check"]=@(true);
  36. // NSString* model =jitem[@"model"];
  37. NSString* description =jitem[@"description"];
  38. double unit_price = [jitem[@"unit_price"] doubleValue];
  39. // jitem[@"model"] =model;
  40. // jitem[@"description"] = [NSString stringWithFormat:@"%@ \r\n%@",model,description];
  41. // jitem[@"unit_price"] =@( [item[3] doubleValue]);
  42. // jitem[@"stockUom"] = @([item[4] intValue]);
  43. // jitem[@"count"] = @([item[4] intValue]);
  44. // jitem[@"subtotal_price"] = @([item[4] intValue]* [item[3] doubleValue]);
  45. //
  46. // jitem[@"check"]=@(true);
  47. //
  48. int stockUom =[jitem[@"stockUom"] intValue];
  49. [self init_Stepper:stockUom max:9999 min:stockUom value:stockUom];
  50. self.descriptionLabel.text = description;
  51. self.qtyEdit.text =[NSString stringWithFormat:@"%d",stockUom];
  52. self.priceLabel.text = [NSString stringWithFormat:@"%.2f",unit_price];
  53. self.mpackLabel.text = [NSString stringWithFormat:@"%d",stockUom];
  54. }
  55. -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value
  56. {
  57. // [self.stepper becomeFirstResponder];
  58. if(self.qtystepper!=nil)
  59. {
  60. if(min<=0)
  61. min=1;
  62. if(step<=0)
  63. step=1;
  64. self.qtystepper.minimumValue= min;
  65. self.qtystepper.stepValue= step;
  66. self.qtystepper.value= value;
  67. [self.qtystepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  68. }
  69. }
  70. - (void)valueChanged:(UIStepper *)sender {
  71. self.qtyEdit.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  72. // self.pre_val = (int)sender.value;
  73. // [self set_Count:(int)sender.value ];
  74. //
  75. // [celldelegate stepClicked:(int)sender.value];
  76. // -(void) stepClicked:(int) value;
  77. }
  78. #pragma mark textField delegate
  79. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  80. [textField resignFirstResponder];
  81. return NO;
  82. }
  83. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  84. // DebugLog(@"text:%@",textField.text);
  85. NSCharacterSet *cs;
  86. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  87. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  88. BOOL canChange = [string isEqualToString:filtered];
  89. return canChange;
  90. }
  91. - (void)textFieldDidEndEditing:(UITextField *)textField
  92. {
  93. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  94. int qty = [textField.text intValue];
  95. if(qty==0)
  96. qty=self.qtystepper.minimumValue;
  97. #ifdef MPACK
  98. if ((int)qty % (int)self.stepper.stepValue != 0) {
  99. UIViewController* vc=[RAUtils getViewController :self];
  100. [((CartViewController *)vc).itemListTable reloadData];
  101. [RAUtils message_alert:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] title:@"Change Model Count" controller:vc];
  102. return;
  103. }
  104. #endif
  105. int c=qty;
  106. int m=c%(int)(self.qtystepper.stepValue);
  107. if(m!=0)
  108. {
  109. [RAUtils message_alert:[NSString stringWithFormat:@"QTY must be a multiple of %d",(int)self.qtystepper.stepValue] title:@"Warrning" controller:self];
  110. textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
  111. }
  112. self.pre_val = qty;
  113. }
  114. - (void)textFieldDidBeginEditing:(UITextField *)textField
  115. {
  116. NSString* text = textField.text;
  117. if(text.length==0)
  118. text=@"0";
  119. self.pre_val = [textField.text intValue];
  120. // self.last_edit = textField.text;
  121. }
  122. - (IBAction)onOK:(id)sender {
  123. [self dismissViewControllerAnimated:true completion:^{
  124. if(self.returnValue)
  125. self.returnValue(self.scan_val);
  126. }];
  127. }
  128. - (IBAction)onUpdate:(id)sender {
  129. NSMutableDictionary* jitem =(NSMutableDictionary*) self.scan_val;
  130. int qty = [self.qtyEdit.text intValue];
  131. jitem[@"count"] = @(qty);
  132. jitem[@"subtotal_price"] = @(qty* [jitem[@"unit_price"] doubleValue] );
  133. [self dismissViewControllerAnimated:true completion:^{
  134. if(self.returnValue)
  135. self.returnValue(self.scan_val);
  136. }];
  137. }
  138. /*
  139. #pragma mark - Navigation
  140. // In a storyboard-based application, you will often want to do a little preparation before navigation
  141. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  142. // Get the new view controller using [segue destinationViewController].
  143. // Pass the selected object to the new view controller.
  144. }
  145. */
  146. @end