ModelItemCell.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. //
  2. // CartItemCell.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-7-9.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ModelItemCell.h"
  9. #import "RANetwork.h"
  10. #import "EditModelPriceViewController.h"
  11. #import "RAUtils.h"
  12. #import "BundleModelViewController.h"
  13. #import "CartViewController.h"
  14. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  15. #import "MasterModelViewController.h"
  16. #endif
  17. #define NUMBERS @"0123456789\n"
  18. @implementation ModelItemCell
  19. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  20. {
  21. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  22. if (self) {
  23. // Initialization code
  24. }
  25. return self;
  26. }
  27. -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value
  28. {
  29. // [self.stepper becomeFirstResponder];
  30. if(self.stepper!=nil)
  31. {
  32. if(min<=0)
  33. min=1;
  34. if(step<=0)
  35. step=1;
  36. self.stepper.minimumValue= min;
  37. self.stepper.stepValue= step;
  38. self.stepper.value= value;
  39. self.pre_val = value;
  40. [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  41. self.totalchange=0;
  42. }
  43. }
  44. - (IBAction)setpperChanged:(UIStepper *)sender {
  45. DebugLog(@"setpperChanged");
  46. int val_change=(int)sender.value-self.pre_val;
  47. _totalchange=_totalchange+val_change;
  48. //
  49. // if(val_change==(int)sender.stepValue)
  50. // {
  51. //
  52. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  53. //
  54. // NSDictionary* cart_json = [iSalesNetwork cart_incresement:self.cart_id increse:true];
  55. //
  56. // dispatch_async(dispatch_get_main_queue(), ^{
  57. //
  58. //
  59. //
  60. // if([[cart_json valueForKey:@"result"] intValue]==2)
  61. // {
  62. // if(self.onReturnQTY)
  63. // self.onReturnQTY(sender.value,cart_json);
  64. ////
  65. //// if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  66. //// [self.delegate returnQTY:sender.value ];
  67. // // }
  68. // }
  69. // else
  70. // {
  71. // UIViewController* vc=[RAUtils getViewController :self];
  72. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  73. // }
  74. //
  75. //
  76. //
  77. //
  78. // });
  79. // });
  80. // //increse;
  81. // }
  82. // else if(val_change==-(int)sender.stepValue)
  83. // {
  84. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  85. //
  86. // NSDictionary* cart_json = [iSalesNetwork cart_incresement:self.cart_id increse:false];
  87. //
  88. // dispatch_async(dispatch_get_main_queue(), ^{
  89. //
  90. //
  91. //
  92. //
  93. // if([[cart_json valueForKey:@"result"] intValue]==2)
  94. // {
  95. // if(self.onReturnQTY)
  96. // self.onReturnQTY(sender.value,cart_json);
  97. // }
  98. // else
  99. // {
  100. // UIViewController* vc=[RAUtils getViewController :self];
  101. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  102. // }
  103. //
  104. //
  105. // });
  106. // });
  107. //
  108. // }
  109. // else
  110. // {
  111. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  112. //
  113. // NSDictionary* cart_json = [iSalesNetwork cart_setQTY:self.cart_id value:sender.value];
  114. //
  115. // dispatch_async(dispatch_get_main_queue(), ^{
  116. //
  117. // if([[cart_json valueForKey:@"result"] intValue]==2)
  118. // {
  119. // if(self.onReturnQTY)
  120. // self.onReturnQTY(sender.value,cart_json);
  121. //// if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  122. //// [self.delegate returnQTY:sender.value ];
  123. //// }
  124. // }
  125. // else
  126. // {
  127. // UIViewController* vc=[RAUtils getViewController :self];
  128. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  129. // }
  130. //
  131. //
  132. //
  133. // });
  134. // });
  135. //
  136. // //setvalue;
  137. // }
  138. self.pre_val = (int)sender.value;
  139. [self cancel_setQTY];
  140. [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8];
  141. }
  142. -(void) cancel_setQTY
  143. {
  144. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil];
  145. }
  146. -(void) delayRun
  147. {
  148. // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil];
  149. // // 启动
  150. // [thread start];
  151. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  152. NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:self.pre_val];
  153. dispatch_async(dispatch_get_main_queue(), ^{
  154. self.totalchange=0;
  155. if([[cart_json valueForKey:@"result"] intValue]==2)
  156. {
  157. if(self.onReturnQTY)
  158. self.onReturnQTY(self.pre_val,cart_json);
  159. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  160. // [self.delegate returnQTY:sender.value ];
  161. // }
  162. }
  163. else
  164. {
  165. UIViewController* vc=[RAUtils getViewController :self];
  166. [((CartViewController *)vc).itemListTable reloadData];
  167. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  168. }
  169. });
  170. });
  171. }
  172. - (void)valueChanged:(UIStepper *)sender {
  173. self.editCount.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  174. [self set_Count:(int)sender.value ];
  175. //
  176. // [celldelegate stepClicked:(int)sender.value];
  177. // -(void) stepClicked:(int) value;
  178. }
  179. -(void) set_Count:(int) count
  180. {
  181. self.stepper.value=count;
  182. if(self.enable_longpress)
  183. {
  184. // [self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
  185. }
  186. double dprice=0;
  187. if(self.bundle_item!=nil)
  188. {
  189. int citem=0;
  190. int bcount=[[self.bundle_item valueForKey:@"count"] intValue];
  191. for(int bc=0;bc<bcount;bc++)
  192. {
  193. NSDictionary * bitem = [self.bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  194. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  195. citem+= modulus;
  196. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  197. dprice+= uprice*modulus;
  198. }
  199. if(citem==1)
  200. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle item with price of $ %.2f",citem,dprice ];
  201. else
  202. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle items with price of $ %.2f",citem,dprice ];
  203. self.bundleButton.hidden = NO;
  204. self.bundleLabel.hidden = NO;
  205. }
  206. else
  207. {
  208. self.bundleButton.hidden = YES;
  209. self.bundleLabel.hidden = YES;
  210. }
  211. // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price];
  212. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  213. NSString* price = [NSString stringWithFormat:@"%.2f",(self.unit_price* (1.0-self.discount/100.0)+dprice)*count ];
  214. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  215. {
  216. }
  217. else
  218. {
  219. price=nil;
  220. }
  221. if(self.noprice&&self.unit_price==0.0)
  222. self.labelPrice.text =@"No Price";
  223. else
  224. self.labelPrice.text =price;
  225. self.editCount.text =[NSString stringWithFormat:@"%d",count];
  226. }
  227. - (void)awakeFromNib
  228. {
  229. // Initialization code
  230. }
  231. - (IBAction)masterBundleClick:(UIButton *)sender {
  232. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  233. // BundleModelViewController * bundleVC =[ vc.storyboard instantiateViewControllerWithIdentifier:@"BundleModelViewController"];
  234. //
  235. // bundleVC.content_data = self.master_items;
  236. //
  237. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:bundleVC] ;
  238. //
  239. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  240. // [vc presentViewController:navi animated:YES completion:^{
  241. //
  242. //
  243. // }];
  244. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  245. MasterModelViewController *masterModelVC = [MasterModelViewController buildMasterModelViewController];
  246. masterModelVC.content_data = self.master_items;
  247. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:masterModelVC];
  248. [vc presentViewController:nav animated:YES completion:nil];
  249. #endif
  250. }
  251. //-(UIViewController*) getViewController
  252. //{
  253. // for (UIView* next = [self superview]; next; next = next.superview) {
  254. // UIResponder* nextResponder = [next nextResponder];
  255. // if ([nextResponder isKindOfClass:[UIViewController class]]) {
  256. // return (UIViewController*)nextResponder;
  257. // }
  258. // }
  259. // return nil;
  260. //
  261. //}
  262. - (IBAction)OnClick_BundleDetail:(id)sender {
  263. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  264. BundleModelViewController * bundleVC =[ vc.storyboard instantiateViewControllerWithIdentifier:@"BundleModelViewController"];
  265. bundleVC.content_data = self.bundle_item;
  266. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:bundleVC] ;
  267. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  268. [vc presentViewController:navi animated:YES completion:^{
  269. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  270. DebugLog(@"bundle present.........");
  271. // self.btop = false;
  272. // <#code#>
  273. }];
  274. }
  275. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  276. {
  277. [super setSelected:selected animated:animated];
  278. // Configure the view for the selected state
  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)textFieldDidEndEditing:(UITextField *)textField
  294. {
  295. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  296. int qty = [textField.text intValue];
  297. if(qty==0)
  298. qty=self.stepper.minimumValue;
  299. #ifdef MPACK
  300. if ((int)qty % (int)self.stepper.stepValue != 0) {
  301. UIViewController* vc=[RAUtils getViewController :self];
  302. [((CartViewController *)vc).itemListTable reloadData];
  303. [RAUtils message_alert:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] title:@"Change Model Count" controller:vc];
  304. return;
  305. }
  306. #endif
  307. int c=qty;
  308. int m=c%(int)(self.stepper.stepValue);
  309. if(m!=0&&appDelegate.alert_sold_in_quantities)
  310. {
  311. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",(int)(self.stepper.stepValue)] message:nil preferredStyle:UIAlertControllerStyleAlert];
  312. //block代码块取代了delegate
  313. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  314. [self set_Count:qty];
  315. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  316. NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:qty];
  317. dispatch_async(dispatch_get_main_queue(), ^{
  318. if([[cart_json valueForKey:@"result"] intValue]==2)
  319. {
  320. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  321. // [self.delegate returnQTY:qty];
  322. // }
  323. if(self.onReturnQTY)
  324. self.onReturnQTY(qty,cart_json);
  325. }
  326. else
  327. {
  328. UIViewController* vc=[RAUtils getViewController :self];
  329. [((CartViewController *)vc).itemListTable reloadData];
  330. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  331. }
  332. });
  333. });
  334. }];
  335. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  336. DebugLog(@"No");
  337. textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
  338. }];
  339. [alertControl addAction:actionOne];
  340. [alertControl addAction:alertthree];
  341. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  342. UIViewController *vc= [RAUtils getViewController:self];
  343. [vc presentViewController:alertControl animated:YES completion:nil];
  344. }
  345. else
  346. {
  347. [self set_Count:qty];
  348. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  349. NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:qty];
  350. dispatch_async(dispatch_get_main_queue(), ^{
  351. if([[cart_json valueForKey:@"result"] intValue]==2)
  352. {
  353. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  354. // [self.delegate returnQTY:qty];
  355. // }
  356. if(self.onReturnQTY)
  357. self.onReturnQTY(qty,cart_json);
  358. }
  359. else
  360. {
  361. UIViewController* vc=[RAUtils getViewController :self];
  362. [((CartViewController *)vc).itemListTable reloadData];
  363. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  364. }
  365. });
  366. });
  367. }
  368. }
  369. - (void)textFieldDidBeginEditing:(UITextField *)textField
  370. {
  371. NSString* text = textField.text;
  372. if(text.length==0)
  373. text=@"0";
  374. self.pre_val = [textField.text intValue];
  375. // self.last_edit = textField.text;
  376. }
  377. @end