ScanOrderModelListViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // ScanOrderModelListViewController.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/17/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanOrderModelListViewController.h"
  9. #import "ScanModelListCell.h"
  10. #define NUMBERS @"0123456789\n"
  11. #import "RASingleton.h"
  12. #import "ActiveViewController.h"
  13. #import "RAUtils.h"
  14. @interface ScanOrderModelListViewController ()
  15. @end
  16. @implementation ScanOrderModelListViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. self.edit_check = true;
  20. // Do any additional setup after loading the view.
  21. }
  22. - (IBAction)stepChange:(id)sender {
  23. UIStepper * stepper = (UIStepper *) sender;
  24. UITableViewCell *cell = (UITableViewCell *) stepper.superview.superview;
  25. NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  26. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  27. NSMutableDictionary* item= self.modellist[indexPath.row];
  28. int qty = (int)((UIStepper*)sender).value;
  29. scancell.editQTY.text = [NSString stringWithFormat:@"%d",qty ];
  30. item[@"count"] = @(qty);
  31. item[@"subtotal_price"] = @(qty*[item[@"unit_price"] doubleValue]);
  32. self.modellist[indexPath.row] = item;
  33. }
  34. - (void)setScan_val:(id)scan_val
  35. {
  36. [super setScan_val:scan_val];
  37. self.modellist = [NSMutableArray new];
  38. NSArray* arr = (NSArray*) self.scan_val;
  39. bool iscollection=false;
  40. if(arr.count>1)
  41. iscollection = true;
  42. for(int i=0;i<arr.count;i++)
  43. {
  44. NSArray* item = arr[i];
  45. NSMutableDictionary* jitem = [NSMutableDictionary new];
  46. NSString* pid = item[0];
  47. NSString * model =item[1];
  48. if(model.length==0)
  49. model = @"";
  50. NSString * description=item[2];
  51. if(description.length==0)
  52. description = @"";
  53. NSString * dimension=item[3];
  54. if(dimension.length==0)
  55. dimension = @"N/A";
  56. NSString * mpack=item[4];
  57. if(mpack.length==0)
  58. mpack = @"1";
  59. NSString * unit_cuft=item[5];
  60. if(unit_cuft.length==0)
  61. unit_cuft = @"N/A";
  62. else
  63. {
  64. unit_cuft = [NSString stringWithFormat:@"%.2f", [item[5] doubleValue]];
  65. }
  66. NSString * port=item[6];
  67. if(port.length==0)
  68. port = @"N/A";
  69. NSString * origin=item[7];
  70. if(origin.length==0)
  71. origin = @"N/A";
  72. NSString * available=item[8];
  73. if(available.length==0)
  74. available = @"N/A";
  75. NSString* price0;
  76. NSString* price1;
  77. NSString* price2;
  78. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  79. if (addressDic) {
  80. int price_index =[addressDic[ @"price_index"] intValue];
  81. // NSString* ia=addressDic[@"internalAddress"];
  82. // NSString* ea=addressDic[@"externalAddress"];
  83. // NSString* sa=addressDic[@"serverAddress"];
  84. // if(sa.length==0)
  85. // {
  86. // if(ea.length>0)
  87. // sa=ea;
  88. // else if(ia.length>0)
  89. // sa=ia;
  90. // }
  91. // NSString* server_name=[addressDic objectForKey:@"name"];
  92. int idx = 9+price_index;
  93. price0=item[idx];
  94. if(price0.length==0)
  95. price0 = @"N/A";
  96. else
  97. price0 = [NSString stringWithFormat:@"%.2f",[item[idx] doubleValue]];
  98. price1=item[idx+1];
  99. if(price1.length==0)
  100. price1 = @"N/A";
  101. else
  102. price1 = [NSString stringWithFormat:@"%.2f",[item[idx+1] doubleValue]];
  103. if(![price1 isEqualToString:@"N/A"])
  104. {
  105. price2 = [NSString stringWithFormat:@"%.2f",[item[idx+1] doubleValue] *1.25];
  106. }
  107. else
  108. {
  109. price2 = @"N/A";
  110. }
  111. }
  112. jitem[@"product_id"]= pid;
  113. jitem[@"model"] =model;
  114. jitem[@"description"] = description;
  115. if(RASingleton.sharedInstance.price_type==2)
  116. {
  117. jitem[@"unit_price"] =price2;
  118. }
  119. else if(RASingleton.sharedInstance.price_type==1)
  120. {
  121. jitem[@"unit_price"] =price1;
  122. }
  123. else
  124. {
  125. jitem[@"unit_price"] =price0;
  126. }
  127. jitem[@"price0"] =price0;
  128. jitem[@"price1"] =price1;
  129. jitem[@"price2"] =price2;
  130. jitem[@"stockUom"] = mpack;
  131. if(iscollection)
  132. jitem[@"count"]=@"0";
  133. else
  134. jitem[@"count"] = mpack;
  135. jitem[@"subtotal_price"] = @([mpack intValue]* [jitem[@"unit_price"] doubleValue]);
  136. jitem[@"dimension"] = dimension;
  137. jitem[@"unit_cuft"] = unit_cuft;
  138. jitem[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  139. jitem[@"origin"] = origin;
  140. jitem[@"port"] = port;
  141. jitem[@"available"] = available;
  142. // jitem[@"iscollection"]= @(iscollection);
  143. // jitem[@"unit_price"] =@( [item[3] doubleValue]);
  144. // jitem[@"stockUom"] = @([item[4] intValue]);
  145. // jitem[@"count"] = @([item[4] intValue]);
  146. // jitem[@"subtotal_price"] = @([item[4] intValue]* [item[3] doubleValue]);
  147. jitem[@"check"]=@(true);
  148. [self.modellist addObject:jitem];
  149. if(RASingleton.sharedInstance.scan_list == nil)
  150. RASingleton.sharedInstance.scan_list = [NSMutableArray new];
  151. int idx = [RASingleton.sharedInstance.scan_list indexOfObject:jitem];
  152. if(idx>=0)
  153. {
  154. //如果存在,移动到最前。
  155. [RASingleton.sharedInstance.scan_list removeObject:jitem];
  156. }
  157. if(RASingleton.sharedInstance.scan_list.count>=200)
  158. [RASingleton.sharedInstance.scan_list removeLastObject];
  159. [RASingleton.sharedInstance.scan_list insertObject:jitem atIndex:0];
  160. [ActiveViewController Notify:@"ScanHistoryViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  161. }
  162. [self.tableView reloadData];
  163. }
  164. #pragma mark - TableView DataSource
  165. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  166. NSMutableArray* arr = (NSMutableArray*) self.scan_val;
  167. return arr.count;
  168. }
  169. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  170. return 136.0f;
  171. }
  172. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. ScanModelListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScanModelListCell"];
  175. if (!cell) {
  176. cell = [[ScanModelListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScanModelListCell"];
  177. }
  178. NSMutableArray* arr = (NSMutableArray*) self.modellist;
  179. [cell setModelJson:arr[indexPath.row]];
  180. cell.editQTY.delegate = self;
  181. return cell;
  182. }
  183. //#pragma mark - TableView Delegate
  184. /*
  185. #pragma mark - Navigation
  186. // In a storyboard-based application, you will often want to do a little preparation before navigation
  187. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  188. // Get the new view controller using [segue destinationViewController].
  189. // Pass the selected object to the new view controller.
  190. }
  191. */
  192. - (IBAction)onScanNext:(id)sender {
  193. [self dismissViewControllerAnimated:true completion:^{
  194. if(self.returnValue)
  195. self.returnValue(nil);
  196. }];
  197. }
  198. - (IBAction)onAddToCart:(id)sender {
  199. // [self.lastedit endEditing:true];
  200. if(self.lastedit)
  201. [self textFieldDidEndEditing:self.lastedit];
  202. if(self.edit_check)
  203. [self dismissViewControllerAnimated:true completion:^{
  204. if(self.returnValue)
  205. self.returnValue(self.modellist);
  206. }];
  207. }
  208. #pragma mark textField delegate
  209. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  210. [textField resignFirstResponder];
  211. return NO;
  212. }
  213. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  214. // DebugLog(@"text:%@",textField.text);
  215. NSCharacterSet *cs;
  216. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  217. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  218. BOOL canChange = [string isEqualToString:filtered];
  219. return canChange;
  220. }
  221. - (void)textFieldDidBeginEditing:(UITextField *)textField
  222. {
  223. self.lastedit = textField;
  224. // self.lastedit = textField;
  225. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  226. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  227. // scancell.pre_val=[textField.text intValue];
  228. // self.lastedit_from = [self.editorTable indexPathForCell:cell];
  229. //
  230. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[self.lastedit_from.section])[self.lastedit_from.row];
  231. //
  232. // if ([item_json valueForKey:@"clear"]) {
  233. // //开始编辑时清掉旧内容
  234. // BOOL clear = [[item_json valueForKey:@"clear"] doubleValue] == [textField.text doubleValue];
  235. //
  236. // if (clear) {
  237. // textField.text = nil;
  238. // }
  239. // }
  240. }
  241. - (void)textFieldDidEndEditing:(UITextField *)textField
  242. {
  243. self.lastedit = nil;
  244. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  245. NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  246. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  247. NSMutableDictionary* item= self.modellist[indexPath.row];
  248. int qty = [textField.text intValue];
  249. int c=qty;
  250. int mpack= [item[@"stockUom"] intValue];
  251. int m=c%mpack;
  252. if(m!=0)
  253. {
  254. [RAUtils message_alert:[NSString stringWithFormat:@"QTY must be a multiple of %d",mpack] title:@"Warrning" controller:self];
  255. textField.text = [NSString stringWithFormat:@"%d",[item[@"count"] intValue]];
  256. self.edit_check = false;
  257. return;
  258. }
  259. //self.pre_val = qty;
  260. self.edit_check=true;
  261. scancell.steper.value = qty;
  262. item[@"count"] = @(qty);
  263. item[@"subtotal_price"] = @(qty*[item[@"unit_price"] doubleValue]);
  264. self.modellist[indexPath.row] = item;
  265. // NSMutableArray* item_arr=arr[indexPath.row] mutableCopy;
  266. // NSIndexPath * indexPath = self.lastedit_from;
  267. //
  268. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  269. // if ([item_json objectForKey:@"fill"]) {
  270. // NSString *fillText = [item_json objectForKey:@"fill"];
  271. //
  272. // if (fillText && textField.text.length == 0) {
  273. // textField.text = fillText;
  274. // }
  275. // }
  276. //
  277. // [self textfieldSetValue:indexPath value:textField.text];
  278. //
  279. //
  280. }
  281. @end