OLDataProvider.m 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. @interface OLDataProvider ()
  14. @end
  15. @implementation OLDataProvider
  16. +(bool) check_offlinedata
  17. {
  18. UIApplication * app = [UIApplication sharedApplication];
  19. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  20. sqlite3 *db = [iSalesDB get_db];
  21. NSString * where=@"1=1";
  22. // if(appDelegate.user!=nil)
  23. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  24. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  25. if(count==0)
  26. {
  27. return false;
  28. }
  29. return true;
  30. //
  31. sqlite3_close(db);
  32. }
  33. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  34. {
  35. UIApplication * app = [UIApplication sharedApplication];
  36. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  37. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  38. sqlite3 *db = [iSalesDB get_db];
  39. NSString* collectId=params[@"collectId"];
  40. NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  41. [iSalesDB execSql:sqlQuery];
  42. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  43. sqlite3_close(db);
  44. appDelegate.wish_count =count;
  45. [appDelegate update_count_mark];
  46. ret[@"result"]= [NSNumber numberWithInt:2];
  47. return ret;
  48. }
  49. +(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  50. {
  51. UIApplication * app = [UIApplication sharedApplication];
  52. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  53. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  54. sqlite3 *db = [iSalesDB get_db];
  55. NSString* product_id=params[@"product_id"];
  56. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  57. for(int i=0;i<arr.count;i++)
  58. {
  59. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  60. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  61. if(count==0)
  62. {
  63. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  64. [iSalesDB execSql:sqlQuery];
  65. }
  66. }
  67. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  68. sqlite3_close(db);
  69. appDelegate.wish_count =count;
  70. [appDelegate update_count_mark];
  71. ret[@"result"]= [NSNumber numberWithInt:2];
  72. return ret;
  73. //
  74. //return ret;
  75. }
  76. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  77. {
  78. UIApplication * app = [UIApplication sharedApplication];
  79. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  80. NSString* user = appDelegate.user;
  81. sqlite3 *db = [iSalesDB get_db];
  82. NSString* sqlQuery = @"select w.product_id,m.name||'\n'||m.description,w._id from wishlist as w left join model as m on w.product_id=m.product_id order by w.create_time;";
  83. sqlite3_stmt * statement;
  84. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  85. int count=0;
  86. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  87. {
  88. while (sqlite3_step(statement) == SQLITE_ROW)
  89. {
  90. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  91. int product_id = sqlite3_column_double(statement, 0);
  92. char *description = (char*)sqlite3_column_text(statement, 1);
  93. if(description==nil)
  94. description= "";
  95. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  96. int item_id = sqlite3_column_double(statement, 2);
  97. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil];
  98. // char *url = (char*)sqlite3_column_text(statement, 3);
  99. // if(url==nil)
  100. // url="";
  101. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  102. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  103. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  104. item[@"description"]= nsdescription;
  105. item[@"img"]= nsurl;
  106. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  107. count++;
  108. }
  109. ret[@"count"]= [NSNumber numberWithInt:count];
  110. ret[@"total_count"]= [NSNumber numberWithInt:count];
  111. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  112. ret[@"result"]= [NSNumber numberWithInt:2];
  113. appDelegate.wish_count =count;
  114. [appDelegate update_count_mark];
  115. sqlite3_finalize(statement);
  116. }
  117. sqlite3_close(db);
  118. return ret;
  119. }
  120. +(NSDictionary*) offline_notimpl
  121. {
  122. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  123. ret[@"result"]=@"8";
  124. ret[@"err_msg"]=@"offline mode does not support this function.";
  125. return ret;
  126. }
  127. +(NSDictionary*) offline_home
  128. {
  129. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  130. NSString *cachefolder = [paths objectAtIndex:0];
  131. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  132. NSData* json =nil;
  133. json=[NSData dataWithContentsOfFile:img_cache];
  134. NSError *error=nil;
  135. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  136. return menu;
  137. }
  138. +(NSDictionary*) offline_category_menu
  139. {
  140. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  141. NSString *cachefolder = [paths objectAtIndex:0];
  142. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  143. NSData* json =nil;
  144. json=[NSData dataWithContentsOfFile:img_cache];
  145. NSError *error=nil;
  146. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  147. return menu;
  148. }
  149. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  150. {
  151. NSString* offline_command=params[@"offline_Command"];
  152. NSDictionary* ret=nil;
  153. if([offline_command isEqualToString:@"model_NIYMAL"])
  154. {
  155. NSString* category = params[@"category"];
  156. ret = [self refresh_model_NIYMAL:category];
  157. }
  158. return ret;
  159. }
  160. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  161. {
  162. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  163. [ret setValue:@"2" forKey:@"result"];
  164. [ret setValue:@"Regular Mode" forKey:@"mode"];
  165. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category] mutableCopy];
  166. [ret setObject:detail1_section forKey:@"detail_1"];
  167. return ret;
  168. }
  169. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id product_id:(int) product_id
  170. {
  171. UIApplication * app = [UIApplication sharedApplication];
  172. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  173. // NSArray* arr1 = [self get_user_all_price_type];
  174. NSArray* arr2 = [self get_contact_default_price_type:contact_id];
  175. // NSSet *set1 = [NSSet setWithArray:arr1];
  176. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  177. // if(appDelegate.contact_id==nil)
  178. // set2=[set1 mutableCopy];
  179. // else
  180. // [set2 intersectsSet:set1];
  181. // NSArray *retarr = [set2 allObjects];
  182. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  183. sqlite3 *db = [iSalesDB get_db];
  184. NSString* sqlQuery = nil;
  185. if(appDelegate.contact_id==nil)
  186. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.product_id=%d order by a.order_by;",product_id];
  187. else
  188. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.product_id=%d and b.price_name in(%@) order by a.order_by;",product_id,whereprice];
  189. sqlite3_stmt * statement;
  190. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  191. int count=0;
  192. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  193. {
  194. while (sqlite3_step(statement) == SQLITE_ROW)
  195. {
  196. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  197. char *name = (char*)sqlite3_column_text(statement, 0);
  198. if(name==nil)
  199. name="";
  200. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  201. // double price = sqlite3_column_double(statement, 1);
  202. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  203. // if(isnull==nil)
  204. // item[nsname]= @"No Price";
  205. // else
  206. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  207. char *price = (char*)sqlite3_column_text(statement, 1);
  208. if(price!=nil)
  209. {
  210. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  211. nsprice=[AESCrypt fastdecrypt:nsprice];
  212. if(nsprice.length>0)
  213. {
  214. double dp= [nsprice doubleValue];
  215. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  216. }
  217. }
  218. else
  219. {
  220. item[nsname]= @"No Price";
  221. }
  222. // item[nsname]= nsprice;
  223. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  224. count++;
  225. }
  226. ret[@"count"]= [NSNumber numberWithInt:count];
  227. sqlite3_finalize(statement);
  228. }
  229. sqlite3_close(db);
  230. return ret;
  231. }
  232. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id
  233. {
  234. NSArray* arr1 = [self get_user_all_price_type];
  235. NSArray* arr2 = [self get_contact_default_price_type:contact_id];
  236. // NSSet *set1 = [NSSet setWithArray:arr1];
  237. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  238. // [set2 intersectsSet:set1];
  239. //
  240. //
  241. // NSArray *retarr = [set2 allObjects];
  242. NSString* whereprice=nil;
  243. if(contact_id==nil)
  244. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  245. else
  246. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  247. sqlite3 *db = [iSalesDB get_db];
  248. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  249. sqlite3_stmt * statement;
  250. NSNumber* ret = nil;
  251. double dprice=DBL_MAX;
  252. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  253. {
  254. while (sqlite3_step(statement) == SQLITE_ROW)
  255. {
  256. // double val = sqlite3_column_double(statement, 0);
  257. char *price = (char*)sqlite3_column_text(statement, 0);
  258. if(price!=nil)
  259. {
  260. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  261. nsprice=[AESCrypt fastdecrypt:nsprice];
  262. if(nsprice.length>0)
  263. {
  264. double dp= [nsprice doubleValue];
  265. if(dp<dprice)
  266. dprice=dp;
  267. }
  268. }
  269. }
  270. sqlite3_finalize(statement);
  271. }
  272. sqlite3_close(db);
  273. if(dprice==DBL_MAX)
  274. ret= nil;
  275. else
  276. ret= [NSNumber numberWithDouble:dprice];
  277. return ret;
  278. }
  279. +(NSArray*) get_user_all_price_type
  280. {
  281. NSArray* ret=nil;
  282. sqlite3 *db = [iSalesDB get_db];
  283. // no customer assigned , use login user contact_id
  284. UIApplication * app = [UIApplication sharedApplication];
  285. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  286. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  287. sqlite3_stmt * statement;
  288. // int count=0;
  289. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  290. {
  291. if (sqlite3_step(statement) == SQLITE_ROW)
  292. {
  293. char *val = (char*)sqlite3_column_text(statement, 0);
  294. if(val==nil)
  295. val="";
  296. NSString* price = [[NSString alloc]initWithUTF8String:val];
  297. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  298. }
  299. sqlite3_finalize(statement);
  300. }
  301. sqlite3_close(db);
  302. return ret;
  303. }
  304. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id
  305. {
  306. sqlite3 *db = [iSalesDB get_db];
  307. if(contact_id==nil)
  308. {
  309. // no customer assigned , use login user contact_id
  310. UIApplication * app = [UIApplication sharedApplication];
  311. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  312. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  313. sqlite3_stmt * statement;
  314. // int count=0;
  315. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  316. {
  317. if (sqlite3_step(statement) == SQLITE_ROW)
  318. {
  319. char *val = (char*)sqlite3_column_text(statement, 0);
  320. if(val==nil)
  321. val="";
  322. contact_id = [[NSString alloc]initWithUTF8String:val];
  323. }
  324. sqlite3_finalize(statement);
  325. }
  326. if(contact_id.length<=0)
  327. {
  328. sqlite3_close(db);
  329. return nil;
  330. }
  331. }
  332. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  333. sqlite3_stmt * statement;
  334. NSArray* ret=nil;
  335. // int count=0;
  336. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  337. {
  338. if (sqlite3_step(statement) == SQLITE_ROW)
  339. {
  340. char *price_type = (char*)sqlite3_column_text(statement, 0);
  341. if(price_type==nil)
  342. price_type="";
  343. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  344. if(nsprice_type.length>0)
  345. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  346. else
  347. ret=nil;
  348. }
  349. sqlite3_finalize(statement);
  350. }
  351. sqlite3_close(db);
  352. return ret;
  353. }
  354. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name
  355. {
  356. NSString* ret= nil;
  357. sqlite3 *db = [iSalesDB get_db];
  358. NSString *sqlQuery = nil;
  359. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  360. if(product_id==nil)
  361. sqlQuery = [NSString stringWithFormat:@"select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];
  362. else
  363. sqlQuery = [NSString stringWithFormat:@"select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];
  364. sqlite3_stmt * statement;
  365. // int count=0;
  366. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  367. {
  368. if (sqlite3_step(statement) == SQLITE_ROW)
  369. {
  370. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  371. if(imgurl==nil)
  372. imgurl="";
  373. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  374. ret=nsimgurl;
  375. }
  376. sqlite3_finalize(statement);
  377. }
  378. else
  379. {
  380. [ret setValue:@"8" forKey:@"result"];
  381. }
  382. sqlite3_close(db);
  383. DebugLog(@"data string: %@",ret );
  384. return ret;
  385. }
  386. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  387. {
  388. NSString* ret= nil;
  389. sqlite3 *db = [iSalesDB get_db];
  390. NSString *sqlQuery = nil;
  391. if(product_id==nil)
  392. sqlQuery = [NSString stringWithFormat:@"select default_category from model where name='%@';",model_name];
  393. else
  394. sqlQuery = [NSString stringWithFormat:@"select default_category from model where product_id=%@;",product_id];
  395. sqlite3_stmt * statement;
  396. // int count=0;
  397. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  398. {
  399. if (sqlite3_step(statement) == SQLITE_ROW)
  400. {
  401. char *default_category = (char*)sqlite3_column_text(statement, 0);
  402. if(default_category==nil)
  403. default_category="";
  404. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  405. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  406. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  407. ret=nsdefault_category;
  408. }
  409. sqlite3_finalize(statement);
  410. }
  411. else
  412. {
  413. [ret setValue:@"8" forKey:@"result"];
  414. }
  415. sqlite3_close(db);
  416. DebugLog(@"data string: %@",ret );
  417. return ret;
  418. }
  419. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  420. {
  421. NSString* model_name = [params valueForKey:@"product_name"];
  422. NSString* product_id = [params valueForKey:@"product_id"];
  423. NSString* category = [params valueForKey:@"category"];
  424. if(category==nil)
  425. category = [self model_default_category:product_id model_name:model_name];
  426. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  427. sqlite3 *db = [iSalesDB get_db];
  428. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  429. NSString *sqlQuery = nil;
  430. if(product_id==nil)
  431. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';",model_name];
  432. else
  433. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;",product_id];
  434. sqlite3_stmt * statement;
  435. [ret setValue:@"2" forKey:@"result"];
  436. [ret setValue:@"3" forKey:@"detail_section_count"];
  437. // int count=0;
  438. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  439. {
  440. if (sqlite3_step(statement) == SQLITE_ROW)
  441. {
  442. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  443. char *name = (char*)sqlite3_column_text(statement, 0);
  444. if(name==nil)
  445. name="";
  446. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  447. char *description = (char*)sqlite3_column_text(statement, 1);
  448. if(description==nil)
  449. description="";
  450. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  451. int product_id = sqlite3_column_int(statement, 2);
  452. char *color = (char*)sqlite3_column_text(statement, 3);
  453. if(color==nil)
  454. color="";
  455. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  456. //
  457. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  458. // if(legcolor==nil)
  459. // legcolor="";
  460. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  461. //
  462. //
  463. int availability = sqlite3_column_int(statement, 5);
  464. //
  465. // int incoming_stock = sqlite3_column_int(statement, 6);
  466. char *demension = (char*)sqlite3_column_text(statement, 7);
  467. if(demension==nil)
  468. demension="";
  469. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  470. // ,,,,,,,,,
  471. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  472. if(seat_height==nil)
  473. seat_height="";
  474. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  475. char *material = (char*)sqlite3_column_text(statement, 9);
  476. if(material==nil)
  477. material="";
  478. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  479. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  480. if(box_dim==nil)
  481. box_dim="";
  482. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  483. char *volume = (char*)sqlite3_column_text(statement, 11);
  484. if(volume==nil)
  485. volume="";
  486. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  487. double weight = sqlite3_column_double(statement, 12);
  488. char *model_set = (char*)sqlite3_column_text(statement, 13);
  489. if(model_set==nil)
  490. model_set="";
  491. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  492. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  493. if(load_ability==nil)
  494. load_ability="";
  495. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  496. char *default_category = (char*)sqlite3_column_text(statement, 15);
  497. if(default_category==nil)
  498. default_category="";
  499. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  500. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  501. if(fabric_content==nil)
  502. fabric_content="";
  503. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  504. char *assembling = (char*)sqlite3_column_text(statement, 17);
  505. if(assembling==nil)
  506. assembling="";
  507. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  508. char *made_in = (char*)sqlite3_column_text(statement, 18);
  509. if(made_in==nil)
  510. made_in="";
  511. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  512. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  513. if(special_remarks==nil)
  514. special_remarks="";
  515. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  516. int stockUcom = sqlite3_column_double(statement, 20);
  517. char *product_group = (char*)sqlite3_column_text(statement, 21);
  518. if(product_group==nil)
  519. product_group="";
  520. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  521. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  522. // if(fashion_selector==nil)
  523. // fashion_selector="";
  524. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  525. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  526. if(selector_field==nil)
  527. selector_field="";
  528. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  529. char *property_field = (char*)sqlite3_column_text(statement, 23);
  530. if(property_field==nil)
  531. property_field="";
  532. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  533. char *packaging = (char*)sqlite3_column_text(statement, 24);
  534. if(packaging==nil)
  535. packaging="";
  536. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  537. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  538. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  539. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name];
  540. [img_section setValue:model_s_img forKey:@"model_s_img"];
  541. [img_section setObject:[self model_img:product_id] forKey:@"images"];
  542. [img_section setObject:[self model_property:product_id field:nsproperty_field] forKey:@"property"];
  543. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field] forKey:@"selector"];
  544. NSString* Availability=nil;
  545. if(availability>0)
  546. Availability=[NSString stringWithFormat:@"%d",availability];
  547. else
  548. Availability = @"In Production";
  549. [img_section setValue:Availability forKey:@"Availability"];
  550. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  551. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  552. NSString* Price=nil;
  553. if(appDelegate.bLogin==false)
  554. Price=@"Must Sign in.";
  555. else
  556. {
  557. NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id];
  558. if(price==nil)
  559. Price=@"No Price.";
  560. else
  561. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  562. }
  563. [img_section setValue:Price forKey:@"price"];
  564. [img_section setValue:nsname forKey:@"model_name"];
  565. [img_section setValue:nsdescription forKey:@"model_descrition"];
  566. [ret setObject:img_section forKey:@"img_section"];
  567. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  568. int detail0_item_count=0;
  569. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  570. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  571. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  572. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  573. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  574. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  575. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  576. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  577. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  578. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  579. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  580. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  581. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  582. NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id];
  583. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  584. {
  585. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  586. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  587. }
  588. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  589. [detail0_section setValue:@"kv" forKey:@"type"];
  590. [detail0_section setValue:@"Product Information" forKey:@"title"];
  591. [ret setObject:detail0_section forKey:@"detail_0"];
  592. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category] mutableCopy];
  593. // [detail1_section setValue:@"detail" forKey:@"target"];
  594. // [detail1_section setValue:@"popup" forKey:@"action"];
  595. // [detail1_section setValue:@"content" forKey:@"type"];
  596. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  597. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  598. // [detail1_section setValue:@"true" forKey:@"single_row"];
  599. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  600. [ret setObject:detail1_section forKey:@"detail_1"];
  601. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  602. [detail2_section setValue:@"detail" forKey:@"target"];
  603. [detail2_section setValue:@"popup" forKey:@"action"];
  604. [detail2_section setValue:@"content" forKey:@"type"];
  605. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  606. [detail2_section setValue:@"true" forKey:@"single_row"];
  607. [detail2_section setValue:@"local" forKey:@"data"];
  608. [ret setObject:detail2_section forKey:@"detail_2"];
  609. }
  610. sqlite3_finalize(statement);
  611. }
  612. else
  613. {
  614. [ret setValue:@"8" forKey:@"result"];
  615. }
  616. NSLog(@"count:%d",count);
  617. sqlite3_close(db);
  618. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  619. return ret;
  620. }
  621. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  622. {
  623. NSString* orderCode = [params valueForKey:@"orderCode"];
  624. NSString* category = [params valueForKey:@"category"];
  625. int limit = [[params valueForKey:@"limit"] intValue];
  626. int offset = [[params valueForKey:@"offset"] intValue];
  627. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  628. sqlite3 *db = [iSalesDB get_db];
  629. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'%%#%@#%%'",category]];
  630. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where m.category like'%%#%@#%%' order by m.name limit %d offset %d ;",category,limit, offset];
  631. DebugLog(@"offline_category sql:%@",sqlQuery);
  632. sqlite3_stmt * statement;
  633. [ret setValue:@"2" forKey:@"result"];
  634. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  635. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  636. // int count=0;
  637. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  638. {
  639. int i=0;
  640. while (sqlite3_step(statement) == SQLITE_ROW)
  641. {
  642. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  643. // char *name = (char*)sqlite3_column_text(statement, 1);
  644. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  645. char *name = (char*)sqlite3_column_text(statement, 0);
  646. if(name==nil)
  647. name="";
  648. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  649. char *description = (char*)sqlite3_column_text(statement, 1);
  650. if(description==nil)
  651. description="";
  652. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  653. int product_id = sqlite3_column_int(statement, 2);
  654. // char *url = (char*)sqlite3_column_text(statement, 3);
  655. // if(url==nil)
  656. // url="";
  657. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  658. int wid = sqlite3_column_int(statement, 3);
  659. int closeout = sqlite3_column_int(statement, 4);
  660. NSMutableDictionary* imgjson=[self category_img:product_id];
  661. if(wid==0)
  662. [item setValue:@"false" forKey:@"wish_exists"];
  663. else
  664. [item setValue:@"true" forKey:@"wish_exists"];
  665. if(closeout==0)
  666. [item setValue:@"false" forKey:@"is_closeout"];
  667. else
  668. [item setValue:@"true" forKey:@"is_closeout"];
  669. [item addEntriesFromDictionary:imgjson];
  670. // [item setValue:nsurl forKey:@"img"];
  671. [item setValue:nsname forKey:@"name"];
  672. [item setValue:nsdescription forKey:@"description"];
  673. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  674. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  675. i++;
  676. }
  677. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  678. [ret setObject:items forKey:@"items"];
  679. sqlite3_finalize(statement);
  680. }
  681. NSLog(@"count:%d",count);
  682. sqlite3_close(db);
  683. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  684. return ret;
  685. }
  686. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  687. {
  688. NSString* orderCode = [params valueForKey:@"orderCode"];
  689. NSString* keyword = [params valueForKey:@"keyword"];
  690. keyword=keyword.lowercaseString;
  691. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  692. int limit = [[params valueForKey:@"limit"] intValue];
  693. int offset = [[params valueForKey:@"offset"] intValue];
  694. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  695. sqlite3 *db = [iSalesDB get_db];
  696. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"lower(name) like'%%%@%%' or lower(description) like'%%%@%%'",keyword,keyword]];
  697. NSString *sqlQuery = nil;
  698. if(exactMatch )
  699. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;",keyword,limit, offset];
  700. else
  701. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or lower(description) like'%%%@%%' order by m.name limit %d offset %d ;",keyword,keyword,limit, offset];
  702. DebugLog(@"offline_search sql:%@",sqlQuery);
  703. sqlite3_stmt * statement;
  704. [ret setValue:@"2" forKey:@"result"];
  705. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  706. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  707. // int count=0;
  708. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  709. {
  710. int i=0;
  711. while (sqlite3_step(statement) == SQLITE_ROW)
  712. {
  713. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  714. // char *name = (char*)sqlite3_column_text(statement, 1);
  715. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  716. char *name = (char*)sqlite3_column_text(statement, 0);
  717. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  718. char *description = (char*)sqlite3_column_text(statement, 1);
  719. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  720. int product_id = sqlite3_column_int(statement, 2);
  721. // char *url = (char*)sqlite3_column_text(statement, 3);
  722. // if(url==nil)
  723. // url="";
  724. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  725. int wid = sqlite3_column_int(statement, 3);
  726. int closeout = sqlite3_column_int(statement, 4);
  727. NSMutableDictionary* imgjson=[self category_img:product_id];
  728. if(wid==0)
  729. [item setValue:@"false" forKey:@"wish_exists"];
  730. else
  731. [item setValue:@"true" forKey:@"wish_exists"];
  732. if(closeout==0)
  733. [item setValue:@"false" forKey:@"is_closeout"];
  734. else
  735. [item setValue:@"true" forKey:@"is_closeout"];
  736. [item addEntriesFromDictionary:imgjson];
  737. // [item setValue:nsurl forKey:@"img"];
  738. [item setValue:nsname forKey:@"fash_name"];
  739. [item setValue:nsdescription forKey:@"description"];
  740. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  741. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  742. i++;
  743. }
  744. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  745. [ret setObject:items forKey:@"items"];
  746. sqlite3_finalize(statement);
  747. }
  748. NSLog(@"count:%d",count);
  749. sqlite3_close(db);
  750. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  751. return ret;
  752. }
  753. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  754. {
  755. // [params setValue:covertype forKey:@"covertype"];
  756. // [params setValue:ctgid forKey:@"ctgId"];
  757. // [params setValue:modelname forKey:@"modelName"];
  758. // [params setValue:modeldescrip forKey:@"modelDescription"];
  759. // [params setValue:alert forKey:@"alert"];
  760. // [params setValue:qty forKey:@"sold_by_qty"];
  761. // [params setValue:available forKey:@"available"];
  762. // [params setValue:price forKey:@"price"];
  763. // [params setValue:bestseller forKey:@"bestseller"];
  764. NSString* orderCode = [params valueForKey:@"orderCode"];
  765. int covertype = [[params valueForKey:@"covertype"] intValue];
  766. NSString* where= nil;
  767. NSString* orderby= @"m.name";
  768. switch (covertype) {
  769. case 0:
  770. {
  771. where=@"m.category like'%%#005#%%'";
  772. break;
  773. }
  774. case 1:
  775. {
  776. where=@"m.alert like '%QS%'";
  777. break;
  778. }
  779. case 2:
  780. {
  781. where=@"m.availability>0";
  782. break;
  783. }
  784. case 3:
  785. {
  786. where=@"m.best_seller>0";
  787. orderby=@"m.best_seller desc";
  788. break;
  789. }
  790. default:
  791. where=@"1=1";
  792. break;
  793. }
  794. // NSString* keyword = [params valueForKey:@"keyword"];
  795. // bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  796. int limit = [[params valueForKey:@"limit"] intValue];
  797. int offset = [[params valueForKey:@"offset"] intValue];
  798. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  799. sqlite3 *db = [iSalesDB get_db];
  800. int count = [iSalesDB get_recordcount:db table:@"model" where:where];
  801. NSString *sqlQuery = nil;
  802. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where %@ order by %@ limit %d offset %d ;",where,orderby,limit, offset];
  803. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  804. sqlite3_stmt * statement;
  805. [ret setValue:@"2" forKey:@"result"];
  806. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  807. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  808. // int count=0;
  809. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  810. {
  811. int i=0;
  812. while (sqlite3_step(statement) == SQLITE_ROW)
  813. {
  814. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  815. // char *name = (char*)sqlite3_column_text(statement, 1);
  816. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  817. char *name = (char*)sqlite3_column_text(statement, 0);
  818. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  819. char *description = (char*)sqlite3_column_text(statement, 1);
  820. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  821. int product_id = sqlite3_column_int(statement, 2);
  822. // char *url = (char*)sqlite3_column_text(statement, 3);
  823. // if(url==nil)
  824. // url="";
  825. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  826. int wid = sqlite3_column_int(statement, 3);
  827. int closeout = sqlite3_column_int(statement, 4);
  828. NSMutableDictionary* imgjson=[self category_img:product_id];
  829. if(wid==0)
  830. [item setValue:@"false" forKey:@"wish_exists"];
  831. else
  832. [item setValue:@"true" forKey:@"wish_exists"];
  833. if(closeout==0)
  834. [item setValue:@"false" forKey:@"is_closeout"];
  835. else
  836. [item setValue:@"true" forKey:@"is_closeout"];
  837. [item addEntriesFromDictionary:imgjson];
  838. // [item setValue:nsurl forKey:@"img"];
  839. [item setValue:nsname forKey:@"fash_name"];
  840. [item setValue:nsdescription forKey:@"description"];
  841. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  842. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  843. i++;
  844. }
  845. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  846. [ret setObject:items forKey:@"items"];
  847. sqlite3_finalize(statement);
  848. }
  849. NSLog(@"count:%d",count);
  850. sqlite3_close(db);
  851. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  852. return ret;
  853. }
  854. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  855. {
  856. //
  857. // NSString* user = [params valueForKey:@"user"];
  858. //
  859. // NSString* password = [params valueForKey:@"password"];
  860. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  861. ret[@"result"]=[NSNumber numberWithInt:2 ];
  862. UIApplication * app = [UIApplication sharedApplication];
  863. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  864. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  865. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  866. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  867. [appDelegate update_count_mark];
  868. appDelegate.can_show_price =false;
  869. appDelegate.can_see_price =false;
  870. appDelegate.can_create_portfolio =false;
  871. appDelegate.can_create_order =false;
  872. appDelegate.can_cancel_order =false;
  873. appDelegate.can_set_cart_price =false;
  874. appDelegate.can_delete_order =false;
  875. appDelegate.can_submit_order =false;
  876. appDelegate.can_set_tearsheet_price =false;
  877. appDelegate.can_update_contact_info = false;
  878. appDelegate.save_order_logout = false;
  879. appDelegate.submit_order_logout = false;
  880. appDelegate.alert_sold_in_quantities = false;
  881. appDelegate.ipad_perm =nil ;
  882. appDelegate.user_type = USER_ROLE_UNKNOWN;
  883. appDelegate.OrderFilter= nil;
  884. [appDelegate SetSo:nil];
  885. [appDelegate set_main_button_panel];
  886. // sqlite3 *db = [iSalesDB get_db];
  887. //
  888. //
  889. //
  890. //
  891. //
  892. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  893. //
  894. //
  895. //
  896. //
  897. // DebugLog(@"offline_login sql:%@",sqlQuery);
  898. // sqlite3_stmt * statement;
  899. //
  900. //
  901. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  902. //
  903. //
  904. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  905. // {
  906. //
  907. //
  908. // if (sqlite3_step(statement) == SQLITE_ROW)
  909. // {
  910. //
  911. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  912. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  913. //
  914. //
  915. //
  916. // int can_show_price = sqlite3_column_int(statement, 0);
  917. // int can_see_price = sqlite3_column_int(statement, 1);
  918. //
  919. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  920. // if(contact_id==nil)
  921. // contact_id="";
  922. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  923. //
  924. // int user_type = sqlite3_column_int(statement, 3);
  925. //
  926. // int can_cancel_order = sqlite3_column_int(statement, 4);
  927. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  928. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  929. // int can_delete_order = sqlite3_column_int(statement, 7);
  930. // int can_submit_order = sqlite3_column_int(statement, 8);
  931. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  932. // int can_create_order = sqlite3_column_int(statement, 10);
  933. //
  934. //
  935. // char *mode = (char*)sqlite3_column_text(statement, 11);
  936. // if(mode==nil)
  937. // mode="";
  938. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  939. //
  940. //
  941. // char *username = (char*)sqlite3_column_text(statement, 12);
  942. // if(username==nil)
  943. // username="";
  944. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  945. //
  946. //
  947. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  948. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  949. // [header setValue:nscontact_id forKey:@"contact_id"];
  950. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  951. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  952. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  953. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  954. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  955. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  956. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  957. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  958. //
  959. // [header setValue:nsusername forKey:@"username"];
  960. //
  961. //
  962. // [ret setObject:header forKey:@"header"];
  963. // [ret setValue:nsmode forKey:@"mode"];
  964. //
  965. //
  966. // }
  967. //
  968. //
  969. //
  970. // sqlite3_finalize(statement);
  971. // }
  972. //
  973. //
  974. //
  975. // sqlite3_close(db);
  976. //
  977. //
  978. //
  979. //
  980. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  981. return ret;
  982. }
  983. +(NSDictionary*) offline_login :(NSMutableDictionary *) params
  984. {
  985. NSString* user = [params valueForKey:@"user"];
  986. NSString* password = [params valueForKey:@"password"];
  987. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  988. sqlite3 *db = [iSalesDB get_db];
  989. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  990. DebugLog(@"offline_login sql:%@",sqlQuery);
  991. sqlite3_stmt * statement;
  992. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  993. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  994. {
  995. if (sqlite3_step(statement) == SQLITE_ROW)
  996. {
  997. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  998. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  999. int can_show_price = sqlite3_column_int(statement, 0);
  1000. int can_see_price = sqlite3_column_int(statement, 1);
  1001. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1002. if(contact_id==nil)
  1003. contact_id="";
  1004. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1005. int user_type = sqlite3_column_int(statement, 3);
  1006. int can_cancel_order = sqlite3_column_int(statement, 4);
  1007. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1008. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1009. int can_delete_order = sqlite3_column_int(statement, 7);
  1010. int can_submit_order = sqlite3_column_int(statement, 8);
  1011. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1012. int can_create_order = sqlite3_column_int(statement, 10);
  1013. char *mode = (char*)sqlite3_column_text(statement, 11);
  1014. if(mode==nil)
  1015. mode="";
  1016. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1017. char *username = (char*)sqlite3_column_text(statement, 12);
  1018. if(username==nil)
  1019. username="";
  1020. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1021. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1022. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1023. [header setValue:nscontact_id forKey:@"contact_id"];
  1024. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1025. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1026. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1027. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1028. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1029. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1030. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1031. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1032. [header setValue:nsusername forKey:@"username"];
  1033. [ret setObject:header forKey:@"header"];
  1034. [ret setValue:nsmode forKey:@"mode"];
  1035. }
  1036. sqlite3_finalize(statement);
  1037. }
  1038. sqlite3_close(db);
  1039. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1040. return ret;
  1041. }
  1042. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1043. {
  1044. NSString* contactId = [params valueForKey:@"contactId"];
  1045. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1046. sqlite3 *db = [iSalesDB get_db];
  1047. NSString *sqlQuery = nil;
  1048. {
  1049. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep from offline_contact where contact_id='%@'",contactId];
  1050. }
  1051. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1052. sqlite3_stmt * statement;
  1053. [ret setValue:@"2" forKey:@"result"];
  1054. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1055. {
  1056. //int i = 0;
  1057. if (sqlite3_step(statement) == SQLITE_ROW)
  1058. {
  1059. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1060. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1061. // int editable = sqlite3_column_int(statement, 0);
  1062. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1063. NSString *nscompany_name =nil;
  1064. if(company_name==nil)
  1065. nscompany_name=@"";
  1066. else
  1067. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1068. char *country = (char*)sqlite3_column_text(statement, 2);
  1069. if(country==nil)
  1070. country="";
  1071. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1072. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1073. // if(addr==nil)
  1074. // addr="";
  1075. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1076. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1077. if(zipcode==nil)
  1078. zipcode="";
  1079. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1080. char *state = (char*)sqlite3_column_text(statement, 5);
  1081. if(state==nil)
  1082. state="";
  1083. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1084. char *city = (char*)sqlite3_column_text(statement, 6);
  1085. if(city==nil)
  1086. city="";
  1087. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1088. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1089. // NSString *nscontact_name = nil;
  1090. // if(contact_name==nil)
  1091. // nscontact_name=@"";
  1092. // else
  1093. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1094. char *phone = (char*)sqlite3_column_text(statement, 8);
  1095. NSString *nsphone = nil;
  1096. if(phone==nil)
  1097. nsphone=@"";
  1098. else
  1099. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1100. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1101. if(contact_id==nil)
  1102. contact_id="";
  1103. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1104. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1105. if(addr_1==nil)
  1106. addr_1="";
  1107. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1108. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1109. if(addr_2==nil)
  1110. addr_2="";
  1111. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1112. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1113. if(addr_3==nil)
  1114. addr_3="";
  1115. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1116. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1117. if(addr_4==nil)
  1118. addr_4="";
  1119. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1120. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1121. if(first_name==nil)
  1122. first_name="";
  1123. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1124. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1125. if(last_name==nil)
  1126. last_name="";
  1127. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1128. char *fax = (char*)sqlite3_column_text(statement, 16);
  1129. NSString *nsfax = nil;
  1130. if(fax==nil)
  1131. nsfax=@"";
  1132. else
  1133. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1134. char *email = (char*)sqlite3_column_text(statement, 17);
  1135. NSString *nsemail = nil;
  1136. if(email==nil)
  1137. nsemail=@"";
  1138. else
  1139. nsemail= [[NSString alloc]initWithUTF8String:email];
  1140. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1141. NSString *nsimg_0 = nil;
  1142. if(img_0==nil)
  1143. nsimg_0=@"";
  1144. else
  1145. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1146. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1147. NSString *nsimg_1 = nil;
  1148. if(img_1==nil)
  1149. nsimg_1=@"";
  1150. else
  1151. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1152. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1153. NSString *nsimg_2 = nil;
  1154. if(img_2==nil)
  1155. nsimg_2=@"";
  1156. else
  1157. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1158. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1159. NSString *nsprice_type = nil;
  1160. if(price_type==nil)
  1161. nsprice_type=@"";
  1162. else
  1163. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1164. char *notes = (char*)sqlite3_column_text(statement, 22);
  1165. NSString *nsnotes = nil;
  1166. if(notes==nil)
  1167. nsnotes=@"";
  1168. else
  1169. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1170. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1171. NSString *nssalesrep = nil;
  1172. if(salesrep==nil)
  1173. nssalesrep=@"";
  1174. else
  1175. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1176. {
  1177. // decrypt
  1178. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1179. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1180. nsphone=[AESCrypt fastdecrypt:nsphone];
  1181. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1182. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1183. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1184. }
  1185. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1186. [arr_name addObject:nsfirst_name];
  1187. [arr_name addObject:nslast_name];
  1188. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1189. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1190. [arr_ext addObject:nsaddr_1];
  1191. [arr_ext addObject:nsaddr_2];
  1192. [arr_ext addObject:nsaddr_3];
  1193. [arr_ext addObject:nsaddr_4];
  1194. [arr_ext addObject:@"\r\n"];
  1195. [arr_ext addObject:nscity];
  1196. [arr_ext addObject:nsstate];
  1197. [arr_ext addObject:nszipcode];
  1198. [arr_ext addObject:nscountry];
  1199. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1200. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1201. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1202. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1203. [item setValue:nscountry forKey:@"customer_country"];
  1204. [item setValue:nsphone forKey:@"customer_phone"];
  1205. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1206. [item setValue:nscompany_name forKey:@"customer_name"];
  1207. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1208. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1209. [item setValue:nsext forKey:@"customer_contact_ext"];
  1210. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1211. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1212. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1213. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1214. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1215. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1216. [item setValue:nslast_name forKey:@"customer_last_name"];
  1217. [item setValue:nscity forKey:@"customer_city"];
  1218. [item setValue:nsstate forKey:@"customer_state"];
  1219. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1220. [item setValue:contactId forKey:@"customer_cid"];
  1221. [item setValue:nscontact_name forKey:@"customer_contact"];
  1222. [item setValue:nsfax forKey:@"customer_fax"];
  1223. [item setValue:nsemail forKey:@"customer_email"];
  1224. [ret setObject:item forKey:@"customerInfo"];
  1225. // i++;
  1226. }
  1227. UIApplication * app = [UIApplication sharedApplication];
  1228. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1229. [ret setValue:appDelegate.mode forKey:@"mode"];
  1230. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1231. sqlite3_finalize(statement);
  1232. }
  1233. sqlite3_close(db);
  1234. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1235. return ret;
  1236. }
  1237. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  1238. {
  1239. NSString* contactType = [params valueForKey:@"contactType"];
  1240. NSString* keyword = [params valueForKey:@"keyword"];
  1241. // advanced search
  1242. // NSString* contact_name = [params valueForKey:@"contact_name"];
  1243. // NSString* customer_phone = [params valueForKey:@"customer_phone"];
  1244. // NSString* customer_fax = [params valueForKey:@"customer_fax"];
  1245. // NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"];
  1246. // NSString* sales_rep = [params valueForKey:@"sales_rep"];
  1247. // NSString* customer_state = [params valueForKey:@"customer_state"];
  1248. // NSString* customer_name = [params valueForKey:@"customer_name"];
  1249. // NSString* customer_country = [params valueForKey:@"customer_country"];
  1250. // NSString* customer_cid = [params valueForKey:@"customer_cid"];
  1251. // NSString* customer_city = [params valueForKey:@"customer_city"];
  1252. // NSString* customer_address = [params valueForKey:@"customer_address"];
  1253. // NSString* customer_email = [params valueForKey:@"customer_email"];
  1254. int limit = [[params valueForKey:@"limit"] intValue];
  1255. int offset = [[params valueForKey:@"offset"] intValue];
  1256. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1257. sqlite3 *db = [iSalesDB get_db];
  1258. int result= [iSalesDB AddExFunction:db];
  1259. int count =0;
  1260. NSString *sqlQuery = nil;
  1261. if(keyword.length==0)
  1262. {
  1263. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by create_time limit %d offset %d",contactType , limit, offset];
  1264. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  1265. }
  1266. else
  1267. {
  1268. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  1269. keyword = keyword.lowercaseString;
  1270. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%') order by create_time limit %d offset %d",contactType , keyword,keyword,limit, offset];
  1271. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@')",contactType,keyword,keyword]];
  1272. }
  1273. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1274. sqlite3_stmt * statement;
  1275. [ret setValue:@"2" forKey:@"result"];
  1276. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  1277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1278. {
  1279. int i = 0;
  1280. while (sqlite3_step(statement) == SQLITE_ROW)
  1281. {
  1282. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1283. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1284. // int editable = sqlite3_column_int(statement, 0);
  1285. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1286. NSString *nscompany_name =nil;
  1287. if(company_name==nil)
  1288. nscompany_name=@"";
  1289. else
  1290. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  1291. char *country = (char*)sqlite3_column_text(statement, 2);
  1292. if(country==nil)
  1293. country="";
  1294. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1295. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1296. // if(addr==nil)
  1297. // addr="";
  1298. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1299. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1300. if(zipcode==nil)
  1301. zipcode="";
  1302. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1303. char *state = (char*)sqlite3_column_text(statement, 5);
  1304. if(state==nil)
  1305. state="";
  1306. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1307. char *city = (char*)sqlite3_column_text(statement, 6);
  1308. if(city==nil)
  1309. city="";
  1310. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1311. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1312. // NSString *nscontact_name = nil;
  1313. // if(contact_name==nil)
  1314. // nscontact_name=@"";
  1315. // else
  1316. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1317. char *phone = (char*)sqlite3_column_text(statement, 8);
  1318. NSString *nsphone = nil;
  1319. if(phone==nil)
  1320. nsphone=@"";
  1321. else
  1322. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1323. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1324. if(contact_id==nil)
  1325. contact_id="";
  1326. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1327. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1328. if(addr_1==nil)
  1329. addr_1="";
  1330. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1331. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1332. if(addr_2==nil)
  1333. addr_2="";
  1334. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1335. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1336. if(addr_3==nil)
  1337. addr_3="";
  1338. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1339. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1340. if(addr_4==nil)
  1341. addr_4="";
  1342. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1343. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1344. if(first_name==nil)
  1345. first_name="";
  1346. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1347. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1348. if(last_name==nil)
  1349. last_name="";
  1350. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1351. char *fax = (char*)sqlite3_column_text(statement, 16);
  1352. NSString *nsfax = nil;
  1353. if(fax==nil)
  1354. nsfax=@"";
  1355. else
  1356. nsfax= [NSString stringWithFormat:@"FAX:%@",[[NSString alloc]initWithUTF8String:fax]];
  1357. char *email = (char*)sqlite3_column_text(statement, 17);
  1358. NSString *nsemail = nil;
  1359. if(email==nil)
  1360. nsemail=@"";
  1361. else
  1362. nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  1363. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1364. [arr_name addObject:nsfirst_name];
  1365. [arr_name addObject:nslast_name];
  1366. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1367. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  1368. {
  1369. // decrypt
  1370. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1371. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1372. nsphone=[AESCrypt fastdecrypt:nsphone];
  1373. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1374. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1375. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1376. }
  1377. [arr_addr addObject:nscompany_name];
  1378. [arr_addr addObject:nscontact_name];
  1379. [arr_addr addObject:@"<br>"];
  1380. [arr_addr addObject:nsaddr_1];
  1381. [arr_addr addObject:nsaddr_2];
  1382. [arr_addr addObject:nsaddr_3];
  1383. [arr_addr addObject:nsaddr_4];
  1384. //[arr_addr addObject:nsaddr];
  1385. [arr_addr addObject:nszipcode];
  1386. [arr_addr addObject:nscity];
  1387. [arr_addr addObject:nsstate];
  1388. [arr_addr addObject:nscountry];
  1389. [arr_addr addObject:@"<br>"];
  1390. [arr_addr addObject:nsphone];
  1391. [arr_addr addObject:nsfax];
  1392. [arr_addr addObject:nsemail];
  1393. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  1394. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  1395. [item setValue:name forKey:@"name"];
  1396. [item setValue:nscontact_id forKey:@"contact_id"];
  1397. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1398. i++;
  1399. }
  1400. UIApplication * app = [UIApplication sharedApplication];
  1401. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1402. [ret setValue:appDelegate.mode forKey:@"mode"];
  1403. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  1404. sqlite3_finalize(statement);
  1405. }
  1406. sqlite3_close(db);
  1407. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1408. return ret;
  1409. }
  1410. +(NSDictionary*) model_NIYMAL:(NSString*) category
  1411. {
  1412. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1413. category = [category substringToIndex:3];
  1414. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1415. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1416. params[@"category"]= category;
  1417. ret[@"params"]= params;
  1418. [ret setValue:@"detail" forKey:@"target"];
  1419. [ret setValue:@"popup" forKey:@"action"];
  1420. [ret setValue:@"content" forKey:@"type"];
  1421. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1422. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1423. [ret setValue:@"true" forKey:@"single_row"];
  1424. [ret setValue:@"true" forKey:@"partial_refresh"];
  1425. sqlite3 *db = [iSalesDB get_db];
  1426. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1427. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY];
  1428. sqlite3_stmt * statement;
  1429. int count = 0;
  1430. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1431. // int count=0;
  1432. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1433. {
  1434. int i=0;
  1435. while (sqlite3_step(statement) == SQLITE_ROW)
  1436. {
  1437. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1438. // char *name = (char*)sqlite3_column_text(statement, 1);
  1439. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1440. char *name = (char*)sqlite3_column_text(statement, 0);
  1441. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1442. int product_id = sqlite3_column_int(statement, 1);
  1443. char *url = (char*)sqlite3_column_text(statement, 2);
  1444. if(url==nil)
  1445. url="";
  1446. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1447. [item setValue:nsurl forKey:@"picture_path"];
  1448. [item setValue:nsname forKey:@"fash_name"];
  1449. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1450. [item setValue:category forKey:@"category"];
  1451. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1452. i++;
  1453. }
  1454. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1455. sqlite3_finalize(statement);
  1456. }
  1457. NSLog(@"count:%d",count);
  1458. sqlite3_close(db);
  1459. return ret;
  1460. }
  1461. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1462. {
  1463. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1464. [ret setValue:key forKey:@"key"];
  1465. [ret setValue:value forKey:@"val"];
  1466. return ret;
  1467. }
  1468. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field
  1469. {
  1470. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1471. [ret setValue:@"0" forKey:@"img_count"];
  1472. sqlite3 *db = [iSalesDB get_db];
  1473. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from model where product_id=%d ;",field,product_id];
  1474. sqlite3_stmt * statement;
  1475. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1476. {
  1477. int i=0;
  1478. if (sqlite3_step(statement) == SQLITE_ROW)
  1479. {
  1480. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1481. // char *name = (char*)sqlite3_column_text(statement, 1);
  1482. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1483. char *value = (char*)sqlite3_column_text(statement, 0);
  1484. if(value==nil)
  1485. value="";
  1486. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1487. char *key = (char*)sqlite3_column_text(statement, 1);
  1488. if(key==nil)
  1489. key="";
  1490. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1491. [item setValue:nsvalue forKey:@"val"];
  1492. [item setValue:nskey forKey:@"key"];
  1493. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1494. [ret setValue:@"1" forKey:@"count"];
  1495. i++;
  1496. }
  1497. sqlite3_finalize(statement);
  1498. }
  1499. sqlite3_close(db);
  1500. return ret;
  1501. }
  1502. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field
  1503. {
  1504. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1505. [ret setValue:@"0" forKey:@"count"];
  1506. sqlite3 *db = [iSalesDB get_db];
  1507. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from model where product_group='%@' order by name;",field,product_group];
  1508. sqlite3_stmt * statement;
  1509. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1510. {
  1511. int i=0;
  1512. while (sqlite3_step(statement) == SQLITE_ROW)
  1513. {
  1514. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1515. // char *name = (char*)sqlite3_column_text(statement, 1);
  1516. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1517. char *value = (char*)sqlite3_column_text(statement, 0);
  1518. if(value==nil)
  1519. value="";
  1520. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1521. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1522. if(selector_display==nil)
  1523. selector_display="";
  1524. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1525. int product_id = sqlite3_column_int(statement, 2);
  1526. char *category = (char*)sqlite3_column_text(statement, 3);
  1527. if(category==nil)
  1528. category="";
  1529. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1530. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil];//[self category_img:product_id];
  1531. [item setValue:nsvalue forKey:@"title"];
  1532. [item setValue:url forKey:@"pic_url"];
  1533. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1534. [params setValue:@"2" forKey:@"count"];
  1535. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1536. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1537. [param0 setValue:@"product_id" forKey:@"name"];
  1538. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1539. [param1 setValue:nscategory forKey:@"val"];
  1540. [param1 setValue:@"category" forKey:@"name"];
  1541. [params setObject:param0 forKey:@"param_0"];
  1542. [params setObject:param1 forKey:@"param_1"];
  1543. [item setObject:params forKey:@"params"];
  1544. [ret setValue:nsselector_display forKey:@"name"];
  1545. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1546. i++;
  1547. }
  1548. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1549. [ret setValue:@"switch" forKey:@"action"];
  1550. sqlite3_finalize(statement);
  1551. }
  1552. sqlite3_close(db);
  1553. return ret;
  1554. }
  1555. +(NSMutableDictionary*) category_img :(int)product_id
  1556. {
  1557. // model 在 category search 显示的图片。
  1558. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1559. sqlite3 *db = [iSalesDB get_db];
  1560. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  1561. sqlite3_stmt * statement;
  1562. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1563. {
  1564. while (sqlite3_step(statement) == SQLITE_ROW)
  1565. {
  1566. char *url = (char*)sqlite3_column_text(statement, 0);
  1567. if(url==nil)
  1568. url="";
  1569. int type = sqlite3_column_int(statement, 1);
  1570. if(type==0)
  1571. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1572. else
  1573. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1574. }
  1575. sqlite3_finalize(statement);
  1576. }
  1577. sqlite3_close(db);
  1578. return ret;
  1579. }
  1580. +(NSDictionary*) model_img :(int)product_id
  1581. {
  1582. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1583. [ret setValue:@"0" forKey:@"img_count"];
  1584. sqlite3 *db = [iSalesDB get_db];
  1585. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  1586. sqlite3_stmt * statement;
  1587. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1588. {
  1589. int i=0;
  1590. while (sqlite3_step(statement) == SQLITE_ROW)
  1591. {
  1592. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1593. // char *name = (char*)sqlite3_column_text(statement, 1);
  1594. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1595. char *url = (char*)sqlite3_column_text(statement, 0);
  1596. if(url==nil)
  1597. url="";
  1598. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1599. [item setValue:nsurl forKey:@"s"];
  1600. [item setValue:nsurl forKey:@"l"];
  1601. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  1602. i++;
  1603. }
  1604. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  1605. sqlite3_finalize(statement);
  1606. }
  1607. sqlite3_close(db);
  1608. return ret;
  1609. }
  1610. @end