ScanOrderModelListViewController.m 15 KB

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