ScanOrderModelListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. NSString * unit_price;
  162. if(RASingleton.sharedInstance.price_type==0)
  163. unit_price=jitem[price_group[@"price_0"][@"name"]];
  164. else if(jitem [@"special_price"])
  165. {
  166. unit_price= jitem[price_group[@"price_2"][@"name"]];
  167. }
  168. else if(jitem [@"net_price"])
  169. unit_price=jitem[price_group[@"price_3"][@"name"]];
  170. else
  171. {
  172. unit_price= jitem[price_group[@"price_1"][@"name"]];
  173. }
  174. if([unit_price isEqualToString:@"N/A"])
  175. unit_price = @"0";
  176. else
  177. {
  178. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  179. }
  180. jitem[@"unit_price"] =unit_price;
  181. jitem[@"erp_unit_price"] =unit_price;
  182. // jitem[@"price0"] =price0;
  183. // jitem[@"price1"] =price1;
  184. // jitem[@"price2"] =price2;
  185. // jitem[@"stockUom"] = mpack;
  186. if(iscollection)
  187. jitem[@"count"]=@"0";
  188. else
  189. jitem[@"count"] = mpack;
  190. jitem[@"subtotal_price"] = @([mpack intValue]* [unit_price doubleValue]);//@([mpack intValue]* [jitem[@"unit_price"] doubleValue]);
  191. // jitem[@"dimension"] = dimension;
  192. // jitem[@"unit_cuft"] = unit_cuft;
  193. jitem[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  194. // jitem[@"origin"] = origin;
  195. // jitem[@"port"] = port;
  196. // jitem[@"available"] = available;
  197. // jitem[@"iscollection"]= @(iscollection);
  198. // jitem[@"unit_price"] =@( [item[3] doubleValue]);
  199. // jitem[@"stockUom"] = @([item[4] intValue]);
  200. // jitem[@"count"] = @([item[4] intValue]);
  201. // jitem[@"subtotal_price"] = @([item[4] intValue]* [item[3] doubleValue]);
  202. jitem[@"check"]=@(true);
  203. [self.modellist addObject:jitem];
  204. if(RASingleton.sharedInstance.scan_list == nil)
  205. RASingleton.sharedInstance.scan_list = [NSMutableArray new];
  206. for(int sli=0;sli<RASingleton.sharedInstance.scan_list.count;sli++)
  207. {
  208. if([RASingleton.sharedInstance.scan_list[sli][@"product_id"] isEqualToString:jitem[@"product_id"]])
  209. {
  210. [RASingleton.sharedInstance.scan_list removeObject:RASingleton.sharedInstance.scan_list[sli]];
  211. }
  212. }
  213. if(RASingleton.sharedInstance.scan_list.count>=200)
  214. [RASingleton.sharedInstance.scan_list removeLastObject];
  215. [RASingleton.sharedInstance.scan_list insertObject:jitem atIndex:0];
  216. [ActiveViewController Notify:@"ScanHistoryViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  217. // int idx = [RASingleton.sharedInstance.scan_list indexOfObject:jitem];
  218. // if(idx>=0)
  219. // {
  220. // //如果存在,移动到最前。
  221. //
  222. //
  223. // }
  224. }
  225. [self.tableView reloadData];
  226. }
  227. #pragma mark - TableView DataSource
  228. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  229. NSMutableArray* arr = (NSMutableArray*) self.scan_val;
  230. return arr.count;
  231. }
  232. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  233. // return 166.0f;
  234. UILabel * label = [UILabel new];
  235. NSMutableArray* arr = self.modellist;
  236. NSMutableDictionary* model = arr[indexPath.row];
  237. label.frame = CGRectMake(0, 0, tableView.frame.size.width-32-30, 0);
  238. label.text = model[@"description"];
  239. label.font = [UIFont systemFontOfSize:15];
  240. label.numberOfLines=0;
  241. label.lineBreakMode = NSLineBreakByWordWrapping;
  242. double s = label.font.pointSize;
  243. // NSLog(label.text);
  244. // CGSize s=[label sizeThatFits:CGSizeMake(tableView.frame.size.width-32, 0)];
  245. [label sizeToFit];
  246. return label.frame.size.height+107.5;
  247. }
  248. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  249. {
  250. ScanModelListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ScanModelListCell"];
  251. if (!cell) {
  252. cell = [[ScanModelListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ScanModelListCell"];
  253. }
  254. NSMutableArray* arr = (NSMutableArray*) self.modellist;
  255. [cell setModelJson:arr[indexPath.row]];
  256. cell.editQTY.delegate = self;
  257. cell.labelStock.text = [RADataProvider queryStock:cell.labelModel.text];
  258. return cell;
  259. }
  260. //#pragma mark - TableView Delegate
  261. /*
  262. #pragma mark - Navigation
  263. // In a storyboard-based application, you will often want to do a little preparation before navigation
  264. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  265. // Get the new view controller using [segue destinationViewController].
  266. // Pass the selected object to the new view controller.
  267. }
  268. */
  269. - (IBAction)onScanNext:(id)sender {
  270. [self dismissViewControllerAnimated:true completion:^{
  271. if(self.returnValue)
  272. self.returnValue(nil);
  273. }];
  274. }
  275. - (IBAction)onAddToCart:(id)sender {
  276. // [self.lastedit endEditing:true];
  277. if(self.lastedit)
  278. [self textFieldDidEndEditing:self.lastedit];
  279. if(self.edit_check)
  280. {
  281. NSMutableArray* arrname = [NSMutableArray new];
  282. for(int j=0;j<self.modellist.count;j++)
  283. {
  284. if([self.modellist[j][@"count"] intValue]==0)
  285. continue;
  286. NSMutableDictionary* jitem = nil;
  287. jitem = [self.modellist[j] mutableCopy];
  288. [arrname addObject:jitem[@"model"]];
  289. }
  290. NSString* models=[RAConvertor arr2string:arrname separator:@"," trim:false];
  291. [RAUtils message_box:[NSString stringWithFormat: @"%@ added to cart",models] message:@"Successfully" completion:^{
  292. [self dismissViewControllerAnimated:true completion:^{
  293. if(self.returnValue)
  294. self.returnValue(self.modellist);
  295. }];
  296. }];
  297. }
  298. }
  299. #pragma mark textField delegate
  300. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  301. [textField resignFirstResponder];
  302. return NO;
  303. }
  304. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  305. // DebugLog(@"text:%@",textField.text);
  306. NSCharacterSet *cs;
  307. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  308. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  309. BOOL canChange = [string isEqualToString:filtered];
  310. return canChange;
  311. }
  312. - (void)textFieldDidBeginEditing:(UITextField *)textField
  313. {
  314. self.lastedit = textField;
  315. // self.lastedit = textField;
  316. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  317. // ScanModelListCell * scancell = (ScanModelListCell*)cell;
  318. // scancell.pre_val=[textField.text intValue];
  319. // self.lastedit_from = [self.editorTable indexPathForCell:cell];
  320. //
  321. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[self.lastedit_from.section])[self.lastedit_from.row];
  322. //
  323. // if ([item_json valueForKey:@"clear"]) {
  324. // //开始编辑时清掉旧内容
  325. // BOOL clear = [[item_json valueForKey:@"clear"] doubleValue] == [textField.text doubleValue];
  326. //
  327. // if (clear) {
  328. // textField.text = nil;
  329. // }
  330. // }
  331. }
  332. - (void)textFieldDidEndEditing:(UITextField *)textField
  333. {
  334. self.lastedit = nil;
  335. UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  336. NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
  337. ScanModelListCell * scancell = (ScanModelListCell*)cell;
  338. NSMutableDictionary* item= self.modellist[indexPath.row];
  339. int qty = [textField.text intValue];
  340. int c=qty;
  341. int mpack= [item[@"stockUom"] intValue];
  342. int m=c%mpack;
  343. if(m!=0)
  344. {
  345. [RAUtils message_box:@"Warrning" message:[NSString stringWithFormat:@"QTY must be a multiple of %d",mpack] completion:nil];
  346. textField.text = [NSString stringWithFormat:@"%d",[item[@"count"] intValue]];
  347. self.edit_check = false;
  348. return;
  349. }
  350. //self.pre_val = qty;
  351. self.edit_check=true;
  352. scancell.steper.value = qty;
  353. item[@"count"] = @(qty);
  354. item[@"subtotal_price"] = @(qty*[item[@"unit_price"] doubleValue]);
  355. self.modellist[indexPath.row] = item;
  356. // NSMutableArray* item_arr=arr[indexPath.row] mutableCopy;
  357. // NSIndexPath * indexPath = self.lastedit_from;
  358. //
  359. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  360. // if ([item_json objectForKey:@"fill"]) {
  361. // NSString *fillText = [item_json objectForKey:@"fill"];
  362. //
  363. // if (fillText && textField.text.length == 0) {
  364. // textField.text = fillText;
  365. // }
  366. // }
  367. //
  368. // [self textfieldSetValue:indexPath value:textField.text];
  369. //
  370. //
  371. }
  372. @end