PortfolioEditQTYViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. //
  2. // PortfolioEditQTYViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 3/18/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PortfolioEditQTYViewController.h"
  9. #import "iSalesNetwork.h"
  10. #define INTNUMBERS @"0123456789\n"
  11. #define NUMBERS @"0123456789.\n"
  12. @interface PortfolioEditQTYViewController ()
  13. @end
  14. @implementation PortfolioEditQTYViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.edgesForExtendedLayout = UIRectEdgeNone;
  18. self.tv_linenote.delegate = self;
  19. self.tv_linenote.text=self.linenotes ;
  20. if(self.qty>=0)
  21. {
  22. self.editQTY.text = [NSString stringWithFormat:@"%d",self.qty];
  23. }
  24. else if(self.qty_p>=0)
  25. {
  26. self.editQTY.text= [NSString stringWithFormat:@"%.2f",self.qty_p];
  27. [self.switchQTY setOn: true];
  28. }
  29. [self requestqty];
  30. self.editPrice.text = [NSString stringWithFormat:@"%.2f", self.price ];
  31. self.editDiscount.text = [NSString stringWithFormat:@"%@", [RAUtils FloatFormat:self.discount] ];
  32. self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
  33. if(self.arr_fashion_ids.count!=1)
  34. {
  35. self.label_multiItems.text=[NSString stringWithFormat:@"%d items selected",self.ids_count ];
  36. self.label_multiItems.hidden = false;
  37. self.editPrice.enabled = false;
  38. self.editPrice.text = nil;
  39. self.buttonRefresh.hidden = true;
  40. self.labelQueryQTY.hidden = true;
  41. self.labelNewPrice.hidden = true;
  42. self.labelpriceafter.hidden = true;
  43. }
  44. self.tv_linenote.layer.backgroundColor = [[UIColor clearColor] CGColor];
  45. self.tv_linenote.layer.borderColor = [[UIColor lightGrayColor] CGColor];
  46. self.tv_linenote.layer.borderWidth = 0.5;
  47. self.tv_linenote.layer.cornerRadius = 8.0f;
  48. [self.tv_linenote.layer setMasksToBounds:YES];
  49. // if(self.hide_discount)
  50. // {
  51. // self.editDiscount.hidden=true;
  52. // self.labelDiscount.hidden=true;
  53. // self.labelCalPrice.text = @"New price:";
  54. //
  55. //
  56. //
  57. // }
  58. // Do any additional setup after loading the view.
  59. }
  60. - (IBAction)onRefreshClick:(id)sender {
  61. [self requestqty];
  62. }
  63. - (void)didReceiveMemoryWarning {
  64. [super didReceiveMemoryWarning];
  65. // Dispose of any resources that can be recreated.
  66. }
  67. -(void) requestqty
  68. {
  69. if(self.arr_fashion_ids.count!=1)
  70. return;
  71. self.buttonRefresh.enabled=false;
  72. self.labelQueryQTY.text = @"Query current QTY...";
  73. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  74. NSDictionary* detail_json = [iSalesNetwork request_model_qty:self.arr_fashion_ids[0]];
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. self.buttonRefresh.enabled=true;
  77. // NSDictionary* imgsection = [detail_json objectForKey:@"img_section"];
  78. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  79. if([[detail_json valueForKey:@"result"] intValue]==2)
  80. {
  81. NSString* qty = [detail_json valueForKey:@"quantity_available"];
  82. self.labelQueryQTY.text = [NSString stringWithFormat:@"Current Available QTY: %@",qty];
  83. // self.detailTable.hidden = false;
  84. }
  85. else
  86. {
  87. self.labelQueryQTY.text=@"Tap Refresh to query current QTY";
  88. [RAUtils message_alert:[detail_json valueForKey:@"err_msg"] title:@"Request Available QTY"/*@"Loading Model Detail Failed."*/ controller:self] ;
  89. }
  90. });
  91. });
  92. }
  93. - (IBAction)QTYPercentageValueChanged:(id)sender {
  94. UISwitch * switchqty =(UISwitch*)sender;
  95. if(switchqty.isOn)
  96. {
  97. if([self.editQTY.text intValue]>100)
  98. self.editQTY.text=@"100";
  99. self.labelQTYTitle.text = @"Available QTY %";
  100. self.editQTY.placeholder =@"Percentage";
  101. }
  102. else
  103. {
  104. self.editQTY.text=[NSString stringWithFormat:@"%d",[self.editQTY.text intValue]];
  105. self.labelQTYTitle.text = @"Available QTY";
  106. self.editQTY.placeholder =@"QTY";
  107. }
  108. }
  109. - (IBAction)onSaveClick:(id)sender {
  110. // if([self.editDiscount.text floatValue]>100.0)
  111. // {
  112. // // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  113. // //
  114. // //
  115. // // [alert show];
  116. //
  117. // [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
  118. //
  119. // self.editDiscount.text=@"0";
  120. //
  121. // return;
  122. // }
  123. //
  124. //
  125. // [self update_newprice];
  126. if([self.editDiscount.text floatValue]>100.0)
  127. {
  128. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  129. //
  130. //
  131. // [alert show];
  132. [RAUtils alert_view:@"Percentage must less than 100." title:@"Input Error"];
  133. self.editDiscount.text=@"0";
  134. return;
  135. }
  136. if([self.editQTY.text floatValue]>100.0 && self.switchQTY.isOn)
  137. {
  138. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  139. //
  140. //
  141. // [alert show];
  142. [RAUtils alert_view:@"Percentage must less than 100." title:@"Input Error"];
  143. self.editDiscount.text=@"0";
  144. return;
  145. }
  146. [self update_newprice];
  147. bool qtypercentage = self.switchQTY.isOn;
  148. if(self.editQTY.text.length==0)
  149. {
  150. self.qty=-1;
  151. self.qty_p=-1;
  152. }
  153. else
  154. if(qtypercentage)
  155. {
  156. self.qty_p=[self.editQTY.text floatValue];
  157. self.qty=-1;
  158. }
  159. else
  160. {
  161. self.qty=[self.editQTY.text intValue];
  162. self.qty_p=-1;
  163. }
  164. [self dismissViewControllerAnimated:NO
  165. completion:^{
  166. if(self.onSetValue)
  167. self.onSetValue( self.qty, self.qty_p,self.price,self.discount, self.linenotes);
  168. }];
  169. }
  170. - (IBAction)onCloseClicked:(id)sender {
  171. [self dismissViewControllerAnimated:NO
  172. completion:^{
  173. }];
  174. }
  175. /*
  176. #pragma mark - Navigation
  177. // In a storyboard-based application, you will often want to do a little preparation before navigation
  178. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  179. // Get the new view controller using [segue destinationViewController].
  180. // Pass the selected object to the new view controller.
  181. }
  182. */
  183. #pragma mark textView delegate
  184. - (void)textViewDidEndEditing:(UITextView *)textView
  185. {
  186. [self update_newprice];
  187. }
  188. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  189. {
  190. int lenth = 20;
  191. return (lenth>textView.text.length||[text isEqualToString:@""]);
  192. }
  193. #pragma mark textField delegate
  194. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  195. [textField resignFirstResponder];
  196. return NO;
  197. }
  198. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  199. // int tag = textField.tag;
  200. //
  201. // int len = textField.text.length;
  202. //
  203. // if(textField.tag==2&&textField.text.length>=2 && string.length>0)
  204. // return false;
  205. if(textField.text.length==0 && [string isEqualToString:@"."])
  206. return false;
  207. // NSLog(@"text:%@",textField.text);
  208. //
  209. //
  210. //
  211. //
  212. //
  213. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  214. //
  215. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  216. // // NSLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
  217. // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
  218. //
  219. // NSString* required = [item_json valueForKey:@"required"];
  220. //
  221. // if([required isEqualToString:@"true"] && string.length==0 && (textField.text.length<=1|| textField.text==nil))
  222. // {
  223. // CALayer *layer = [cell.contentView layer];
  224. // // layer.borderColor = [[UIColor redColor] CGColor];
  225. // // layer.borderWidth = 1.0;
  226. //
  227. // layer.shadowColor = [UIColor redColor].CGColor;
  228. // layer.shadowOffset = CGSizeMake(0, 0);
  229. // layer.shadowOpacity = 1;
  230. // layer.shadowRadius = 2.0;
  231. // }
  232. // else
  233. // {
  234. // CALayer *layer = [cell.contentView layer];
  235. // // layer.borderColor = [[UIColor redColor] CGColor];
  236. // // layer.borderWidth = 1.0;
  237. //
  238. // layer.shadowColor = [UIColor clearColor].CGColor;
  239. // layer.shadowOffset = CGSizeMake(0, 0);
  240. // layer.shadowOpacity = 1;
  241. // layer.shadowRadius = 2.0;
  242. // }
  243. //
  244. //
  245. // NSString* keyboard = [item_json valueForKey:@"keyboard"];
  246. // if(![keyboard isEqualToString:@"number"])
  247. // return TRUE;
  248. NSCharacterSet *cs;
  249. if(textField.tag==199&& ! self.switchQTY.isOn)
  250. {
  251. cs = [[NSCharacterSet characterSetWithCharactersInString:INTNUMBERS]invertedSet];
  252. }
  253. else
  254. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  255. //
  256. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  257. BOOL canChange = [string isEqualToString:filtered];
  258. //
  259. //
  260. //
  261. //
  262. return canChange;
  263. // return true;
  264. }
  265. -(void) update_newprice
  266. {
  267. self.price = [self.editPrice.text doubleValue];
  268. self.discount = [self.editDiscount.text doubleValue];
  269. self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
  270. self.linenotes = self.tv_linenote.text;
  271. }
  272. - (void)textFieldDidEndEditing:(UITextField *)textField
  273. {
  274. if(textField.tag==2 || (textField.tag==199&& self.switchQTY.isOn))
  275. {
  276. if(textField.text.length==0)
  277. textField.text=@"0";
  278. if([textField.text floatValue]>100.0)
  279. {
  280. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  281. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  282. // [alert show];
  283. [RAUtils alert_view:@"Percentage must less than 100." title:@"Input Error."];
  284. textField.text=@"0";
  285. }
  286. }
  287. else
  288. {
  289. if(textField.tag==199)
  290. {
  291. int i = [textField.text intValue];
  292. textField.text=[NSString stringWithFormat:@"%d",i];
  293. }
  294. else
  295. {
  296. float f = [textField.text floatValue];
  297. textField.text=[NSString stringWithFormat:@"%.2f",f];
  298. }
  299. }
  300. [self update_newprice];
  301. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  302. // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  303. //
  304. //
  305. // NSMutableDictionary* section_json=nil;
  306. //
  307. //
  308. // // NSLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  309. // NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  310. //
  311. //
  312. // [item_json setValue:@"true" forKey:@"dirty"];
  313. // [item_json setValue:textField.text forKey:@"value"];
  314. // section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  315. // int count=0;
  316. //
  317. // count=[[section_json valueForKey:@"count"] intValue];
  318. //
  319. // for(int i=0;i<count;i++)
  320. // {
  321. // NSDictionary * olditem=[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]];
  322. // if([[olditem valueForKey:@"name"] isEqualToString:[item_json valueForKey:@"name" ]])
  323. // [section_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i ]];
  324. //
  325. //
  326. // }
  327. //
  328. // [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  329. }
  330. - (void)textFieldDidBeginEditing:(UITextField *)textField
  331. {
  332. //
  333. // self.lastedit = textField;
  334. }
  335. //
  336. //#pragma mark textField delegate
  337. //- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  338. // [textField resignFirstResponder];
  339. // return NO;
  340. //}
  341. //-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  342. //
  343. //
  344. //
  345. // if(textField.text.length==0 && [string isEqualToString:@"."])
  346. // return false;
  347. //
  348. // NSCharacterSet *cs;
  349. // cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  350. // //
  351. // NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  352. //
  353. // BOOL canChange = [string isEqualToString:filtered];
  354. // //
  355. // //
  356. // //
  357. // //
  358. // return canChange;
  359. // // return true;
  360. //}
  361. //- (void)textFieldDidEndEditing:(UITextField *)textField
  362. //{
  363. //
  364. //// if(textField.tag==2)
  365. //// {
  366. //// if(textField.text.length==0)
  367. //// textField.text=@"0";
  368. ////
  369. //// if([textField.text floatValue]>100.0)
  370. //// {
  371. //// // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  372. //// // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  373. //// // [alert show];
  374. ////
  375. ////
  376. ////
  377. //// [RAUtils alert_view:@"Discount must less than 100." title:@"Input Error."];
  378. //// textField.text=@"0";
  379. //// }
  380. //// }
  381. //// else
  382. //// {
  383. //// float f = [textField.text floatValue];
  384. //// textField.text=[NSString stringWithFormat:@"%.2f",f];
  385. //// }
  386. ////
  387. //// [self update_newprice];
  388. //
  389. // // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  390. // // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
  391. // //
  392. // //
  393. // // NSMutableDictionary* section_json=nil;
  394. // //
  395. // //
  396. // // // NSLog(@"endedit %d_%d",indexPath.section,indexPath.row);
  397. // // NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
  398. // //
  399. // //
  400. // // [item_json setValue:@"true" forKey:@"dirty"];
  401. // // [item_json setValue:textField.text forKey:@"value"];
  402. // // section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
  403. // // int count=0;
  404. // //
  405. // // count=[[section_json valueForKey:@"count"] intValue];
  406. // //
  407. // // for(int i=0;i<count;i++)
  408. // // {
  409. // // NSDictionary * olditem=[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]];
  410. // // if([[olditem valueForKey:@"name"] isEqualToString:[item_json valueForKey:@"name" ]])
  411. // // [section_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i ]];
  412. // //
  413. // //
  414. // // }
  415. // //
  416. // // [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
  417. //}
  418. //
  419. //- (void)textFieldDidBeginEditing:(UITextField *)textField
  420. //{
  421. // //
  422. // // self.lastedit = textField;
  423. //}
  424. @end