PortfolioEditQTYViewController.m 20 KB

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