ScanListCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // ScanListCell.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 3/30/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ScanListCell.h"
  9. #import "RASingleton.h"
  10. #import "OLDataProvider.h"
  11. #import "ActiveViewController.h"
  12. #import "RAConvertor.h"
  13. #import "RAUtils.h"
  14. #import "AppDelegate.h"
  15. #import "RADataProvider.h"
  16. @implementation ScanListCell
  17. - (void)awakeFromNib {
  18. [super awakeFromNib];
  19. // Initialization code
  20. }
  21. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  22. [super setSelected:selected animated:animated];
  23. // Configure the view for the selected state
  24. }
  25. - (IBAction)onAddToCart:(id)sender {
  26. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  27. if(RASingleton.sharedInstance.scan_cart ==nil)
  28. {
  29. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_CART ofType:@"json" ]];
  30. NSMutableDictionary* cartTemplate=[OLDataProvider loadScanTemplate:@"scan_cart.json"];
  31. RASingleton.sharedInstance.scan_cart=cartTemplate;//[[RAConvertor data2dict:json] mutableCopy];
  32. }
  33. // 初始化为 FOB CTNR;
  34. if(RASingleton.sharedInstance.scan_cart[@"price_type"] ==nil)
  35. {
  36. RASingleton.sharedInstance.scan_cart[@"price_type"] = @1;
  37. RASingleton.sharedInstance.price_type = 1;
  38. }
  39. NSMutableDictionary* section =[RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  40. int count =[section[@"count"] intValue];
  41. NSMutableDictionary* jitem = nil;
  42. jitem = [self.modelJson mutableCopy];
  43. int stockUom =[jitem[@"stockUom"] intValue];
  44. if(stockUom==0)
  45. stockUom=1;
  46. bool newitem = true;
  47. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  48. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  49. for(int i=0;i<count;i++)
  50. {
  51. NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  52. if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  53. {
  54. // int oldcount = [litem[@"stockUom"] intValue];
  55. newitem = false;
  56. litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  57. litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  58. // 计算subtotal
  59. double discount = [litem[@"discount"] doubleValue];
  60. int qty = [litem[@"count"] intValue];
  61. double unit_price = [litem[@"unit_price"] doubleValue];
  62. litem[@"subtotal_price"]= [NSString stringWithFormat:@"%f",unit_price*qty*(1-discount/100.0)];
  63. section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  64. break;
  65. }
  66. }
  67. if(newitem)
  68. {
  69. jitem[@"count"]=@(stockUom);
  70. jitem[@"check"]=@(true);
  71. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  72. NSString * unit_price;
  73. if(RASingleton.sharedInstance.price_type==0)
  74. unit_price=_modelJson[price_group[@"price_0"][@"name"]];
  75. else if(_modelJson [@"special_price"])
  76. {
  77. unit_price= _modelJson[price_group[@"price_2"][@"name"]];
  78. }
  79. else if(_modelJson [@"net_price"])
  80. unit_price=_modelJson[price_group[@"price_3"][@"name"]];
  81. else
  82. {
  83. unit_price= _modelJson[price_group[@"price_1"][@"name"]];
  84. }
  85. if([unit_price isEqualToString:@"N/A"])
  86. unit_price = @"0";
  87. else
  88. {
  89. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  90. }
  91. // [jitem[@"subtotal_price"]= [NSString stringWithFormat:@"%f",unit_price*qty*(1-discount/100.0)];
  92. jitem[@"unit_price"] = unit_price;
  93. jitem[@"erp_unit_price"] = unit_price;
  94. // 计算subtotal
  95. double discount = [jitem[@"discount"] doubleValue];
  96. int qty = [jitem[@"count"] intValue];
  97. double dunit_price = [jitem[@"unit_price"] doubleValue];
  98. jitem[@"subtotal_price"]= [NSString stringWithFormat:@"%f",dunit_price*qty*(1-discount/100.0)];
  99. section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  100. section[@"count"]= @(count+1);
  101. count++;
  102. }
  103. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  104. //加list
  105. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  106. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  107. [RAUtils message_box:[NSString stringWithFormat: @"%@ added to cart",jitem[@"model"]] message:@"Successfully" completion:nil];
  108. }
  109. - (void)setModelJson:(NSMutableDictionary *)modelJson
  110. {
  111. _modelJson = modelJson;
  112. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  113. // self.labelPrice0.text = appDelegate.price0_name;
  114. // self.labelPrice1.text = appDelegate.price1_name;
  115. // self.labelPrice2.text = appDelegate.price2_name;
  116. // self.labelPrice3.text = appDelegate.price3_name;
  117. NSDictionary* price_group = [RADataProvider get_price_group:appDelegate.price_group];
  118. self.labelPrice0.text = price_group[@"price_0"][@"display"];//appDelegate.price0_name;
  119. self.labelPrice1.text = price_group[@"price_1"][@"display"];//appDelegate.price1_name;
  120. self.labelPrice2.text = price_group[@"price_2"][@"display"];//appDelegate.price2_name;
  121. self.labelPrice3.text = price_group[@"price_3"][@"display"];//appDelegate.price3_name;
  122. self.labelPrice4.text = price_group[@"price_4"][@"display"];//appDelegate.price4_name;
  123. // remove net price
  124. // if(price_group[@"price_4"])
  125. // {
  126. // self.labelPrice3.text = price_group[@"price_4"][@"display"];
  127. // }
  128. //
  129. #ifdef DEBUG
  130. NSLog(@"%@",[RAConvertor dict2string:modelJson]);
  131. #endif
  132. // NSString* s=[RAConvertor dict2string:modelJson];
  133. // @try {
  134. //
  135. //// NSLog( s);
  136. //
  137. // NSLog([RAConvertor dict2string:modelJson]);
  138. //
  139. // }
  140. // @catch (NSException * e) {
  141. // NSLog(@"Exception: %@", e);
  142. // [RAUtils message_box:@"exception" message:[NSString stringWithFormat:@"%@ %@",e,modelJson] completion:nil];
  143. // }
  144. NSString * port=_modelJson[@"port"];
  145. if(port.length==0)
  146. port = @"N/A";
  147. NSString * origin=_modelJson[@"origin"];
  148. if(origin.length==0)
  149. origin = @"N/A";
  150. NSString * dimension=_modelJson[@"dimension"];
  151. if(dimension.length==0)
  152. dimension = @"N/A";
  153. _labelModel.text=_modelJson[@"model"];
  154. _labelDescription.text=_modelJson[@"description"];
  155. _labelDimension.text=dimension;
  156. _labelCuft.text=_modelJson[@"unit_cuft"];
  157. _labelOrigin.text=origin;
  158. _labelPort.text=port;
  159. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  160. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  161. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  162. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  163. if(![price0 isEqualToString: @"N/A"])
  164. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  165. if(![price1 isEqualToString: @"N/A"])
  166. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  167. if(![price2 isEqualToString: @"N/A"])
  168. {
  169. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  170. // if(RASingleton.sharedInstance.price_type==1)
  171. _modelJson [@"special_price"] = @true;
  172. }
  173. if(![price3 isEqualToString: @"N/A"])
  174. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  175. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  176. if((![price1 isEqualToString: @"N/A"] && [price1 isEqualToString:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqualToString:price3]))
  177. {
  178. // if(RASingleton.sharedInstance.price_type==1)
  179. // _modelJson [@"net_price"] = @true;
  180. // remove net price
  181. _modelJson [@"net_price"] = @false;
  182. }
  183. //
  184. // if(![price1 isEqualToString:@"N/A"])
  185. // {
  186. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  187. // _modelJson[@"price2"]= price2;
  188. // }
  189. // else
  190. // {
  191. // price2=@"N/A";
  192. // }
  193. //
  194. if([price0 isEqualToString:@"N/A"])
  195. _labelPriceCTNR.text= @"";//price0;
  196. else
  197. _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  198. //
  199. // if([price3 isEqualToString:@"N/A"])
  200. // _labelPriceNCA.text= price3;
  201. // else
  202. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  203. // _labelPrice25p.text=price2;
  204. if([_modelJson [@"special_price"] boolValue])
  205. {
  206. if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  207. _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  208. else
  209. _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  210. }
  211. else
  212. {
  213. _labelPriceSpecial.text=@"";//@"N/A";
  214. }
  215. // remove net price
  216. // if([_modelJson [@"net_price"] boolValue])
  217. // {
  218. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  219. //// {
  220. //// _labelPriceNet.text=_modelJson[@"price1"];
  221. ////// _labelPriceNCA.text=@"N/A";
  222. //// }
  223. //// else
  224. //// {
  225. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  226. ////// _labelPriceNCA.text=@"N/A";
  227. //// }
  228. //
  229. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  230. // {
  231. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  232. //// _labelPriceNCA.text=@"N/A";
  233. // }
  234. // else
  235. // {
  236. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  237. //// _labelPriceNCA.text=@"N/A";
  238. // }
  239. //
  240. // }
  241. // else
  242. // {
  243. // _labelPriceNet.text=@"";//@"N/A";
  244. // }
  245. if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"]||_modelJson[price_group[@"price_3"][@"name"]]==nil)
  246. {
  247. _labelPriceDisplay1.text=@"";//_modelJson[@"price3"];
  248. // _labelPriceNCA.text=@"N/A";
  249. }
  250. else
  251. {
  252. _labelPriceDisplay1.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  253. // _labelPriceNCA.text=@"N/A";
  254. }
  255. if([_modelJson[price_group[@"price_4"][@"name"]] isEqualToString:@"N/A"]||_modelJson[price_group[@"price_4"][@"name"]]==nil )
  256. {
  257. _labelPriceDisplay2.text=@"";//_modelJson[@"price3"];
  258. // _labelPriceNCA.text=@"N/A";
  259. }
  260. else
  261. {
  262. _labelPriceDisplay2.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_4"][@"name"]] floatValue]];
  263. // _labelPriceNCA.text=@"N/A";
  264. }
  265. // remove net price
  266. // if(price_group[@"price_4"])
  267. // {
  268. // // price4 如果存在,会显示在price3的位置
  269. // NSString* price4=_modelJson[price_group[@"price_4"][@"name"]]; //%25
  270. //
  271. // if(![price4 isEqualToString: @"N/A"])
  272. // price4 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_4"][@"name"]] doubleValue]];
  273. // else
  274. // price4 =@"";
  275. // _labelPriceNet.text=price4;
  276. // }
  277. {
  278. if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  279. {
  280. _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  281. // _labelPriceNCA.text=@"N/A";
  282. }
  283. else
  284. {
  285. _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  286. // _labelPriceNet.text=@"N/A";
  287. }
  288. }
  289. NSString * available=_modelJson[@"available"];
  290. if(available.length==0)
  291. available = @"N/A";
  292. _labelAvailable.text=available;
  293. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  294. if(unit_cuft.length==0)
  295. unit_cuft = @"N/A";
  296. else
  297. {
  298. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  299. }
  300. NSString * cell_price;
  301. if(RASingleton.sharedInstance.price_type==1)
  302. {
  303. // if(_modelJson [@"net_price"])
  304. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  305. // else if(_modelJson [@"special_price"])
  306. // {
  307. // unit_price= _modelJson[@"price2"];
  308. // }
  309. // else
  310. // {
  311. // unit_price= _modelJson[@"price1"];
  312. // }
  313. }
  314. else
  315. {
  316. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  317. }
  318. NSString * mpack=_modelJson[@"stockUom"];
  319. if([mpack isEqualToString:@"N/A"])
  320. mpack=@"1";
  321. _modelJson[@"count"]=_modelJson[@"stockUom"];
  322. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  323. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  324. }
  325. @end