| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- //
- // PortfolioEditQTYViewController.m
- // iSales-NPD
- //
- // Created by Ray on 3/18/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "PortfolioEditQTYViewController.h"
- #import "RANetwork.h"
- #define INTNUMBERS @"0123456789\n"
- #define NUMBERS @"0123456789.\n"
- @interface PortfolioEditQTYViewController ()
- @property (nonatomic,assign) int checkedCount;
- @property (strong, nonatomic) IBOutlet UIButton *saveButton;
- @end
- @implementation PortfolioEditQTYViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // self.edgesForExtendedLayout = UIRectEdgeNone;
- self.checkedCount = 0;
- self.saveButton.hidden = YES;
- [self addObserver:self forKeyPath:@"checkedCount" options:NSKeyValueObservingOptionNew context:nil];
-
- // self.tv_linenote.delegate = self;
- // self.tv_linenote.text=self.linenotes ;
- self.editLineNotes.text = self.linenotes;
- if(self.qty>=0)
- {
- self.editQTY.text = [NSString stringWithFormat:@"%d",self.qty];
- }
- else if(self.qty_p>=0)
- {
- self.editQTY.text= [NSString stringWithFormat:@"%.2f",self.qty_p];
- [self.switchQTY setOn: true];
- }
- [self requestqty];
-
- DebugLog(@"price:%f",self.price);
- self.editPrice.text = [NSString stringWithFormat:@"%.2f", self.price ];
-
-
- self.editDiscount.text = [NSString stringWithFormat:@"%@", [RAUtils FloatFormat:self.discount] ];
-
- self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
- // if(self.arr_fashion_ids.count != 1)
- // {
- self.editQTY.enabled = NO;
- self.editPrice.enabled = NO;
- self.editDiscount.enabled = NO;
- // self.tv_linenote.editable = NO;
-
- self.editLineNotes.enabled = NO;
-
- 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在单选情况才不为空
-
- self.editPrice.enabled = false;
- // self.editPrice.text = nil;
- if (self.arr_fashion_ids.count != 1) {
- self.buttonRefresh.hidden = true;
- self.labelQueryQTY.hidden = true;
- // CGRect frame = CGRectMake(self.labelQueryQTY.frame.origin.x, self.labelQueryQTY.frame.origin.y, self.labelQueryQTY.frame.size.width, 0);
- // CGSize size = CGSizeMake(self.labelQueryQTY.frame.size.width, 0);
- // self.labelQueryQTY.frame=frame;
- self.label_multiItems.hidden = false;
- } else {
- self.buttonRefresh.hidden = false;
- self.labelQueryQTY.hidden = false;
- // CGRect frame = CGRectMake(self.labelQueryQTY.frame.origin.x, self.labelQueryQTY.frame.origin.y, self.labelQueryQTY.frame.size.width, 0);
- self.label_multiItems.hidden = true;
- }
-
- self.labelNewPrice.hidden = true;
- self.labelpriceafter.hidden = true;
- // }
-
-
-
- // self.tv_linenote.layer.backgroundColor = [[UIColor clearColor] CGColor];
- //
- // self.tv_linenote.layer.borderColor = [[UIColor lightGrayColor] CGColor];
- //
- // self.tv_linenote.layer.borderWidth = 0.5;
- //
- // self.tv_linenote.layer.cornerRadius = 8.0f;
- //
- // [self.tv_linenote.layer setMasksToBounds:YES];
-
- // if(self.hide_discount)
- // {
- // self.editDiscount.hidden=true;
- // self.labelDiscount.hidden=true;
- // self.labelCalPrice.text = @"New price:";
- //
- //
- //
- // }
-
- // Do any additional setup after loading the view.
- }
- - (IBAction)onRefreshClick:(id)sender {
- [self requestqty];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- -(void) requestqty
- {
-
- if(self.arr_fashion_ids.count!=1)
- return;
- self.buttonRefresh.enabled=false;
- self.labelQueryQTY.text = @"Querying current available QTY...";
-
- [RANetwork request_modelqty:self.arr_fashion_ids[0] completionHandler:^(NSMutableDictionary *result) {
- NSDictionary* detail_json = result;
- self.buttonRefresh.enabled=true;
- // NSDictionary* imgsection = [detail_json objectForKey:@"img_section"];
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- if([[detail_json valueForKey:@"result"] intValue]==2)
- {
-
- NSString* qty = [detail_json valueForKey:@"quantity_available"];
- self.labelQueryQTY.text = [NSString stringWithFormat:@"Current Available QTY: %@",qty];
- // self.detailTable.hidden = false;
-
-
- }
- else
-
- {
- self.labelQueryQTY.text=@"Tap Refresh to query current QTY";
-
-
- [RAUtils message_box:@"Request Available QTY" message:[detail_json valueForKey:@"err_msg"] completion:nil];
-
-
-
- }
-
-
-
- }];
- }
- - (IBAction)QTYPercentageValueChanged:(id)sender {
- UISwitch * switchqty =(UISwitch*)sender;
- if(switchqty.isOn)
- {
- if([self.editQTY.text intValue]>100)
- self.editQTY.text=@"100";
-
-
- self.labelQTYTitle.text = @"Available QTY %";
- self.editQTY.placeholder =@"Percentage";
- }
- else
- {
- self.editQTY.text=[NSString stringWithFormat:@"%d",[self.editQTY.text intValue]];
-
- self.labelQTYTitle.text = @"Available QTY";
- self.editQTY.placeholder =@"QTY";
- }
- }
- - (IBAction)onSaveClick:(id)sender {
-
- for (id view in self.view.subviews) {
- [view resignFirstResponder];
- }
-
- // if([self.editDiscount.text floatValue]>100.0)
- // {
- // // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
- // //
- // //
- // // [alert show];
- //
- // [RAUtils alert_view:@"Fields with * mark cannot be empty." title:@"Some Requried Fields Are Missing."];
- //
- // self.editDiscount.text=@"0";
- //
- // return;
- // }
- //
- //
- // [self update_newprice];
-
-
- if([self.editDiscount.text floatValue]>100.0)
- {
-
- [RAUtils message_box:@"Input Error" message:@"Percentage must less than 100." completion:nil];
-
-
- self.editDiscount.text=@"0";
-
- return;
- }
- if([self.editQTY.text floatValue]>100.0 && self.switchQTY.isOn)
- {
-
- [RAUtils message_box:@"Input Error" message:@"Percentage must less than 100." completion:nil];
-
- self.editDiscount.text=@"0";
-
- return;
- }
-
-
- [self update_newprice];
-
- bool qtypercentage = self.switchQTY.isOn;
-
-
- if (!self.qtyCheckButton.selected) {
- self.qty = -INT32_MAX;
- self.qty_p = -MAXFLOAT;
- }
-
- if(self.editQTY.text.length==0)
- {
- self.qty=-INT32_MAX;
- self.qty_p=-MAXFLOAT;
- } else if(qtypercentage) {
- if (self.qtyCheckButton.selected) {
- self.qty_p=[self.editQTY.text floatValue];
- self.qty=-INT32_MAX;
- }
- } else {
- if (self.qtyCheckButton.selected) {
- self.qty=[self.editQTY.text intValue];
- self.qty_p=-MAXFLOAT;
- }
- }
-
- if (!self.priceCheckButton.selected) {
- self.price = -MAXFLOAT;
- }
-
- if (!self.discountCheckButton.selected) {
- self.discount = -MAXFLOAT;
- }
-
- if (!self.lineNoteCheckButton.selected) {
- self.linenotes = nil;
- }
- [self dismissViewControllerAnimated:NO
- completion:^{
- if(self.onSetValue)
- self.onSetValue(self.qty, self.qty_p,self.price,self.discount, self.linenotes);
- }];
- }
- - (IBAction)onCloseClicked:(id)sender {
- [self dismissViewControllerAnimated:NO
- completion:^{
-
-
- }];
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- /*
- #pragma mark textView delegate
- - (void)textViewDidEndEditing:(UITextView *)textView
- {
- [self update_newprice];
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- int lenth = 20;
- return (lenth>textView.text.length||[text isEqualToString:@""]);
-
- }
- */
- #pragma mark textField delegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return NO;
- }
- -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
-
- // int tag = textField.tag;
- //
- // int len = textField.text.length;
- //
- // if(textField.tag==2&&textField.text.length>=2 && string.length>0)
- // return false;
-
- if(textField.text.length==0 && [string isEqualToString:@"."])
- return false;
-
- // DebugLog(@"text:%@",textField.text);
- //
- //
- //
- //
- //
- // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
- //
- // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
- // // DebugLog(@"shouldchangeedit %d_%d",indexPath.section,indexPath.row);
- // NSDictionary* item_json =((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row];
- //
- // NSString* required = [item_json valueForKey:@"required"];
- //
- // if([required isEqualToString:@"true"] && string.length==0 && (textField.text.length<=1|| textField.text==nil))
- // {
- // CALayer *layer = [cell.contentView layer];
- // // layer.borderColor = [[UIColor redColor] CGColor];
- // // layer.borderWidth = 1.0;
- //
- // layer.shadowColor = [UIColor redColor].CGColor;
- // layer.shadowOffset = CGSizeMake(0, 0);
- // layer.shadowOpacity = 1;
- // layer.shadowRadius = 2.0;
- // }
- // else
- // {
- // CALayer *layer = [cell.contentView layer];
- // // layer.borderColor = [[UIColor redColor] CGColor];
- // // layer.borderWidth = 1.0;
- //
- // layer.shadowColor = [UIColor clearColor].CGColor;
- // layer.shadowOffset = CGSizeMake(0, 0);
- // layer.shadowOpacity = 1;
- // layer.shadowRadius = 2.0;
- // }
- //
- //
- // NSString* keyboard = [item_json valueForKey:@"keyboard"];
- // if(![keyboard isEqualToString:@"number"])
- // return TRUE;
- NSCharacterSet *cs;
- if(textField.tag==199&& ! self.switchQTY.isOn)
- {
-
- cs = [[NSCharacterSet characterSetWithCharactersInString:INTNUMBERS]invertedSet];
- }
- else
- cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
- //
- NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
-
- BOOL canChange = [string isEqualToString:filtered];
- //
- //
- //
- //
- return canChange;
- // return true;
- }
- -(void) update_newprice
- {
-
- // if (!self.priceCheckButton.selected) {
- // self.price = -MAXFLOAT;
- // }
- // else
- self.price = [self.editPrice.text doubleValue];
-
- // if (!self.discountCheckButton.selected) {
- // self.discount = -MAXFLOAT;
- // }
- // else
- self.discount = [self.editDiscount.text doubleValue];
-
-
-
-
-
- self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)];
-
- // self.linenotes = self.tv_linenote.text;
-
- self.linenotes = self.editLineNotes.text;
-
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField
- {
-
-
-
- if(textField.tag==2 || (textField.tag==199&& self.switchQTY.isOn))
- {
- if(textField.text.length==0)
- textField.text=@"0";
-
- if([textField.text floatValue]>100.0)
- {
-
- [RAUtils message_box:@"Input Error" message:@"Percentage must less than 100." completion:nil];
- // [RAUtils alert_view:@"Percentage must less than 100." title:@"Input Error."];
- textField.text=@"0";
- }
- }
- else
- {
- if(textField.tag==199)
- {
- int i = [textField.text intValue];
- textField.text=[NSString stringWithFormat:@"%d",i];
- }
- else
- {
- float f = [textField.text floatValue];
- textField.text=[NSString stringWithFormat:@"%.2f",f];
- }
- }
-
- [self update_newprice];
-
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField
- {
- //
- // self.lastedit = textField;
- }
- //
- //#pragma mark textField delegate
- //- (BOOL)textFieldShouldReturn:(UITextField *)textField {
- // [textField resignFirstResponder];
- // return NO;
- //}
- //-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
- //
- //
- //
- // if(textField.text.length==0 && [string isEqualToString:@"."])
- // return false;
- //
- // NSCharacterSet *cs;
- // cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
- // //
- // NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
- //
- // BOOL canChange = [string isEqualToString:filtered];
- // //
- // //
- // //
- // //
- // return canChange;
- // // return true;
- //}
- //- (void)textFieldDidEndEditing:(UITextField *)textField
- //{
- //
- //// if(textField.tag==2)
- //// {
- //// if(textField.text.length==0)
- //// textField.text=@"0";
- ////
- //// if([textField.text floatValue]>100.0)
- //// {
- //// // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Discount error" message:@"Discount must less than 100" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
- //// // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
- //// // [alert show];
- ////
- ////
- ////
- //// [RAUtils alert_view:@"Discount must less than 100." title:@"Input Error."];
- //// textField.text=@"0";
- //// }
- //// }
- //// else
- //// {
- //// float f = [textField.text floatValue];
- //// textField.text=[NSString stringWithFormat:@"%.2f",f];
- //// }
- ////
- //// [self update_newprice];
- //
- // // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
- // // NSIndexPath * indexPath = [self.editorTable indexPathForCell:cell];
- // //
- // //
- // // NSMutableDictionary* section_json=nil;
- // //
- // //
- // // // DebugLog(@"endedit %d_%d",indexPath.section,indexPath.row);
- // // NSMutableDictionary* item_json =[((NSMutableArray*)self.content_data_control[indexPath.section])[indexPath.row] mutableCopy];
- // //
- // //
- // // [item_json setValue:@"true" forKey:@"dirty"];
- // // [item_json setValue:textField.text forKey:@"value"];
- // // section_json = [[self.content_data_download objectForKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]] mutableCopy];
- // // int count=0;
- // //
- // // count=[[section_json valueForKey:@"count"] intValue];
- // //
- // // for(int i=0;i<count;i++)
- // // {
- // // NSDictionary * olditem=[section_json objectForKey:[NSString stringWithFormat:@"item_%d",i ]];
- // // if([[olditem valueForKey:@"name"] isEqualToString:[item_json valueForKey:@"name" ]])
- // // [section_json setObject:item_json forKey:[NSString stringWithFormat:@"item_%d",i ]];
- // //
- // //
- // // }
- // //
- // // [self.content_data_download setObject:section_json forKey:[NSString stringWithFormat:@"section_%ld",(long)indexPath.section ]];
- //}
- //
- //- (void)textFieldDidBeginEditing:(UITextField *)textField
- //{
- // //
- // // self.lastedit = textField;
- //}
- #pragma mark Jack
- - (IBAction)qtyCheckButtonClicked:(UIButton *)sender {
- sender.selected = !sender.selected;
- self.editQTY.enabled = sender.selected;
-
- if (sender.selected) {
- self.checkedCount++;
- } else {
- self.checkedCount--;
- }
-
- }
- - (IBAction)priceCheckButtonClicked:(id)sender {
- if ([sender isKindOfClass:[UIButton class]]) {
- UIButton *btn = (UIButton *)sender;
- btn.selected = !btn.selected;
-
- self.editPrice.enabled = btn.selected;
-
- if (btn.selected) {
- self.checkedCount++;
- } else {
- self.checkedCount--;
- }
- }
- }
- - (IBAction)dicountCheckButtonClicked:(UIButton *)sender {
- sender.selected = !sender.selected;
-
- self.editDiscount.enabled = sender.selected;
- if (sender.selected) {
- self.checkedCount++;
- } else {
- self.checkedCount--;
- }
- }
- - (IBAction)lineNoteCheckButtonClicked:(UIButton *)sender {
- sender.selected = !sender.selected;
-
- // self.tv_linenote.editable = sender.selected;
- self.editLineNotes.enabled = sender.selected;
- if (sender.selected) {
- self.checkedCount++;
- } else {
- self.checkedCount--;
- }
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
- if ([keyPath isEqualToString:@"checkedCount"]) {
-
- self.saveButton.hidden = self.checkedCount == 0;
-
- }
- }
- -(void)dealloc {
- [self removeObserver:self forKeyPath:@"checkedCount"];
- }
- @end
|