ContrastModelItemCell.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 "ContrastModelItemCell.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 ContrastModelItemCell
  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. [RANetwork request_update_cartqty:self.cart_id value:self.pre_val completionHandler:^(NSMutableDictionary *result) {
  152. NSDictionary* cart_json =result;
  153. self.totalchange=0;
  154. if([[cart_json valueForKey:@"result"] intValue]==2)
  155. {
  156. if(self.onReturnQTY)
  157. self.onReturnQTY(self.pre_val,cart_json);
  158. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  159. // [self.delegate returnQTY:sender.value ];
  160. // }
  161. }
  162. else
  163. {
  164. UIViewController* vc=[RAUtils getViewController :self];
  165. [((CartViewController *)vc).itemListTable reloadData];
  166. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  167. }
  168. }];
  169. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  170. //
  171. // NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:self.pre_val];
  172. //
  173. // dispatch_async(dispatch_get_main_queue(), ^{
  174. // self.totalchange=0;
  175. // if([[cart_json valueForKey:@"result"] intValue]==2)
  176. // {
  177. // if(self.onReturnQTY)
  178. // self.onReturnQTY(self.pre_val,cart_json);
  179. // // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  180. // // [self.delegate returnQTY:sender.value ];
  181. // // }
  182. // }
  183. // else
  184. // {
  185. // UIViewController* vc=[RAUtils getViewController :self];
  186. // [((CartViewController *)vc).itemListTable reloadData];
  187. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  188. // }
  189. //
  190. //
  191. //
  192. // });
  193. // });
  194. }
  195. - (void)valueChanged:(UIStepper *)sender {
  196. self.editCount.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  197. [self set_Count:(int)sender.value ];
  198. //
  199. // [celldelegate stepClicked:(int)sender.value];
  200. // -(void) stepClicked:(int) value;
  201. }
  202. -(void) set_Count:(int) count
  203. {
  204. self.stepper.value=count;
  205. if(self.enable_longpress)
  206. {
  207. // [self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
  208. }
  209. double dprice=0;
  210. if(self.bundle_item!=nil)
  211. {
  212. int citem=0;
  213. int bcount=[[self.bundle_item valueForKey:@"count"] intValue];
  214. for(int bc=0;bc<bcount;bc++)
  215. {
  216. NSDictionary * bitem = [self.bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  217. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  218. citem+= modulus;
  219. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  220. dprice+= uprice*modulus;
  221. }
  222. if(citem==1)
  223. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle item with price of $ %.2f",citem,dprice ];
  224. else
  225. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle items with price of $ %.2f",citem,dprice ];
  226. self.bundleButton.hidden = NO;
  227. self.bundleLabel.hidden = NO;
  228. }
  229. else
  230. {
  231. self.bundleButton.hidden = YES;
  232. self.bundleLabel.hidden = YES;
  233. }
  234. // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price];
  235. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  236. NSString* price = [NSString stringWithFormat:@"%.2f",(self.unit_price* (1.0-self.discount/100.0)+dprice)*count ];
  237. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  238. {
  239. }
  240. else
  241. {
  242. price=nil;
  243. }
  244. if(self.noprice&&self.unit_price==0.0)
  245. self.labelPrice.text =@"No Price";
  246. else
  247. self.labelPrice.text =price;
  248. self.editCount.text =[NSString stringWithFormat:@"%d",count];
  249. }
  250. //- (void)awakeFromNib
  251. //{
  252. // // Initialization code
  253. //
  254. //
  255. //
  256. //}
  257. - (IBAction)masterBundleClick:(UIButton *)sender {
  258. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  259. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  260. MasterModelViewController *masterModelVC = [MasterModelViewController buildMasterModelViewController];
  261. masterModelVC.content_data = self.master_items;
  262. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:masterModelVC];
  263. [vc presentViewController:nav animated:YES completion:nil];
  264. #endif
  265. }
  266. //-(UIViewController*) getViewController
  267. //{
  268. // for (UIView* next = [self superview]; next; next = next.superview) {
  269. // UIResponder* nextResponder = [next nextResponder];
  270. // if ([nextResponder isKindOfClass:[UIViewController class]]) {
  271. // return (UIViewController*)nextResponder;
  272. // }
  273. // }
  274. // return nil;
  275. //
  276. //}
  277. - (IBAction)OnClick_BundleDetail:(id)sender {
  278. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  279. BundleModelViewController * bundleVC =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"BundleModelViewController"];
  280. bundleVC.content_data = self.bundle_item;
  281. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:bundleVC] ;
  282. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  283. [vc presentViewController:navi animated:YES completion:^{
  284. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  285. DebugLog(@"bundle present.........");
  286. // self.btop = false;
  287. // <#code#>
  288. }];
  289. }
  290. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  291. {
  292. [super setSelected:selected animated:animated];
  293. // Configure the view for the selected state
  294. }
  295. #pragma mark textField delegate
  296. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  297. [textField resignFirstResponder];
  298. return NO;
  299. }
  300. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  301. DebugLog(@"text:%@",textField.text);
  302. NSCharacterSet *cs;
  303. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  304. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  305. BOOL canChange = [string isEqualToString:filtered];
  306. return canChange;
  307. }
  308. - (void)textFieldDidEndEditing:(UITextField *)textField
  309. {
  310. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  311. int qty = [textField.text intValue];
  312. if(qty==0)
  313. qty=self.stepper.minimumValue;
  314. #ifdef MPACK
  315. if ((int)qty % (int)self.stepper.stepValue != 0) {
  316. UIViewController* vc=[RAUtils getViewController :self];
  317. [((CartViewController *)vc).itemListTable reloadData];
  318. [RAUtils message_alert:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] title:@"Change Model Count" controller:vc];
  319. return;
  320. }
  321. #endif
  322. int c=qty;
  323. int m=c%(int)(self.stepper.stepValue);
  324. if(m!=0&&appDelegate.alert_sold_in_quantities)
  325. {
  326. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",(int)(self.stepper.stepValue)] message:nil preferredStyle:UIAlertControllerStyleAlert];
  327. //block代码块取代了delegate
  328. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  329. [self set_Count:qty];
  330. [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
  331. NSDictionary* cart_json =result;
  332. if([[cart_json valueForKey:@"result"] intValue]==2)
  333. {
  334. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  335. // [self.delegate returnQTY:qty];
  336. // }
  337. if(self.onReturnQTY)
  338. self.onReturnQTY(qty,cart_json);
  339. }
  340. else
  341. {
  342. UIViewController* vc=[RAUtils getViewController :self];
  343. [((CartViewController *)vc).itemListTable reloadData];
  344. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  345. }
  346. }];
  347. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  348. //
  349. // NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:qty];
  350. //
  351. // dispatch_async(dispatch_get_main_queue(), ^{
  352. //
  353. // if([[cart_json valueForKey:@"result"] intValue]==2)
  354. // {
  355. // // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  356. // // [self.delegate returnQTY:qty];
  357. // // }
  358. //
  359. // if(self.onReturnQTY)
  360. // self.onReturnQTY(qty,cart_json);
  361. // }
  362. // else
  363. // {
  364. // UIViewController* vc=[RAUtils getViewController :self];
  365. // [((CartViewController *)vc).itemListTable reloadData];
  366. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  367. // }
  368. //
  369. //
  370. //
  371. // });
  372. // });
  373. }];
  374. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  375. DebugLog(@"No");
  376. textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
  377. }];
  378. [alertControl addAction:actionOne];
  379. [alertControl addAction:alertthree];
  380. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  381. UIViewController *vc= [RAUtils getViewController:self];
  382. [vc presentViewController:alertControl animated:YES completion:nil];
  383. }
  384. else
  385. {
  386. [self set_Count:qty];
  387. [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
  388. NSDictionary* cart_json =result;
  389. if([[cart_json valueForKey:@"result"] intValue]==2)
  390. {
  391. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  392. // [self.delegate returnQTY:qty];
  393. // }
  394. if(self.onReturnQTY)
  395. self.onReturnQTY(qty,cart_json);
  396. }
  397. else
  398. {
  399. UIViewController* vc=[RAUtils getViewController :self];
  400. [((CartViewController *)vc).itemListTable reloadData];
  401. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  402. }
  403. }];
  404. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  405. //
  406. // NSDictionary* cart_json = [RANetwork cart_setQTY:self.cart_id value:qty];
  407. //
  408. // dispatch_async(dispatch_get_main_queue(), ^{
  409. //
  410. // if([[cart_json valueForKey:@"result"] intValue]==2)
  411. // {
  412. // // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  413. // // [self.delegate returnQTY:qty];
  414. // // }
  415. //
  416. // if(self.onReturnQTY)
  417. // self.onReturnQTY(qty,cart_json);
  418. // }
  419. // else
  420. // {
  421. // UIViewController* vc=[RAUtils getViewController :self];
  422. // [((CartViewController *)vc).itemListTable reloadData];
  423. // [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Model Count" controller:vc] ;
  424. // }
  425. //
  426. //
  427. //
  428. // });
  429. // });
  430. }
  431. }
  432. - (void)textFieldDidBeginEditing:(UITextField *)textField
  433. {
  434. NSString* text = textField.text;
  435. if(text.length==0)
  436. text=@"0";
  437. self.pre_val = [textField.text intValue];
  438. // self.last_edit = textField.text;
  439. }
  440. @end