PortfolioEditQTYViewController.m 18 KB

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