ScanOrderModelListViewController.m 13 KB

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