ScanCartItemCell.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // ScanCartItemCell.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/21/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanCartItemCell.h"
  9. #import "AppDelegate.h"
  10. #import "RANetwork.h"
  11. #import "CartViewController.h"
  12. #define NUMBERS @"0123456789\n"
  13. @implementation ScanCartItemCell
  14. - (void)awakeFromNib {
  15. [super awakeFromNib];
  16. // Initialization code
  17. }
  18. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  19. [super setSelected:selected animated:animated];
  20. // Configure the view for the selected state
  21. }
  22. -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value
  23. {
  24. // [self.stepper becomeFirstResponder];
  25. if(self.setpperQTY!=nil)
  26. {
  27. if(min<=0)
  28. min=1;
  29. if(step<=0)
  30. step=1;
  31. self.setpperQTY.minimumValue= min;
  32. self.setpperQTY.stepValue= step;
  33. self.setpperQTY.value= value;
  34. self.pre_val = value;
  35. [self.setpperQTY addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  36. // self.totalchange=0;
  37. }
  38. }
  39. - (void)valueChanged:(UIStepper *)sender {
  40. self.editQTY.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  41. [self set_Count:(int)sender.value ];
  42. //
  43. // [celldelegate stepClicked:(int)sender.value];
  44. // -(void) stepClicked:(int) value;
  45. }
  46. - (IBAction)setpperChanged:(UIStepper *)sender {
  47. DebugLog(@"setpperChanged");
  48. [self cancel_setQTY];
  49. [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8];
  50. }
  51. -(void) cancel_setQTY
  52. {
  53. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil];
  54. }
  55. -(void) delayRun
  56. {
  57. // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil];
  58. // // 启动
  59. // [thread start];
  60. [RANetwork request_update_cartqty:self.cart_id value:self.setpperQTY.value completionHandler:^(NSMutableDictionary *result) {
  61. NSDictionary* cart_json =result;
  62. if([[cart_json valueForKey:@"result"] intValue]==2)
  63. {
  64. if(self.onReturnQTY)
  65. self.onReturnQTY(self.setpperQTY.value,cart_json);
  66. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  67. // [self.delegate returnQTY:sender.value ];
  68. // }
  69. }
  70. else
  71. {
  72. UIViewController* vc=[RAUtils getViewController :self];
  73. [((CartViewController *)vc).itemListTable reloadData];
  74. // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  75. [RAUtils message_box:@"Change Model Count" message:cart_json[@"err_msg"] completion:nil];
  76. }
  77. }];
  78. }
  79. -(void) set_Count:(int) count
  80. {
  81. self.setpperQTY.value=count;
  82. // double dprice=0;
  83. // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price];
  84. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  85. // NSString* price = [NSString stringWithFormat:@"%.2f",self.unit_price*count*(1-self.discount/100.0) ];
  86. // if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  87. // {
  88. //
  89. // }
  90. // else
  91. // {
  92. // price=nil;
  93. // }
  94. if(self.noprice&&self.unit_price==0.0)
  95. self.labelSubtotal.text =@"No Price";
  96. else
  97. self.labelSubtotal.text =[RAConvertor currencyNumber:self.unit_price*count*(1-self.discount/100.0)];//price;
  98. self.editQTY.text =[NSString stringWithFormat:@"%d",count];
  99. }
  100. #pragma mark textField delegate
  101. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  102. [textField resignFirstResponder];
  103. return NO;
  104. }
  105. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  106. DebugLog(@"text:%@",textField.text);
  107. NSCharacterSet *cs;
  108. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  109. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  110. BOOL canChange = [string isEqualToString:filtered];
  111. return canChange;
  112. }
  113. - (void)textFieldDidEndEditing:(UITextField *)textField
  114. {
  115. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  116. int qty = [textField.text intValue];
  117. if(qty==0)
  118. qty=self.setpperQTY.minimumValue;
  119. // NSMutableDictionary* item= self.modellist[indexPath.row];
  120. // int qty = [textField.text intValue];
  121. //
  122. int c=qty;
  123. int mpack= self.setpperQTY.stepValue;
  124. int m=c%mpack;
  125. if(m!=0)
  126. {
  127. [RAUtils message_box:@"Warrning" message:[NSString stringWithFormat:@"QTY must be a multiple of %d",mpack] completion:nil];
  128. textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
  129. return;
  130. }
  131. // int c=qty;
  132. // int m=c%(int)(self.setpperQTY.stepValue);
  133. // if(m!=0&&appDelegate.alert_sold_in_quantities)
  134. //
  135. else
  136. {
  137. [self set_Count:qty];
  138. [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
  139. NSDictionary* cart_json =result;
  140. if([[cart_json valueForKey:@"result"] intValue]==2)
  141. {
  142. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  143. // [self.delegate returnQTY:qty];
  144. // }
  145. if(self.onReturnQTY)
  146. self.onReturnQTY(qty,cart_json);
  147. }
  148. else
  149. {
  150. UIViewController* vc=[RAUtils getViewController :self];
  151. [((CartViewController *)vc).itemListTable reloadData];
  152. // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  153. [RAUtils message_box:@"Change Model Count" message:cart_json[@"err_msg"] completion:nil];
  154. }
  155. }];
  156. }
  157. }
  158. - (void)textFieldDidBeginEditing:(UITextField *)textField
  159. {
  160. NSString* text = textField.text;
  161. if(text.length==0)
  162. text=@"0";
  163. self.pre_val = [textField.text intValue];
  164. // self.last_edit = textField.text;
  165. }
  166. @end