Ver Fonte

161226
cart utils separate from rautils

Ray Zhang há 9 anos atrás
pai
commit
d3ef81f3b0

BIN
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/Ray.xcuserdatad/UserInterfaceState.xcuserstate


+ 2 - 2
RedAnt ERP Mobile/common/Functions/camscan/ScannerViewController.m

@@ -25,7 +25,7 @@
 #import "AppDelegate.h"
 #import "MainViewController.h"
 #import "ContactListViewController.h"
-
+#import "CartUtils.h"
 
 #define SCANNER_TARGET_DETAIL 0
 #define SCANNER_TARGET_CART 1
@@ -421,7 +421,7 @@
         {
             
             
-            [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
+            [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
 //            return;
 //            [self add_toCart];
             //            DetailViewController* dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController" ];

+ 2 - 1
RedAnt ERP Mobile/common/Functions/category/CategoryViewController.m

@@ -32,6 +32,7 @@
 #import "MainViewController.h"
 //#import "ScannerViewController.h"
 #import "UIColor+JK_HEX.h"
+#import "CartUtils.h"
 
 #define  DEF_CELL_HEIGHT 44
 #define  DEF_TABLE_HEIGHT 44
@@ -311,7 +312,7 @@
 //    
 //    
 //    
-    [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
+    [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
     return;
     
     UIApplication * app = [UIApplication sharedApplication];

+ 2 - 1
RedAnt ERP Mobile/common/Functions/modelDetail/DetailHeaderCell.m

@@ -16,6 +16,7 @@
 
 
 #import "ImageScrollerViewController.h"
+#import "CartUtils.h"
 
 
 @implementation DetailHeaderCell
@@ -326,7 +327,7 @@
 - (IBAction)add_toCart:(id)sender {
     
     UIViewController *vc= [RAUtils getViewController:self];
-    [RAUtils add_to_cart:vc selectorholder:self selector:@selector(addtocart)];
+    [CartUtils add_to_cart:vc selectorholder:self selector:@selector(addtocart)];
     return;
     
     

+ 2 - 2
RedAnt ERP Mobile/common/Functions/search+itemsearch/ItemSearchViewController.m

@@ -18,7 +18,7 @@
 #import "MainViewController.h"
 #import "DefaultAppearance.h"
 #import "UIColor+JK_HEX.h"
-
+#import "CartUtils.h"
 
 #define  DEF_CELL_HEIGHT 44
 #define  DEF_TABLE_HEIGHT 44
@@ -783,7 +783,7 @@
 }
 - (IBAction)onAddCartClick:(id)sender {
     
-    [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
+    [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
     return;
     
     UIApplication * app = [UIApplication sharedApplication];

+ 2 - 2
RedAnt ERP Mobile/common/Functions/search+itemsearch/SearchViewController.m

@@ -16,7 +16,7 @@
 #import "MainViewController.h"
 #import "DefaultAppearance.h"
 #import "UIColor+JK_HEX.h"
-
+#import "CartUtils.h"
 
 #define  DEF_CELL_HEIGHT 44
 #define  DEF_TABLE_HEIGHT 44
@@ -567,7 +567,7 @@
     // Dispose of any resources that can be recreated.
 }
 - (IBAction)onAddCartClick:(id)sender {
-    [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
+    [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
     return;
     
     UIApplication * app = [UIApplication sharedApplication];

+ 2 - 1
RedAnt ERP Mobile/common/Functions/watchlist/WatchListViewController.m

@@ -21,6 +21,7 @@
 #import "SortItemViewController.h"
 #import "SortButton.h"
 #import "UIColor+JK_HEX.h"
+#import "CartUtils.h"
 
 #define ALERT_DEL 1025
 //#import "PDFViewController.h"
@@ -70,7 +71,7 @@
         [self.itemListTable reloadData];
 }
 - (IBAction)onAddtoCart:(id)sender {
-    [RAUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
+    [CartUtils add_to_cart:self selectorholder:self selector:@selector(addtocart)];
     return;
     
     UIApplication * app = [UIApplication sharedApplication];

+ 16 - 0
RedAnt ERP Mobile/common/NetworkUtils.h

@@ -0,0 +1,16 @@
+//
+//  NetworkUtils.h
+//  AntsContract
+//
+//  Created by Ray on 12/26/16.
+//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "const.h"
+typedef NSMutableDictionary*(^resultBlock)(NSMutableDictionary* result);
+
+@interface NetworkUtils : NSObject
++(NSData*)get_json : (NSString*) url parameters:(NSMutableDictionary *) params err_recorder:(NSString* )recorder_url result_handler:(resultBlock)resultBlock;
++(NSDictionary*)err_log:(NSString*) up_params result:(NSString*)result module:(NSString*) module code:(int) code;
+@end

+ 221 - 0
RedAnt ERP Mobile/common/NetworkUtils.m

@@ -0,0 +1,221 @@
+//
+//  NetworkUtils.m
+//  AntsContract
+//
+//  Created by Ray on 12/26/16.
+//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
+//
+
+#import "NetworkUtils.h"
+#import "RAUtils.h"
+
+@implementation NetworkUtils
+
+
+//+(NSDictionary*) error_dict:(NSError*)error
+//{
+//    if(error==nil)
+//        return nil;
+//    NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
+//    [ret setValue:[NSString stringWithFormat:@"%d",error.code] forKey:@"error_code"];
+//    [ret setValue:error.domain forKey:@"err_domain"];
+//    [ret setValue:[error localizedDescription] forKey:@"err_message"];
+//    // [ret setObject:error.userInfo forKey:@"user_info"];
+//    return ret;
+//}
++(NSData*)get_json : (NSString*) url parameters:(NSMutableDictionary *) params err_recorder:(NSString* )recorder_url result_handler:(resultBlock)resultBlock
+{
+    int retry = 0;
+    
+
+    
+    
+repeat:
+    {
+        
+        //    return [self fake_json:url];
+        
+        //    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
+        //分界线的标识符
+        NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
+        //根据url初始化request
+        NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
+                                                               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
+                                                           timeoutInterval:JSON_TIMEOUT];
+        //分界线 --AaB03x
+        NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
+        //结束符 AaB03x--
+        NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
+        //要上传的图片
+        //    UIImage *image=[params objectForKey:@"pic"];
+        //得到图片的data
+        //    NSData* data = UIImagePNGRepresentation(image);
+        //http body的字符串
+        NSMutableString *body=[[NSMutableString alloc]init];
+        
+        //    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
+        //     [params setValue:@"handset_login" forKey:@"action"];
+        //    [headers setValue:[NSString stringWithFormat:@"%d",dataLength] forKey:@"Content-Length"];
+        
+        //    [params setValue:appDelegate.sessionid forKey:@"sessionid"];
+        //   [params setValue:password forKey:@"password"];
+        //    [params setValue:[NSString stringWithFormat:@"%d",ver]  forKey:@"auth_ver"];
+        
+        
+        
+        //    [headers setValue:headQuerlString forKey:@"param"];
+        
+        //参数的集合的所有key的集合
+        NSArray *keys= [params allKeys];
+        DebugLog(@"================parms==================");
+        //遍历keys
+        for(int i=0;i<[keys count];i++)
+        {
+            //得到当前key
+            NSString *key=[keys objectAtIndex:i];
+            //如果key不是pic,说明value是字符类型,比如name:Boris
+            if(![key isEqualToString:@"pic"])
+            {
+                //添加分界线,换行
+                [body appendFormat:@"%@\r\n",MPboundary];
+                //添加字段名称,换2行
+                [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
+                //添加字段的值
+                [body appendFormat:@"%@\r\n",[params objectForKey:key]];
+                DebugLog(@"parameter: key=%@   value=%@",key,[params objectForKey:key]);
+            }
+        }
+        DebugLog(@"================parms==================");
+        ////添加分界线,换行
+        [body appendFormat:@"%@\r\n",MPboundary];
+        //声明pic字段,文件名为boris.png
+        [body appendFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"boris.png\"\r\n"];
+        //声明上传文件的格式
+        [body appendFormat:@"Content-Type: image/png\r\n\r\n"];
+        
+        //声明结束符:--AaB03x--
+        NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
+        //声明myRequestData,用来放入http body
+        NSMutableData *myRequestData=[NSMutableData data];
+        //将body字符串转化为UTF8格式的二进制
+        [myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
+        //将image的data加入
+        //    [myRequestData appendData:data];
+        //加入结束符--AaB03x--
+        [myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
+        
+        //设置HTTPHeader中Content-Type的值
+        NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
+        //设置HTTPHeader
+        [request setValue:content forHTTPHeaderField:@"Content-Type"];
+        
+        // 关闭keep alive
+        [request setValue:@"close" forHTTPHeaderField:@"Connection"];
+        //设置Content-Length
+        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
+        //设置http body
+        [request setHTTPBody:myRequestData];
+        //http method
+        [request setHTTPMethod:@"POST"];
+        
+        
+        NSHTTPURLResponse* urlResponse = nil;
+        
+        NSError *error = nil;
+        
+        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
+        
+        
+        DebugLog(@"url: %@",url);
+        
+        DebugLog(@"response: %@",[NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode]);
+        
+        if (responseData==nil) {
+            
+            //            DebugLog(@"response error:%@", [error localizedDescription]);
+            
+            
+            NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
+            //#define RESULT_FALSE 0
+            //#define RESULT_TRUE		 2
+            //#define RESULT_NET_ERROR			 -3
+            //#define RESULT_NET_NOTAVAILABLE		  -4
+            //#define RESULT_ERROR				 -5
+            //#define RESULT_LOCALFILE_ERROR			 -7
+            //#define RESULT_USERAUTH_ERROR			 -9
+            //#define RESULT_UPDATE_USERAUTH_ERROR	 -11
+            //#define RESULT_SESSION_EXPIRED			  -13
+            //#define RESULT_VER_LOW
+            
+            //     NSString * moreinfo = error.description ;
+            
+            NSString* err_msg = [error localizedDescription];
+            
+            
+            //-----------------retry 3 times for error code -1005------------------------
+            if(error.code==-1005&&retry<3)
+            {
+                retry++;
+                goto repeat;
+                
+            }
+            //-----------------end retry 3 times for error code -1005------------------------
+            
+            NSDictionary* error_json = [RAUtils error_dict:error];
+            [ret setObject:error_json forKey:@"err_obj"];
+            NSString* resp_msg= nil;
+            
+            if(err_msg.length==0)
+            {
+                err_msg =[NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode];
+                resp_msg = err_msg;
+            }
+            [ret setValue:[NSString stringWithFormat:@"%d",RESULT_NET_ERROR] forKey:@"result"];
+            [ret setValue:err_msg forKey:@"err_msg"];
+            [ret setValue:resp_msg forKey:@"resp_msg"];
+            [ret setValue:[NSString stringWithFormat:@"%ld",(long)urlResponse.statusCode] forKey:@"resp_code"];
+            
+            
+            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ret
+                                                               options:0
+                                                                 error:nil];
+            if(![url isEqualToString:recorder_url])
+            {
+                [NetworkUtils err_log:[RAUtils base64en:[RAUtils dict2string:params]] result:[RAUtils base64en:[RAUtils dict2string:ret]] module:url code:RESULT_NET_ERROR];
+            }
+            return jsonData;
+            
+        }
+        else
+        {
+          //  NSString* err_msg = [error localizedDescription];
+            NSMutableString *str = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
+            
+            DebugLog(@"data string: %@",str);
+            NSError *error1 = nil;
+            
+            NSMutableDictionary* jsobj = [[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error1] mutableCopy];
+            
+            if(jsobj==nil)// 服务器返回不是json
+            {
+                
+                jsobj=[[NSMutableDictionary alloc] init];
+                [jsobj setValue:@"1" forKey:@"result"];
+                
+            }
+            jsobj = resultBlock(jsobj);
+                    int result=[[jsobj valueForKey:@"result"] intValue];
+            if(![url isEqualToString:recorder_url]&&/*![url isEqualToString:URL_DOWNLOAD_OFFLINE]&&*/result!=2&&result!=99)
+            {
+                [self err_log:[RAUtils base64en:[RAUtils dict2string:params]] result:[RAUtils base64en:str] module:url code:result];
+            }
+            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsobj
+                                                               options:0
+                                                                 error:nil];
+            return jsonData;
+            
+        }
+        return responseData;
+    }
+}
+@end

+ 2 - 2
RedAnt ERP Mobile/common/RAUtils.h

@@ -16,7 +16,7 @@
 //    
 //    
 //}MyTestStruct;
-typedef void (^add_to_cart_Handler)(void);
+
 
 @interface RAUtils : NSObject
 +(NSData*) dict2data:(NSDictionary*) dict;
@@ -60,7 +60,7 @@ typedef void (^add_to_cart_Handler)(void);
 + (long long) freeDiskSpaceInMegaBytes;
 
 +(NSArray*) string2arr:(NSString*) string separator:(NSString*)separator;
-+(void) add_to_cart:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart;
+
 
 
 + (bool)mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir error:(NSError**)err;

+ 2 - 739
RedAnt ERP Mobile/common/RAUtils.m

@@ -7,8 +7,8 @@
 //
 
 #import "RAUtils.h"
-#import "LoginViewController.h"
-#import "MainViewController.h"
+//#import "LoginViewController.h"
+//#import "MainViewController.h"
 #import <sys/param.h>
 #import <sys/mount.h>
 #import "const.h"
@@ -556,744 +556,7 @@
     
 }
 
-+(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];
-                    
-                    
-                    //    [self ReloadData];
-                    
-                    
-                    //                    if(self.shopCartBlock!=nil)
-                    //                    {
-                    //                        UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
-                    //
-                    //
-                    //                        CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
-                    //                        UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
-                    //                        iv.image = img;
-                    //                        //                        [self.contentView addSubview:iv];
-                    //                        self.shopCartBlock(iv);
-                    //
-                    //                    }
-                    
-                }
-            }
-            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:false];
-                    
-                    
-                    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<count;i++)
-    {
-        NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
-        if([mitem[@"product_id"] isEqualToString:model[@"product_id"]])
-            continue;
-        [newdict setObject:mitem forKey:[NSString stringWithFormat:@"item_%lu",(unsigned long)newdict.allKeys.count]];
-        
-    }
-    newdict[@"count"]=[NSString stringWithFormat:@"%lu",(unsigned long)newdict.allKeys.count];
-    
-    
-    appDelegate.recent_model = newdict;
-    //
-    //
-    //    if(!bexist)
-    //    {
-    //        [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
-    //        appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
-    //    }
-}
 +(void) alert_view :(NSString*) msg title:(NSString*) title
 {
     if(title==nil)

+ 6 - 0
RedAnt ERP Mobile/iSales-NPD.xcodeproj/project.pbxproj

@@ -83,6 +83,7 @@
 		714B1F401C7BF74100539193 /* OrderDetailSignatureCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 714B1F3F1C7BF74100539193 /* OrderDetailSignatureCell.m */; };
 		715001FF1D114D9100F5927F /* BundleDetailButton.m in Sources */ = {isa = PBXBuildFile; fileRef = 715001FE1D114D9100F5927F /* BundleDetailButton.m */; };
 		715671331E10A0EF006324A0 /* RectDrawable.m in Sources */ = {isa = PBXBuildFile; fileRef = 715671321E10A0EF006324A0 /* RectDrawable.m */; };
+		715671531E10B229006324A0 /* CartUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 715671521E10B229006324A0 /* CartUtils.m */; };
 		715850461CF6F0E500856B20 /* DefaultAppearance.m in Sources */ = {isa = PBXBuildFile; fileRef = 715850451CF6F0E500856B20 /* DefaultAppearance.m */; };
 		715F30BD1DAB37EB00490EED /* OfflineUnlockViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 715F30BC1DAB37EB00490EED /* OfflineUnlockViewController.m */; };
 		7161FEB51D61B24900157EE1 /* creditcardpayment.html in Resources */ = {isa = PBXBuildFile; fileRef = 7161FEB21D61B24900157EE1 /* creditcardpayment.html */; };
@@ -374,6 +375,8 @@
 		715001FE1D114D9100F5927F /* BundleDetailButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BundleDetailButton.m; path = common/Functions/BundleDetailButton.m; sourceTree = SOURCE_ROOT; };
 		715671311E10A0EF006324A0 /* RectDrawable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RectDrawable.h; path = common/pdfCreator/RectDrawable.h; sourceTree = SOURCE_ROOT; };
 		715671321E10A0EF006324A0 /* RectDrawable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RectDrawable.m; path = common/pdfCreator/RectDrawable.m; sourceTree = SOURCE_ROOT; };
+		715671511E10B229006324A0 /* CartUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CartUtils.h; sourceTree = "<group>"; };
+		715671521E10B229006324A0 /* CartUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CartUtils.m; sourceTree = "<group>"; };
 		715850441CF6F0E500856B20 /* DefaultAppearance.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DefaultAppearance.h; path = common/Functions/DefaultAppearance.h; sourceTree = SOURCE_ROOT; };
 		715850451CF6F0E500856B20 /* DefaultAppearance.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DefaultAppearance.m; path = common/Functions/DefaultAppearance.m; sourceTree = SOURCE_ROOT; };
 		715F30BB1DAB37EB00490EED /* OfflineUnlockViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OfflineUnlockViewController.h; path = common/Functions/offline/OfflineUnlockViewController.h; sourceTree = SOURCE_ROOT; };
@@ -1051,6 +1054,8 @@
 				71DF74291C57560600F2789C /* Reachability.h */,
 				71DF742A1C57560600F2789C /* Reachability.m */,
 				71DF742D1C5756C600F2789C /* const.h */,
+				715671511E10B229006324A0 /* CartUtils.h */,
+				715671521E10B229006324A0 /* CartUtils.m */,
 				71DF742E1C57572600F2789C /* RAUtils.h */,
 				71DF742F1C57572600F2789C /* RAUtils.m */,
 				71D01B141E0A2055006620CE /* ImageUtils.h */,
@@ -1652,6 +1657,7 @@
 				7162A5491C58719D00AB630E /* RATreeView+TableViewDelegate.m in Sources */,
 				7162A5B41C58738600AB630E /* ScannerControllerView.m in Sources */,
 				7162A5831C5872EF00AB630E /* HomeTableViewCellSlide.m in Sources */,
+				715671531E10B229006324A0 /* CartUtils.m in Sources */,
 				7162A5851C5872EF00AB630E /* HomeViewController.m in Sources */,
 				7162A5E31C5877CE00AB630E /* WatchListViewController.m in Sources */,
 				426D40151DACB51F0058B96B /* CategoryPriceCell.m in Sources */,

+ 13 - 0
RedAnt ERP Mobile/iSales-NPD/CartUtils.h

@@ -0,0 +1,13 @@
+//
+//  CartUtils.h
+//  iSales-NPD
+//
+//  Created by Ray on 12/26/16.
+//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+typedef void (^add_to_cart_Handler)(void);
+@interface CartUtils : NSObject
++(void) add_to_cart:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart;
+@end

+ 756 - 0
RedAnt ERP Mobile/iSales-NPD/CartUtils.m

@@ -0,0 +1,756 @@
+//
+//  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];
+
+
+                    //    [self ReloadData];
+
+
+                    //                    if(self.shopCartBlock!=nil)
+                    //                    {
+                    //                        UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
+                    //
+                    //
+                    //                        CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
+                    //                        UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
+                    //                        iv.image = img;
+                    //                        //                        [self.contentView addSubview:iv];
+                    //                        self.shopCartBlock(iv);
+                    //
+                    //                    }
+
+                }
+            }
+            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:false];
+
+
+                    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<count;i++)
+    {
+        NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
+        if([mitem[@"product_id"] isEqualToString:model[@"product_id"]])
+            continue;
+        [newdict setObject:mitem forKey:[NSString stringWithFormat:@"item_%lu",(unsigned long)newdict.allKeys.count]];
+
+    }
+    newdict[@"count"]=[NSString stringWithFormat:@"%lu",(unsigned long)newdict.allKeys.count];
+
+
+    appDelegate.recent_model = newdict;
+    //
+    //
+    //    if(!bexist)
+    //    {
+    //        [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
+    //        appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
+    //    }
+}
+@end