OLDataProvider.m 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729
  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. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  984. {
  985. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  986. return [RAUtils dict2data:ret];
  987. }
  988. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  989. {
  990. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  991. int limit = [[params valueForKey:@"limit"] intValue];
  992. int offset = [[params valueForKey:@"offset"] intValue];
  993. NSString* keyword = [params valueForKey:@"keyWord"];
  994. NSString* where=@"1=1";
  995. if(keyword!=nil)
  996. where=[NSString stringWithFormat:@"lower(o.order_id) like '%%%@%%' or lower(c.company_name) like '%%%@%%' or lower(c.company_name) like '%%%@%%' or lower(o.create_by) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  997. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,c.company_name,o.create_time,o.total_price from offline_order o left join offline_contact c on o.contact_id=c.contact_idr where %@ order by create_time limit %d offset %d" ,where, limit, offset];
  998. sqlite3 *db = [iSalesDB get_db];
  999. sqlite3_stmt * statement;
  1000. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1001. {
  1002. int count=0;
  1003. while (sqlite3_step(statement) == SQLITE_ROW)
  1004. {
  1005. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1006. int order_id = sqlite3_column_double(statement, 0);
  1007. char *soid = (char*)sqlite3_column_text(statement, 1);
  1008. if(soid==nil)
  1009. soid= "";
  1010. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  1011. int status = sqlite3_column_double(statement, 2);
  1012. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  1013. if(sales_rep==nil)
  1014. sales_rep= "";
  1015. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  1016. char *create_by = (char*)sqlite3_column_text(statement, 4);
  1017. if(create_by==nil)
  1018. create_by= "";
  1019. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  1020. char *company_name = (char*)sqlite3_column_text(statement, 5);
  1021. if(company_name==nil)
  1022. company_name= "";
  1023. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  1024. char *create_time = (char*)sqlite3_column_text(statement, 6);
  1025. if(create_time==nil)
  1026. create_time= "";
  1027. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1028. double total_price = sqlite3_column_double(statement, 7);
  1029. item[@"sales_rep"]= nssales_rep;
  1030. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  1031. item[@"so#"]= nssoid;
  1032. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];;
  1033. item[@"create_by"]= nscreate_by;
  1034. item[@"customer_name"]= nscompany_name;
  1035. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];;
  1036. item[@"purchase_time"]= nscreate_time;
  1037. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1038. count++;
  1039. }
  1040. ret[@"count"]= [NSNumber numberWithInt:count];
  1041. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  1042. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1043. ret[@"result"]= [NSNumber numberWithInt:2];
  1044. sqlite3_finalize(statement);
  1045. }
  1046. sqlite3_close(db);
  1047. return [RAUtils dict2data:ret];
  1048. }
  1049. +(NSDictionary*) offline_login :(NSMutableDictionary *) params
  1050. {
  1051. NSString* user = [params valueForKey:@"user"];
  1052. NSString* password = [params valueForKey:@"password"];
  1053. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1054. sqlite3 *db = [iSalesDB get_db];
  1055. 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,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1056. DebugLog(@"offline_login sql:%@",sqlQuery);
  1057. sqlite3_stmt * statement;
  1058. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1059. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1060. {
  1061. if (sqlite3_step(statement) == SQLITE_ROW)
  1062. {
  1063. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1064. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1065. int can_show_price = sqlite3_column_int(statement, 0);
  1066. int can_see_price = sqlite3_column_int(statement, 1);
  1067. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1068. if(contact_id==nil)
  1069. contact_id="";
  1070. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1071. int user_type = sqlite3_column_int(statement, 3);
  1072. int can_cancel_order = sqlite3_column_int(statement, 4);
  1073. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1074. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1075. int can_delete_order = sqlite3_column_int(statement, 7);
  1076. int can_submit_order = sqlite3_column_int(statement, 8);
  1077. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1078. int can_create_order = sqlite3_column_int(statement, 10);
  1079. char *mode = (char*)sqlite3_column_text(statement, 11);
  1080. if(mode==nil)
  1081. mode="";
  1082. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1083. char *username = (char*)sqlite3_column_text(statement, 12);
  1084. if(username==nil)
  1085. username="";
  1086. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1087. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1088. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1089. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1090. [header setValue:nscontact_id forKey:@"contact_id"];
  1091. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1092. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1093. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1094. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1095. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1096. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1097. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1098. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1099. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1100. [header setValue:nsusername forKey:@"username"];
  1101. [ret setObject:header forKey:@"header"];
  1102. [ret setValue:nsmode forKey:@"mode"];
  1103. }
  1104. sqlite3_finalize(statement);
  1105. }
  1106. sqlite3_close(db);
  1107. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1108. return ret;
  1109. }
  1110. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1111. {
  1112. NSString* contactId = [params valueForKey:@"contactId"];
  1113. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1114. sqlite3 *db = [iSalesDB get_db];
  1115. NSString *sqlQuery = nil;
  1116. {
  1117. 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];
  1118. }
  1119. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1120. sqlite3_stmt * statement;
  1121. [ret setValue:@"2" forKey:@"result"];
  1122. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1123. {
  1124. //int i = 0;
  1125. if (sqlite3_step(statement) == SQLITE_ROW)
  1126. {
  1127. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1128. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1129. // int editable = sqlite3_column_int(statement, 0);
  1130. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1131. NSString *nscompany_name =nil;
  1132. if(company_name==nil)
  1133. nscompany_name=@"";
  1134. else
  1135. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1136. char *country = (char*)sqlite3_column_text(statement, 2);
  1137. if(country==nil)
  1138. country="";
  1139. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1140. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1141. // if(addr==nil)
  1142. // addr="";
  1143. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1144. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1145. if(zipcode==nil)
  1146. zipcode="";
  1147. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1148. char *state = (char*)sqlite3_column_text(statement, 5);
  1149. if(state==nil)
  1150. state="";
  1151. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1152. char *city = (char*)sqlite3_column_text(statement, 6);
  1153. if(city==nil)
  1154. city="";
  1155. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1156. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1157. // NSString *nscontact_name = nil;
  1158. // if(contact_name==nil)
  1159. // nscontact_name=@"";
  1160. // else
  1161. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1162. char *phone = (char*)sqlite3_column_text(statement, 8);
  1163. NSString *nsphone = nil;
  1164. if(phone==nil)
  1165. nsphone=@"";
  1166. else
  1167. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1168. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1169. if(contact_id==nil)
  1170. contact_id="";
  1171. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1172. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1173. if(addr_1==nil)
  1174. addr_1="";
  1175. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1176. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1177. if(addr_2==nil)
  1178. addr_2="";
  1179. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1180. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1181. if(addr_3==nil)
  1182. addr_3="";
  1183. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1184. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1185. if(addr_4==nil)
  1186. addr_4="";
  1187. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1188. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1189. if(first_name==nil)
  1190. first_name="";
  1191. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1192. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1193. if(last_name==nil)
  1194. last_name="";
  1195. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1196. char *fax = (char*)sqlite3_column_text(statement, 16);
  1197. NSString *nsfax = nil;
  1198. if(fax==nil)
  1199. nsfax=@"";
  1200. else
  1201. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1202. char *email = (char*)sqlite3_column_text(statement, 17);
  1203. NSString *nsemail = nil;
  1204. if(email==nil)
  1205. nsemail=@"";
  1206. else
  1207. nsemail= [[NSString alloc]initWithUTF8String:email];
  1208. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1209. NSString *nsimg_0 = nil;
  1210. if(img_0==nil)
  1211. nsimg_0=@"";
  1212. else
  1213. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1214. [self copy_bcardImg:nsimg_0];
  1215. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1216. NSString *nsimg_1 = nil;
  1217. if(img_1==nil)
  1218. nsimg_1=@"";
  1219. else
  1220. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1221. [self copy_bcardImg:nsimg_1];
  1222. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1223. NSString *nsimg_2 = nil;
  1224. if(img_2==nil)
  1225. nsimg_2=@"";
  1226. else
  1227. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1228. [self copy_bcardImg:nsimg_2];
  1229. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1230. NSString *nsprice_type = nil;
  1231. if(price_type==nil)
  1232. nsprice_type=@"";
  1233. else
  1234. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1235. char *notes = (char*)sqlite3_column_text(statement, 22);
  1236. NSString *nsnotes = nil;
  1237. if(notes==nil)
  1238. nsnotes=@"";
  1239. else
  1240. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1241. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1242. NSString *nssalesrep = nil;
  1243. if(salesrep==nil)
  1244. nssalesrep=@"";
  1245. else
  1246. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1247. {
  1248. // decrypt
  1249. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1250. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1251. nsphone=[AESCrypt fastdecrypt:nsphone];
  1252. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1253. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1254. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1255. }
  1256. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1257. [arr_name addObject:nsfirst_name];
  1258. [arr_name addObject:nslast_name];
  1259. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1260. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1261. [arr_ext addObject:nsaddr_1];
  1262. [arr_ext addObject:nsaddr_2];
  1263. [arr_ext addObject:nsaddr_3];
  1264. [arr_ext addObject:nsaddr_4];
  1265. [arr_ext addObject:@"\r\n"];
  1266. [arr_ext addObject:nscity];
  1267. [arr_ext addObject:nsstate];
  1268. [arr_ext addObject:nszipcode];
  1269. [arr_ext addObject:nscountry];
  1270. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1271. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1272. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1273. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1274. [item setValue:nscountry forKey:@"customer_country"];
  1275. [item setValue:nsphone forKey:@"customer_phone"];
  1276. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1277. [item setValue:nscompany_name forKey:@"customer_name"];
  1278. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1279. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1280. [item setValue:nsext forKey:@"customer_contact_ext"];
  1281. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1282. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1283. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1284. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1285. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1286. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1287. [item setValue:nslast_name forKey:@"customer_last_name"];
  1288. [item setValue:nscity forKey:@"customer_city"];
  1289. [item setValue:nsstate forKey:@"customer_state"];
  1290. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1291. [item setValue:contactId forKey:@"customer_cid"];
  1292. [item setValue:nscontact_name forKey:@"customer_contact"];
  1293. [item setValue:nsfax forKey:@"customer_fax"];
  1294. [item setValue:nsemail forKey:@"customer_email"];
  1295. [ret setObject:item forKey:@"customerInfo"];
  1296. // i++;
  1297. }
  1298. UIApplication * app = [UIApplication sharedApplication];
  1299. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1300. [ret setValue:appDelegate.mode forKey:@"mode"];
  1301. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1302. sqlite3_finalize(statement);
  1303. }
  1304. sqlite3_close(db);
  1305. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1306. return ret;
  1307. }
  1308. + (bool) copy_bcardImg:(NSString*) filename
  1309. {
  1310. if(filename.length==0)
  1311. return false;
  1312. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1313. bool ret=false;
  1314. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1315. NSString *cachefolder = [paths objectAtIndex:0];
  1316. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1317. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1318. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1319. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1320. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1321. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1322. //
  1323. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1324. NSFileManager* fileManager = [NSFileManager defaultManager];
  1325. BOOL bdir=NO;
  1326. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1327. {
  1328. NSError *error = nil;
  1329. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1330. {
  1331. ret=false;
  1332. }
  1333. else
  1334. {
  1335. ret=true;
  1336. }
  1337. // NSError *error = nil;
  1338. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1339. //
  1340. // if(!bsuccess)
  1341. // {
  1342. // DebugLog(@"Create offline_createimg folder failed");
  1343. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1344. // return [RAUtils dict2data:ret];
  1345. // }
  1346. // if(bsuccess)
  1347. // {
  1348. // sqlite3 *db = [self get_db];
  1349. //
  1350. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1351. // sqlite3_close(db);
  1352. // }
  1353. }
  1354. return ret;
  1355. //
  1356. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1357. // if(bsuccess)
  1358. // {
  1359. // NSError *error = nil;
  1360. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1361. // {
  1362. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1363. // }
  1364. // else
  1365. // {
  1366. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1367. //
  1368. // ret[@"img_url_aname"]=filename;
  1369. // ret[@"img_url"]=savedImagePath;
  1370. // }
  1371. // }
  1372. }
  1373. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1374. {
  1375. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1376. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1377. NSString *cachefolder = [paths objectAtIndex:0];
  1378. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1379. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1380. NSFileManager* fileManager = [NSFileManager defaultManager];
  1381. BOOL bdir=YES;
  1382. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1383. {
  1384. NSError *error = nil;
  1385. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1386. if(!bsuccess)
  1387. {
  1388. DebugLog(@"Create offline_createimg folder failed");
  1389. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1390. return [RAUtils dict2data:ret];
  1391. }
  1392. // if(bsuccess)
  1393. // {
  1394. // sqlite3 *db = [self get_db];
  1395. //
  1396. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1397. // sqlite3_close(db);
  1398. // }
  1399. }
  1400. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1401. //JEPG格式
  1402. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1403. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1404. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1405. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1406. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1407. if(bsuccess)
  1408. {
  1409. NSError *error = nil;
  1410. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1411. {
  1412. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1413. }
  1414. else
  1415. {
  1416. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1417. ret[@"img_url_aname"]=filename;
  1418. ret[@"img_url"]=filename;
  1419. }
  1420. }
  1421. else
  1422. {
  1423. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1424. }
  1425. return [RAUtils dict2data:ret];
  1426. }
  1427. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  1428. {
  1429. NSString* contactType = [params valueForKey:@"contactType"];
  1430. NSString* keyword = [params valueForKey:@"keyword"];
  1431. // advanced search
  1432. // NSString* contact_name = [params valueForKey:@"contact_name"];
  1433. // NSString* customer_phone = [params valueForKey:@"customer_phone"];
  1434. // NSString* customer_fax = [params valueForKey:@"customer_fax"];
  1435. // NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"];
  1436. // NSString* sales_rep = [params valueForKey:@"sales_rep"];
  1437. // NSString* customer_state = [params valueForKey:@"customer_state"];
  1438. // NSString* customer_name = [params valueForKey:@"customer_name"];
  1439. // NSString* customer_country = [params valueForKey:@"customer_country"];
  1440. // NSString* customer_cid = [params valueForKey:@"customer_cid"];
  1441. // NSString* customer_city = [params valueForKey:@"customer_city"];
  1442. // NSString* customer_address = [params valueForKey:@"customer_address"];
  1443. // NSString* customer_email = [params valueForKey:@"customer_email"];
  1444. int limit = [[params valueForKey:@"limit"] intValue];
  1445. int offset = [[params valueForKey:@"offset"] intValue];
  1446. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1447. sqlite3 *db = [iSalesDB get_db];
  1448. int result= [iSalesDB AddExFunction:db];
  1449. int count =0;
  1450. NSString *sqlQuery = nil;
  1451. if(keyword.length==0)
  1452. {
  1453. 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 decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  1454. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  1455. }
  1456. else
  1457. {
  1458. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  1459. keyword = keyword.lowercaseString;
  1460. 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 decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,limit, offset];
  1461. 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]];
  1462. }
  1463. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1464. sqlite3_stmt * statement;
  1465. [ret setValue:@"2" forKey:@"result"];
  1466. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  1467. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1468. {
  1469. int i = 0;
  1470. while (sqlite3_step(statement) == SQLITE_ROW)
  1471. {
  1472. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1473. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1474. int editable = sqlite3_column_int(statement, 0);
  1475. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1476. NSString *nscompany_name =nil;
  1477. if(company_name==nil)
  1478. nscompany_name=@"";
  1479. else
  1480. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  1481. char *country = (char*)sqlite3_column_text(statement, 2);
  1482. if(country==nil)
  1483. country="";
  1484. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1485. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1486. // if(addr==nil)
  1487. // addr="";
  1488. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1489. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1490. if(zipcode==nil)
  1491. zipcode="";
  1492. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1493. char *state = (char*)sqlite3_column_text(statement, 5);
  1494. if(state==nil)
  1495. state="";
  1496. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1497. char *city = (char*)sqlite3_column_text(statement, 6);
  1498. if(city==nil)
  1499. city="";
  1500. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1501. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1502. // NSString *nscontact_name = nil;
  1503. // if(contact_name==nil)
  1504. // nscontact_name=@"";
  1505. // else
  1506. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1507. char *phone = (char*)sqlite3_column_text(statement, 8);
  1508. NSString *nsphone = nil;
  1509. if(phone==nil)
  1510. nsphone=@"";
  1511. else
  1512. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1513. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1514. if(contact_id==nil)
  1515. contact_id="";
  1516. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1517. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1518. if(addr_1==nil)
  1519. addr_1="";
  1520. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1521. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1522. if(addr_2==nil)
  1523. addr_2="";
  1524. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1525. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1526. if(addr_3==nil)
  1527. addr_3="";
  1528. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1529. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1530. if(addr_4==nil)
  1531. addr_4="";
  1532. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1533. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1534. if(first_name==nil)
  1535. first_name="";
  1536. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1537. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1538. if(last_name==nil)
  1539. last_name="";
  1540. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1541. char *fax = (char*)sqlite3_column_text(statement, 16);
  1542. NSString *nsfax = nil;
  1543. if(fax==nil)
  1544. nsfax=@"";
  1545. else
  1546. nsfax= [NSString stringWithFormat:@"FAX:%@",[[NSString alloc]initWithUTF8String:fax]];
  1547. char *email = (char*)sqlite3_column_text(statement, 17);
  1548. NSString *nsemail = nil;
  1549. if(email==nil)
  1550. nsemail=@"";
  1551. else
  1552. nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  1553. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1554. [arr_name addObject:nsfirst_name];
  1555. [arr_name addObject:nslast_name];
  1556. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1557. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  1558. {
  1559. // decrypt
  1560. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1561. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1562. nsphone=[AESCrypt fastdecrypt:nsphone];
  1563. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1564. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1565. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1566. }
  1567. [arr_addr addObject:nscompany_name];
  1568. [arr_addr addObject:nscontact_name];
  1569. [arr_addr addObject:@"<br>"];
  1570. [arr_addr addObject:nsaddr_1];
  1571. [arr_addr addObject:nsaddr_2];
  1572. [arr_addr addObject:nsaddr_3];
  1573. [arr_addr addObject:nsaddr_4];
  1574. //[arr_addr addObject:nsaddr];
  1575. [arr_addr addObject:nszipcode];
  1576. [arr_addr addObject:nscity];
  1577. [arr_addr addObject:nsstate];
  1578. [arr_addr addObject:nscountry];
  1579. [arr_addr addObject:@"<br>"];
  1580. [arr_addr addObject:nsphone];
  1581. [arr_addr addObject:nsfax];
  1582. [arr_addr addObject:nsemail];
  1583. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  1584. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  1585. [item setValue:name forKey:@"name"];
  1586. [item setValue:nscontact_id forKey:@"contact_id"];
  1587. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  1588. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1589. i++;
  1590. }
  1591. UIApplication * app = [UIApplication sharedApplication];
  1592. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1593. [ret setValue:appDelegate.mode forKey:@"mode"];
  1594. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  1595. sqlite3_finalize(statement);
  1596. }
  1597. sqlite3_close(db);
  1598. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1599. return ret;
  1600. }
  1601. +(NSDictionary*) model_NIYMAL:(NSString*) category
  1602. {
  1603. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1604. category = [category substringToIndex:3];
  1605. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1606. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1607. params[@"category"]= category;
  1608. ret[@"params"]= params;
  1609. [ret setValue:@"detail" forKey:@"target"];
  1610. [ret setValue:@"popup" forKey:@"action"];
  1611. [ret setValue:@"content" forKey:@"type"];
  1612. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1613. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1614. [ret setValue:@"true" forKey:@"single_row"];
  1615. [ret setValue:@"true" forKey:@"partial_refresh"];
  1616. sqlite3 *db = [iSalesDB get_db];
  1617. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1618. 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];
  1619. sqlite3_stmt * statement;
  1620. int count = 0;
  1621. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1622. // int count=0;
  1623. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1624. {
  1625. int i=0;
  1626. while (sqlite3_step(statement) == SQLITE_ROW)
  1627. {
  1628. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1629. // char *name = (char*)sqlite3_column_text(statement, 1);
  1630. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1631. char *name = (char*)sqlite3_column_text(statement, 0);
  1632. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1633. int product_id = sqlite3_column_int(statement, 1);
  1634. char *url = (char*)sqlite3_column_text(statement, 2);
  1635. if(url==nil)
  1636. url="";
  1637. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1638. [item setValue:nsurl forKey:@"picture_path"];
  1639. [item setValue:nsname forKey:@"fash_name"];
  1640. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1641. [item setValue:category forKey:@"category"];
  1642. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1643. i++;
  1644. }
  1645. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1646. sqlite3_finalize(statement);
  1647. }
  1648. NSLog(@"count:%d",count);
  1649. sqlite3_close(db);
  1650. return ret;
  1651. }
  1652. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1653. {
  1654. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1655. [ret setValue:key forKey:@"key"];
  1656. [ret setValue:value forKey:@"val"];
  1657. return ret;
  1658. }
  1659. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field
  1660. {
  1661. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1662. [ret setValue:@"0" forKey:@"img_count"];
  1663. sqlite3 *db = [iSalesDB get_db];
  1664. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from model where product_id=%d ;",field,product_id];
  1665. sqlite3_stmt * statement;
  1666. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1667. {
  1668. int i=0;
  1669. if (sqlite3_step(statement) == SQLITE_ROW)
  1670. {
  1671. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1672. // char *name = (char*)sqlite3_column_text(statement, 1);
  1673. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1674. char *value = (char*)sqlite3_column_text(statement, 0);
  1675. if(value==nil)
  1676. value="";
  1677. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1678. char *key = (char*)sqlite3_column_text(statement, 1);
  1679. if(key==nil)
  1680. key="";
  1681. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1682. [item setValue:nsvalue forKey:@"val"];
  1683. [item setValue:nskey forKey:@"key"];
  1684. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1685. [ret setValue:@"1" forKey:@"count"];
  1686. i++;
  1687. }
  1688. sqlite3_finalize(statement);
  1689. }
  1690. sqlite3_close(db);
  1691. return ret;
  1692. }
  1693. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field
  1694. {
  1695. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1696. [ret setValue:@"0" forKey:@"count"];
  1697. sqlite3 *db = [iSalesDB get_db];
  1698. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from model where product_group='%@' order by name;",field,product_group];
  1699. sqlite3_stmt * statement;
  1700. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1701. {
  1702. int i=0;
  1703. while (sqlite3_step(statement) == SQLITE_ROW)
  1704. {
  1705. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1706. // char *name = (char*)sqlite3_column_text(statement, 1);
  1707. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1708. char *value = (char*)sqlite3_column_text(statement, 0);
  1709. if(value==nil)
  1710. value="";
  1711. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1712. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1713. if(selector_display==nil)
  1714. selector_display="";
  1715. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1716. int product_id = sqlite3_column_int(statement, 2);
  1717. char *category = (char*)sqlite3_column_text(statement, 3);
  1718. if(category==nil)
  1719. category="";
  1720. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1721. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil];//[self category_img:product_id];
  1722. [item setValue:nsvalue forKey:@"title"];
  1723. [item setValue:url forKey:@"pic_url"];
  1724. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1725. [params setValue:@"2" forKey:@"count"];
  1726. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1727. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1728. [param0 setValue:@"product_id" forKey:@"name"];
  1729. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1730. [param1 setValue:nscategory forKey:@"val"];
  1731. [param1 setValue:@"category" forKey:@"name"];
  1732. [params setObject:param0 forKey:@"param_0"];
  1733. [params setObject:param1 forKey:@"param_1"];
  1734. [item setObject:params forKey:@"params"];
  1735. [ret setValue:nsselector_display forKey:@"name"];
  1736. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1737. i++;
  1738. }
  1739. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1740. [ret setValue:@"switch" forKey:@"action"];
  1741. sqlite3_finalize(statement);
  1742. }
  1743. sqlite3_close(db);
  1744. return ret;
  1745. }
  1746. +(NSMutableDictionary*) category_img :(int)product_id
  1747. {
  1748. // model 在 category search 显示的图片。
  1749. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1750. sqlite3 *db = [iSalesDB get_db];
  1751. 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];
  1752. sqlite3_stmt * statement;
  1753. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1754. {
  1755. while (sqlite3_step(statement) == SQLITE_ROW)
  1756. {
  1757. char *url = (char*)sqlite3_column_text(statement, 0);
  1758. if(url==nil)
  1759. url="";
  1760. int type = sqlite3_column_int(statement, 1);
  1761. if(type==0)
  1762. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1763. else
  1764. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1765. }
  1766. sqlite3_finalize(statement);
  1767. }
  1768. sqlite3_close(db);
  1769. return ret;
  1770. }
  1771. +(NSDictionary*) model_img :(int)product_id
  1772. {
  1773. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1774. [ret setValue:@"0" forKey:@"img_count"];
  1775. sqlite3 *db = [iSalesDB get_db];
  1776. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  1777. sqlite3_stmt * statement;
  1778. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1779. {
  1780. int i=0;
  1781. while (sqlite3_step(statement) == SQLITE_ROW)
  1782. {
  1783. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1784. // char *name = (char*)sqlite3_column_text(statement, 1);
  1785. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1786. char *url = (char*)sqlite3_column_text(statement, 0);
  1787. if(url==nil)
  1788. url="";
  1789. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1790. [item setValue:nsurl forKey:@"s"];
  1791. [item setValue:nsurl forKey:@"l"];
  1792. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  1793. i++;
  1794. }
  1795. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  1796. sqlite3_finalize(statement);
  1797. }
  1798. sqlite3_close(db);
  1799. return ret;
  1800. }
  1801. #pragma mark - Jack
  1802. #pragma mark create new contact
  1803. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  1804. // "val_227" : {
  1805. // "check" : 1,
  1806. // "value" : "US United States",
  1807. // "value_id" : "228"
  1808. // },
  1809. if (!countryCode) {
  1810. countryCode = @"US";
  1811. }
  1812. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1813. // sqlite3 *db = [iSalesDB get_db];
  1814. //
  1815. // const char *sqlQuery = "select _id,name,code,countrycode_id from offline_country;";
  1816. // sqlite3_stmt * statement;
  1817. //
  1818. // if (sqlite3_prepare_v2(db, sqlQuery, -1, &statement, nil) == SQLITE_OK) {
  1819. //
  1820. // long count = 0;
  1821. // while (sqlite3_step(statement) == SQLITE_ROW) {
  1822. // char *name = (char *) sqlite3_column_text(statement, 1); // 全称
  1823. // char *code = (char *) sqlite3_column_text(statement, 2); // 缩写
  1824. // int code_id = sqlite3_column_int(statement, 3); // id
  1825. //
  1826. // NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1827. // [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  1828. // [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  1829. // [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1830. //
  1831. // if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  1832. // [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1833. // }
  1834. //
  1835. // NSString *key = [NSString stringWithFormat:@"val_%ld",count++];
  1836. // [dic setValue:countryDic forKey:key];
  1837. // }
  1838. //
  1839. // sqlite3_finalize(statement);
  1840. // }
  1841. //
  1842. // sqlite3_close(db);
  1843. // [dic setValue:[NSNumber numberWithInt:dic.allKeys.count] forKey:@"count"];
  1844. // return dic;
  1845. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  1846. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  1847. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  1848. int code_id = sqlite3_column_int(stmt, 3); // id
  1849. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1850. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  1851. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  1852. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1853. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  1854. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1855. }
  1856. long n = *count;
  1857. *count = n + 1;
  1858. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  1859. [container setValue:countryDic forKey:key];
  1860. }] mutableCopy];
  1861. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1862. return ret;
  1863. }
  1864. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  1865. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1866. //
  1867. // sqlite3 *db = [iSalesDB get_db];
  1868. //
  1869. // NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  1870. //
  1871. // // NSString *sqlQuery = [NSString stringWithFormat:@"select * from offline_state"];
  1872. // DebugLog(@"query state:%@",sqlQuery);
  1873. //
  1874. // sqlite3_stmt * statement;
  1875. //
  1876. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1877. // int i = 0;
  1878. // while (sqlite3_step(statement) == SQLITE_ROW) {
  1879. // char *name = (char *) sqlite3_column_text(statement, 1); // value
  1880. // char *code = (char *) sqlite3_column_text(statement, 2); // value_id
  1881. //
  1882. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1883. // [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1884. // [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1885. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1886. //
  1887. // if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  1888. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1889. // }
  1890. //
  1891. // NSString *key = [NSString stringWithFormat:@"val_%d",i++];
  1892. // [dic setValue:stateDic forKey:key];
  1893. // }
  1894. //
  1895. // sqlite3_finalize(statement);
  1896. // }
  1897. //
  1898. // sqlite3_close(db);
  1899. // [dic setValue:[NSNumber numberWithInt:dic.allKeys.count] forKey:@"count"];
  1900. // return dic;
  1901. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  1902. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1903. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  1904. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  1905. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1906. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1907. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1908. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1909. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  1910. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1911. }
  1912. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1913. [container setValue:stateDic forKey:key];
  1914. }] mutableCopy];
  1915. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1916. return ret;
  1917. }
  1918. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  1919. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1920. //
  1921. // sqlite3 *db = [iSalesDB get_db];
  1922. //
  1923. // NSString *sqlQuery = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  1924. //
  1925. // DebugLog(@"query state:%@",sqlQuery);
  1926. //
  1927. // sqlite3_stmt * statement;
  1928. //
  1929. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1930. // int i = 0;
  1931. // while (sqlite3_step(statement) == SQLITE_ROW) {
  1932. // char *name = (char *) sqlite3_column_text(statement, 0); // value
  1933. // char *code = (char *) sqlite3_column_text(statement, 1); // value_id
  1934. //
  1935. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1936. // [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1937. // [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1938. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1939. //
  1940. // if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  1941. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1942. // }
  1943. //
  1944. // NSString *key = [NSString stringWithFormat:@"val_%d",i++];
  1945. // [dic setValue:stateDic forKey:key];
  1946. // }
  1947. //
  1948. // sqlite3_finalize(statement);
  1949. // }
  1950. //
  1951. // sqlite3_close(db);
  1952. // [dic setValue:[NSNumber numberWithInt:dic.allKeys.count] forKey:@"count"];
  1953. // return dic;
  1954. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  1955. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1956. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  1957. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  1958. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1959. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1960. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1961. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1962. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  1963. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1964. }
  1965. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1966. [container setValue:stateDic forKey:key];
  1967. }] mutableCopy];
  1968. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1969. return ret;
  1970. }
  1971. + (NSDictionary *)offline_getPrice {
  1972. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1973. //
  1974. // sqlite3 *db = [iSalesDB get_db];
  1975. // NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  1976. // sqlite3_stmt * statement;
  1977. //
  1978. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1979. // while (sqlite3_step(statement) == SQLITE_ROW) {
  1980. // char *name = (char *) sqlite3_column_text(statement, 1);
  1981. // int type = sqlite3_column_int(statement, 2);
  1982. // int orderBy = sqlite3_column_int(statement, 3);
  1983. //
  1984. // NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  1985. // [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1986. // [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  1987. // NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  1988. // [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1989. //
  1990. // if (orderBy == 0) {
  1991. // [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1992. // }
  1993. //
  1994. // [dic setValue:priceDic forKey:key];
  1995. // }
  1996. //
  1997. // sqlite3_finalize(statement);
  1998. // }
  1999. //
  2000. // sqlite3_close(db);
  2001. //
  2002. // [dic setValue:[NSNumber numberWithInt:dic.allKeys.count] forKey:@"count"];
  2003. //
  2004. // return dic;
  2005. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2006. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2007. char *name = (char *) sqlite3_column_text(stmt, 1);
  2008. int type = sqlite3_column_int(stmt, 2);
  2009. int orderBy = sqlite3_column_int(stmt, 3);
  2010. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2011. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2012. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2013. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2014. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2015. if (orderBy == 0) {
  2016. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2017. }
  2018. [container setValue:priceDic forKey:key];
  2019. }] mutableCopy];
  2020. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2021. return ret;
  2022. }
  2023. + (NSDictionary *)offline_getSalesRep {
  2024. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  2025. //
  2026. // sqlite3 *db = [iSalesDB get_db];
  2027. // NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2028. // sqlite3_stmt * statement;
  2029. //
  2030. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2031. // long count = 0;
  2032. // while (sqlite3_step(statement) == SQLITE_ROW) {
  2033. // // 1 name 2 code 3 salesrep_id
  2034. // char *name = (char *)sqlite3_column_text(statement, 1);
  2035. // char *code = (char *)sqlite3_column_text(statement, 2);
  2036. // int salesrep_id = sqlite3_column_int(statement, 3);
  2037. // // int _id = sqlite3_column_int(statement, 0);
  2038. // //
  2039. // // "val_0" : {
  2040. // // "value" : "BBSC - BARBARA BORROUGHS",
  2041. // // "value_id" : 121
  2042. // // },
  2043. // NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2044. // [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2045. // [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2046. // [dic setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",count++]];
  2047. // }
  2048. //
  2049. // sqlite3_finalize(statement);
  2050. // }
  2051. // sqlite3_close(db);
  2052. //
  2053. // [dic setValue:[NSNumber numberWithInt:dic.allKeys.count] forKey:@"count"];
  2054. //
  2055. // return dic;
  2056. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2057. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2058. // 1 name 2 code 3 salesrep_id
  2059. char *name = (char *)sqlite3_column_text(stmt, 1);
  2060. char *code = (char *)sqlite3_column_text(stmt, 2);
  2061. int salesrep_id = sqlite3_column_int(stmt, 3);
  2062. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2063. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2064. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2065. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2066. }] mutableCopy];
  2067. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2068. return ret;
  2069. }
  2070. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2071. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2072. [item setValue:value forKey:valueKey];
  2073. [dic setValue:item forKey:itemKey];
  2074. }
  2075. + (NSString *)countryCodeByid:(NSString *)code_id {
  2076. NSString *ret = nil;
  2077. sqlite3 *db = [iSalesDB get_db];
  2078. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2079. sqlite3_stmt * statement;
  2080. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2081. while (sqlite3_step(statement) == SQLITE_ROW) {
  2082. char *code = (char *)sqlite3_column_text(statement, 0);
  2083. ret = [NSString stringWithUTF8String:code];
  2084. }
  2085. sqlite3_finalize(statement);
  2086. }
  2087. sqlite3_close(db);
  2088. return ret;
  2089. }
  2090. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2091. NSString *ret = nil;
  2092. sqlite3 *db = [iSalesDB get_db];
  2093. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2094. sqlite3_stmt * statement;
  2095. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2096. while (sqlite3_step(statement) == SQLITE_ROW) {
  2097. char *_id = (char *)sqlite3_column_text(statement, 0);
  2098. ret = [NSString stringWithFormat:@"%s",_id];
  2099. }
  2100. sqlite3_finalize(statement);
  2101. }
  2102. sqlite3_close(db);
  2103. return ret;
  2104. }
  2105. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2106. // NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  2107. //
  2108. // sqlite3 *db = [iSalesDB get_db];
  2109. // NSString *sqlQuery = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2110. // sqlite3_stmt * statement;
  2111. //
  2112. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2113. // while (sqlite3_step(statement) == SQLITE_ROW) {
  2114. // char *country = (char *)sqlite3_column_text(statement, 0);
  2115. // char *state = (char *)sqlite3_column_text(statement, 1);
  2116. // char *city = (char *)sqlite3_column_text(statement, 2);
  2117. // char *country_code = (char *)sqlite3_column_text(statement, 3);
  2118. // char *state_code = (char *)sqlite3_column_text(statement, 4);
  2119. //
  2120. // [dic setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2121. // [dic setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2122. // [dic setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2123. // [dic setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2124. // [dic setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2125. // }
  2126. //
  2127. // sqlite3_finalize(statement);
  2128. // }
  2129. // sqlite3_close(db);
  2130. //
  2131. // return dic;
  2132. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2133. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2134. char *country = (char *)sqlite3_column_text(stmt, 0);
  2135. char *state = (char *)sqlite3_column_text(stmt, 1);
  2136. char *city = (char *)sqlite3_column_text(stmt, 2);
  2137. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2138. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2139. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2140. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2141. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2142. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2143. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2144. }] mutableCopy];
  2145. return ret;
  2146. }
  2147. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  2148. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  2149. NSData *data = [NSData dataWithContentsOfFile:path];
  2150. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2151. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2152. NSString *countryCode = nil;
  2153. NSString *countryCode_id = nil;
  2154. NSString *stateCode = nil;
  2155. NSString *city = nil;
  2156. NSString *zipCode = nil;
  2157. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2158. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  2159. NSString *code_id = params[@"country"];
  2160. countryCode_id = code_id;
  2161. countryCode = [self countryCodeByid:code_id];
  2162. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  2163. NSString *zip_code = params[@"zipcode"];
  2164. zipCode = zip_code;
  2165. countryCode_id = params[@"country"];
  2166. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2167. countryCode = [dic valueForKey:@"country_code"];
  2168. stateCode = [dic valueForKey:@"state_code"];
  2169. city = [dic valueForKey:@"city"];
  2170. // zip code
  2171. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  2172. [zipDic setValue:zipCode forKey:@"value"];
  2173. [section_0 setValue:zipDic forKey:@"item_10"];
  2174. }
  2175. } else {
  2176. // default: US United States
  2177. countryCode = @"US";
  2178. countryCode_id = @"228";
  2179. }
  2180. // country
  2181. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2182. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  2183. // state
  2184. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2185. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  2186. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  2187. // city
  2188. if (city) {
  2189. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  2190. [cityDic setValue:city forKey:@"value"];
  2191. [section_0 setValue:cityDic forKey:@"item_12"];
  2192. }
  2193. // price type
  2194. NSDictionary *priceDic = [self offline_getPrice];
  2195. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  2196. // Sales Rep
  2197. NSDictionary *repDic = [self offline_getSalesRep];
  2198. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2199. [ret setValue:section_0 forKey:@"section_0"];
  2200. return [RAUtils dict2data:ret];
  2201. }
  2202. #pragma mark save
  2203. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update {
  2204. NSString *companyName = [params objectForKey:@"company"];
  2205. if (companyName) {
  2206. companyName = [AESCrypt fastencrypt:companyName];
  2207. } else {
  2208. companyName = @"";
  2209. }
  2210. NSString *addr1 = [params objectForKey:@"address"];
  2211. if (addr1) {
  2212. addr1 = [AESCrypt fastencrypt:addr1];
  2213. } else {
  2214. addr1 = @"";
  2215. }
  2216. NSString *addr2 = [params objectForKey:@"address2"];
  2217. if (!addr2) {
  2218. addr2 = @"";
  2219. }
  2220. NSString *addr3 = [params objectForKey:@"address_3"];
  2221. if (!addr3) {
  2222. addr3 = @"";
  2223. }
  2224. NSString *addr4 = [params objectForKey:@"address_4"];
  2225. if (!addr4) {
  2226. addr4 = @"";
  2227. }
  2228. NSString *country = [params objectForKey:@"country"];
  2229. if (country) {
  2230. country = [self countryNameByCountryCodeId:country];
  2231. } else {
  2232. country = @"";
  2233. }
  2234. NSString *state = [params objectForKey:@"state"];
  2235. if (!state) {
  2236. state = @"";
  2237. }
  2238. NSString *city = [params objectForKey:@"city"];
  2239. if (!city) {
  2240. city = @"";
  2241. }
  2242. NSString *zipcode = [params objectForKey:@"zipcode"];
  2243. if (!zipcode) {
  2244. zipcode = @"";
  2245. }
  2246. NSString *fistName = [params objectForKey:@"firstname"];
  2247. if (!fistName) {
  2248. fistName = @"";
  2249. }
  2250. NSString *lastName = [params objectForKey:@"lastname"];
  2251. if (!lastName) {
  2252. lastName = @"";
  2253. }
  2254. NSString *phone = [params objectForKey:@"phone"];
  2255. if (phone) {
  2256. phone = [AESCrypt fastencrypt:phone];
  2257. } else {
  2258. phone = @"";
  2259. }
  2260. NSString *fax = [params objectForKey:@"fax"];
  2261. if (!fax) {
  2262. fax = @"";
  2263. }
  2264. NSString *email = [params objectForKey:@"email"];
  2265. if (!email) {
  2266. email = @"";
  2267. }
  2268. NSString *notes = [params objectForKey:@"contact_notes"];
  2269. if (!notes) {
  2270. notes = @"";
  2271. }
  2272. NSString *price = [params objectForKey:@"price_name"];
  2273. if (price) {
  2274. price = [self priceNameByPriceId:price];
  2275. } else {
  2276. price = @"";
  2277. }
  2278. NSString *salesRep = [params objectForKey:@"sales_rep"];
  2279. if (salesRep) {
  2280. salesRep = [self salesRepCodeById:salesRep];
  2281. } else {
  2282. salesRep = @"";
  2283. }
  2284. NSString *img = [params objectForKey:@"business_card"];
  2285. NSArray *array = [img componentsSeparatedByString:@","];
  2286. NSString *img_0 = array[0];
  2287. if (!img_0) {
  2288. img_0 = @"";
  2289. }
  2290. NSString *img_1 = array[1];
  2291. if (!img_1) {
  2292. img_1 = @"";
  2293. }
  2294. NSString *img_2 = array[2];
  2295. if (!img_2) {
  2296. img_2 = @"";
  2297. }
  2298. NSString *contact_id = [NSUUID UUID].UUIDString;
  2299. NSString *sql = nil;
  2300. if (update){
  2301. contact_id = [params objectForKey:@"contact_id"];
  2302. if (!contact_id) {
  2303. contact_id = [NSUUID UUID].UUIDString;
  2304. }
  2305. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id];
  2306. } else
  2307. sql = [NSString stringWithFormat:@"insert into offline_contact (company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',1,1,0,1,0,1)",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id];
  2308. int result = [iSalesDB execSql:sql];
  2309. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2310. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2311. }
  2312. #pragma mark save new contact
  2313. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2314. NSString *name = nil;
  2315. sqlite3 *db = [iSalesDB get_db];
  2316. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2317. sqlite3_stmt * statement;
  2318. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2319. while (sqlite3_step(statement) == SQLITE_ROW) {
  2320. char *value = (char *)sqlite3_column_text(statement, 0);
  2321. name = [NSString stringWithUTF8String:value];
  2322. }
  2323. sqlite3_finalize(statement);
  2324. }
  2325. sqlite3_close(db);
  2326. return name;
  2327. }
  2328. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2329. NSString *ret = nil;
  2330. sqlite3 *db = [iSalesDB get_db];
  2331. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where order_by = %@;",priceId];
  2332. sqlite3_stmt * statement;
  2333. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2334. while (sqlite3_step(statement) == SQLITE_ROW) {
  2335. char *name = (char *)sqlite3_column_text(statement, 0);
  2336. ret = [NSString stringWithUTF8String:name];
  2337. }
  2338. sqlite3_finalize(statement);
  2339. }
  2340. sqlite3_close(db);
  2341. return ret;
  2342. }
  2343. + (NSString *)salesRepCodeById:(NSString *)_id {
  2344. NSString *ret = nil;
  2345. sqlite3 *db = [iSalesDB get_db];
  2346. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2347. sqlite3_stmt * statement;
  2348. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2349. while (sqlite3_step(statement) == SQLITE_ROW) {
  2350. char *rep = (char *)sqlite3_column_text(statement, 0);
  2351. ret = [NSString stringWithUTF8String:rep];
  2352. }
  2353. sqlite3_finalize(statement);
  2354. }
  2355. sqlite3_close(db);
  2356. return ret;
  2357. }
  2358. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  2359. {
  2360. // NSLog(@"%@",params);
  2361. // NSString *companyName = [params objectForKey:@"company"];
  2362. // if (companyName) {
  2363. // companyName = [AESCrypt fastencrypt:companyName];
  2364. // }
  2365. //
  2366. // NSString *addr1 = [params objectForKey:@"address"];
  2367. // if (addr1) {
  2368. // addr1 = [AESCrypt fastencrypt:addr1];
  2369. // }
  2370. // NSString *addr2 = [params objectForKey:@"address2"];
  2371. //// if (addr2) {
  2372. //// addr2 = [AESCrypt fastencrypt:addr2];
  2373. //// }
  2374. // NSString *addr3 = [params objectForKey:@"address_3"];
  2375. //// if (addr3) {
  2376. //// addr3 = [AESCrypt fastencrypt:addr3];
  2377. //// }
  2378. // NSString *addr4 = [params objectForKey:@"address_4"];
  2379. //// if (addr4) {
  2380. //// addr4 = [AESCrypt fastencrypt:addr4];
  2381. //// }
  2382. //
  2383. // NSString *country = [params objectForKey:@"country"];
  2384. // if (country) {
  2385. // country = [self countryNameByCountryCodeId:country];
  2386. // }
  2387. // NSString *state = [params objectForKey:@"state"];
  2388. // NSString *city = [params objectForKey:@"city"];
  2389. // NSString *zipcode = [params objectForKey:@"zipcode"];
  2390. //
  2391. // NSString *fistName = [params objectForKey:@"firstname"];
  2392. // NSString *lastName = [params objectForKey:@"lastname"];
  2393. //
  2394. // NSString *phone = [params objectForKey:@"phone"];
  2395. // if (phone) {
  2396. // phone = [AESCrypt fastencrypt:phone];
  2397. // }
  2398. //
  2399. // NSString *fax = [params objectForKey:@"fax"];
  2400. // NSString *email = [params objectForKey:@"email"];
  2401. //
  2402. // NSString *notes = [params objectForKey:@"contact_notes"];
  2403. //
  2404. // NSString *price = [params objectForKey:@"price_name"];
  2405. // if (price) {
  2406. // price = [self priceNameByPriceId:price];
  2407. // }
  2408. // NSString *salesRep = [params objectForKey:@"sales_rep"];
  2409. // if (salesRep) {
  2410. // salesRep = [self salesRepCodeById:salesRep];
  2411. // }
  2412. //
  2413. // NSString *img = [params objectForKey:@"business_card"];
  2414. // NSArray *array = [img componentsSeparatedByString:@","];
  2415. // NSString *img_0 = array[0];
  2416. // NSString *img_1 = array[1];
  2417. // NSString *img_2 = array[2];
  2418. //
  2419. // NSString *contact_id = [NSUUID UUID].UUIDString;
  2420. //
  2421. // NSString *sql = [NSString stringWithFormat:@"insert into offline_contact (company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',1,1,0,1,0,1)",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id];
  2422. //
  2423. //// int result = [iSalesDB jk_execSql:sql withDatabase:YES];
  2424. // int result = [iSalesDB execSql:sql];
  2425. //
  2426. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2427. //
  2428. // return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2429. return [self offline_saveContact:params update:NO];
  2430. }
  2431. #pragma mark edit contact
  2432. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2433. NSString *text = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(stmt, col)];
  2434. if (!text) {
  2435. text = @"";
  2436. }
  2437. return text;
  2438. }
  2439. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  2440. {
  2441. // {
  2442. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  2443. // password = 123456;
  2444. // user = EvanK;
  2445. // }
  2446. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  2447. NSData *data = [NSData dataWithContentsOfFile:path];
  2448. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2449. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2450. NSString *countryCode = nil;
  2451. NSString *countryCode_id = nil;
  2452. NSString *stateCode = nil;
  2453. /*------contact infor------*/
  2454. __block NSString *country = nil;
  2455. __block NSString *company_name = nil;
  2456. __block NSString *contact_id = params[@"contact_id"];
  2457. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  2458. __block NSString *zipcode = nil;
  2459. __block NSString *state = nil; // state_code
  2460. __block NSString *city = nil; //
  2461. __block NSString *firt_name,*last_name;
  2462. __block NSString *phone,*fax,*email;
  2463. __block NSString *notes,*price_type,*sales_rep;
  2464. __block NSString *img_0,*img_1,*img_2;
  2465. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  2466. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2467. country = [self textAtColumn:0 statement:stmt]; // country name
  2468. company_name = [self textAtColumn:1 statement:stmt];
  2469. addr_1 = [self textAtColumn:2 statement:stmt];
  2470. addr_2 = [self textAtColumn:3 statement:stmt];
  2471. addr_3 = [self textAtColumn:4 statement:stmt];
  2472. addr_4 = [self textAtColumn:5 statement:stmt];
  2473. zipcode = [self textAtColumn:6 statement:stmt];
  2474. state = [self textAtColumn:7 statement:stmt]; // state code
  2475. city = [self textAtColumn:8 statement:stmt];
  2476. firt_name = [self textAtColumn:9 statement:stmt];
  2477. last_name = [self textAtColumn:10 statement:stmt];
  2478. phone = [self textAtColumn:11 statement:stmt];
  2479. fax = [self textAtColumn:12 statement:stmt];
  2480. email = [self textAtColumn:13 statement:stmt];
  2481. notes = [self textAtColumn:14 statement:stmt];
  2482. price_type = [self textAtColumn:15 statement:stmt]; // name
  2483. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  2484. img_0 = [self textAtColumn:17 statement:stmt];
  2485. img_1 = [self textAtColumn:18 statement:stmt];
  2486. img_2 = [self textAtColumn:19 statement:stmt];
  2487. }];
  2488. // decrypt
  2489. if (company_name) {
  2490. company_name = [AESCrypt fastdecrypt:company_name];
  2491. }
  2492. if (addr_1) {
  2493. addr_1 = [AESCrypt fastdecrypt:addr_1];
  2494. }
  2495. if (phone) {
  2496. phone = [AESCrypt fastdecrypt:phone];
  2497. }
  2498. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",country];
  2499. countryCode = [iSalesDB jk_queryText:countrySql];
  2500. stateCode = state;
  2501. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2502. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  2503. NSString *code_id = params[@"country"];
  2504. countryCode_id = code_id;
  2505. countryCode = [self countryCodeByid:code_id];
  2506. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  2507. NSString *zip_code = params[@"zipcode"];
  2508. zipcode = zip_code;
  2509. countryCode_id = params[@"country"];
  2510. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2511. countryCode = [dic valueForKey:@"country_code"];
  2512. stateCode = [dic valueForKey:@"state_code"];
  2513. city = [dic valueForKey:@"city"];
  2514. // zip code
  2515. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  2516. [zipDic setValue:zipcode forKey:@"value"];
  2517. [section_0 setValue:zipDic forKey:@"item_8"];
  2518. }
  2519. }
  2520. // 0 Country
  2521. // 1 Company Name
  2522. // 2 Contact ID
  2523. // 3 Picture
  2524. // 4 Address 1
  2525. // 5 Address 2
  2526. // 6 Address 3
  2527. // 7 Address 4
  2528. // 8 Zip Code
  2529. // 9 State/Province
  2530. // 10 City
  2531. // 11 Contact First Name
  2532. // 12 Contact Last Name
  2533. // 13 Phone
  2534. // 14 Fax
  2535. // 15 Email
  2536. // 16 Contact Notes
  2537. // 17 Price Type
  2538. // 18 Sales Rep
  2539. // country
  2540. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2541. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  2542. // company
  2543. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  2544. // contact_id
  2545. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  2546. // picture
  2547. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  2548. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  2549. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  2550. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  2551. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  2552. // addr 1 2 3 4
  2553. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  2554. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  2555. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  2556. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  2557. // zip code
  2558. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  2559. // state
  2560. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2561. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  2562. // city
  2563. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  2564. // first last
  2565. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  2566. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  2567. // phone fax email
  2568. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  2569. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  2570. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  2571. // notes
  2572. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  2573. // price
  2574. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  2575. for (NSString *key in priceDic.allKeys) {
  2576. if ([key containsString:@"val_"]) {
  2577. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  2578. if ([dic[@"value"] isEqualToString:price_type]) {
  2579. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2580. [priceDic setValue:dic forKey:key];
  2581. }
  2582. }
  2583. }
  2584. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2585. // Sales Rep
  2586. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  2587. for (NSString *key in repDic.allKeys) {
  2588. if ([key containsString:@"val_"]) {
  2589. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  2590. NSString *value = dic[@"value"];
  2591. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  2592. if (code && [code isEqualToString:sales_rep]) {
  2593. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2594. [repDic setValue:dic forKey:key];
  2595. }
  2596. }
  2597. }
  2598. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  2599. [ret setValue:section_0 forKey:@"section_0"];
  2600. return [RAUtils dict2data:ret];
  2601. }
  2602. #pragma mark - save contact
  2603. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  2604. {
  2605. return [self offline_saveContact:params update:YES];
  2606. }
  2607. @end