EditModelPriceViewController.m 8.8 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 "RANetwork.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 message_alert:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing." controller:self];
  53. // [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  54. self.editDiscount.text=@"0";
  55. return;
  56. }
  57. [self update_newprice];
  58. [self dismissViewControllerAnimated:NO
  59. completion:^{
  60. if(self.onSetValue)
  61. self.onSetValue( self.price,self.discount);
  62. }];
  63. /*
  64. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  65. NSDictionary* cart_json = nil;
  66. if(self.set_watch_list)
  67. cart_json= [iSalesNetwork portfolio_setPrice:self.cart_id price:self.price discount:self.discount];
  68. else
  69. cart_json = [iSalesNetwork cart_setPrice:self.cart_id price:self.price discount:self.discount];
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. if([[cart_json valueForKey:@"result"] intValue]==2)
  72. {
  73. [self dismissViewControllerAnimated:NO
  74. completion:^{
  75. }];
  76. }
  77. else
  78. {
  79. [RAUtils message_alert:[cart_json valueForKey:@"err_msg"] title:@"Change Price" controller:self] ;
  80. }
  81. });
  82. });
  83. */
  84. }
  85. #pragma mark textField delegate
  86. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  87. [textField resignFirstResponder];
  88. return NO;
  89. }
  90. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  91. // int tag = textField.tag;
  92. //
  93. // int len = textField.text.length;
  94. //
  95. // if(textField.tag==2&&textField.text.length>=2 && string.length>0)
  96. // return false;
  97. if(textField.text.length==0 && [string isEqualToString:@"."])
  98. return false;
  99. // DebugLog(@"text:%@",textField.text);
  100. //
  101. //
  102. //
  103. //
  104. //
  105. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  106. //
  107. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  108. // // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  109. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  110. //
  111. // NSString* required = [item_json valueForKey:@"required"];
  112. //
  113. // if([required isEqualToString:@"true"] && string.length==0 && (textField.text.length<=1|| textField.text==nil))
  114. // {
  115. // CALayer *layer = [cell.contentView layer];
  116. // // layer.borderColor = [[UIColor redColor] CGColor];
  117. // // layer.borderWidth = 1.0;
  118. //
  119. // layer.shadowColor = [UIColor redColor].CGColor;
  120. // layer.shadowOffset = CGSizeMake(0, 0);
  121. // layer.shadowOpacity = 1;
  122. // layer.shadowRadius = 2.0;
  123. // }
  124. // else
  125. // {
  126. // CALayer *layer = [cell.contentView layer];
  127. // // layer.borderColor = [[UIColor redColor] CGColor];
  128. // // layer.borderWidth = 1.0;
  129. //
  130. // layer.shadowColor = [UIColor clearColor].CGColor;
  131. // layer.shadowOffset = CGSizeMake(0, 0);
  132. // layer.shadowOpacity = 1;
  133. // layer.shadowRadius = 2.0;
  134. // }
  135. //
  136. //
  137. // NSString* keyboard = [item_json valueForKey:@"keyboard"];
  138. // if(![keyboard isEqualToString:@"number"])
  139. // return TRUE;
  140. NSCharacterSet *cs;
  141. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  142. //
  143. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  144. BOOL canChange = [string isEqualToString:filtered];
  145. //
  146. //
  147. //
  148. //
  149. return canChange;
  150. // return true;
  151. }
  152. -(void) update_newprice
  153. {
  154. self.price = [self.editPrice.text doubleValue];
  155. self.discount = [self.editDiscount.text doubleValue];
  156. self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
  157. }
  158. - (void)textFieldDidEndEditing:(UITextField *)textField
  159. {
  160. if(textField.tag==2)
  161. {
  162. if(textField.text.length==0)
  163. textField.text=@"0";
  164. if([textField.text floatValue]>100.0)
  165. {
  166. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  167. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  168. // [alert show];
  169. [RAUtils message_alert:@"Discount must less than 100." title:@"Input Error." controller:self];
  170. // [RAUtils alert_view:@"Discount must less than 100." title:@"Input Error."];
  171. textField.text=@"0";
  172. }
  173. }
  174. else
  175. {
  176. float f = [textField.text floatValue];
  177. textField.text=[NSString stringWithFormat:@"%.2f",f];
  178. }
  179. [self update_newprice];
  180. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  181. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  182. //
  183. //
  184. // NSMutableDictionary* section_json=nil;
  185. //
  186. //
  187. // // DebugLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  188. // NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  189. //
  190. //
  191. // [item_json setValue:@"true" forKey:@"dirty"];
  192. // [item_json setValue:textField.text forKey:@"value"];
  193. // section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  194. // int count=0;
  195. //
  196. // count=[[section_json valueForKey:@"count"] intValue];
  197. //
  198. // for(int i=0;i<count;i++)
  199. // {
  200. // NSDictionary * olditem=[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]];
  201. // if([[olditem valueForKey:@"name"] isEqualToString:[item_json valueForKey:@"name" ]])
  202. // [section_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i ]];
  203. //
  204. //
  205. // }
  206. //
  207. // [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  208. }
  209. - (void)textFieldDidBeginEditing:(UITextField *)textField
  210. {
  211. //
  212. // self.lastedit = textField;
  213. }
  214. @end