ScanOrderModelListViewController.m 16 KB

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