ModelItemCell.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. self.pre_val = (int)sender.value;
  49. [self cancel_setQTY];
  50. [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8];
  51. }
  52. -(void) cancel_setQTY
  53. {
  54. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil];
  55. }
  56. -(void) delayRun
  57. {
  58. // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil];
  59. // // 启动
  60. // [thread start];
  61. [RANetwork request_update_cartqty:self.cart_id value:self.pre_val completionHandler:^(NSMutableDictionary *result) {
  62. NSDictionary* cart_json =result;
  63. self.totalchange=0;
  64. if([[cart_json valueForKey:@"result"] intValue]==2)
  65. {
  66. if(self.onReturnQTY)
  67. self.onReturnQTY(self.pre_val,cart_json);
  68. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  69. // [self.delegate returnQTY:sender.value ];
  70. // }
  71. }
  72. else
  73. {
  74. UIViewController* vc=[RAUtils getViewController :self];
  75. [((CartViewController *)vc).itemListTable reloadData];
  76. // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  77. [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil];
  78. }
  79. }];
  80. }
  81. - (void)valueChanged:(UIStepper *)sender {
  82. self.editCount.text = [NSString stringWithFormat:@"%d",(int)sender.value ];
  83. [self set_Count:(int)sender.value ];
  84. //
  85. // [celldelegate stepClicked:(int)sender.value];
  86. // -(void) stepClicked:(int) value;
  87. }
  88. -(void) set_Count:(int) count
  89. {
  90. self.stepper.value=count;
  91. if(self.enable_longpress)
  92. {
  93. // [self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
  94. }
  95. double dprice=0;
  96. if(self.bundle_item!=nil)
  97. {
  98. int citem=0;
  99. int bcount=[[self.bundle_item valueForKey:@"count"] intValue];
  100. for(int bc=0;bc<bcount;bc++)
  101. {
  102. NSDictionary * bitem = [self.bundle_item objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  103. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  104. citem+= modulus;
  105. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  106. dprice+= uprice*modulus;
  107. }
  108. if(citem==1)
  109. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle item with price of $ %.2f",citem,dprice ];
  110. else
  111. self.bundleLabel.text= [NSString stringWithFormat: @"%d bundle items with price of $ %.2f",citem,dprice ];
  112. self.bundleButton.hidden = NO;
  113. self.bundleLabel.hidden = NO;
  114. }
  115. else
  116. {
  117. self.bundleButton.hidden = YES;
  118. self.bundleLabel.hidden = YES;
  119. }
  120. // self.labelUnitPrice.text =[NSString stringWithFormat:@"%.2f",self.unit_price];
  121. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  122. NSString* price = [NSString stringWithFormat:@"%.2f",(self.unit_price* (1.0-self.discount/100.0)+dprice)*count ];
  123. if(appDelegate.can_see_price&&appDelegate.price_hidden==false)
  124. {
  125. }
  126. else
  127. {
  128. price=nil;
  129. }
  130. if(self.noprice&&self.unit_price==0.0)
  131. self.labelPrice.text =@"No Price";
  132. else
  133. self.labelPrice.text =price;
  134. self.editCount.text =[NSString stringWithFormat:@"%d",count];
  135. }
  136. //- (void)awakeFromNib
  137. //{
  138. // // Initialization code
  139. //
  140. //
  141. //
  142. //}
  143. - (IBAction)masterBundleClick:(UIButton *)sender {
  144. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  145. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  146. MasterModelViewController *masterModelVC = [MasterModelViewController buildMasterModelViewController];
  147. masterModelVC.content_data = self.master_items;
  148. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:masterModelVC];
  149. [vc presentViewController:nav animated:YES completion:nil];
  150. #endif
  151. }
  152. //-(UIViewController*) getViewController
  153. //{
  154. // for (UIView* next = [self superview]; next; next = next.superview) {
  155. // UIResponder* nextResponder = [next nextResponder];
  156. // if ([nextResponder isKindOfClass:[UIViewController class]]) {
  157. // return (UIViewController*)nextResponder;
  158. // }
  159. // }
  160. // return nil;
  161. //
  162. //}
  163. - (IBAction)OnClick_BundleDetail:(id)sender {
  164. UIViewController* vc=[RAUtils getViewController :self.bundleButton];
  165. BundleModelViewController * bundleVC =[[UIStoryboard storyboardWithName:@"ERP_Mobile_Model" bundle:nil] instantiateViewControllerWithIdentifier:@"BundleModelViewController"];
  166. bundleVC.content_data = self.bundle_item;
  167. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:bundleVC] ;
  168. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  169. [vc presentViewController:navi animated:YES completion:^{
  170. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  171. DebugLog(@"bundle present.........");
  172. // self.btop = false;
  173. // <#code#>
  174. }];
  175. }
  176. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  177. {
  178. [super setSelected:selected animated:animated];
  179. // Configure the view for the selected state
  180. }
  181. #pragma mark textField delegate
  182. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  183. [textField resignFirstResponder];
  184. return NO;
  185. }
  186. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  187. DebugLog(@"text:%@",textField.text);
  188. NSCharacterSet *cs;
  189. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  190. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  191. BOOL canChange = [string isEqualToString:filtered];
  192. return canChange;
  193. }
  194. - (void)textFieldDidEndEditing:(UITextField *)textField
  195. {
  196. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  197. int qty = [textField.text intValue];
  198. if(qty==0)
  199. qty=self.stepper.minimumValue;
  200. #ifdef MPACK
  201. if ((int)qty % (int)self.stepper.stepValue != 0) {
  202. UIViewController* vc=[RAUtils getViewController :self];
  203. [((CartViewController *)vc).itemListTable reloadData];
  204. [RAUtils message_box:@"Change Model Count" message:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] completion:nil];
  205. return;
  206. }
  207. #endif
  208. int c=qty;
  209. int m=c%(int)(self.stepper.stepValue);
  210. if(m!=0&&appDelegate.alert_sold_in_quantities)
  211. {
  212. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",(int)(self.stepper.stepValue)] message:nil preferredStyle:UIAlertControllerStyleAlert];
  213. //block代码块取代了delegate
  214. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  215. [self set_Count:qty];
  216. [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
  217. NSDictionary* cart_json =result;
  218. if([[cart_json valueForKey:@"result"] intValue]==2)
  219. {
  220. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  221. // [self.delegate returnQTY:qty];
  222. // }
  223. if(self.onReturnQTY)
  224. self.onReturnQTY(qty,cart_json);
  225. }
  226. else
  227. {
  228. UIViewController* vc=[RAUtils getViewController :self];
  229. [((CartViewController *)vc).itemListTable reloadData];
  230. // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  231. [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil];
  232. }
  233. }];
  234. }];
  235. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  236. DebugLog(@"No");
  237. textField.text = [NSString stringWithFormat:@"%d",self.pre_val];
  238. }];
  239. [alertControl addAction:actionOne];
  240. [alertControl addAction:alertthree];
  241. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  242. UIViewController *vc= [RAUtils getViewController:self];
  243. [vc presentViewController:alertControl animated:YES completion:nil];
  244. }
  245. else
  246. {
  247. [self set_Count:qty];
  248. [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) {
  249. NSDictionary* cart_json =result;
  250. if([[cart_json valueForKey:@"result"] intValue]==2)
  251. {
  252. // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) {
  253. // [self.delegate returnQTY:qty];
  254. // }
  255. if(self.onReturnQTY)
  256. self.onReturnQTY(qty,cart_json);
  257. }
  258. else
  259. {
  260. UIViewController* vc=[RAUtils getViewController :self];
  261. [((CartViewController *)vc).itemListTable reloadData];
  262. // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ;
  263. [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil];
  264. }
  265. }];
  266. }
  267. }
  268. - (void)textFieldDidBeginEditing:(UITextField *)textField
  269. {
  270. NSString* text = textField.text;
  271. if(text.length==0)
  272. text=@"0";
  273. self.pre_val = [textField.text intValue];
  274. // self.last_edit = textField.text;
  275. }
  276. @end