Forráskód Böngészése

实现PDF离线直接保存和预览保存。

Pen Li 9 éve
szülő
commit
e0d27081a8

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


+ 0 - 12
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/macmini1.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -2,16 +2,4 @@
 <Bucket
    type = "0"
    version = "2.0">
-   <Breakpoints>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
-         <BreakpointContent
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            scope = "0"
-            stopOnStyle = "0">
-         </BreakpointContent>
-      </BreakpointProxy>
-   </Breakpoints>
 </Bucket>

+ 2 - 0
RedAnt ERP Mobile/common/Functions/offline/OLDataProvider.h

@@ -102,6 +102,8 @@
 
 + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params;
 
++ (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params;
+
 + (NSData *)offline_pdfList:(NSMutableDictionary *)params;
 
 + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params;

+ 81 - 14
RedAnt ERP Mobile/common/Functions/offline/OLDataProvider.m

@@ -207,8 +207,18 @@
     NSMutableDictionary* template = [self get_pdftemplate:template_file];
     
     NSString* file=[pdfCreator CreatePdf: template dataSource:data];
-    NSLog(file);
-    return nil;
+    DebugLog(@"%@",file);
+    
+    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
+    [dic setObject:@"Regular Mode" forKey:@"mode"];
+    if (file) {
+        [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
+        [dic setObject:file forKey:@"pdf_path"];
+        return [RAUtils dict2data:dic];
+    }
+    [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
+    
+    return [RAUtils dict2data:dic];
 }
 +(NSString*) get_offline_soid:(sqlite3*)db
 {
@@ -9934,8 +9944,7 @@
     return [RAUtils dict2data:dic];
 }
 
-+ (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
-    
++ (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
     NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
     [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
     
@@ -9943,17 +9952,29 @@
     tear_name = [self translateSingleQuote:tear_name];
     NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
     tear_note = [self translateSingleQuote:tear_note];
-    NSString *pdf_path = [self valueInParams:params key:@"pdfUrl"];
+    
+    NSString *configureParams = [self valueInParams:params key:@"configureParams"];
+    
+    NSString *pdf_path = @"";
+    if (direct) {
+     
+        configureParams = [self valueInParams:params key:@"pdfUrl"];
+        
+    } else {
+        
+        pdf_path = [self valueInParams:params key:@"pdfPath"];
+        
+    }
+    
     pdf_path = [self translateSingleQuote:pdf_path];
     NSString *create_user = [self valueInParams:params key:@"user"];
     create_user = [self translateSingleQuote:create_user];
     
-    NSString *product_ids = [Singleton sharedInstance].pdf_product_ids;
-    NSString *item_ids = [Singleton sharedInstance].pdf_item_ids;
+    NSString *product_ids = [self valueInParams:params key:@"product_ids"];
+    NSString *item_ids = [self valueInParams:params key:@"item_ids"];
     
-    [Singleton sharedInstance].pdf_product_ids = nil;
-    [Singleton sharedInstance].pdf_item_ids = nil;
     
+    // model info
     NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),sheet_discount,ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
     
     __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
@@ -10009,6 +10030,8 @@
         
     }];
     
+    [iSalesDB close_db:db];
+    
     if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
         
         [iSalesDB close_db:db];
@@ -10016,22 +10039,66 @@
         
         return [RAUtils dict2data:resultDictionary];
     }
-
+    
     NSString *model_info = [RAUtils dict2string:dic];
     model_info = [self translateSingleQuote:model_info];
     
-    NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@');",pdf_path,create_user,tear_note,tear_name,model_info];
     
+    // 创建PDF
+
+    // 在preview情况下保存,则不需要新建了
+    if (direct) {
+        NSData *pdfData = [self offline_request_tearsheet:params]; // 新建PDF,获取本地路径
+        
+        NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
+        pdf_path = [pdfInfo objectForKey:@"pdf_path"];
+        
+        if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
+            
+            resultDictionary = pdfInfo.mutableCopy;
+            
+        } else { // 创建PDF失败
+            return pdfData;
+        }
+    } else {
+        // pdf_path 就是本地路径
+        [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
+    }
+    
+    // 将文件移动到PDF Cache文件夹
+    NSString *newPath = [pdf_path lastPathComponent];
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+    NSString *cachefolder = [paths objectAtIndex:0];
+    NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
+    newPath = [pdfFolder stringByAppendingPathComponent:newPath];
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSError *error = nil;
+    [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
+    if (error) { // 移动文件失败
+        [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
+        return [RAUtils dict2data:resultDictionary];
+    }
+    pdf_path = newPath;
     
+    // 保存信息
     
-    int result = [iSalesDB execSql:save_pdf_sql db:db];
+    NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,configureParams) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@','%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams];
     
-    [iSalesDB close_db:db];
-    [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
+    int result = [iSalesDB execSql:save_pdf_sql];
     
+    [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
     return [RAUtils dict2data:resultDictionary];
 }
 
++ (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
+    
+    return [self offline_savePDF:params direct:YES];
+}
+
++ (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
+    return [self offline_savePDF:params direct:NO];
+}
+
 + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
     
     int offset = [[params valueForKey:@"offset"] integerValue];

+ 4 - 11
RedAnt ERP Mobile/common/Functions/portfolio/TearSheetParamViewController.m

@@ -82,7 +82,6 @@
 }
 - (void)onPreviewClick:(UIButton *)sender {
     
-    
     // add params check here.
     [self.lastedit endEditing:true];
     [self.lasttextview endEditing:true];
@@ -364,7 +363,7 @@
                     UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Saving..."];
                     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                         
-                        NSDictionary* json = [iSalesNetwork save_TearSheet:path name:name delete:del_ids note:internal_note];
+                        NSDictionary* json = [iSalesNetwork save_TearSheet:path name:name delete:del_ids note:internal_note configureParams:pdf_url modelIds:upparams];
                         
                         dispatch_async(dispatch_get_main_queue(), ^{
                             [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
@@ -427,13 +426,6 @@
 }
 - (void)onSaveClick:(UIButton *)sender {
     
-    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-    if (appDelegate.offline_mode) {
-        [Singleton sharedInstance].pdf_item_ids = [self.content_data_download objectForKey:@"item_ids"];
-        [Singleton sharedInstance].pdf_product_ids = [self.content_data_download objectForKey:@"product_ids"];
-    }
-    
-    
     // add params check here.
     [self.lastedit endEditing:true];
     [self.lasttextview endEditing:true];
@@ -618,7 +610,8 @@
         //    [ret setValue:value forKey:valuefrom];
     }
     
-    
+    upparams[@"product_ids"]=self.product_ids;
+    upparams[@"item_ids"]=self.item_ids;
     
     __block NSString* saveName = catelog_name;
     
@@ -656,7 +649,7 @@
             UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Processing"];
             dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                 
-                NSDictionary* editor_json = [iSalesNetwork direct_save_TearSheet:pdf_url name:saveName delete:del_ids note:saveNote];
+                NSDictionary* editor_json = [iSalesNetwork direct_save_TearSheet:pdf_url name:saveName delete:del_ids note:saveNote modelIds:upparams];
                 
                 dispatch_async(dispatch_get_main_queue(), ^{
                     [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];

+ 1 - 1
RedAnt ERP Mobile/common/data_provider/iSalesDB.m

@@ -724,7 +724,7 @@ void decryptfield (sqlite3_context *context, int argc, sqlite3_value **argv) {
     
     NSString *create_offline_portfolio = @"CREATE TABLE IF NOT EXISTS offline_portfolio (_id integer PRIMARY KEY,product_id integer,name text,description tex,item_id integer,fashion_id integer,available_qty integer,percentage integer,percent double,sheet_price double,sheet_discount double,img text,line_note text,is_delete integer,is_dirty integer,createtime timestamp DEFAULT(datetime('now','localtime')),modifytime timestamp DEFAULT(datetime('now','localtime')));";
     
-    NSString *create_offline_pdf = @"create table if not exists offline_pdf (_id integer primary key,tearsheets_id integer,pdf_path text,create_user text,tear_note text,tear_name text,model_info text,is_dirty integer,createtime timestamp default(datetime('now','localtime')));";
+    NSString *create_offline_pdf = @"create table if not exists offline_pdf (_id integer primary key,tearsheets_id integer,pdf_path text,create_user text,tear_note text,tear_name text,model_info text,configureParams text,is_dirty integer,createtime timestamp default(datetime('now','localtime')));";
     
     
     NSString* create_order_trigger=@"CREATE TRIGGER  if not exists offline_order_insert after insert on offline_order BEGIN select offline_dirty();  UPDATE offline_order SET modify_time= datetime('now', 'localtime') WHERE _id=new._id;END;CREATE TRIGGER  if not exists offline_order_update after update on offline_order BEGIN select offline_dirty();  UPDATE offline_order SET modify_time= datetime('now', 'localtime') WHERE _id=new._id;END;CREATE TRIGGER  if not exists offline_order_delete after delete on offline_order BEGIN select offline_dirty();  END;";

+ 2 - 2
RedAnt ERP Mobile/common/data_provider/iSalesNetwork.h

@@ -104,8 +104,8 @@
 
 +(NSDictionary*)request_PDFList:(int ) offset limit:(int)limit keywords:(NSString* ) keywords ;
 +(NSDictionary*)request_TearSheet:(NSMutableDictionary*)params;
-+(NSDictionary*)save_TearSheet:(NSString*) path name:(NSString*)name delete:(NSString* )ids note:(NSString* )note;
-+(NSDictionary*)direct_save_TearSheet:(NSString*) request_url name:(NSString*)name delete:(NSString* )ids note:(NSString* )saveNote;
++(NSDictionary*)save_TearSheet:(NSString*) path name:(NSString*)name delete:(NSString* )ids note:(NSString* )note configureParams:(NSString *)configureParams modelIds:(NSDictionary *)modelIds;
++(NSDictionary*)direct_save_TearSheet:(NSString*) request_url name:(NSString*)name delete:(NSString* )ids note:(NSString* )saveNote modelIds:(NSDictionary *)modelIds;
 
 
 +(NSDictionary*)Category_addall: (NSString*) category customid:(int) customid price_template:(int) price_template sort:(int) sort_by filter:(NSString*) filter keyword:(NSString*) keyword  offset:(long) offset limit:(long) limit alert:(NSString*)alert qty:(NSString*)qty available:(NSString*)available price:(NSString*)price bestseller:(NSString*)bestseller modelname:(NSString*) modelname modeldescrip:(NSString*)modeldescrip orderCode:(NSString*) orderCode addTo:(NSString*) addTo;

+ 16 - 7
RedAnt ERP Mobile/common/data_provider/iSalesNetwork.m

@@ -1471,7 +1471,7 @@ repeat:
     return jsobj;
     
 }
-+(NSDictionary*)direct_save_TearSheet:(NSString*) request_url name:(NSString*)name delete:(NSString* )ids note:(NSString* )saveNote
++(NSDictionary*)direct_save_TearSheet:(NSString*) request_url name:(NSString*)name delete:(NSString* )ids note:(NSString* )saveNote modelIds:(NSDictionary *)modelIds
 {
     NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
     AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
@@ -1486,6 +1486,9 @@ repeat:
     [params setValue:name forKey:@"pdfName"];
     [params setValue:ids forKey:@"itemIds"];
     [params setValue:saveNote forKey:@"pdfNote"];
+    [params setValue:[modelIds objectForKey:@"product_ids"] forKey:@"product_ids"];
+    [params setValue:[modelIds objectForKey:@"item_ids"] forKey:@"item_ids"];
+    
     NSData* json= nil;
     
     if(appDelegate.offline_mode)
@@ -1578,7 +1581,7 @@ repeat:
     NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
     return jsobj;
 }
-+(NSDictionary*)save_TearSheet:(NSString*) path name:(NSString*)name delete:(NSString* )ids note:(NSString*)note
++(NSDictionary*)save_TearSheet:(NSString*) path name:(NSString*)name delete:(NSString* )ids note:(NSString*)note configureParams:(NSString *)configureParams modelIds:(NSDictionary *)modelIds
 {
     NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
     AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
@@ -1593,14 +1596,20 @@ repeat:
     [params setValue:name forKey:@"pdfName"];
     [params setValue:note forKey:@"pdfNote"];
     [params setValue:ids forKey:@"itemIds"];
+    [params setValue:configureParams forKey:@"configureParams"];
+    [params setValue:[modelIds objectForKey:@"product_ids"] forKey:@"product_ids"];
+    [params setValue:[modelIds objectForKey:@"item_ids"] forKey:@"item_ids"];
+    
+    NSData* json = nil;
     if(appDelegate.offline_mode)
     {
-        return [OLDataProvider offline_notimpl];
+        json = [OLDataProvider offline_save_TearSheet:params];
+    } else {
+        if(![self IsNetworkAvailable])
+            return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
+        
+        json=[self get_json:URL_SAVE_DM parameters:params];
     }
-    if(![self IsNetworkAvailable])
-        return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
-    
-    NSData* json=[self get_json:URL_SAVE_DM parameters:params];
     if(json==nil)
         return nil;
     NSError *error=nil;

+ 13 - 0
RedAnt ERP Mobile/iSales-NPD/AppDelegate.m

@@ -784,6 +784,19 @@ void UncaughtExceptionHandler(NSException *exception) {
         
     }
     
+    NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
+    
+    if(![fileManager fileExistsAtPath:pdfFolder isDirectory:&bdir])
+    {
+        NSError *error = nil;
+        bool bsuccess=[fileManager createDirectoryAtPath:pdfFolder withIntermediateDirectories:YES attributes:nil error:&error];
+        
+        if(!bsuccess)
+            DebugLog(@"Create PDF Cache folder failed");
+        
+    }
+
+    
     //    if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
     //
     //        NSLog(@"文件已经存在了");