ScanOrderModelListViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. }
  96. // if(RASingleton.sharedInstance.price_type==3)
  97. // {
  98. // jitem[@"unit_price"] =jitem[@"price3"];
  99. // }
  100. // if(RASingleton.sharedInstance.price_type==2)
  101. // {
  102. // jitem[@"unit_price"] =jitem[@"price2"];
  103. // }
  104. // else if(RASingleton.sharedInstance.price_type==1)
  105. // {
  106. // jitem[@"unit_price"] =jitem[@"price1"];
  107. // }
  108. // else
  109. // {
  110. // jitem[@"unit_price"] =jitem[@"price0"];
  111. // }
  112. NSString * cell_price;
  113. if(RASingleton.sharedInstance.price_type==1)
  114. {
  115. // if(_modelJson [@"net_price"])
  116. cell_price=jitem[price_group[@"price_3"][@"name"]];
  117. // jitem[@"unit_price"] =jitem[@"price3"];
  118. // jitem[@"erp_unit_price"] =jitem[@"price3"];
  119. // else if(_modelJson [@"special_price"])
  120. // {
  121. // unit_price= _modelJson[@"price2"];
  122. // }
  123. // else
  124. // {
  125. // unit_price= _modelJson[@"price1"];
  126. // }
  127. }
  128. else
  129. {
  130. cell_price=jitem[price_group[@"price_0"][@"name"]];
  131. // jitem[@"unit_price"] =jitem[@"price0"];
  132. // jitem[@"erp_unit_price"] =jitem[@"price0"];
  133. }
  134. NSString * unit_price;
  135. if(RASingleton.sharedInstance.price_type==0)
  136. unit_price=jitem[price_group[@"price_0"][@"name"]];
  137. else if(jitem [@"special_price"])
  138. {
  139. unit_price= jitem[price_group[@"price_2"][@"name"]];
  140. }
  141. else if(jitem [@"net_price"])
  142. unit_price=jitem[price_group[@"price_3"][@"name"]];
  143. else
  144. {
  145. unit_price= jitem[price_group[@"price_1"][@"name"]];
  146. }
  147. if([unit_price isEqualToString:@"N/A"])
  148. unit_price = @"0";
  149. else
  150. {
  151. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  152. }
  153. jitem[@"unit_price"] =unit_price;
  154. jitem[@"erp_unit_price"] =unit_price;
  155. // jitem[@"price0"] =price0;
  156. // jitem[@"price1"] =price1;
  157. // jitem[@"price2"] =price2;
  158. // jitem[@"stockUom"] = mpack;
  159. if(iscollection)
  160. jitem[@"count"]=@"0";
  161. else
  162. jitem[@"count"] = mpack;
  163. jitem[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);//@([mpack intValue]* [jitem[@"unit_price"] doubleValue]);
  164. // jitem[@"dimension"] = dimension;
  165. // jitem[@"unit_cuft"] = unit_cuft;
  166. jitem[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  167. // jitem[@"origin"] = origin;
  168. // jitem[@"port"] = port;
  169. // jitem[@"available"] = available;
  170. // jitem[@"iscollection"]= @(iscollection);
  171. // jitem[@"unit_price"] =@( [item[3] doubleValue]);
  172. // jitem[@"stockUom"] = @([item[4] intValue]);
  173. // jitem[@"count"] = @([item[4] intValue]);
  174. // jitem[@"subtotal_price"] = @([item[4] intValue]* [item[3] doubleValue]);
  175. jitem[@"check"]=@(true);
  176. [self.modellist addObject:jitem];
  177. if(RASingleton.sharedInstance.scan_list == nil)
  178. RASingleton.sharedInstance.scan_list = [NSMutableArray new];
  179. for(int sli=0;sli<RASingleton.sharedInstance.scan_list.count;sli++)
  180. {
  181. if([RASingleton.sharedInstance.scan_list[sli][@"product_id"] isEqualToString:jitem[@"product_id"]])
  182. {
  183. [RASingleton.sharedInstance.scan_list removeObject:RASingleton.sharedInstance.scan_list[sli]];
  184. }
  185. }
  186. if(RASingleton.sharedInstance.scan_list.count>=200)
  187. [RASingleton.sharedInstance.scan_list removeLastObject];
  188. [RASingleton.sharedInstance.scan_list insertObject:jitem atIndex:0];
  189. [ActiveViewController Notify:@"ScanHistoryViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  190. // int idx = [RASingleton.sharedInstance.scan_list indexOfObject:jitem];
  191. // if(idx>=0)
  192. // {
  193. // //如果存在,移动到最前。
  194. //
  195. //
  196. // }
  197. }
  198. [self.tableView reloadData];
  199. }
  200. #pragma mark - TableView DataSource
  201. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  202. NSMutableArray* arr = (NSMutableArray*) self.scan_val;
  203. return arr.count;
  204. }
  205. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  206. return 136.0f;
  207. }
  208. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  209. {
  210. ScanModelListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScanModelListCell"];
  211. if (!cell) {
  212. cell = [[ScanModelListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScanModelListCell"];
  213. }
  214. NSMutableArray* arr = (NSMutableArray*) self.modellist;
  215. [cell setModelJson:arr[indexPath.row]];
  216. cell.editQTY.delegate = self;
  217. return cell;
  218. }
  219. //#pragma mark - TableView Delegate
  220. /*
  221. #pragma mark - Navigation
  222. // In a storyboard-based application, you will often want to do a little preparation before navigation
  223. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  224. // Get the new view controller using [segue destinationViewController].
  225. // Pass the selected object to the new view controller.
  226. }
  227. */
  228. - (IBAction)onScanNext:(id)sender {
  229. [self dismissViewControllerAnimated:true completion:^{
  230. if(self.returnValue)
  231. self.returnValue(nil);
  232. }];
  233. }
  234. - (IBAction)onAddToCart:(id)sender {
  235. // [self.lastedit endEditing:true];
  236. if(self.lastedit)
  237. [self textFieldDidEndEditing:self.lastedit];
  238. if(self.edit_check)
  239. {
  240. NSMutableArray* arrname = [NSMutableArray new];
  241. for(int j=0;j<self.modellist.count;j++)
  242. {
  243. if([self.modellist[j][@"count"] intValue]==0)
  244. continue;
  245. NSMutableDictionary* jitem = nil;
  246. jitem = [self.modellist[j] mutableCopy];
  247. [arrname addObject:jitem[@"model"]];
  248. }
  249. NSString* models=[RAConvertor arr2string:arrname separator:@"," trim:false];
  250. [RAUtils message_alert:@"Successfully" title:[NSString stringWithFormat: @"%@ added to cart",models] controller:self action_handler:^(UIAlertAction * _Nonnull action) {
  251. [self dismissViewControllerAnimated:true completion:^{
  252. if(self.returnValue)
  253. self.returnValue(self.modellist);
  254. }];
  255. } completion:nil];
  256. // [RAUtils message_alert:@"Successful" title:[NSString stringWithFormat: @"%@ added to cart",models] controller:[RAUtils getViewController:self]];
  257. }
  258. }
  259. #pragma mark textField delegate
  260. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  261. [textField resignFirstResponder];
  262. return NO;
  263. }
  264. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  265. // DebugLog(@"text:%@",textField.text);
  266. NSCharacterSet *cs;
  267. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  268. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  269. BOOL canChange = [string isEqualToString:filtered];
  270. return canChange;
  271. }
  272. - (void)textFieldDidBeginEditing:(UITextField *)textField
  273. {
  274. self.lastedit = textField;
  275. // self.lastedit = textField;
  276. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  277. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  278. // scancell.pre_val=[textField.text intValue];
  279. // self.lastedit_from = [self.editorTable indexPathForCell:cell];
  280. //
  281. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[self.lastedit_from.section])[self.lastedit_from.row];
  282. //
  283. // if ([item_json valueForKey:@"clear"]) {
  284. // //开始编辑时清掉旧内容
  285. // BOOL clear = [[item_json valueForKey:@"clear"] doubleValue] == [textField.text doubleValue];
  286. //
  287. // if (clear) {
  288. // textField.text = nil;
  289. // }
  290. // }
  291. }
  292. - (void)textFieldDidEndEditing:(UITextField *)textField
  293. {
  294. self.lastedit = nil;
  295. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  296. NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  297. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  298. NSMutableDictionary* item= self.modellist[indexPath.row];
  299. int qty = [textField.text intValue];
  300. int c=qty;
  301. int mpack= [item[@"stockUom"] intValue];
  302. int m=c%mpack;
  303. if(m!=0)
  304. {
  305. [RAUtils message_alert:[NSString stringWithFormat:@"QTY must be a multiple of %d",mpack] title:@"Warrning" controller:self];
  306. textField.text = [NSString stringWithFormat:@"%d",[item[@"count"] intValue]];
  307. self.edit_check = false;
  308. return;
  309. }
  310. //self.pre_val = qty;
  311. self.edit_check=true;
  312. scancell.steper.value = qty;
  313. item[@"count"] = @(qty);
  314. item[@"subtotal_price"] = @(qty*[item[@"unit_price"] doubleValue]);
  315. self.modellist[indexPath.row] = item;
  316. // NSMutableArray* item_arr=arr[indexPath.row] mutableCopy;
  317. // NSIndexPath * indexPath = self.lastedit_from;
  318. //
  319. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  320. // if ([item_json objectForKey:@"fill"]) {
  321. // NSString *fillText = [item_json objectForKey:@"fill"];
  322. //
  323. // if (fillText && textField.text.length == 0) {
  324. // textField.text = fillText;
  325. // }
  326. // }
  327. //
  328. // [self textfieldSetValue:indexPath value:textField.text];
  329. //
  330. //
  331. }
  332. @end