EditModelPriceViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // EditModelPriceViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 9/16/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "EditModelPriceViewController.h"
  9. #import "RAUtils.h"
  10. #import "iSalesNetwork.h"
  11. #define NUMBERS @"0123456789.\n"
  12. @interface EditModelPriceViewController ()
  13. @end
  14. @implementation EditModelPriceViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.editPrice.text = [NSString stringWithFormat:@"%.2f", self.price ];
  18. self.editDiscount.text = [NSString stringWithFormat:@"%@", [RAUtils FloatFormat:self.discount] ];
  19. self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
  20. if(self.hide_discount)
  21. {
  22. self.editDiscount.hidden=true;
  23. self.labelDiscount.hidden=true;
  24. self.labelCalPrice.text = @"New price:";
  25. }
  26. // Do any additional setup after loading the view.
  27. }
  28. - (void)didReceiveMemoryWarning {
  29. [super didReceiveMemoryWarning];
  30. // Dispose of any resources that can be recreated.
  31. }
  32. /*
  33. #pragma mark - Navigation
  34. // In a storyboard-based application, you will often want to do a little preparation before navigation
  35. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  36. // Get the new view controller using [segue destinationViewController].
  37. // Pass the selected object to the new view controller.
  38. }
  39. */
  40. - (IBAction)onCloseClicked:(id)sender {
  41. [self dismissViewControllerAnimated:NO
  42. completion:^{
  43. }];
  44. }
  45. - (IBAction)onSaveClicked:(id)sender {
  46. if([self.editDiscount.text floatValue]>100.0)
  47. {
  48. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  49. //
  50. //
  51. // [alert show];
  52. [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  53. self.editDiscount.text=@"0";
  54. return;
  55. }
  56. [self update_newprice];
  57. [self dismissViewControllerAnimated:NO
  58. completion:^{
  59. if(self.onSetValue)
  60. self.onSetValue( self.price,self.discount);
  61. }];
  62. /*
  63. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  64. NSDictionary* cart_json = nil;
  65. if(self.set_watch_list)
  66. cart_json= [iSalesNetwork portfolio_setPrice:self.cart_id price:self.price discount:self.discount];
  67. else
  68. cart_json = [iSalesNetwork cart_setPrice:self.cart_id price:self.price discount:self.discount];
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. if([[cart_json valueForKey:@"result"] intValue]==2)
  71. {
  72. [self dismissViewControllerAnimated:NO
  73. completion:^{
  74. }];
  75. }
  76. else
  77. {
  78. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Price" controller:self] ;
  79. }
  80. });
  81. });
  82. */
  83. }
  84. #pragma mark textField delegate
  85. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  86. [textField resignFirstResponder];
  87. return NO;
  88. }
  89. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  90. // int tag = textField.tag;
  91. //
  92. // int len = textField.text.length;
  93. //
  94. // if(textField.tag==2&&textField.text.length>=2 && string.length>0)
  95. // return false;
  96. if(textField.text.length==0 && [string isEqualToString:@"."])
  97. return false;
  98. // DebugLog(@"text:%@",textField.text);
  99. //
  100. //
  101. //
  102. //
  103. //
  104. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  105. //
  106. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  107. // // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  108. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  109. //
  110. // NSString* required = [item_json valueForKey:@"required"];
  111. //
  112. // if([required isEqualToString:@"true"] && string.length==0 && (textField.text.length<=1|| textField.text==nil))
  113. // {
  114. // CALayer *layer = [cell.contentView layer];
  115. // // layer.borderColor = [[UIColor redColor] CGColor];
  116. // // layer.borderWidth = 1.0;
  117. //
  118. // layer.shadowColor = [UIColor redColor].CGColor;
  119. // layer.shadowOffset = CGSizeMake(0, 0);
  120. // layer.shadowOpacity = 1;
  121. // layer.shadowRadius = 2.0;
  122. // }
  123. // else
  124. // {
  125. // CALayer *layer = [cell.contentView layer];
  126. // // layer.borderColor = [[UIColor redColor] CGColor];
  127. // // layer.borderWidth = 1.0;
  128. //
  129. // layer.shadowColor = [UIColor clearColor].CGColor;
  130. // layer.shadowOffset = CGSizeMake(0, 0);
  131. // layer.shadowOpacity = 1;
  132. // layer.shadowRadius = 2.0;
  133. // }
  134. //
  135. //
  136. // NSString* keyboard = [item_json valueForKey:@"keyboard"];
  137. // if(![keyboard isEqualToString:@"number"])
  138. // return TRUE;
  139. NSCharacterSet *cs;
  140. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  141. //
  142. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  143. BOOL canChange = [string isEqualToString:filtered];
  144. //
  145. //
  146. //
  147. //
  148. return canChange;
  149. // return true;
  150. }
  151. -(void) update_newprice
  152. {
  153. self.price = [self.editPrice.text doubleValue];
  154. self.discount = [self.editDiscount.text doubleValue];
  155. self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
  156. }
  157. - (void)textFieldDidEndEditing:(UITextField *)textField
  158. {
  159. if(textField.tag==2)
  160. {
  161. if(textField.text.length==0)
  162. textField.text=@"0";
  163. if([textField.text floatValue]>100.0)
  164. {
  165. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  166. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  167. // [alert show];
  168. [RAUtils alert_view:@"Discount must less than 100." title:@"Input Error."];
  169. textField.text=@"0";
  170. }
  171. }
  172. else
  173. {
  174. float f = [textField.text floatValue];
  175. textField.text=[NSString stringWithFormat:@"%.2f",f];
  176. }
  177. [self update_newprice];
  178. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  179. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  180. //
  181. //
  182. // NSMutableDictionary* section_json=nil;
  183. //
  184. //
  185. // // DebugLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  186. // NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  187. //
  188. //
  189. // [item_json setValue:@"true" forKey:@"dirty"];
  190. // [item_json setValue:textField.text forKey:@"value"];
  191. // section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  192. // int count=0;
  193. //
  194. // count=[[section_json valueForKey:@"count"] intValue];
  195. //
  196. // for(int i=0;i<count;i++)
  197. // {
  198. // NSDictionary * olditem=[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]];
  199. // if([[olditem valueForKey:@"name"] isEqualToString:[item_json valueForKey:@"name" ]])
  200. // [section_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i ]];
  201. //
  202. //
  203. // }
  204. //
  205. // [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  206. }
  207. - (void)textFieldDidBeginEditing:(UITextField *)textField
  208. {
  209. //
  210. // self.lastedit = textField;
  211. }
  212. @end