// // CartUtils.m // iSales-NPD // // Created by Ray on 12/26/16. // Copyright © 2016 United Software Applications, Inc. All rights reserved. // #import "CartUtils.h" #import "RAUtils.h" #import "AppDelegate.h" #import "iSalesNetwork.h" #import "LoginViewController.h" #import "MainViewController.h" @implementation CartUtils +(void) neworder:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* return_json = [iSalesNetwork new_Order]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if([[return_json valueForKey:@"result"] intValue]==2) { int result=[[return_json valueForKey:@"result"] intValue]; if(result==2) { //successed. NSString* order_code = [return_json valueForKey:@"orderCode"]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.order_code = order_code; appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue]; [holder performSelector:addtocart]; } } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:vc] ; } }); }); } +(void) add_to_cart:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart { UIApplication * app = [UIApplication sharedApplication]; AppDelegate *appDelegate = (AppDelegate *)[app delegate]; MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; if(appDelegate.bLogin==false) { LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"]; loginvc.returnValue = ^(bool blogin){ if(blogin) { if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true) { [main_vc checklogin:YES]; NSString* msg =@""; // if(appDelegate.contact_id.length>0) // { // msg = [msg stringByAppendingString:@"Customer:"]; // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]]; // // } UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // vc.disable_refresh = true; OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; //ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ [holder performSelector:addtocart]; }; ovc.init_style = OL_OPEN; ovc.onCancel = ^(){ // self.disable_refresh = false; }; [vc.navigationController pushViewController:ovc animated:true]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { { //create new; // self.disable_refresh = true; if(appDelegate.customerInfo==nil)// select contact if current contact not exist { ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; cvc.showNavibar = true; cvc.contact_type = @"Sales_Order_Customer"; cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){ appDelegate.contact_id=[value valueForKey:@"customer_cid"]; appDelegate.customerInfo = value; if(appDelegate.order_code==nil) [self neworder:vc selectorholder:holder selector:addtocart]; // neworder(); // [main_vc checklogin:true]; // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION]; // // if(self.returnValue) // self.returnValue(value); }; cvc.onCancel = ^(){ [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc]; // self.disable_refresh = false; }; cvc.onReset = ^(){ // [main_vc checklogin:true]; }; [vc.navigationController pushViewController:cvc animated:true]; } else { [self neworder:vc selectorholder:holder selector:addtocart]; } } DebugLog(@"No"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; } else { //customer login; [main_vc checklogin:false]; //[self addtocart]; [holder performSelector:addtocart]; } } }; UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ; // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 [vc presentViewController:navi animated:YES completion:^{ // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); DebugLog(@"LoginViewController present........."); // self.btop = false; // <#code#> }]; } else { if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil) { if(appDelegate.contact_id.length==0) { NSString* msg =@""; UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // vc.disable_refresh = true; OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; //ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ [holder performSelector:addtocart]; }; ovc.init_style = OL_OPEN; ovc.onCancel = ^(){ // self.disable_refresh = false; }; [vc.navigationController pushViewController:ovc animated:true]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { { //create new; // self.disable_refresh = true; if(appDelegate.customerInfo==nil)// select contact if current contact not exist { ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; cvc.showNavibar = true; cvc.contact_type = @"Sales_Order_Customer"; cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){ appDelegate.contact_id=[value valueForKey:@"customer_cid"]; appDelegate.customerInfo = value; if(appDelegate.order_code==nil) [self neworder:vc selectorholder:holder selector:addtocart]; // neworder(); // [main_vc checklogin:true]; // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION]; // // if(self.returnValue) // self.returnValue(value); }; cvc.onCancel = ^(){ [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc]; // self.disable_refresh = false; }; cvc.onReset = ^(){ // [main_vc checklogin:true]; }; [vc.navigationController pushViewController:cvc animated:true]; } else { [self neworder:vc selectorholder:holder selector:addtocart]; } } DebugLog(@"No"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; } else { UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking Pending Order"]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary* return_json = [iSalesNetwork request_PendingOrder:appDelegate.contact_id]; dispatch_async(dispatch_get_main_queue(), ^{ [waitalert dismissWithClickedButtonIndex:0 animated:FALSE]; if([[return_json valueForKey:@"result"] intValue]==2) { bool openPendingOrder= [[return_json valueForKey:@"hasPending"] boolValue]; bool createNewOrder=appDelegate.can_create_order; if(openPendingOrder&&createNewOrder) { NSString* msg =@""; if(appDelegate.contact_id.length>0) { msg = [msg stringByAppendingString:@"Customer:"]; msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]]; } UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // vc.disable_refresh = true; OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ [holder performSelector:addtocart]; }; ovc.init_style = OL_OPEN; ovc.onCancel = ^(){ // self.disable_refresh = false; }; [vc.navigationController pushViewController:ovc animated:true]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { { //create new; // self.disable_refresh = true; if(appDelegate.customerInfo==nil)// select contact if current contact not exist { ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; cvc.showNavibar = true; cvc.contact_type = @"Sales_Order_Customer"; cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){ appDelegate.contact_id=[value valueForKey:@"customer_cid"]; appDelegate.customerInfo = value; if(appDelegate.order_code==nil) [self neworder:vc selectorholder:holder selector:addtocart]; // neworder(); // [main_vc checklogin:true]; // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION]; // // if(self.returnValue) // self.returnValue(value); }; cvc.onCancel = ^(){ [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc]; // self.disable_refresh = false; }; cvc.onReset = ^(){ // [main_vc checklogin:true]; }; [vc.navigationController pushViewController:cvc animated:true]; } else { [self neworder:vc selectorholder:holder selector:addtocart]; } } DebugLog(@"No"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; } else { if(openPendingOrder) { NSString* msg =@""; if(appDelegate.contact_id.length>0) { msg = [msg stringByAppendingString:@"Customer:"]; msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]]; } UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // vc.disable_refresh = true; OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ [holder performSelector:addtocart]; }; ovc.init_style = OL_OPEN; ovc.onCancel = ^(){ // self.disable_refresh = false; }; [vc.navigationController pushViewController:ovc animated:true]; }]; [alertControl addAction:actionOne]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; // [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; } else if(createNewOrder) { NSString* msg =@""; if(appDelegate.contact_id.length>0) { msg = [msg stringByAppendingString:@"Customer:"]; msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]]; } UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { { //create new; // self.disable_refresh = true; if(appDelegate.customerInfo==nil)// select contact if current contact not exist { ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; cvc.showNavibar = true; cvc.contact_type = @"Sales_Order_Customer"; cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){ appDelegate.contact_id=[value valueForKey:@"customer_cid"]; appDelegate.customerInfo = value; if(appDelegate.order_code==nil) [self neworder:vc selectorholder:holder selector:addtocart]; // neworder(); // [main_vc checklogin:true]; // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION]; // // if(self.returnValue) // self.returnValue(value); }; cvc.onCancel = ^(){ [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc]; // self.disable_refresh = false; }; cvc.onReset = ^(){ // [main_vc checklogin:true]; }; [vc.navigationController pushViewController:cvc animated:true]; } else { [self neworder:vc selectorholder:holder selector:addtocart]; } } DebugLog(@"No"); }]; // [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; [alertControl addAction:alertcancel]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [vc presentViewController:alertControl animated:YES completion:nil]; } else { [RAUtils message_alert:[return_json valueForKey:@"You donot have permission to access order"] title:@"Add To Cart" controller:vc]; } } } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Check Pending Order" controller:vc] ; } }); }); } // // [main_vc checklogin:false]; // // if(appDelegate.can_create_order) // { // NSString* msg =@""; // if(appDelegate.contact_id.length>0) // { // msg = [msg stringByAppendingString:@"\n\nCustomer:"]; // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]]; // // } // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil),NSLocalizedString(@"Create new order", nil), nil]; // // // alert. // [alert show]; // } // else // { // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil), nil]; // // // alert. // [alert show]; // } } else { // if(appDelegate.order_code==nil) // [ self neworder]; // else [holder performSelector:addtocart]; } } } +(void) add_recent_model:(NSDictionary*) model { NSMutableDictionary * newdict = [[NSMutableDictionary alloc]init]; [newdict setObject:model forKey:@"item_0"]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; int count=[appDelegate.recent_model[@"count"] intValue]; for(int i=0;i