ScanListCell.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. @implementation ScanListCell
  15. - (void)awakeFromNib {
  16. [super awakeFromNib];
  17. // Initialization code
  18. }
  19. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  20. [super setSelected:selected animated:animated];
  21. // Configure the view for the selected state
  22. }
  23. - (IBAction)onAddToCart:(id)sender {
  24. // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  25. if(RASingleton.sharedInstance.scan_cart ==nil)
  26. {
  27. // NSData* json =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:URL_SO_CART ofType:@"json" ]];
  28. NSMutableDictionary* cartTemplate=[OLDataProvider loadScanTemplate:@"scan_cart.json"];
  29. RASingleton.sharedInstance.scan_cart=cartTemplate;//[[RAConvertor data2dict:json] mutableCopy];
  30. }
  31. // 初始化为 FOB CTNR;
  32. if(RASingleton.sharedInstance.scan_cart[@"price_type"] ==nil)
  33. {
  34. RASingleton.sharedInstance.scan_cart[@"price_type"] = @0;
  35. RASingleton.sharedInstance.price_type = 0;
  36. }
  37. NSMutableDictionary* section =[RASingleton.sharedInstance.scan_cart[@"section_0"] mutableCopy];
  38. int count =[section[@"count"] intValue];
  39. NSMutableDictionary* jitem = nil;
  40. jitem = [self.modelJson mutableCopy];
  41. int stockUom =[jitem[@"stockUom"] intValue];
  42. if(stockUom==0)
  43. stockUom=1;
  44. bool newitem = true;
  45. for(int i=0;i<count;i++)
  46. {
  47. NSMutableDictionary* litem = [section[[NSString stringWithFormat:@"item_%i",i]] mutableCopy];
  48. if([litem[@"product_id"] isEqualToString:jitem[@"product_id"]])
  49. {
  50. // int oldcount = [litem[@"stockUom"] intValue];
  51. newitem = false;
  52. litem[@"count"]=@([litem[@"count"] intValue] +[jitem[@"count"] intValue]);
  53. litem[@"cuft"]=@([litem[@"count"] intValue] * [litem[@"unit_cuft"] doubleValue]);
  54. section[[NSString stringWithFormat:@"item_%i",i]] = litem;
  55. break;
  56. }
  57. }
  58. if(newitem)
  59. {
  60. jitem[@"count"]=@(stockUom);
  61. jitem[@"check"]=@(true);
  62. jitem[@"cart_item_id"]=[NSUUID UUID].UUIDString;
  63. NSString * unit_price;
  64. if(RASingleton.sharedInstance.price_type==2)
  65. {
  66. unit_price=jitem[@"price2"];
  67. }
  68. else if(RASingleton.sharedInstance.price_type==1)
  69. {
  70. unit_price=jitem[@"price1"];
  71. }
  72. else
  73. {
  74. unit_price=jitem[@"price0"];
  75. }
  76. if(unit_price.length==0)
  77. unit_price = @"0";
  78. else
  79. {
  80. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  81. }
  82. jitem[@"unit_price"] = unit_price;
  83. section[[NSString stringWithFormat:@"item_%i",count]] = jitem;
  84. section[@"count"]= @(count+1);
  85. count++;
  86. }
  87. RASingleton.sharedInstance.scan_cart[@"section_0"] = section;
  88. //加list
  89. [ActiveViewController Notify:@"CartViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  90. [OLDataProvider saveScanCart:RASingleton.sharedInstance.scan_cart];
  91. [RAUtils message_alert:@"Successful" title:@"Add to cart" controller:[RAUtils getViewController:self]];
  92. }
  93. - (void)setModelJson:(NSMutableDictionary *)modelJson
  94. {
  95. _modelJson = modelJson;
  96. NSLog( [RAConvertor dict2string:modelJson]);
  97. NSString * port=_modelJson[@"port"];
  98. if(port.length==0)
  99. port = @"N/A";
  100. NSString * origin=_modelJson[@"origin"];
  101. if(origin.length==0)
  102. origin = @"N/A";
  103. NSString * dimension=_modelJson[@"dimension"];
  104. if(dimension.length==0)
  105. dimension = @"N/A";
  106. _labelModel.text=_modelJson[@"model"];
  107. _labelDescription.text=_modelJson[@"description"];
  108. _labelDimension.text=dimension;
  109. _labelCuft.text=_modelJson[@"unit_cuft"];
  110. _labelOrigin.text=origin;
  111. _labelPort.text=port;
  112. NSString* price0=_modelJson[@"price0"];
  113. NSString* price1=_modelJson[@"price1"];
  114. NSString* price2;
  115. if(price0.length==0)
  116. price0 = @"N/A";
  117. else
  118. price0 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price0"] doubleValue]];
  119. if(price1.length==0)
  120. price1 = @"N/A";
  121. else
  122. price1 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue]];
  123. if(![price1 isEqualToString:@"N/A"])
  124. {
  125. price2 = [NSString stringWithFormat:@"%.2f",[_modelJson[@"price1"] doubleValue] *1.25];
  126. _modelJson[@"price2"]= price2;
  127. }
  128. else
  129. {
  130. price2=@"N/A";
  131. }
  132. _labelPriceCTNR.text=price0;
  133. _labelPriceNCA.text=price1;
  134. _labelPrice25p.text=price2;
  135. NSString * available=_modelJson[@"available"];
  136. if(available.length==0)
  137. available = @"N/A";
  138. _labelAvailable.text=available;
  139. NSString * unit_cuft=_modelJson[@"unit_cuft"];
  140. if(unit_cuft.length==0)
  141. unit_cuft = @"N/A";
  142. else
  143. {
  144. unit_cuft = [NSString stringWithFormat:@"%.2f", [_modelJson[@"unit_cuft"] doubleValue]];
  145. }
  146. NSString * unit_price;
  147. if(RASingleton.sharedInstance.price_type==2)
  148. {
  149. unit_price=_modelJson[@"price2"];
  150. }
  151. else if(RASingleton.sharedInstance.price_type==1)
  152. {
  153. unit_price=_modelJson[@"price1"];
  154. }
  155. else
  156. {
  157. unit_price=_modelJson[@"price0"];
  158. }
  159. if(unit_price.length==0)
  160. unit_price = @"N/A";
  161. else
  162. {
  163. unit_price = [NSString stringWithFormat:@"%.2f", [unit_price doubleValue]];
  164. }
  165. NSString * mpack=_modelJson[@"stockUom"];
  166. _modelJson[@"cuft"] = @([mpack intValue]* [unit_cuft doubleValue]);
  167. _modelJson[@"subtotal_price"] = @([mpack intValue]* [unit_price doubleValue]);
  168. }
  169. @end