Просмотр исходного кода

增加zip code,增加方法countryCodeByid:、countryCodeIdByCode:和offline_dealZipCode:,修改方法offline_getAllCountryDefault:以及offline_getStateByCountryCode: checkedState:,修改val_值的设定。

Pen Li 9 лет назад
Родитель
Сommit
9a0021a897

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


+ 115 - 32
RedAnt ERP Mobile/common/Functions/offline/OLDataProvider.m

@@ -2587,24 +2587,29 @@
 #pragma mark - Jack
 #pragma mark - Jack
 
 
 #pragma mark create new contact
 #pragma mark create new contact
-+ (NSDictionary *)offline_getAllCountry {
++ (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
 //    "val_227" : {
 //    "val_227" : {
 //        "check" : 1,
 //        "check" : 1,
 //        "value" : "US United States",
 //        "value" : "US United States",
 //        "value_id" : "228"
 //        "value_id" : "228"
 //    },
 //    },
     
     
+    if (!countryCode) {
+        countryCode = @"US";
+    }
+    
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     sqlite3 *db = [iSalesDB get_db];
     sqlite3 *db = [iSalesDB get_db];
     
     
-    const char *sqlQuery = "select * from offline_country;";
+    const char *sqlQuery = "select _id,name,code,countrycode_id from offline_country;";
     sqlite3_stmt * statement;
     sqlite3_stmt * statement;
     
     
     if (sqlite3_prepare_v2(db, sqlQuery, -1, &statement, nil) == SQLITE_OK) {
     if (sqlite3_prepare_v2(db, sqlQuery, -1, &statement, nil) == SQLITE_OK) {
         
         
+        long count = 0;
         while (sqlite3_step(statement) == SQLITE_ROW) {
         while (sqlite3_step(statement) == SQLITE_ROW) {
             char *name = (char *) sqlite3_column_text(statement, 1); // 全称
             char *name = (char *) sqlite3_column_text(statement, 1); // 全称
-//            char *code = (char *) sqlite3_column_text(statement, 2); // 缩写
+            char *code = (char *) sqlite3_column_text(statement, 2); // 缩写
             int code_id = sqlite3_column_int(statement, 3); // id
             int code_id = sqlite3_column_int(statement, 3); // id
             
             
             NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
             NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
@@ -2612,7 +2617,11 @@
             [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
             [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
             [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
             [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
             
             
-            NSString *key = [NSString stringWithFormat:@"val_%d",code_id - 1];
+            if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
+                [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
+            }
+            
+            NSString *key = [NSString stringWithFormat:@"val_%ld",count++];
             [dic setValue:countryDic forKey:key];
             [dic setValue:countryDic forKey:key];
         }
         }
         
         
@@ -2624,13 +2633,13 @@
     return dic;
     return dic;
 }
 }
 
 
-+ (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode {
++ (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
     
     
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     
     
     sqlite3 *db = [iSalesDB get_db];
     sqlite3 *db = [iSalesDB get_db];
     
     
-    NSString *sqlQuery = [NSString stringWithFormat:@"select * from offline_state where country_code = '%@';",countryCode];
+    NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
     
     
 //    NSString *sqlQuery = [NSString stringWithFormat:@"select * from offline_state"];
 //    NSString *sqlQuery = [NSString stringWithFormat:@"select * from offline_state"];
     DebugLog(@"query state:%@",sqlQuery);
     DebugLog(@"query state:%@",sqlQuery);
@@ -2648,6 +2657,10 @@
             [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
             [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
             [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
             [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
             
             
+            if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
+                [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
+            }
+            
             NSString *key = [NSString stringWithFormat:@"val_%d",i++];
             NSString *key = [NSString stringWithFormat:@"val_%d",i++];
             [dic setValue:stateDic forKey:key];
             [dic setValue:stateDic forKey:key];
         }
         }
@@ -2664,7 +2677,7 @@
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     
     
     sqlite3 *db = [iSalesDB get_db];
     sqlite3 *db = [iSalesDB get_db];
-    NSString *sqlQuery = [NSString stringWithFormat:@"select * from price;"];
+    NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
     sqlite3_stmt * statement;
     sqlite3_stmt * statement;
     
     
     if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
     if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
@@ -2700,16 +2713,17 @@
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     NSMutableDictionary *dic = [NSMutableDictionary dictionary];
     
     
     sqlite3 *db = [iSalesDB get_db];
     sqlite3 *db = [iSalesDB get_db];
-    NSString *sqlQuery = [NSString stringWithFormat:@"select * from offline_salesrep;"];
+    NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
     sqlite3_stmt * statement;
     sqlite3_stmt * statement;
     
     
     if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
     if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
+        long count = 0;
         while (sqlite3_step(statement) == SQLITE_ROW) {
         while (sqlite3_step(statement) == SQLITE_ROW) {
             // 1 name 2 code 3 salesrep_id
             // 1 name 2 code 3 salesrep_id
             char *name = (char *)sqlite3_column_text(statement, 1);
             char *name = (char *)sqlite3_column_text(statement, 1);
             char *code = (char *)sqlite3_column_text(statement, 2);
             char *code = (char *)sqlite3_column_text(statement, 2);
             int salesrep_id = sqlite3_column_int(statement, 3);
             int salesrep_id = sqlite3_column_int(statement, 3);
-            int _id = sqlite3_column_int(statement, 0);
+//            int _id = sqlite3_column_int(statement, 0);
 //
 //
 //            "val_0" : {
 //            "val_0" : {
 //                "value" : "BBSC - BARBARA BORROUGHS",
 //                "value" : "BBSC - BARBARA BORROUGHS",
@@ -2718,7 +2732,7 @@
             NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
             NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
             [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
             [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
             [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
             [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
-            [dic setValue:repDic forKey:[NSString stringWithFormat:@"val_%d",_id - 1]];
+            [dic setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",count++]];
         }
         }
         
         
         sqlite3_finalize(statement);
         sqlite3_finalize(statement);
@@ -2759,57 +2773,126 @@
     return ret;
     return ret;
 }
 }
 
 
++ (NSString *)countryCodeIdByCode:(NSString *)code {
+    NSString *ret = nil;
+    
+    sqlite3 *db = [iSalesDB get_db];
+    NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
+    sqlite3_stmt * statement;
+    
+    if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
+        while (sqlite3_step(statement) == SQLITE_ROW) {
+            char *_id = (char *)sqlite3_column_text(statement, 0);
+            ret = [NSString stringWithFormat:@"%s",_id];
+        }
+        
+        sqlite3_finalize(statement);
+    }
+    sqlite3_close(db);
+
+    return ret;
+}
+
++ (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
+    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
+    
+    sqlite3 *db = [iSalesDB get_db];
+    NSString *sqlQuery = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
+    sqlite3_stmt * statement;
+    
+    if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
+        while (sqlite3_step(statement) == SQLITE_ROW) {
+            char *country = (char *)sqlite3_column_text(statement, 0);
+            char *state = (char *)sqlite3_column_text(statement, 1);
+            char *city = (char *)sqlite3_column_text(statement, 2);
+            char *country_code = (char *)sqlite3_column_text(statement, 3);
+            char *state_code = (char *)sqlite3_column_text(statement, 4);
+            
+            [dic setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
+            [dic setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
+            [dic setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
+            [dic setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
+            [dic setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
+        }
+        
+        sqlite3_finalize(statement);
+    }
+    sqlite3_close(db);
+
+    
+    return dic;
+}
+
 + (NSData *)offline_createContact:(NSMutableDictionary *)params {
 + (NSData *)offline_createContact:(NSMutableDictionary *)params {
     
     
     NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
     NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
     NSData *data = [NSData dataWithContentsOfFile:path];
     NSData *data = [NSData dataWithContentsOfFile:path];
     NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
     NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
     
     
-    // country
-    NSDictionary *allCountry = [self offline_getAllCountry];
+    NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
+    
+    
     
     
-    NSString *key = nil;
     NSString *countryCode = nil;
     NSString *countryCode = nil;
+    NSString *countryCode_id = nil;
+    NSString *stateCode = nil;
+    NSString *city = nil;
+    NSString *zipCode = nil;
+    
     if ([params.allKeys containsObject:@"refresh_trigger"]) {
     if ([params.allKeys containsObject:@"refresh_trigger"]) {
+        
         if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
         if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
+            
             NSString *code_id = params[@"country"];
             NSString *code_id = params[@"country"];
-            key = [NSString stringWithFormat:@"val_%d",[code_id integerValue] - 1];
+            countryCode_id = code_id;
             countryCode = [self countryCodeByid:code_id];
             countryCode = [self countryCodeByid:code_id];
+            
+        } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
+            
+            NSString *zip_code = params[@"zipcode"];
+            zipCode = zip_code;
+            
+            NSDictionary *dic = [self offline_dealZipCode:zip_code];
+            
+            countryCode = [dic valueForKey:@"country_code"];
+            
+            stateCode = [dic valueForKey:@"state_code"];
+            
+            city = [dic valueForKey:@"city"];
+            
+            // zip code
+            NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
+            [zipDic setValue:zipCode forKey:@"value"];
+            [section_0 setValue:zipDic forKey:@"item_10"];
         }
         }
+        
     } else {
     } else {
         // default: US United States
         // default: US United States
-        key = @"val_227";
         countryCode = @"US";
         countryCode = @"US";
     }
     }
     
     
-    NSMutableDictionary *country = [allCountry objectForKey:key];
-    [country setValue:[NSNumber numberWithInt:1] forKey:@"check"];
-    
-    NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
-//    NSDictionary *item_country = [section_0 objectForKey:@"item_5"];
-//    [item_country setValue:allCountry forKey:@"cadedate"];
-    
+    // country
+    NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
     [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
     [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
     
     
     // state
     // state
-    NSDictionary *allState = [self offline_getStateByCountryCode:countryCode];
-//    NSMutableDictionary *item_state = [section_0 objectForKey:@"item_11"];
-//    [item_state setValue:allState forKey:@"cadedate"];
-    
+    NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
     [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
     [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
     
     
+    // city
+    if (city) {
+        NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
+        [cityDic setValue:city forKey:@"value"];
+        
+        [section_0 setValue:cityDic forKey:@"item_12"];
+    }
+    
     // price type
     // price type
     NSDictionary *priceDic = [self offline_getPrice];
     NSDictionary *priceDic = [self offline_getPrice];
-//    NSDictionary *item_price = [section_0 objectForKey:@"item_2"];
-//    [item_price setValue:priceDic forKey:@"cadedate"];
-    
     [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
     [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
 
 
     // Sales Rep
     // Sales Rep
     NSDictionary *repDic = [self offline_getSalesRep];
     NSDictionary *repDic = [self offline_getSalesRep];
-//    NSDictionary *item_rep = [section_0 objectForKey:@"item_17"];
-//    [item_rep setValue:repDic forKey:@"cadedate"];
-    
     [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
     [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
     
     
     [ret setValue:section_0 forKey:@"section_0"];
     [ret setValue:section_0 forKey:@"section_0"];