// // 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 "RANetwork.h" #import "LoginViewController.h" #import "MainViewController.h" #import "OrderListViewController.h" @implementation CartUtils +(void) neworder:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart { // __block UIAlertController * waitalert = [RAUtils waiting_alert:vc title:@"Create Order" completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"Create Order" completion:nil]; [RANetwork request_create_order:^(NSMutableDictionary *result) { NSMutableDictionary* return_json = result; // [waitalert dismissViewControllerAnimated:YES completion:^{ [pop hide]; if([[return_json valueForKey:@"result"] intValue]==2) { int resulti=[[return_json valueForKey:@"result"] intValue]; if(resulti==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]; IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [holder performSelector:addtocart]; } } else { [RAUtils message_box:@"Create Order" message:[return_json valueForKey:@"err_msg"] completion:nil]; } // }]; // [waitalert dismissViewControllerAnimated:YES completion:nil]; }]; // }]; } +(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 =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginViewController"]; loginvc.returnValue = ^(bool blogin){ if(blogin) { // if(appDelegate.user_type==USER_ROLE_EMPLOYEE) if(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 =[ [UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; //ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [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 = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; __weak typeof(cvc) weakcvc = cvc; 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:weakcvc 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_box:@"New Order Error" message:@"Cannot create order without customer infomation." completion:nil]; // 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 = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种 [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.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 =[ [UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; //ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [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 = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; __weak typeof(cvc) weakcvc = cvc; 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:weakcvc 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_box:@"New Order Error" message:@"Cannot create order without customer infomation." completion:nil]; // 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 { // __block UIAlertController * waitalert = [RAUtils waiting_alert:vc title:@"Checking Pending Order" completion:^{ PopWaitAlert* pop = [RAUtils waiting_pop:@"Checking Pending Order" completion:nil]; [RANetwork request_pendingorder:appDelegate.contact_id completionHandler:^(NSMutableDictionary *result) { NSMutableDictionary* return_json =result; // [waitalert dismissViewControllerAnimated:YES completion:^{ [pop hide]; 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 =[ [UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [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 = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; __weak typeof(cvc) weakcvc = cvc; 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:weakcvc 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_box:@"New Order Error" message:@"Cannot create order without customer infomation." completion:nil]; // 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 =[ [UIStoryboard storyboardWithName:@"ERP_Mobile_Order" bundle:nil] instantiateViewControllerWithIdentifier:@"OrderListViewController"]; ovc.showNavibar = true; ovc.customer_id = appDelegate.contact_id; ovc.selectOrder = ^(NSMutableDictionary* order_detail){ IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [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 = [[UIStoryboard storyboardWithName:@"ERP_Mobile_Contact" bundle:nil] instantiateViewControllerWithIdentifier:@"ContactListViewController" ]; __weak typeof(cvc) weakcvc = cvc; 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:weakcvc 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_box:@"New Order Error" message:@"Cannot create order without customer infomation." completion:nil]; // 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_box:@"Add To Cart" message:@"You donot have permission to access order" completion:nil]; } } } else { [RAUtils message_box:@"Check Pending Order" message:[return_json valueForKey:@"err_msg"] completion:nil]; } // }]; }]; // }]; } } else { // if(appDelegate.order_code==nil) // [ self neworder]; // else IMP imp = [holder methodForSelector:addtocart]; void (*func)(id, SEL) = (void *)imp; func(holder, addtocart); // [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