ModelItemCell.m 16 KB

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