ScanListCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // remove net price
  123. // if(price_group[@"price_4"])
  124. // {
  125. // self.labelPrice3.text = price_group[@"price_4"][@"display"];
  126. // }
  127. //
  128. #ifdef DEBUG
  129. NSLog(@"%@",[RAConvertor dict2string:modelJson]);
  130. #endif
  131. // NSString* s=[RAConvertor dict2string:modelJson];
  132. // @try {
  133. //
  134. //// NSLog( s);
  135. //
  136. // NSLog([RAConvertor dict2string:modelJson]);
  137. //
  138. // }
  139. // @catch (NSException * e) {
  140. // NSLog(@"Exception: %@", e);
  141. // [RAUtils message_box:@"exception" message:[NSString stringWithFormat:@"%@ %@",e,modelJson] completion:nil];
  142. // }
  143. NSString * port=_modelJson[@"port"];
  144. if(port.length==0)
  145. port = @"N/A";
  146. NSString * origin=_modelJson[@"origin"];
  147. if(origin.length==0)
  148. origin = @"N/A";
  149. NSString * dimension=_modelJson[@"dimension"];
  150. if(dimension.length==0)
  151. dimension = @"N/A";
  152. _labelModel.text=_modelJson[@"model"];
  153. _labelDescription.text=_modelJson[@"description"];
  154. _labelDimension.text=dimension;
  155. _labelCuft.text=_modelJson[@"unit_cuft"];
  156. _labelOrigin.text=origin;
  157. _labelPort.text=port;
  158. NSString* price0=_modelJson[price_group[@"price_0"][@"name"]]; //DDP
  159. NSString* price1=_modelJson[price_group[@"price_1"][@"name"]]; //WHSE
  160. NSString* price2=_modelJson[price_group[@"price_2"][@"name"]]; //SHOW
  161. NSString* price3=_modelJson[price_group[@"price_3"][@"name"]]; //NET
  162. if(![price0 isEqualToString: @"N/A"])
  163. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_0"][@"name"]] doubleValue]];
  164. if(![price1 isEqualToString: @"N/A"])
  165. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_1"][@"name"]] doubleValue]];
  166. if(![price2 isEqualToString: @"N/A"])
  167. {
  168. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_2"][@"name"]] doubleValue]];
  169. // if(RASingleton.sharedInstance.price_type==1)
  170. _modelJson [@"special_price"] = @true;
  171. }
  172. if(![price3 isEqualToString: @"N/A"])
  173. price3 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_3"][@"name"]] doubleValue]];
  174. // if(![_modelJson [@"special_price"] boolValue]&& ![price1 isEqualToString: @"N/A"] && [price1 isEqual:price3])
  175. if((![price1 isEqualToString: @"N/A"] && [price1 isEqualToString:price3])||(![price2 isEqualToString: @"N/A"] && [price2 isEqualToString:price3]))
  176. {
  177. // if(RASingleton.sharedInstance.price_type==1)
  178. _modelJson [@"net_price"] = @true;
  179. // remove net price
  180. _modelJson [@"net_price"] = @false;
  181. }
  182. //
  183. // if(![price1 isEqualToString:@"N/A"])
  184. // {
  185. // price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  186. // _modelJson[@"price2"]= price2;
  187. // }
  188. // else
  189. // {
  190. // price2=@"N/A";
  191. // }
  192. //
  193. if([price0 isEqualToString:@"N/A"])
  194. _labelPriceCTNR.text= @"";//price0;
  195. else
  196. _labelPriceCTNR.text=[RAConvertor currencyNumber:[price0 floatValue]];
  197. //
  198. // if([price3 isEqualToString:@"N/A"])
  199. // _labelPriceNCA.text= price3;
  200. // else
  201. // _labelPriceNCA.text=[RAConvertor currencyNumber:[price3 floatValue]];
  202. // _labelPrice25p.text=price2;
  203. if([_modelJson [@"special_price"] boolValue])
  204. {
  205. if([_modelJson[price_group[@"price_2"][@"name"]] isEqualToString:@"N/A"])
  206. _labelPriceSpecial.text=@"";//_modelJson[@"price2"];
  207. else
  208. _labelPriceSpecial.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_2"][@"name"]] floatValue]];
  209. }
  210. else
  211. {
  212. _labelPriceSpecial.text=@"";//@"N/A";
  213. }
  214. // remove net price
  215. // if([_modelJson [@"net_price"] boolValue])
  216. // {
  217. //// if([_modelJson[@"price1"] isEqualToString:@"N/A"])
  218. //// {
  219. //// _labelPriceNet.text=_modelJson[@"price1"];
  220. ////// _labelPriceNCA.text=@"N/A";
  221. //// }
  222. //// else
  223. //// {
  224. //// _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[@"price1"] floatValue]];
  225. ////// _labelPriceNCA.text=@"N/A";
  226. //// }
  227. //
  228. // if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  229. // {
  230. // _labelPriceNet.text=@"";//_modelJson[@"price3"];
  231. //// _labelPriceNCA.text=@"N/A";
  232. // }
  233. // else
  234. // {
  235. // _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  236. //// _labelPriceNCA.text=@"N/A";
  237. // }
  238. //
  239. // }
  240. // else
  241. // {
  242. // _labelPriceNet.text=@"";//@"N/A";
  243. // }
  244. if([_modelJson[price_group[@"price_3"][@"name"]] isEqualToString:@"N/A"])
  245. {
  246. _labelPriceNet.text=@"";//_modelJson[@"price3"];
  247. // _labelPriceNCA.text=@"N/A";
  248. }
  249. else
  250. {
  251. _labelPriceNet.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_3"][@"name"]] floatValue]];
  252. // _labelPriceNCA.text=@"N/A";
  253. }
  254. // remove net price
  255. // if(price_group[@"price_4"])
  256. // {
  257. // // price4 如果存在,会显示在price3的位置
  258. // NSString* price4=_modelJson[price_group[@"price_4"][@"name"]]; //%25
  259. //
  260. // if(![price4 isEqualToString: @"N/A"])
  261. // price4 = [NSString stringWithFormat:@"%.2f",[_modelJson[price_group[@"price_4"][@"name"]] doubleValue]];
  262. // else
  263. // price4 =@"";
  264. // _labelPriceNet.text=price4;
  265. // }
  266. {
  267. if([_modelJson[price_group[@"price_1"][@"name"]] isEqualToString:@"N/A"])
  268. {
  269. _labelPriceNCA.text=@"";//_modelJson[@"price1"];
  270. // _labelPriceNCA.text=@"N/A";
  271. }
  272. else
  273. {
  274. _labelPriceNCA.text=[RAConvertor currencyNumber:[_modelJson[price_group[@"price_1"][@"name"]] floatValue]];
  275. // _labelPriceNet.text=@"N/A";
  276. }
  277. }
  278. NSString * available=_modelJson[@"available"];
  279. if(available.length==0)
  280. available = @"N/A";
  281. _labelAvailable.text=available;
  282. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  283. if(unit_cuft.length==0)
  284. unit_cuft = @"N/A";
  285. else
  286. {
  287. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  288. }
  289. NSString * cell_price;
  290. if(RASingleton.sharedInstance.price_type==1)
  291. {
  292. // if(_modelJson [@"net_price"])
  293. cell_price=_modelJson[price_group[@"price_3"][@"name"]];
  294. // else if(_modelJson [@"special_price"])
  295. // {
  296. // unit_price= _modelJson[@"price2"];
  297. // }
  298. // else
  299. // {
  300. // unit_price= _modelJson[@"price1"];
  301. // }
  302. }
  303. else
  304. {
  305. cell_price=_modelJson[price_group[@"price_0"][@"name"]];
  306. }
  307. NSString * mpack=_modelJson[@"stockUom"];
  308. if([mpack isEqualToString:@"N/A"])
  309. mpack=@"1";
  310. _modelJson[@"count"]=_modelJson[@"stockUom"];
  311. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  312. _modelJson[@"subtotal_price"] = @([mpack intValue]* [cell_price doubleValue]);
  313. }
  314. @end