ScanOrderModelListViewController.m 14 KB

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