PortfolioEditQTYViewController.m 15 KB

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