ScanOrderModelListViewController.m 13 KB

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