// // OrderListViewController.m // RedAnt ERP Mobile // // Created by Ray on 8/18/15. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved. // #import "OrderListViewController.h" #import "OrderListTableViewCell.h" #import "iSalesNetwork.h" #import "OrderDetailViewController.h" #import "MainViewController.h" #import "ContactListViewController.h" @interface OrderListViewController () @end @implementation OrderListViewController -(void) logout { self.offset = 0; [self.content_data removeAllObjects]; } -(void) reload_container_getdata:(bool) update_data { [super reload_container_getdata:update_data]; if(update_data) { self.offset = 0; [self.content_data removeAllObjects]; self.norefresh=true; [self loadpage]; } else { [self.table_order reloadData]; } AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.can_create_order) { [self.btn_newOrder setImage:[UIImage imageNamed:@"neworder"]]; self.btn_newOrder.enabled = true; } else { [self.btn_newOrder setImage:nil]; self.btn_newOrder.enabled = false; } } -(void) showHidePrice { [self reload_container_getdata:false]; } - (void)viewDidLoad { [super viewDidLoad]; self.edgesForExtendedLayout = UIRectEdgeNone; self.label_net_err.layer.borderColor = [UIColor darkGrayColor].CGColor; self.label_net_err.layer.borderWidth = 2.0; self.label_net_err.layer.cornerRadius=15; self.label_net_err.layer.masksToBounds=true; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refresh_btn_click:)]; // tap.minimumPressDuration = 0.8; //定义按的时间 [self.label_net_err addGestureRecognizer:tap]; // Do any additional setup after loading the view. self.offset = 0; self.limit = 25; self.content_data = [[NSMutableArray alloc]init]; UIRefreshControl *ref = [[UIRefreshControl alloc]init]; ref.tag = 201 ; ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7]; // ref.hidden = true; [ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged]; [self.table_order addSubview:ref]; NSData* json =nil; if(self.init_style==OL_OPEN) json=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"status_filter_cadedate_open" ofType:@"json" ]]; else json=[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"status_filter_cadedate" ofType:@"json" ]]; NSError *error=nil; self.status_cadedate = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy]; // [self.table_order addSubview:ref]; // self.scrollView.contentSize=CGSizeMake(1024, 1024); // self.btnrefresh = [UIButton buttonWithType:UIButtonTypeCustom]; // // self.btnrefresh.titleLabel.text =@"Load more"; // [self.btnrefresh setTitle:NSLocalizedString(@"load_more", nil) forState:UIControlStateNormal]; // // [self.btnrefresh.titleLabel sizeToFit]; // self.btnrefresh.titleLabel.adjustsFontSizeToFitWidth = YES; // self.btnrefresh.hidden = true; // [self.btnrefresh setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // self.btnrefresh.backgroundColor = [UIColor lightGrayColor]; // [self.btnrefresh addTarget:self action:@selector(loadmore) forControlEvents:UIControlEventTouchUpInside]; // self.btnrefresh.frame =CGRectMake(0.0f, 0.0f, self.table_order.bounds.size.width, 44.0f); // self.table_order.tableFooterView = self.btnrefresh;//tableFooterView; // [self loadpage]; // // // self.toolbarView.layer.shadowPath =[UIBezierPath bezierPathWithRect:self.toolbarView.bounds].CGPath; // self.toolbarView.layer.masksToBounds = false; // //添加四个边阴影 // // self.toolbarView.layer.shadowColor = [UIColor blackColor].CGColor; // self.toolbarView.layer.shadowOffset = CGSizeMake(0, 0); // self.toolbarView.layer.shadowOpacity = 0.5; // self.toolbarView.layer.shadowRadius = 2.0; } - (IBAction)onCancel:(id)sender { if(self.onCancel) self.onCancel(); [self.navigationController popViewControllerAnimated:false]; } - (IBAction)onStatusFilterClick:(id)sender { EnumSelectViewController* enumvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"]; enumvc.delegate = self; enumvc.max_select = 0; //enumvc.updatePosition = indexPath; // if([[required lowercaseString] isEqualToString:@"true"]) // enumvc.canbeEmpty = false; // else // enumvc.canbeEmpty =true; enumvc.cadedate = self.status_cadedate ; enumvc.title = @"Order Status Selection"; // enumvc.value = [value_json mutableCopy]; enumvc.single_select =false; [self.navigationController pushViewController:enumvc animated:true]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // UIApplication * app = [UIApplication sharedApplication]; // AppDelegate *appDelegate = (AppDelegate *)[app delegate]; // if(appDelegate.bLogin==false) // { // LoginViewController * loginvc =[ self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; //// loginvc.delegate = self; // // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种 // // loginvc.returnValue = ^(bool blogin){ // // // UIApplication * app = [UIApplication sharedApplication]; // AppDelegate *appDelegate = (AppDelegate *)[app delegate]; // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; // // [main_vc checklogin:true]; // //// self.offset = 0; //// [self.content_data removeAllObjects]; //// [self loadpage]; // // [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO]; // // }; // // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ; // // // // // // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; // // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 // [self presentViewController:navi animated:YES completion:^{ // // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); // // NSLog(@"LoginViewController present........."); // // // self.btop = false; // // <#code#> // }]; // } // else // { // // if(!self.norefresh)//避免 多次刷新 // { // self.norefresh = true; // self.offset = 0; // [self.content_data removeAllObjects]; // [self loadpage]; // // } // // } [[self navigationController] setNavigationBarHidden:!self.showNavibar animated:NO]; } - (IBAction)onNewOrderClicked:(id)sender { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Attention", nil) message:NSLocalizedString(@"Are you sure to create a new order?", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil]; [alert show]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)manually_refresh { UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201]; if(self.isrefreshing) { [reF endRefreshing]; return; } reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing"]; if ([self respondsToSelector:@selector(ReloadData)]) [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1]; } -(void)ReloadData { [self.content_data removeAllObjects]; [self.table_order reloadData]; // self.table_order.hidden = true; UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:201]; [reF endRefreshing]; reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull to refresh"]; self.keywords=self.searchbar.text; self.offset = 0; // [self.content_data removeAllObjects]; // [self.table_order reloadData]; [self loadpage]; // [self loadpage]; } -(void) loadpage { dispatch_async(dispatch_get_main_queue(), ^{ if(self.isrefreshing) return; self.isrefreshing=true; if(self.offset==0) { self.table_order.hidden = true; self.mum.center = self.view.center; self.mum.hidden = false; [self.mum startAnimating]; } self.label_net_err.hidden=true; NSMutableArray* arr_status = [[NSMutableArray alloc] init]; NSMutableArray* arr_statusname = [[NSMutableArray alloc] init]; int count = [[self.status_cadedate valueForKey:@"count"] intValue]; for(int i=0;iself.content_data.count) // { // int debug=true; // } // NSLog(@"row %ld",(long)indexPath.row); if(indexPath.row==self.content_data.count) { UITableViewCell *moreCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mores"]; UILabel *moreDataLabel=[[UILabel alloc] init]; moreDataLabel.tag=200; moreDataLabel.text=self.load_more_hint; [moreDataLabel setFont:[UIFont systemFontOfSize:14.0f]]; [moreDataLabel setTextAlignment:NSTextAlignmentCenter]; moreDataLabel.frame=CGRectMake(0, 10, self.table_order.bounds.size.width, 20); [moreDataLabel setBackgroundColor:[UIColor clearColor]]; // [moreDataLabel addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged]; [moreCell.contentView addSubview:moreDataLabel]; if([self.load_more_hint isEqualToString:@"Load more..."]) [self performSelector:@selector(loadmore) withObject:nil afterDelay:1]; // NSLog(@"更多..."); moreCell.backgroundColor = UIColorFromRGB(0xF2EEEA); return moreCell; } else { NSString *CellIdentifier = @"OrderListTableViewCell"; OrderListTableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; NSString* po= [self.content_data[indexPath.row] valueForKey:@"so#"]; NSString* price= [self.content_data[indexPath.row] valueForKey:@"price"]; if(appDelegate.can_see_price&&appDelegate.price_hidden==false) { } else { price=nil; } NSString* purchase_time= [self.content_data[indexPath.row] valueForKey:@"purchase_time"]; NSString* status = [self.content_data[indexPath.row] valueForKey:@"order_status"] ; NSString* customer_name = [self.content_data[indexPath.row] valueForKey:@"customer_name"]; NSString* model_count = [self.content_data[indexPath.row] valueForKey:@"model_count"]; NSString* sales = [self.content_data[indexPath.row] valueForKey:@"sales_rep"]; NSString* creator = [self.content_data[indexPath.row] valueForKey:@"create_by"]; cell.label_so.text=po; cell.labelprice.text=price; cell.label_date.text=purchase_time; cell.label_contact.text=customer_name; cell.label_model.text = model_count; cell.label_sales.text = sales; cell.label_creator.text = creator; cell.label_status.text=status; cell.backgroundColor = UIColorFromRGB(0xF2EEEA); return cell; } // NSDictionary * item_json = [self.content_data objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]]; // NSDictionary * combine_json =[item_json objectForKey:@"combine"]; // cell.bundle_item=combine_json; // NSString* img_url = [item_json valueForKey:@"img_url"]; // NSString* description = [item_json valueForKey:@"description"]; // // NSString* identifier = [item_json valueForKey:@"identifier"]; // // NSString* attribute = [item_json valueForKey:@"attribute"]; // NSString* currency = [item_json valueForKey:@"currency"]; // int count =[[item_json valueForKey:@"count"] intValue]; // double price =[[item_json valueForKey:@"price"] doubleValue]; // // cell.labelAttribute.text = attribute; // cell.labelCurrency.text = currency; // cell.labelDescription.text = description; // // cell.labelIdentifier.text = identifier; // cell.labelPrice.text = [NSString stringWithFormat:@"%.2f",price]; // // // // [cell set_Count:count]; // NSString* file_name=[img_url lastPathComponent]; // NSData* img_data=[iSalesDB load_cached_img:file_name]; // if(img_data!=nil) // { // // UIImage * img =[UIImage imageWithData:img_data]; // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal]; // } // else // { // // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]]; // // dispatch_async(dispatch_get_main_queue(), ^{ // // // // if(downloadimg_data!=nil) // { // [iSalesDB cache_img:downloadimg_data :file_name ]; // // UIImage * img =[UIImage imageWithData:downloadimg_data]; // [cell.btnImage setBackgroundImage:img forState:UIControlStateNormal]; // } // // }); // }); // // // } // } // else // { // NSString *CellIdentifier = @"OrderInfoListItem"; // UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // return cell; // } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // if(indexPath.row==self.content_data.count) return; NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"]; OrderDetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderDetailViewController" ]; // int i =[[self.content_data[indexPath.row] valueForKey:@"order_code"]; dvc.selectOrder=^(NSMutableDictionary* order_detail){ [self.navigationController popViewControllerAnimated:false]; if(self.selectOrder) self.selectOrder(order_detail); }; NSString* order_code =[self.content_data[indexPath.row] valueForKey:@"order_code"]; dvc.order_code = order_code; // bool temp_order = false ; // NSString* order_status=[self.content_data[indexPath.row] valueForKey:@"order_status"]; // if([order_status isEqualToString:@"Saved"]||[order_status isEqualToString:@"Quoted"]) // temp_order=true; // // dvc.isTempOrder = temp_order; // dvc dvc.orderid=orderid; // dvc.category_id=nil; // dvc.ispush=true; // [dvc reload]; [self.navigationController pushViewController:dvc animated:true]; } #pragma mark - searchBar delegate; - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { self.keywords=searchBar.text; self.offset = 0; [self.content_data removeAllObjects]; [self loadpage]; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if([searchText isEqualToString:@""] && !self.reset) { NSLog(@"clear"); self.reset=true; self.keywords=nil; self.offset = 0; [self.content_data removeAllObjects]; [self loadpage]; } else self.reset = false; } #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { // 添加一个删除按钮 // self.indexPath=indexPath; if(indexPath.row==self.content_data.count) return @[]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.user_type!=USER_ROLE_EMPLOYEE) return @[]; NSString* so= [self.content_data[indexPath.row] valueForKey:@"so#"]; NSString* orderid= [self.content_data[indexPath.row] valueForKey:@"order_id"]; NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"]; UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"delete click"); NSString * title =[NSString stringWithFormat:@"Are you sure to delete order SO#: %@",so]; UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) { // textField.text = self.save_name; // // // }]; UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* return_json = [iSalesNetwork delete_Order:orderid]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if([[return_json valueForKey:@"result"] intValue]==2) { if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]]) { appDelegate.order_code= nil; [appDelegate SetSo:nil]; } [RAUtils message_alert:nil title:@"Order Delete" controller:self] ; [self ReloadData]; } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order" controller:self] ; } }); }); }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; }]; deleteRowAction.backgroundColor = UIColorFromRGB(0x336699); // 添加一个编辑按钮 UITableViewRowAction *cancelAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Cancel"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { NSLog(@"edit click"); NSString * title =[NSString stringWithFormat:@"Are you sure to cancel order SO#: %@",so]; UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Cancel Order"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* return_json = [iSalesNetwork cancel_Order:orderid]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if([[return_json valueForKey:@"result"] intValue]==2) { if([appDelegate.order_code isEqualToString: [self.content_data[indexPath.row] valueForKey:@"order_code"]]) { appDelegate.order_code= nil; [appDelegate SetSo:nil]; } [RAUtils message_alert:nil title:@"Order Canceled" controller:self] ; [self ReloadData]; } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Cancel Order" controller:self] ; } }); }); }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; // }]; cancelAction.backgroundColor = UIColorFromRGB(0xff9933); if([order_status isEqualToString:@"SO Saved"]&&appDelegate.can_cancel_order) return @[cancelAction]; else { if([order_status isEqualToString:@"Quote Saved"]) { NSMutableArray* arr=[@[] mutableCopy]; if(appDelegate.can_delete_order) [arr addObject:deleteRowAction]; if(appDelegate.can_cancel_order) [arr addObject:cancelAction]; return arr; } } return @[]; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row==self.content_data.count) return UITableViewCellEditingStyleNone; NSString* order_status= [self.content_data[indexPath.row] valueForKey:@"order_status"]; if(![order_status isEqualToString:@"SO Saved"]&&! [order_status isEqualToString:@"Quote Saved"]) return UITableViewCellEditingStyleNone; else return UITableViewCellEditingStyleDelete; } /* #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 EnumSelectViewControllerDelegate -(void) EnumValueChanged:(NSMutableDictionary *)value indexPath :(NSIndexPath*) indexPath { self.status_cadedate = value; // self.keywords=searchBar.text; self.offset = 0; [self.content_data removeAllObjects]; [self loadpage]; //[self.btnStatusFilter setTitle:setTitle:alert ]; } @end