OLDataProvider.m 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  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. //[iSalesDB close_db:db];
  32. [iSalesDB close_db:db];
  33. }
  34. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  35. {
  36. UIApplication * app = [UIApplication sharedApplication];
  37. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  38. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  39. sqlite3 *db = [iSalesDB get_db];
  40. NSString* collectId=params[@"collectId"];
  41. NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  42. [iSalesDB execSql:sqlQuery];
  43. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  44. [iSalesDB close_db:db];
  45. appDelegate.wish_count =count;
  46. [appDelegate update_count_mark];
  47. ret[@"result"]= [NSNumber numberWithInt:2];
  48. return ret;
  49. }
  50. +(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  51. {
  52. UIApplication * app = [UIApplication sharedApplication];
  53. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  54. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  55. sqlite3 *db = [iSalesDB get_db];
  56. NSString* product_id=params[@"product_id"];
  57. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  58. for(int i=0;i<arr.count;i++)
  59. {
  60. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  61. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  62. if(count==0)
  63. {
  64. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  65. [iSalesDB execSql:sqlQuery];
  66. }
  67. }
  68. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  69. [iSalesDB close_db:db];
  70. appDelegate.wish_count =count;
  71. [appDelegate update_count_mark];
  72. ret[@"result"]= [NSNumber numberWithInt:2];
  73. return ret;
  74. //
  75. //return ret;
  76. }
  77. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  78. {
  79. UIApplication * app = [UIApplication sharedApplication];
  80. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  81. NSString* user = appDelegate.user;
  82. sqlite3 *db = [iSalesDB get_db];
  83. 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;";
  84. sqlite3_stmt * statement;
  85. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  86. int count=0;
  87. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  88. {
  89. while (sqlite3_step(statement) == SQLITE_ROW)
  90. {
  91. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  92. int product_id = sqlite3_column_double(statement, 0);
  93. char *description = (char*)sqlite3_column_text(statement, 1);
  94. if(description==nil)
  95. description= "";
  96. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  97. int item_id = sqlite3_column_double(statement, 2);
  98. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil];
  99. // char *url = (char*)sqlite3_column_text(statement, 3);
  100. // if(url==nil)
  101. // url="";
  102. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  103. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  104. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  105. item[@"description"]= nsdescription;
  106. item[@"img"]= nsurl;
  107. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  108. count++;
  109. }
  110. ret[@"count"]= [NSNumber numberWithInt:count];
  111. ret[@"total_count"]= [NSNumber numberWithInt:count];
  112. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  113. ret[@"result"]= [NSNumber numberWithInt:2];
  114. appDelegate.wish_count =count;
  115. [appDelegate update_count_mark];
  116. sqlite3_finalize(statement);
  117. }
  118. [iSalesDB close_db:db];
  119. return ret;
  120. }
  121. +(NSDictionary*) offline_notimpl
  122. {
  123. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  124. ret[@"result"]=@"8";
  125. ret[@"err_msg"]=@"offline mode does not support this function.";
  126. return ret;
  127. }
  128. +(NSDictionary*) offline_home
  129. {
  130. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  131. NSString *cachefolder = [paths objectAtIndex:0];
  132. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  133. NSData* json =nil;
  134. json=[NSData dataWithContentsOfFile:img_cache];
  135. NSError *error=nil;
  136. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  137. return menu;
  138. }
  139. +(NSDictionary*) offline_category_menu
  140. {
  141. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  142. NSString *cachefolder = [paths objectAtIndex:0];
  143. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  144. NSData* json =nil;
  145. json=[NSData dataWithContentsOfFile:img_cache];
  146. NSError *error=nil;
  147. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  148. return menu;
  149. }
  150. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  151. {
  152. NSString* offline_command=params[@"offline_Command"];
  153. NSDictionary* ret=nil;
  154. if([offline_command isEqualToString:@"model_NIYMAL"])
  155. {
  156. NSString* category = params[@"category"];
  157. ret = [self refresh_model_NIYMAL:category];
  158. }
  159. return ret;
  160. }
  161. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  162. {
  163. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  164. [ret setValue:@"2" forKey:@"result"];
  165. [ret setValue:@"Regular Mode" forKey:@"mode"];
  166. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category] mutableCopy];
  167. [ret setObject:detail1_section forKey:@"detail_1"];
  168. return ret;
  169. }
  170. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id product_id:(int) product_id
  171. {
  172. UIApplication * app = [UIApplication sharedApplication];
  173. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  174. // NSArray* arr1 = [self get_user_all_price_type];
  175. NSArray* arr2 = [self get_contact_default_price_type:contact_id];
  176. // NSSet *set1 = [NSSet setWithArray:arr1];
  177. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  178. // if(appDelegate.contact_id==nil)
  179. // set2=[set1 mutableCopy];
  180. // else
  181. // [set2 intersectsSet:set1];
  182. // NSArray *retarr = [set2 allObjects];
  183. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  184. sqlite3 *db = [iSalesDB get_db];
  185. NSString* sqlQuery = nil;
  186. if(appDelegate.contact_id==nil)
  187. 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];
  188. else
  189. 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];
  190. sqlite3_stmt * statement;
  191. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  192. int count=0;
  193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  194. {
  195. while (sqlite3_step(statement) == SQLITE_ROW)
  196. {
  197. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  198. char *name = (char*)sqlite3_column_text(statement, 0);
  199. if(name==nil)
  200. name="";
  201. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  202. // double price = sqlite3_column_double(statement, 1);
  203. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  204. // if(isnull==nil)
  205. // item[nsname]= @"No Price";
  206. // else
  207. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  208. char *price = (char*)sqlite3_column_text(statement, 1);
  209. if(price!=nil)
  210. {
  211. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  212. nsprice=[AESCrypt fastdecrypt:nsprice];
  213. if(nsprice.length>0)
  214. {
  215. double dp= [nsprice doubleValue];
  216. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  217. }
  218. }
  219. else
  220. {
  221. item[nsname]= @"No Price";
  222. }
  223. // item[nsname]= nsprice;
  224. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  225. count++;
  226. }
  227. ret[@"count"]= [NSNumber numberWithInt:count];
  228. sqlite3_finalize(statement);
  229. }
  230. [iSalesDB close_db:db];
  231. return ret;
  232. }
  233. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id
  234. {
  235. NSArray* arr1 = [self get_user_all_price_type];
  236. NSArray* arr2 = [self get_contact_default_price_type:contact_id];
  237. // NSSet *set1 = [NSSet setWithArray:arr1];
  238. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  239. // [set2 intersectsSet:set1];
  240. //
  241. //
  242. // NSArray *retarr = [set2 allObjects];
  243. NSString* whereprice=nil;
  244. if(contact_id==nil)
  245. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  246. else
  247. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  248. sqlite3 *db = [iSalesDB get_db];
  249. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  250. sqlite3_stmt * statement;
  251. NSNumber* ret = nil;
  252. double dprice=DBL_MAX;
  253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  254. {
  255. while (sqlite3_step(statement) == SQLITE_ROW)
  256. {
  257. // double val = sqlite3_column_double(statement, 0);
  258. char *price = (char*)sqlite3_column_text(statement, 0);
  259. if(price!=nil)
  260. {
  261. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  262. nsprice=[AESCrypt fastdecrypt:nsprice];
  263. if(nsprice.length>0)
  264. {
  265. double dp= [nsprice doubleValue];
  266. if(dp<dprice)
  267. dprice=dp;
  268. }
  269. }
  270. }
  271. sqlite3_finalize(statement);
  272. }
  273. [iSalesDB close_db:db];
  274. if(dprice==DBL_MAX)
  275. ret= nil;
  276. else
  277. ret= [NSNumber numberWithDouble:dprice];
  278. return ret;
  279. }
  280. +(NSArray*) get_user_all_price_type
  281. {
  282. NSArray* ret=nil;
  283. sqlite3 *db = [iSalesDB get_db];
  284. // no customer assigned , use login user contact_id
  285. UIApplication * app = [UIApplication sharedApplication];
  286. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  287. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  288. sqlite3_stmt * statement;
  289. // int count=0;
  290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  291. {
  292. if (sqlite3_step(statement) == SQLITE_ROW)
  293. {
  294. char *val = (char*)sqlite3_column_text(statement, 0);
  295. if(val==nil)
  296. val="";
  297. NSString* price = [[NSString alloc]initWithUTF8String:val];
  298. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  299. }
  300. sqlite3_finalize(statement);
  301. }
  302. [iSalesDB close_db:db];
  303. return ret;
  304. }
  305. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id
  306. {
  307. sqlite3 *db = [iSalesDB get_db];
  308. if(contact_id==nil)
  309. {
  310. // no customer assigned , use login user contact_id
  311. UIApplication * app = [UIApplication sharedApplication];
  312. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  313. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  314. sqlite3_stmt * statement;
  315. // int count=0;
  316. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  317. {
  318. if (sqlite3_step(statement) == SQLITE_ROW)
  319. {
  320. char *val = (char*)sqlite3_column_text(statement, 0);
  321. if(val==nil)
  322. val="";
  323. contact_id = [[NSString alloc]initWithUTF8String:val];
  324. }
  325. sqlite3_finalize(statement);
  326. }
  327. if(contact_id.length<=0)
  328. {
  329. [iSalesDB close_db:db];
  330. return nil;
  331. }
  332. }
  333. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  334. sqlite3_stmt * statement;
  335. NSArray* ret=nil;
  336. // int count=0;
  337. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  338. {
  339. if (sqlite3_step(statement) == SQLITE_ROW)
  340. {
  341. char *price_type = (char*)sqlite3_column_text(statement, 0);
  342. if(price_type==nil)
  343. price_type="";
  344. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  345. if(nsprice_type.length>0)
  346. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  347. else
  348. ret=nil;
  349. }
  350. sqlite3_finalize(statement);
  351. }
  352. [iSalesDB close_db:db];
  353. return ret;
  354. }
  355. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name
  356. {
  357. NSString* ret= nil;
  358. sqlite3 *db = [iSalesDB get_db];
  359. NSString *sqlQuery = nil;
  360. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  361. if(product_id==nil)
  362. 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];
  363. else
  364. 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];
  365. sqlite3_stmt * statement;
  366. // int count=0;
  367. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  368. {
  369. if (sqlite3_step(statement) == SQLITE_ROW)
  370. {
  371. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  372. if(imgurl==nil)
  373. imgurl="";
  374. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  375. ret=nsimgurl;
  376. }
  377. sqlite3_finalize(statement);
  378. }
  379. else
  380. {
  381. [ret setValue:@"8" forKey:@"result"];
  382. }
  383. [iSalesDB close_db:db];
  384. DebugLog(@"data string: %@",ret );
  385. return ret;
  386. }
  387. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  388. {
  389. NSString* ret= nil;
  390. sqlite3 *db = [iSalesDB get_db];
  391. NSString *sqlQuery = nil;
  392. if(product_id==nil)
  393. sqlQuery = [NSString stringWithFormat:@"select default_category from model where name='%@';",model_name];
  394. else
  395. sqlQuery = [NSString stringWithFormat:@"select default_category from model where product_id=%@;",product_id];
  396. sqlite3_stmt * statement;
  397. // int count=0;
  398. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  399. {
  400. if (sqlite3_step(statement) == SQLITE_ROW)
  401. {
  402. char *default_category = (char*)sqlite3_column_text(statement, 0);
  403. if(default_category==nil)
  404. default_category="";
  405. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  406. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  407. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  408. ret=nsdefault_category;
  409. }
  410. sqlite3_finalize(statement);
  411. }
  412. else
  413. {
  414. [ret setValue:@"8" forKey:@"result"];
  415. }
  416. [iSalesDB close_db:db];
  417. DebugLog(@"data string: %@",ret );
  418. return ret;
  419. }
  420. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  421. {
  422. NSString* model_name = [params valueForKey:@"product_name"];
  423. NSString* product_id = [params valueForKey:@"product_id"];
  424. NSString* category = [params valueForKey:@"category"];
  425. if(category==nil)
  426. category = [self model_default_category:product_id model_name:model_name];
  427. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  428. sqlite3 *db = [iSalesDB get_db];
  429. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  430. NSString *sqlQuery = nil;
  431. if(product_id==nil)
  432. 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];
  433. else
  434. 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];
  435. sqlite3_stmt * statement;
  436. [ret setValue:@"2" forKey:@"result"];
  437. [ret setValue:@"3" forKey:@"detail_section_count"];
  438. // int count=0;
  439. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  440. {
  441. if (sqlite3_step(statement) == SQLITE_ROW)
  442. {
  443. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  444. char *name = (char*)sqlite3_column_text(statement, 0);
  445. if(name==nil)
  446. name="";
  447. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  448. char *description = (char*)sqlite3_column_text(statement, 1);
  449. if(description==nil)
  450. description="";
  451. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  452. int product_id = sqlite3_column_int(statement, 2);
  453. char *color = (char*)sqlite3_column_text(statement, 3);
  454. if(color==nil)
  455. color="";
  456. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  457. //
  458. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  459. // if(legcolor==nil)
  460. // legcolor="";
  461. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  462. //
  463. //
  464. int availability = sqlite3_column_int(statement, 5);
  465. //
  466. // int incoming_stock = sqlite3_column_int(statement, 6);
  467. char *demension = (char*)sqlite3_column_text(statement, 7);
  468. if(demension==nil)
  469. demension="";
  470. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  471. // ,,,,,,,,,
  472. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  473. if(seat_height==nil)
  474. seat_height="";
  475. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  476. char *material = (char*)sqlite3_column_text(statement, 9);
  477. if(material==nil)
  478. material="";
  479. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  480. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  481. if(box_dim==nil)
  482. box_dim="";
  483. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  484. char *volume = (char*)sqlite3_column_text(statement, 11);
  485. if(volume==nil)
  486. volume="";
  487. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  488. double weight = sqlite3_column_double(statement, 12);
  489. char *model_set = (char*)sqlite3_column_text(statement, 13);
  490. if(model_set==nil)
  491. model_set="";
  492. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  493. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  494. if(load_ability==nil)
  495. load_ability="";
  496. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  497. char *default_category = (char*)sqlite3_column_text(statement, 15);
  498. if(default_category==nil)
  499. default_category="";
  500. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  501. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  502. if(fabric_content==nil)
  503. fabric_content="";
  504. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  505. char *assembling = (char*)sqlite3_column_text(statement, 17);
  506. if(assembling==nil)
  507. assembling="";
  508. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  509. char *made_in = (char*)sqlite3_column_text(statement, 18);
  510. if(made_in==nil)
  511. made_in="";
  512. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  513. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  514. if(special_remarks==nil)
  515. special_remarks="";
  516. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  517. int stockUcom = sqlite3_column_double(statement, 20);
  518. char *product_group = (char*)sqlite3_column_text(statement, 21);
  519. if(product_group==nil)
  520. product_group="";
  521. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  522. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  523. // if(fashion_selector==nil)
  524. // fashion_selector="";
  525. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  526. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  527. if(selector_field==nil)
  528. selector_field="";
  529. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  530. char *property_field = (char*)sqlite3_column_text(statement, 23);
  531. if(property_field==nil)
  532. property_field="";
  533. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  534. char *packaging = (char*)sqlite3_column_text(statement, 24);
  535. if(packaging==nil)
  536. packaging="";
  537. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  538. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  539. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  540. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name];
  541. [img_section setValue:model_s_img forKey:@"model_s_img"];
  542. [img_section setObject:[self model_img:product_id] forKey:@"images"];
  543. [img_section setObject:[self model_property:product_id field:nsproperty_field] forKey:@"property"];
  544. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field] forKey:@"selector"];
  545. NSString* Availability=nil;
  546. if(availability>0)
  547. Availability=[NSString stringWithFormat:@"%d",availability];
  548. else
  549. Availability = @"In Production";
  550. [img_section setValue:Availability forKey:@"Availability"];
  551. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  552. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  553. NSString* Price=nil;
  554. if(appDelegate.bLogin==false)
  555. Price=@"Must Sign in.";
  556. else
  557. {
  558. NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id];
  559. if(price==nil)
  560. Price=@"No Price.";
  561. else
  562. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  563. }
  564. [img_section setValue:Price forKey:@"price"];
  565. [img_section setValue:nsname forKey:@"model_name"];
  566. [img_section setValue:nsdescription forKey:@"model_descrition"];
  567. [ret setObject:img_section forKey:@"img_section"];
  568. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  569. int detail0_item_count=0;
  570. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  571. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  572. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  573. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  574. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  575. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  576. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  577. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  578. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  579. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  580. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  581. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  582. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  583. NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id];
  584. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  585. {
  586. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  587. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  588. }
  589. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  590. [detail0_section setValue:@"kv" forKey:@"type"];
  591. [detail0_section setValue:@"Product Information" forKey:@"title"];
  592. [ret setObject:detail0_section forKey:@"detail_0"];
  593. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category] mutableCopy];
  594. // [detail1_section setValue:@"detail" forKey:@"target"];
  595. // [detail1_section setValue:@"popup" forKey:@"action"];
  596. // [detail1_section setValue:@"content" forKey:@"type"];
  597. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  598. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  599. // [detail1_section setValue:@"true" forKey:@"single_row"];
  600. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  601. [ret setObject:detail1_section forKey:@"detail_1"];
  602. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  603. [detail2_section setValue:@"detail" forKey:@"target"];
  604. [detail2_section setValue:@"popup" forKey:@"action"];
  605. [detail2_section setValue:@"content" forKey:@"type"];
  606. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  607. [detail2_section setValue:@"true" forKey:@"single_row"];
  608. [detail2_section setValue:@"local" forKey:@"data"];
  609. [ret setObject:detail2_section forKey:@"detail_2"];
  610. }
  611. sqlite3_finalize(statement);
  612. }
  613. else
  614. {
  615. [ret setValue:@"8" forKey:@"result"];
  616. }
  617. NSLog(@"count:%d",count);
  618. [iSalesDB close_db:db];
  619. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  620. return ret;
  621. }
  622. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  623. {
  624. NSString* orderCode = [params valueForKey:@"orderCode"];
  625. NSString* keyword = [params valueForKey:@"keyword"];
  626. keyword=keyword.lowercaseString;
  627. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  628. int limit = [[params valueForKey:@"limit"] intValue];
  629. int offset = [[params valueForKey:@"offset"] intValue];
  630. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  631. sqlite3 *db = [iSalesDB get_db];
  632. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"lower(name) like'%%%@%%' or lower(description) like'%%%@%%'",keyword,keyword]];
  633. NSString *sqlQuery = nil;
  634. if(exactMatch )
  635. 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];
  636. else
  637. 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];
  638. DebugLog(@"offline_search sql:%@",sqlQuery);
  639. sqlite3_stmt * statement;
  640. [ret setValue:@"2" forKey:@"result"];
  641. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  642. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  643. // int count=0;
  644. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  645. {
  646. int i=0;
  647. while (sqlite3_step(statement) == SQLITE_ROW)
  648. {
  649. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  650. // char *name = (char*)sqlite3_column_text(statement, 1);
  651. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  652. char *name = (char*)sqlite3_column_text(statement, 0);
  653. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  654. char *description = (char*)sqlite3_column_text(statement, 1);
  655. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  656. int product_id = sqlite3_column_int(statement, 2);
  657. // char *url = (char*)sqlite3_column_text(statement, 3);
  658. // if(url==nil)
  659. // url="";
  660. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  661. int wid = sqlite3_column_int(statement, 3);
  662. int closeout = sqlite3_column_int(statement, 4);
  663. NSMutableDictionary* imgjson=[self category_img:product_id];
  664. if(wid==0)
  665. [item setValue:@"false" forKey:@"wish_exists"];
  666. else
  667. [item setValue:@"true" forKey:@"wish_exists"];
  668. if(closeout==0)
  669. [item setValue:@"false" forKey:@"is_closeout"];
  670. else
  671. [item setValue:@"true" forKey:@"is_closeout"];
  672. [item addEntriesFromDictionary:imgjson];
  673. // [item setValue:nsurl forKey:@"img"];
  674. [item setValue:nsname forKey:@"fash_name"];
  675. [item setValue:nsdescription forKey:@"description"];
  676. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  677. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  678. i++;
  679. }
  680. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  681. [ret setObject:items forKey:@"items"];
  682. sqlite3_finalize(statement);
  683. }
  684. NSLog(@"count:%d",count);
  685. [iSalesDB close_db:db];
  686. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  687. return ret;
  688. }
  689. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  690. {
  691. // [params setValue:covertype forKey:@"covertype"];
  692. // [params setValue:ctgid forKey:@"ctgId"];
  693. // [params setValue:modelname forKey:@"modelName"];
  694. // [params setValue:modeldescrip forKey:@"modelDescription"];
  695. // [params setValue:alert forKey:@"alert"];
  696. // [params setValue:qty forKey:@"sold_by_qty"];
  697. // [params setValue:available forKey:@"available"];
  698. // [params setValue:price forKey:@"price"];
  699. // [params setValue:bestseller forKey:@"bestseller"];
  700. NSString* orderCode = [params valueForKey:@"orderCode"];
  701. int covertype = [[params valueForKey:@"covertype"] intValue];
  702. NSString* where= nil;
  703. NSString* orderby= @"m.name";
  704. switch (covertype) {
  705. case 0:
  706. {
  707. where=@"m.category like'%%#005#%%'";
  708. break;
  709. }
  710. case 1:
  711. {
  712. where=@"m.alert like '%QS%'";
  713. break;
  714. }
  715. case 2:
  716. {
  717. where=@"m.availability>0";
  718. break;
  719. }
  720. case 3:
  721. {
  722. where=@"m.best_seller>0";
  723. orderby=@"m.best_seller desc";
  724. break;
  725. }
  726. default:
  727. where=@"1=1";
  728. break;
  729. }
  730. // NSString* keyword = [params valueForKey:@"keyword"];
  731. // bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  732. int limit = [[params valueForKey:@"limit"] intValue];
  733. int offset = [[params valueForKey:@"offset"] intValue];
  734. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  735. sqlite3 *db = [iSalesDB get_db];
  736. int count = [iSalesDB get_recordcount:db table:@"model" where:where];
  737. NSString *sqlQuery = nil;
  738. 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];
  739. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  740. sqlite3_stmt * statement;
  741. [ret setValue:@"2" forKey:@"result"];
  742. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  743. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  744. // int count=0;
  745. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  746. {
  747. int i=0;
  748. while (sqlite3_step(statement) == SQLITE_ROW)
  749. {
  750. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  751. // char *name = (char*)sqlite3_column_text(statement, 1);
  752. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  753. char *name = (char*)sqlite3_column_text(statement, 0);
  754. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  755. char *description = (char*)sqlite3_column_text(statement, 1);
  756. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  757. int product_id = sqlite3_column_int(statement, 2);
  758. // char *url = (char*)sqlite3_column_text(statement, 3);
  759. // if(url==nil)
  760. // url="";
  761. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  762. int wid = sqlite3_column_int(statement, 3);
  763. int closeout = sqlite3_column_int(statement, 4);
  764. NSMutableDictionary* imgjson=[self category_img:product_id];
  765. if(wid==0)
  766. [item setValue:@"false" forKey:@"wish_exists"];
  767. else
  768. [item setValue:@"true" forKey:@"wish_exists"];
  769. if(closeout==0)
  770. [item setValue:@"false" forKey:@"is_closeout"];
  771. else
  772. [item setValue:@"true" forKey:@"is_closeout"];
  773. [item addEntriesFromDictionary:imgjson];
  774. // [item setValue:nsurl forKey:@"img"];
  775. [item setValue:nsname forKey:@"fash_name"];
  776. [item setValue:nsdescription forKey:@"description"];
  777. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  778. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  779. i++;
  780. }
  781. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  782. [ret setObject:items forKey:@"items"];
  783. sqlite3_finalize(statement);
  784. }
  785. NSLog(@"count:%d",count);
  786. [iSalesDB close_db:db];
  787. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  788. return ret;
  789. }
  790. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  791. {
  792. //
  793. // NSString* user = [params valueForKey:@"user"];
  794. //
  795. // NSString* password = [params valueForKey:@"password"];
  796. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  797. ret[@"result"]=[NSNumber numberWithInt:2 ];
  798. UIApplication * app = [UIApplication sharedApplication];
  799. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  800. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  801. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  802. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  803. [appDelegate update_count_mark];
  804. appDelegate.can_show_price =false;
  805. appDelegate.can_see_price =false;
  806. appDelegate.can_create_portfolio =false;
  807. appDelegate.can_create_order =false;
  808. appDelegate.can_cancel_order =false;
  809. appDelegate.can_set_cart_price =false;
  810. appDelegate.can_delete_order =false;
  811. appDelegate.can_submit_order =false;
  812. appDelegate.can_set_tearsheet_price =false;
  813. appDelegate.can_update_contact_info = false;
  814. appDelegate.save_order_logout = false;
  815. appDelegate.submit_order_logout = false;
  816. appDelegate.alert_sold_in_quantities = false;
  817. appDelegate.ipad_perm =nil ;
  818. appDelegate.user_type = USER_ROLE_UNKNOWN;
  819. appDelegate.OrderFilter= nil;
  820. [appDelegate SetSo:nil];
  821. [appDelegate set_main_button_panel];
  822. // sqlite3 *db = [iSalesDB get_db];
  823. //
  824. //
  825. //
  826. //
  827. //
  828. // 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"]];
  829. //
  830. //
  831. //
  832. //
  833. // DebugLog(@"offline_login sql:%@",sqlQuery);
  834. // sqlite3_stmt * statement;
  835. //
  836. //
  837. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  838. //
  839. //
  840. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  841. // {
  842. //
  843. //
  844. // if (sqlite3_step(statement) == SQLITE_ROW)
  845. // {
  846. //
  847. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  848. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  849. //
  850. //
  851. //
  852. // int can_show_price = sqlite3_column_int(statement, 0);
  853. // int can_see_price = sqlite3_column_int(statement, 1);
  854. //
  855. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  856. // if(contact_id==nil)
  857. // contact_id="";
  858. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  859. //
  860. // int user_type = sqlite3_column_int(statement, 3);
  861. //
  862. // int can_cancel_order = sqlite3_column_int(statement, 4);
  863. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  864. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  865. // int can_delete_order = sqlite3_column_int(statement, 7);
  866. // int can_submit_order = sqlite3_column_int(statement, 8);
  867. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  868. // int can_create_order = sqlite3_column_int(statement, 10);
  869. //
  870. //
  871. // char *mode = (char*)sqlite3_column_text(statement, 11);
  872. // if(mode==nil)
  873. // mode="";
  874. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  875. //
  876. //
  877. // char *username = (char*)sqlite3_column_text(statement, 12);
  878. // if(username==nil)
  879. // username="";
  880. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  881. //
  882. //
  883. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  884. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  885. // [header setValue:nscontact_id forKey:@"contact_id"];
  886. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  887. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  888. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  889. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  890. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  891. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  892. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  893. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  894. //
  895. // [header setValue:nsusername forKey:@"username"];
  896. //
  897. //
  898. // [ret setObject:header forKey:@"header"];
  899. // [ret setValue:nsmode forKey:@"mode"];
  900. //
  901. //
  902. // }
  903. //
  904. //
  905. //
  906. // sqlite3_finalize(statement);
  907. // }
  908. //
  909. //
  910. //
  911. // [iSalesDB close_db:db];
  912. //
  913. //
  914. //
  915. //
  916. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  917. return ret;
  918. }
  919. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  920. {
  921. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  922. return [RAUtils dict2data:ret];
  923. }
  924. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  925. {
  926. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  927. int limit = [[params valueForKey:@"limit"] intValue];
  928. int offset = [[params valueForKey:@"offset"] intValue];
  929. NSString* keyword = [params valueForKey:@"keyWord"];
  930. NSString* where=@"1=1";
  931. if(keyword!=nil)
  932. 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];
  933. 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];
  934. sqlite3 *db = [iSalesDB get_db];
  935. sqlite3_stmt * statement;
  936. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  937. {
  938. int count=0;
  939. while (sqlite3_step(statement) == SQLITE_ROW)
  940. {
  941. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  942. int order_id = sqlite3_column_double(statement, 0);
  943. char *soid = (char*)sqlite3_column_text(statement, 1);
  944. if(soid==nil)
  945. soid= "";
  946. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  947. int status = sqlite3_column_double(statement, 2);
  948. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  949. if(sales_rep==nil)
  950. sales_rep= "";
  951. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  952. char *create_by = (char*)sqlite3_column_text(statement, 4);
  953. if(create_by==nil)
  954. create_by= "";
  955. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  956. char *company_name = (char*)sqlite3_column_text(statement, 5);
  957. if(company_name==nil)
  958. company_name= "";
  959. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  960. char *create_time = (char*)sqlite3_column_text(statement, 6);
  961. if(create_time==nil)
  962. create_time= "";
  963. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  964. double total_price = sqlite3_column_double(statement, 7);
  965. item[@"sales_rep"]= nssales_rep;
  966. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  967. item[@"so#"]= nssoid;
  968. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];;
  969. item[@"create_by"]= nscreate_by;
  970. item[@"customer_name"]= nscompany_name;
  971. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];;
  972. item[@"purchase_time"]= nscreate_time;
  973. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  974. count++;
  975. }
  976. ret[@"count"]= [NSNumber numberWithInt:count];
  977. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  978. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  979. ret[@"result"]= [NSNumber numberWithInt:2];
  980. sqlite3_finalize(statement);
  981. }
  982. [iSalesDB close_db:db];
  983. return [RAUtils dict2data:ret];
  984. }
  985. +(NSDictionary*) offline_login :(NSMutableDictionary *) params
  986. {
  987. NSString* user = [params valueForKey:@"user"];
  988. NSString* password = [params valueForKey:@"password"];
  989. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  990. sqlite3 *db = [iSalesDB get_db];
  991. 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"]];
  992. DebugLog(@"offline_login sql:%@",sqlQuery);
  993. sqlite3_stmt * statement;
  994. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  995. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  996. {
  997. if (sqlite3_step(statement) == SQLITE_ROW)
  998. {
  999. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1000. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1001. int can_show_price = sqlite3_column_int(statement, 0);
  1002. int can_see_price = sqlite3_column_int(statement, 1);
  1003. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1004. if(contact_id==nil)
  1005. contact_id="";
  1006. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1007. int user_type = sqlite3_column_int(statement, 3);
  1008. int can_cancel_order = sqlite3_column_int(statement, 4);
  1009. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1010. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1011. int can_delete_order = sqlite3_column_int(statement, 7);
  1012. int can_submit_order = sqlite3_column_int(statement, 8);
  1013. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1014. int can_create_order = sqlite3_column_int(statement, 10);
  1015. char *mode = (char*)sqlite3_column_text(statement, 11);
  1016. if(mode==nil)
  1017. mode="";
  1018. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1019. char *username = (char*)sqlite3_column_text(statement, 12);
  1020. if(username==nil)
  1021. username="";
  1022. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1023. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1024. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1025. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1026. [header setValue:nscontact_id forKey:@"contact_id"];
  1027. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1028. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1029. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1030. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1031. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1032. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1033. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1034. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1035. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1036. [header setValue:nsusername forKey:@"username"];
  1037. [ret setObject:header forKey:@"header"];
  1038. [ret setValue:nsmode forKey:@"mode"];
  1039. }
  1040. sqlite3_finalize(statement);
  1041. }
  1042. [iSalesDB close_db:db];
  1043. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1044. return ret;
  1045. }
  1046. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1047. {
  1048. NSString* contactId = [params valueForKey:@"contactId"];
  1049. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1050. sqlite3 *db = [iSalesDB get_db];
  1051. NSString *sqlQuery = nil;
  1052. {
  1053. 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];
  1054. }
  1055. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1056. sqlite3_stmt * statement;
  1057. [ret setValue:@"2" forKey:@"result"];
  1058. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1059. {
  1060. //int i = 0;
  1061. if (sqlite3_step(statement) == SQLITE_ROW)
  1062. {
  1063. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1064. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1065. // int editable = sqlite3_column_int(statement, 0);
  1066. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1067. NSString *nscompany_name =nil;
  1068. if(company_name==nil)
  1069. nscompany_name=@"";
  1070. else
  1071. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1072. char *country = (char*)sqlite3_column_text(statement, 2);
  1073. if(country==nil)
  1074. country="";
  1075. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1076. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1077. // if(addr==nil)
  1078. // addr="";
  1079. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1080. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1081. if(zipcode==nil)
  1082. zipcode="";
  1083. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1084. char *state = (char*)sqlite3_column_text(statement, 5);
  1085. if(state==nil)
  1086. state="";
  1087. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1088. char *city = (char*)sqlite3_column_text(statement, 6);
  1089. if(city==nil)
  1090. city="";
  1091. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1092. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1093. // NSString *nscontact_name = nil;
  1094. // if(contact_name==nil)
  1095. // nscontact_name=@"";
  1096. // else
  1097. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1098. char *phone = (char*)sqlite3_column_text(statement, 8);
  1099. NSString *nsphone = nil;
  1100. if(phone==nil)
  1101. nsphone=@"";
  1102. else
  1103. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1104. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1105. if(contact_id==nil)
  1106. contact_id="";
  1107. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1108. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1109. if(addr_1==nil)
  1110. addr_1="";
  1111. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1112. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1113. if(addr_2==nil)
  1114. addr_2="";
  1115. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1116. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1117. if(addr_3==nil)
  1118. addr_3="";
  1119. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1120. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1121. if(addr_4==nil)
  1122. addr_4="";
  1123. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1124. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1125. if(first_name==nil)
  1126. first_name="";
  1127. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1128. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1129. if(last_name==nil)
  1130. last_name="";
  1131. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1132. char *fax = (char*)sqlite3_column_text(statement, 16);
  1133. NSString *nsfax = nil;
  1134. if(fax==nil)
  1135. nsfax=@"";
  1136. else
  1137. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1138. char *email = (char*)sqlite3_column_text(statement, 17);
  1139. NSString *nsemail = nil;
  1140. if(email==nil)
  1141. nsemail=@"";
  1142. else
  1143. nsemail= [[NSString alloc]initWithUTF8String:email];
  1144. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1145. NSString *nsimg_0 = nil;
  1146. if(img_0==nil)
  1147. nsimg_0=@"";
  1148. else
  1149. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1150. [self copy_bcardImg:nsimg_0];
  1151. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1152. NSString *nsimg_1 = nil;
  1153. if(img_1==nil)
  1154. nsimg_1=@"";
  1155. else
  1156. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1157. [self copy_bcardImg:nsimg_1];
  1158. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1159. NSString *nsimg_2 = nil;
  1160. if(img_2==nil)
  1161. nsimg_2=@"";
  1162. else
  1163. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1164. [self copy_bcardImg:nsimg_2];
  1165. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1166. NSString *nsprice_type = nil;
  1167. if(price_type==nil)
  1168. nsprice_type=@"";
  1169. else
  1170. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1171. char *notes = (char*)sqlite3_column_text(statement, 22);
  1172. NSString *nsnotes = nil;
  1173. if(notes==nil)
  1174. nsnotes=@"";
  1175. else
  1176. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1177. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1178. NSString *nssalesrep = nil;
  1179. if(salesrep==nil)
  1180. nssalesrep=@"";
  1181. else
  1182. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1183. {
  1184. // decrypt
  1185. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1186. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1187. nsphone=[AESCrypt fastdecrypt:nsphone];
  1188. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1189. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1190. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1191. }
  1192. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1193. [arr_name addObject:nsfirst_name];
  1194. [arr_name addObject:nslast_name];
  1195. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1196. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1197. [arr_ext addObject:nsaddr_1];
  1198. [arr_ext addObject:nsaddr_2];
  1199. [arr_ext addObject:nsaddr_3];
  1200. [arr_ext addObject:nsaddr_4];
  1201. [arr_ext addObject:@"\r\n"];
  1202. [arr_ext addObject:nscity];
  1203. [arr_ext addObject:nsstate];
  1204. [arr_ext addObject:nszipcode];
  1205. [arr_ext addObject:nscountry];
  1206. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1207. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1208. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1209. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1210. [item setValue:nscountry forKey:@"customer_country"];
  1211. [item setValue:nsphone forKey:@"customer_phone"];
  1212. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1213. [item setValue:nscompany_name forKey:@"customer_name"];
  1214. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1215. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1216. [item setValue:nsext forKey:@"customer_contact_ext"];
  1217. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1218. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1219. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1220. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1221. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1222. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1223. [item setValue:nslast_name forKey:@"customer_last_name"];
  1224. [item setValue:nscity forKey:@"customer_city"];
  1225. [item setValue:nsstate forKey:@"customer_state"];
  1226. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1227. [item setValue:contactId forKey:@"customer_cid"];
  1228. [item setValue:nscontact_name forKey:@"customer_contact"];
  1229. [item setValue:nsfax forKey:@"customer_fax"];
  1230. [item setValue:nsemail forKey:@"customer_email"];
  1231. [ret setObject:item forKey:@"customerInfo"];
  1232. // i++;
  1233. }
  1234. UIApplication * app = [UIApplication sharedApplication];
  1235. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1236. [ret setValue:appDelegate.mode forKey:@"mode"];
  1237. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1238. sqlite3_finalize(statement);
  1239. }
  1240. [iSalesDB close_db:db];
  1241. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1242. return ret;
  1243. }
  1244. + (bool) copy_bcardImg:(NSString*) filename
  1245. {
  1246. if(filename.length==0)
  1247. return false;
  1248. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1249. bool ret=false;
  1250. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1251. NSString *cachefolder = [paths objectAtIndex:0];
  1252. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1253. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1254. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1255. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1256. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1257. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1258. //
  1259. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1260. NSFileManager* fileManager = [NSFileManager defaultManager];
  1261. BOOL bdir=NO;
  1262. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1263. {
  1264. NSError *error = nil;
  1265. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1266. {
  1267. ret=false;
  1268. }
  1269. else
  1270. {
  1271. ret=true;
  1272. }
  1273. // NSError *error = nil;
  1274. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1275. //
  1276. // if(!bsuccess)
  1277. // {
  1278. // DebugLog(@"Create offline_createimg folder failed");
  1279. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1280. // return [RAUtils dict2data:ret];
  1281. // }
  1282. // if(bsuccess)
  1283. // {
  1284. // sqlite3 *db = [self get_db];
  1285. //
  1286. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1287. // [iSalesDB close_db:db];
  1288. // }
  1289. }
  1290. return ret;
  1291. //
  1292. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1293. // if(bsuccess)
  1294. // {
  1295. // NSError *error = nil;
  1296. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1297. // {
  1298. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1299. // }
  1300. // else
  1301. // {
  1302. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1303. //
  1304. // ret[@"img_url_aname"]=filename;
  1305. // ret[@"img_url"]=savedImagePath;
  1306. // }
  1307. // }
  1308. }
  1309. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1310. {
  1311. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1312. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1313. NSString *cachefolder = [paths objectAtIndex:0];
  1314. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1315. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1316. NSFileManager* fileManager = [NSFileManager defaultManager];
  1317. BOOL bdir=YES;
  1318. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1319. {
  1320. NSError *error = nil;
  1321. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1322. if(!bsuccess)
  1323. {
  1324. DebugLog(@"Create offline_createimg folder failed");
  1325. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1326. return [RAUtils dict2data:ret];
  1327. }
  1328. // if(bsuccess)
  1329. // {
  1330. // sqlite3 *db = [self get_db];
  1331. //
  1332. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1333. // [iSalesDB close_db:db];
  1334. // }
  1335. }
  1336. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1337. //JEPG格式
  1338. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1339. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1340. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1341. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1342. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1343. if(bsuccess)
  1344. {
  1345. NSError *error = nil;
  1346. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1347. {
  1348. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1349. }
  1350. else
  1351. {
  1352. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1353. ret[@"img_url_aname"]=filename;
  1354. ret[@"img_url"]=filename;
  1355. }
  1356. }
  1357. else
  1358. {
  1359. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1360. }
  1361. return [RAUtils dict2data:ret];
  1362. }
  1363. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  1364. {
  1365. NSString* contactType = [params valueForKey:@"contactType"];
  1366. NSString* keyword = [params valueForKey:@"keyword"];
  1367. // advanced search
  1368. // NSString* contact_name = [params valueForKey:@"contact_name"];
  1369. // NSString* customer_phone = [params valueForKey:@"customer_phone"];
  1370. // NSString* customer_fax = [params valueForKey:@"customer_fax"];
  1371. // NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"];
  1372. // NSString* sales_rep = [params valueForKey:@"sales_rep"];
  1373. // NSString* customer_state = [params valueForKey:@"customer_state"];
  1374. // NSString* customer_name = [params valueForKey:@"customer_name"];
  1375. // NSString* customer_country = [params valueForKey:@"customer_country"];
  1376. // NSString* customer_cid = [params valueForKey:@"customer_cid"];
  1377. // NSString* customer_city = [params valueForKey:@"customer_city"];
  1378. // NSString* customer_address = [params valueForKey:@"customer_address"];
  1379. // NSString* customer_email = [params valueForKey:@"customer_email"];
  1380. int limit = [[params valueForKey:@"limit"] intValue];
  1381. int offset = [[params valueForKey:@"offset"] intValue];
  1382. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1383. sqlite3 *db = [iSalesDB get_db];
  1384. int result= [iSalesDB AddExFunction:db];
  1385. int count =0;
  1386. NSString *sqlQuery = nil;
  1387. if(keyword.length==0)
  1388. {
  1389. 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];
  1390. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  1391. }
  1392. else
  1393. {
  1394. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  1395. keyword = keyword.lowercaseString;
  1396. 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];
  1397. 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]];
  1398. }
  1399. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1400. sqlite3_stmt * statement;
  1401. [ret setValue:@"2" forKey:@"result"];
  1402. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  1403. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1404. {
  1405. int i = 0;
  1406. while (sqlite3_step(statement) == SQLITE_ROW)
  1407. {
  1408. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1409. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1410. int editable = sqlite3_column_int(statement, 0);
  1411. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1412. NSString *nscompany_name =nil;
  1413. if(company_name==nil)
  1414. nscompany_name=@"";
  1415. else
  1416. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  1417. char *country = (char*)sqlite3_column_text(statement, 2);
  1418. if(country==nil)
  1419. country="";
  1420. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1421. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1422. // if(addr==nil)
  1423. // addr="";
  1424. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1425. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1426. if(zipcode==nil)
  1427. zipcode="";
  1428. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1429. char *state = (char*)sqlite3_column_text(statement, 5);
  1430. if(state==nil)
  1431. state="";
  1432. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1433. char *city = (char*)sqlite3_column_text(statement, 6);
  1434. if(city==nil)
  1435. city="";
  1436. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1437. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1438. // NSString *nscontact_name = nil;
  1439. // if(contact_name==nil)
  1440. // nscontact_name=@"";
  1441. // else
  1442. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1443. char *phone = (char*)sqlite3_column_text(statement, 8);
  1444. NSString *nsphone = nil;
  1445. if(phone==nil)
  1446. nsphone=@"";
  1447. else
  1448. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1449. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1450. if(contact_id==nil)
  1451. contact_id="";
  1452. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1453. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1454. if(addr_1==nil)
  1455. addr_1="";
  1456. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1457. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1458. if(addr_2==nil)
  1459. addr_2="";
  1460. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1461. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1462. if(addr_3==nil)
  1463. addr_3="";
  1464. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1465. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1466. if(addr_4==nil)
  1467. addr_4="";
  1468. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1469. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1470. if(first_name==nil)
  1471. first_name="";
  1472. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1473. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1474. if(last_name==nil)
  1475. last_name="";
  1476. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1477. char *fax = (char*)sqlite3_column_text(statement, 16);
  1478. NSString *nsfax = nil;
  1479. if(fax==nil)
  1480. nsfax=@"";
  1481. else
  1482. nsfax= [NSString stringWithFormat:@"FAX:%@",[[NSString alloc]initWithUTF8String:fax]];
  1483. char *email = (char*)sqlite3_column_text(statement, 17);
  1484. NSString *nsemail = nil;
  1485. if(email==nil)
  1486. nsemail=@"";
  1487. else
  1488. nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  1489. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1490. [arr_name addObject:nsfirst_name];
  1491. [arr_name addObject:nslast_name];
  1492. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1493. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  1494. {
  1495. // decrypt
  1496. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1497. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1498. nsphone=[AESCrypt fastdecrypt:nsphone];
  1499. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1500. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1501. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1502. }
  1503. [arr_addr addObject:nscompany_name];
  1504. [arr_addr addObject:nscontact_name];
  1505. [arr_addr addObject:@"<br>"];
  1506. [arr_addr addObject:nsaddr_1];
  1507. [arr_addr addObject:nsaddr_2];
  1508. [arr_addr addObject:nsaddr_3];
  1509. [arr_addr addObject:nsaddr_4];
  1510. //[arr_addr addObject:nsaddr];
  1511. [arr_addr addObject:nszipcode];
  1512. [arr_addr addObject:nscity];
  1513. [arr_addr addObject:nsstate];
  1514. [arr_addr addObject:nscountry];
  1515. [arr_addr addObject:@"<br>"];
  1516. [arr_addr addObject:nsphone];
  1517. [arr_addr addObject:nsfax];
  1518. [arr_addr addObject:nsemail];
  1519. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  1520. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  1521. [item setValue:name forKey:@"name"];
  1522. [item setValue:nscontact_id forKey:@"contact_id"];
  1523. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  1524. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1525. i++;
  1526. }
  1527. UIApplication * app = [UIApplication sharedApplication];
  1528. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1529. [ret setValue:appDelegate.mode forKey:@"mode"];
  1530. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  1531. sqlite3_finalize(statement);
  1532. }
  1533. [iSalesDB close_db:db];
  1534. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1535. return ret;
  1536. }
  1537. +(NSDictionary*) model_NIYMAL:(NSString*) category
  1538. {
  1539. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1540. category = [category substringToIndex:3];
  1541. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1542. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1543. params[@"category"]= category;
  1544. ret[@"params"]= params;
  1545. [ret setValue:@"detail" forKey:@"target"];
  1546. [ret setValue:@"popup" forKey:@"action"];
  1547. [ret setValue:@"content" forKey:@"type"];
  1548. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1549. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1550. [ret setValue:@"true" forKey:@"single_row"];
  1551. [ret setValue:@"true" forKey:@"partial_refresh"];
  1552. sqlite3 *db = [iSalesDB get_db];
  1553. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1554. 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];
  1555. sqlite3_stmt * statement;
  1556. int count = 0;
  1557. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1558. // int count=0;
  1559. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1560. {
  1561. int i=0;
  1562. while (sqlite3_step(statement) == SQLITE_ROW)
  1563. {
  1564. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1565. // char *name = (char*)sqlite3_column_text(statement, 1);
  1566. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1567. char *name = (char*)sqlite3_column_text(statement, 0);
  1568. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1569. int product_id = sqlite3_column_int(statement, 1);
  1570. char *url = (char*)sqlite3_column_text(statement, 2);
  1571. if(url==nil)
  1572. url="";
  1573. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1574. [item setValue:nsurl forKey:@"picture_path"];
  1575. [item setValue:nsname forKey:@"fash_name"];
  1576. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1577. [item setValue:category forKey:@"category"];
  1578. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1579. i++;
  1580. }
  1581. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1582. sqlite3_finalize(statement);
  1583. }
  1584. NSLog(@"count:%d",count);
  1585. [iSalesDB close_db:db];
  1586. return ret;
  1587. }
  1588. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1589. {
  1590. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1591. [ret setValue:key forKey:@"key"];
  1592. [ret setValue:value forKey:@"val"];
  1593. return ret;
  1594. }
  1595. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field
  1596. {
  1597. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1598. [ret setValue:@"0" forKey:@"img_count"];
  1599. sqlite3 *db = [iSalesDB get_db];
  1600. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from model where product_id=%d ;",field,product_id];
  1601. sqlite3_stmt * statement;
  1602. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1603. {
  1604. int i=0;
  1605. if (sqlite3_step(statement) == SQLITE_ROW)
  1606. {
  1607. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1608. // char *name = (char*)sqlite3_column_text(statement, 1);
  1609. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1610. char *value = (char*)sqlite3_column_text(statement, 0);
  1611. if(value==nil)
  1612. value="";
  1613. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1614. char *key = (char*)sqlite3_column_text(statement, 1);
  1615. if(key==nil)
  1616. key="";
  1617. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1618. [item setValue:nsvalue forKey:@"val"];
  1619. [item setValue:nskey forKey:@"key"];
  1620. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1621. [ret setValue:@"1" forKey:@"count"];
  1622. i++;
  1623. }
  1624. sqlite3_finalize(statement);
  1625. }
  1626. [iSalesDB close_db:db];
  1627. return ret;
  1628. }
  1629. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field
  1630. {
  1631. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1632. [ret setValue:@"0" forKey:@"count"];
  1633. sqlite3 *db = [iSalesDB get_db];
  1634. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from model where product_group='%@' order by name;",field,product_group];
  1635. sqlite3_stmt * statement;
  1636. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1637. {
  1638. int i=0;
  1639. while (sqlite3_step(statement) == SQLITE_ROW)
  1640. {
  1641. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1642. // char *name = (char*)sqlite3_column_text(statement, 1);
  1643. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1644. char *value = (char*)sqlite3_column_text(statement, 0);
  1645. if(value==nil)
  1646. value="";
  1647. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1648. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1649. if(selector_display==nil)
  1650. selector_display="";
  1651. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1652. int product_id = sqlite3_column_int(statement, 2);
  1653. char *category = (char*)sqlite3_column_text(statement, 3);
  1654. if(category==nil)
  1655. category="";
  1656. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1657. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil];//[self category_img:product_id];
  1658. [item setValue:nsvalue forKey:@"title"];
  1659. [item setValue:url forKey:@"pic_url"];
  1660. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1661. [params setValue:@"2" forKey:@"count"];
  1662. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1663. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1664. [param0 setValue:@"product_id" forKey:@"name"];
  1665. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1666. [param1 setValue:nscategory forKey:@"val"];
  1667. [param1 setValue:@"category" forKey:@"name"];
  1668. [params setObject:param0 forKey:@"param_0"];
  1669. [params setObject:param1 forKey:@"param_1"];
  1670. [item setObject:params forKey:@"params"];
  1671. [ret setValue:nsselector_display forKey:@"name"];
  1672. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1673. i++;
  1674. }
  1675. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1676. [ret setValue:@"switch" forKey:@"action"];
  1677. sqlite3_finalize(statement);
  1678. }
  1679. [iSalesDB close_db:db];
  1680. return ret;
  1681. }
  1682. +(NSMutableDictionary*) category_img :(int)product_id
  1683. {
  1684. // model 在 category search 显示的图片。
  1685. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1686. sqlite3 *db = [iSalesDB get_db];
  1687. 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];
  1688. sqlite3_stmt * statement;
  1689. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1690. {
  1691. while (sqlite3_step(statement) == SQLITE_ROW)
  1692. {
  1693. char *url = (char*)sqlite3_column_text(statement, 0);
  1694. if(url==nil)
  1695. url="";
  1696. int type = sqlite3_column_int(statement, 1);
  1697. if(type==0)
  1698. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1699. else
  1700. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1701. }
  1702. sqlite3_finalize(statement);
  1703. }
  1704. [iSalesDB close_db:db];
  1705. return ret;
  1706. }
  1707. +(NSDictionary*) model_img :(int)product_id
  1708. {
  1709. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1710. [ret setValue:@"0" forKey:@"img_count"];
  1711. sqlite3 *db = [iSalesDB get_db];
  1712. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  1713. sqlite3_stmt * statement;
  1714. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1715. {
  1716. int i=0;
  1717. while (sqlite3_step(statement) == SQLITE_ROW)
  1718. {
  1719. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1720. // char *name = (char*)sqlite3_column_text(statement, 1);
  1721. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1722. char *url = (char*)sqlite3_column_text(statement, 0);
  1723. if(url==nil)
  1724. url="";
  1725. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1726. [item setValue:nsurl forKey:@"s"];
  1727. [item setValue:nsurl forKey:@"l"];
  1728. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  1729. i++;
  1730. }
  1731. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  1732. sqlite3_finalize(statement);
  1733. }
  1734. [iSalesDB close_db:db];
  1735. return ret;
  1736. }
  1737. #pragma mark - Jack
  1738. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  1739. // "val_227" : {
  1740. // "check" : 1,
  1741. // "value" : "US United States",
  1742. // "value_id" : "228"
  1743. // },
  1744. if (!countryCode) {
  1745. countryCode = @"US";
  1746. }
  1747. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  1748. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  1749. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  1750. int code_id = sqlite3_column_int(stmt, 3); // id
  1751. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1752. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  1753. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  1754. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1755. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  1756. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1757. }
  1758. long n = *count;
  1759. *count = n + 1;
  1760. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  1761. [container setValue:countryDic forKey:key];
  1762. }] mutableCopy];
  1763. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1764. return ret;
  1765. }
  1766. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  1767. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  1768. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1769. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  1770. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  1771. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1772. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1773. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1774. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1775. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  1776. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1777. }
  1778. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1779. [container setValue:stateDic forKey:key];
  1780. }] mutableCopy];
  1781. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1782. return ret;
  1783. }
  1784. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  1785. 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];
  1786. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1787. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  1788. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  1789. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1790. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1791. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1792. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1793. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  1794. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1795. }
  1796. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1797. [container setValue:stateDic forKey:key];
  1798. }] mutableCopy];
  1799. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1800. return ret;
  1801. }
  1802. + (NSDictionary *)offline_getPrice {
  1803. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  1804. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1805. char *name = (char *) sqlite3_column_text(stmt, 1);
  1806. int type = sqlite3_column_int(stmt, 2);
  1807. int orderBy = sqlite3_column_int(stmt, 3);
  1808. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  1809. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1810. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  1811. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  1812. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1813. if (orderBy == 0) {
  1814. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1815. }
  1816. [container setValue:priceDic forKey:key];
  1817. }] mutableCopy];
  1818. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1819. return ret;
  1820. }
  1821. + (NSDictionary *)offline_getSalesRep {
  1822. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  1823. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1824. // 1 name 2 code 3 salesrep_id
  1825. char *name = (char *)sqlite3_column_text(stmt, 1);
  1826. char *code = (char *)sqlite3_column_text(stmt, 2);
  1827. int salesrep_id = sqlite3_column_int(stmt, 3);
  1828. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  1829. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  1830. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  1831. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  1832. }] mutableCopy];
  1833. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1834. return ret;
  1835. }
  1836. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  1837. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  1838. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1839. char *country = (char *)sqlite3_column_text(stmt, 0);
  1840. char *state = (char *)sqlite3_column_text(stmt, 1);
  1841. char *city = (char *)sqlite3_column_text(stmt, 2);
  1842. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  1843. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  1844. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  1845. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  1846. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  1847. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  1848. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  1849. }] mutableCopy];
  1850. return ret;
  1851. }
  1852. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  1853. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  1854. [item setValue:value forKey:valueKey];
  1855. [dic setValue:item forKey:itemKey];
  1856. }
  1857. + (NSString *)countryCodeByid:(NSString *)code_id {
  1858. NSString *ret = nil;
  1859. sqlite3 *db = [iSalesDB get_db];
  1860. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  1861. sqlite3_stmt * statement;
  1862. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1863. while (sqlite3_step(statement) == SQLITE_ROW) {
  1864. char *code = (char *)sqlite3_column_text(statement, 0);
  1865. ret = [NSString stringWithUTF8String:code];
  1866. }
  1867. sqlite3_finalize(statement);
  1868. }
  1869. [iSalesDB close_db:db];
  1870. return ret;
  1871. }
  1872. + (NSString *)countryCodeIdByCode:(NSString *)code {
  1873. NSString *ret = nil;
  1874. sqlite3 *db = [iSalesDB get_db];
  1875. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  1876. sqlite3_stmt * statement;
  1877. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1878. while (sqlite3_step(statement) == SQLITE_ROW) {
  1879. char *_id = (char *)sqlite3_column_text(statement, 0);
  1880. ret = [NSString stringWithFormat:@"%s",_id];
  1881. }
  1882. sqlite3_finalize(statement);
  1883. }
  1884. [iSalesDB close_db:db];
  1885. return ret;
  1886. }
  1887. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  1888. NSString *name = nil;
  1889. sqlite3 *db = [iSalesDB get_db];
  1890. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  1891. sqlite3_stmt * statement;
  1892. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1893. while (sqlite3_step(statement) == SQLITE_ROW) {
  1894. char *value = (char *)sqlite3_column_text(statement, 0);
  1895. name = [NSString stringWithUTF8String:value];
  1896. }
  1897. sqlite3_finalize(statement);
  1898. }
  1899. [iSalesDB close_db:db];
  1900. return name;
  1901. }
  1902. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  1903. NSString *ret = nil;
  1904. sqlite3 *db = [iSalesDB get_db];
  1905. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where order_by = %@;",priceId];
  1906. sqlite3_stmt * statement;
  1907. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1908. while (sqlite3_step(statement) == SQLITE_ROW) {
  1909. char *name = (char *)sqlite3_column_text(statement, 0);
  1910. ret = [NSString stringWithUTF8String:name];
  1911. }
  1912. sqlite3_finalize(statement);
  1913. }
  1914. [iSalesDB close_db:db];
  1915. return ret;
  1916. }
  1917. + (NSString *)salesRepCodeById:(NSString *)_id {
  1918. NSString *ret = nil;
  1919. sqlite3 *db = [iSalesDB get_db];
  1920. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  1921. sqlite3_stmt * statement;
  1922. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1923. while (sqlite3_step(statement) == SQLITE_ROW) {
  1924. char *rep = (char *)sqlite3_column_text(statement, 0);
  1925. ret = [NSString stringWithUTF8String:rep];
  1926. }
  1927. sqlite3_finalize(statement);
  1928. }
  1929. [iSalesDB close_db:db];
  1930. return ret;
  1931. }
  1932. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  1933. NSString *text = [NSString stringWithFormat:@"%s",(char *)sqlite3_column_text(stmt, col)];
  1934. if (!text) {
  1935. text = @"";
  1936. }
  1937. return text;
  1938. }
  1939. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  1940. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  1941. NSData *data = [NSData dataWithContentsOfFile:path];
  1942. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  1943. return ret;
  1944. }
  1945. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  1946. return [[dic objectForKey:key] mutableCopy];
  1947. }
  1948. #pragma mark create new contact
  1949. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  1950. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  1951. NSData *data = [NSData dataWithContentsOfFile:path];
  1952. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  1953. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  1954. NSString *countryCode = nil;
  1955. NSString *countryCode_id = nil;
  1956. NSString *stateCode = nil;
  1957. NSString *city = nil;
  1958. NSString *zipCode = nil;
  1959. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  1960. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  1961. NSString *code_id = params[@"country"];
  1962. countryCode_id = code_id;
  1963. countryCode = [self countryCodeByid:code_id];
  1964. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  1965. NSString *zip_code = params[@"zipcode"];
  1966. zipCode = zip_code;
  1967. countryCode_id = params[@"country"];
  1968. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  1969. countryCode = [dic valueForKey:@"country_code"];
  1970. stateCode = [dic valueForKey:@"state_code"];
  1971. city = [dic valueForKey:@"city"];
  1972. // zip code
  1973. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  1974. [zipDic setValue:zipCode forKey:@"value"];
  1975. [section_0 setValue:zipDic forKey:@"item_10"];
  1976. }
  1977. } else {
  1978. // default: US United States
  1979. countryCode = @"US";
  1980. countryCode_id = @"228";
  1981. }
  1982. // country
  1983. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  1984. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  1985. // state
  1986. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  1987. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  1988. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  1989. // city
  1990. if (city) {
  1991. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  1992. [cityDic setValue:city forKey:@"value"];
  1993. [section_0 setValue:cityDic forKey:@"item_12"];
  1994. }
  1995. // price type
  1996. NSDictionary *priceDic = [self offline_getPrice];
  1997. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  1998. // Sales Rep
  1999. NSDictionary *repDic = [self offline_getSalesRep];
  2000. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2001. [ret setValue:section_0 forKey:@"section_0"];
  2002. return [RAUtils dict2data:ret];
  2003. }
  2004. #pragma mark save
  2005. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update {
  2006. NSString *companyName = [params objectForKey:@"company"];
  2007. if (companyName) {
  2008. companyName = [AESCrypt fastencrypt:companyName];
  2009. } else {
  2010. companyName = @"";
  2011. }
  2012. NSString *addr1 = [params objectForKey:@"address"];
  2013. if (addr1) {
  2014. addr1 = [AESCrypt fastencrypt:addr1];
  2015. } else {
  2016. addr1 = @"";
  2017. }
  2018. NSString *addr2 = [params objectForKey:@"address2"];
  2019. if (!addr2) {
  2020. addr2 = @"";
  2021. }
  2022. NSString *addr3 = [params objectForKey:@"address_3"];
  2023. if (!addr3) {
  2024. addr3 = @"";
  2025. }
  2026. NSString *addr4 = [params objectForKey:@"address_4"];
  2027. if (!addr4) {
  2028. addr4 = @"";
  2029. }
  2030. NSString *country = [params objectForKey:@"country"];
  2031. if (country) {
  2032. country = [self countryNameByCountryCodeId:country];
  2033. } else {
  2034. country = @"";
  2035. }
  2036. NSString *state = [params objectForKey:@"state"];
  2037. if (!state) {
  2038. state = @"";
  2039. }
  2040. NSString *city = [params objectForKey:@"city"];
  2041. if (!city) {
  2042. city = @"";
  2043. }
  2044. NSString *zipcode = [params objectForKey:@"zipcode"];
  2045. if (!zipcode) {
  2046. zipcode = @"";
  2047. }
  2048. NSString *fistName = [params objectForKey:@"firstname"];
  2049. if (!fistName) {
  2050. fistName = @"";
  2051. }
  2052. NSString *lastName = [params objectForKey:@"lastname"];
  2053. if (!lastName) {
  2054. lastName = @"";
  2055. }
  2056. NSString *phone = [params objectForKey:@"phone"];
  2057. if (phone) {
  2058. phone = [AESCrypt fastencrypt:phone];
  2059. } else {
  2060. phone = @"";
  2061. }
  2062. NSString *fax = [params objectForKey:@"fax"];
  2063. if (!fax) {
  2064. fax = @"";
  2065. }
  2066. NSString *email = [params objectForKey:@"email"];
  2067. if (!email) {
  2068. email = @"";
  2069. }
  2070. NSString *notes = [params objectForKey:@"contact_notes"];
  2071. if (!notes) {
  2072. notes = @"";
  2073. }
  2074. NSString *price = [params objectForKey:@"price_name"];
  2075. if (price) {
  2076. price = [self priceNameByPriceId:price];
  2077. } else {
  2078. price = @"";
  2079. }
  2080. NSString *salesRep = [params objectForKey:@"sales_rep"];
  2081. if (salesRep) {
  2082. salesRep = [self salesRepCodeById:salesRep];
  2083. } else {
  2084. salesRep = @"";
  2085. }
  2086. NSString *img = [params objectForKey:@"business_card"];
  2087. NSArray *array = [img componentsSeparatedByString:@","];
  2088. NSString *img_0 = array[0];
  2089. if (!img_0) {
  2090. img_0 = @"";
  2091. }
  2092. NSString *img_1 = array[1];
  2093. if (!img_1) {
  2094. img_1 = @"";
  2095. }
  2096. NSString *img_2 = array[2];
  2097. if (!img_2) {
  2098. img_2 = @"";
  2099. }
  2100. NSString *contact_id = [NSUUID UUID].UUIDString;
  2101. NSMutableDictionary *sync_dic = [params mutableCopy];
  2102. NSString *sync_data = nil;
  2103. NSString *sql = nil;
  2104. if (update){
  2105. contact_id = [params objectForKey:@"contact_id"];
  2106. if (!contact_id) {
  2107. contact_id = @"";
  2108. }
  2109. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2110. sync_data = [RAUtils dict2string:sync_dic];
  2111. 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 = '%@',sync_data = '%@';",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,sync_data];
  2112. } else {
  2113. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2114. sync_data = [RAUtils dict2string:sync_dic];
  2115. 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,sync_data) 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,sync_data];
  2116. }
  2117. int result = [iSalesDB execSql:sql];
  2118. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2119. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2120. }
  2121. #pragma mark save new contact
  2122. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  2123. {
  2124. return [self offline_saveContact:params update:NO];
  2125. }
  2126. #pragma mark edit contact
  2127. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  2128. {
  2129. // {
  2130. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  2131. // password = 123456;
  2132. // user = EvanK;
  2133. // }
  2134. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  2135. NSData *data = [NSData dataWithContentsOfFile:path];
  2136. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2137. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2138. NSString *countryCode = nil;
  2139. NSString *countryCode_id = nil;
  2140. NSString *stateCode = nil;
  2141. /*------contact infor------*/
  2142. __block NSString *country = nil;
  2143. __block NSString *company_name = nil;
  2144. __block NSString *contact_id = params[@"contact_id"];
  2145. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  2146. __block NSString *zipcode = nil;
  2147. __block NSString *state = nil; // state_code
  2148. __block NSString *city = nil; //
  2149. __block NSString *firt_name,*last_name;
  2150. __block NSString *phone,*fax,*email;
  2151. __block NSString *notes,*price_type,*sales_rep;
  2152. __block NSString *img_0,*img_1,*img_2;
  2153. 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];
  2154. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2155. country = [self textAtColumn:0 statement:stmt]; // country name
  2156. company_name = [self textAtColumn:1 statement:stmt];
  2157. addr_1 = [self textAtColumn:2 statement:stmt];
  2158. addr_2 = [self textAtColumn:3 statement:stmt];
  2159. addr_3 = [self textAtColumn:4 statement:stmt];
  2160. addr_4 = [self textAtColumn:5 statement:stmt];
  2161. zipcode = [self textAtColumn:6 statement:stmt];
  2162. state = [self textAtColumn:7 statement:stmt]; // state code
  2163. city = [self textAtColumn:8 statement:stmt];
  2164. firt_name = [self textAtColumn:9 statement:stmt];
  2165. last_name = [self textAtColumn:10 statement:stmt];
  2166. phone = [self textAtColumn:11 statement:stmt];
  2167. fax = [self textAtColumn:12 statement:stmt];
  2168. email = [self textAtColumn:13 statement:stmt];
  2169. notes = [self textAtColumn:14 statement:stmt];
  2170. price_type = [self textAtColumn:15 statement:stmt]; // name
  2171. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  2172. img_0 = [self textAtColumn:17 statement:stmt];
  2173. img_1 = [self textAtColumn:18 statement:stmt];
  2174. img_2 = [self textAtColumn:19 statement:stmt];
  2175. }];
  2176. // decrypt
  2177. if (company_name) {
  2178. company_name = [AESCrypt fastdecrypt:company_name];
  2179. }
  2180. if (addr_1) {
  2181. addr_1 = [AESCrypt fastdecrypt:addr_1];
  2182. }
  2183. if (phone) {
  2184. phone = [AESCrypt fastdecrypt:phone];
  2185. }
  2186. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",country];
  2187. countryCode = [iSalesDB jk_queryText:countrySql];
  2188. stateCode = state;
  2189. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2190. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  2191. NSString *code_id = params[@"country"];
  2192. countryCode_id = code_id;
  2193. countryCode = [self countryCodeByid:code_id];
  2194. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  2195. NSString *zip_code = params[@"zipcode"];
  2196. zipcode = zip_code;
  2197. countryCode_id = params[@"country"];
  2198. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2199. countryCode = [dic valueForKey:@"country_code"];
  2200. stateCode = [dic valueForKey:@"state_code"];
  2201. city = [dic valueForKey:@"city"];
  2202. // zip code
  2203. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  2204. [zipDic setValue:zipcode forKey:@"value"];
  2205. [section_0 setValue:zipDic forKey:@"item_8"];
  2206. }
  2207. }
  2208. // 0 Country
  2209. // 1 Company Name
  2210. // 2 Contact ID
  2211. // 3 Picture
  2212. // 4 Address 1
  2213. // 5 Address 2
  2214. // 6 Address 3
  2215. // 7 Address 4
  2216. // 8 Zip Code
  2217. // 9 State/Province
  2218. // 10 City
  2219. // 11 Contact First Name
  2220. // 12 Contact Last Name
  2221. // 13 Phone
  2222. // 14 Fax
  2223. // 15 Email
  2224. // 16 Contact Notes
  2225. // 17 Price Type
  2226. // 18 Sales Rep
  2227. // country
  2228. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2229. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  2230. // company
  2231. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  2232. // contact_id
  2233. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  2234. // picture
  2235. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  2236. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  2237. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  2238. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  2239. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  2240. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  2241. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  2242. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  2243. // addr 1 2 3 4
  2244. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  2245. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  2246. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  2247. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  2248. // zip code
  2249. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  2250. // state
  2251. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2252. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  2253. // city
  2254. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  2255. // first last
  2256. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  2257. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  2258. // phone fax email
  2259. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  2260. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  2261. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  2262. // notes
  2263. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  2264. // price
  2265. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  2266. for (NSString *key in priceDic.allKeys) {
  2267. if ([key containsString:@"val_"]) {
  2268. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  2269. if ([dic[@"value"] isEqualToString:price_type]) {
  2270. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2271. [priceDic setValue:dic forKey:key];
  2272. }
  2273. }
  2274. }
  2275. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2276. // Sales Rep
  2277. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  2278. for (NSString *key in repDic.allKeys) {
  2279. if ([key containsString:@"val_"]) {
  2280. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  2281. NSString *value = dic[@"value"];
  2282. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  2283. if (code && [code isEqualToString:sales_rep]) {
  2284. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2285. [repDic setValue:dic forKey:key];
  2286. }
  2287. }
  2288. }
  2289. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  2290. [ret setValue:section_0 forKey:@"section_0"];
  2291. return [RAUtils dict2data:ret];
  2292. }
  2293. #pragma mark save contact
  2294. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  2295. {
  2296. return [self offline_saveContact:params update:YES];
  2297. }
  2298. #pragma mark category
  2299. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2300. if (ck) {
  2301. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  2302. for (NSString *key in res.allKeys) {
  2303. if (![key isEqualToString:@"count"]) {
  2304. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  2305. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  2306. if ([val[@"value"] isEqualToString:ck]) {
  2307. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2308. }
  2309. [res setValue:val forKey:key];
  2310. }
  2311. }
  2312. [dic setValue:res forKey:valueKey];
  2313. }
  2314. }
  2315. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  2316. {
  2317. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2318. NSString* category = [params valueForKey:@"category"];
  2319. int limit = [[params valueForKey:@"limit"] intValue];
  2320. int offset = [[params valueForKey:@"offset"] intValue];
  2321. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2322. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  2323. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  2324. sqlite3 *db = [iSalesDB get_db];
  2325. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'%%#%@#%%'",category]];
  2326. 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];
  2327. // {
  2328. // alert = SP;
  2329. // available = "Available Now";
  2330. // bestseller = No;
  2331. // category = 001;
  2332. // "custom_id" = 0;
  2333. // limit = 20;
  2334. // offset = 0;
  2335. // password = 123456;
  2336. // price = "Display All";
  2337. // "price_template" = 0;
  2338. // "sold_by_qty" = "Display All";
  2339. // "sort_by" = 0;
  2340. // user = EvanK;
  2341. // }
  2342. if ([params.allKeys containsObject:@"alert"]) {
  2343. // alert
  2344. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  2345. NSString *alert = params[@"alert"];
  2346. // available
  2347. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  2348. NSString *available = params[@"available"];
  2349. int available_count;
  2350. if ([available isEqualToString:@"Display All"]) {
  2351. available_count = -1;
  2352. } else if ([available isEqualToString:@"Available Now"]) {
  2353. available_count = 1;
  2354. } else {
  2355. available_count = 0;
  2356. }
  2357. // best seller
  2358. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  2359. NSString *bestSeller = params[@"bestseller"];
  2360. int best_intValue = bestSeller ? 1 : 0;
  2361. // price
  2362. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  2363. NSString *price = params[@"price"];
  2364. // sold_by_qty : Sold in quantities of %@
  2365. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  2366. NSString *qty = params[@"sold_by_qty"];
  2367. if ([qty isEqualToString:@"Display All"]) {
  2368. qty = @"";
  2369. }
  2370. #warning 分类查询未完待续。。
  2371. // sql query: alert availability(int) best_seller(int) price qty
  2372. 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'%%#%@#%%' alert = '%@' order by m.name limit %d offset %d ;",category,alert,limit, offset];
  2373. }
  2374. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2375. if (!appDelegate.user) {
  2376. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  2377. }
  2378. [ret setValue:filter forKey:@"filter"];
  2379. DebugLog(@"offline_category sql:%@",sqlQuery);
  2380. sqlite3_stmt * statement;
  2381. [ret setValue:@"2" forKey:@"result"];
  2382. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2383. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2384. // int count=0;
  2385. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2386. {
  2387. int i=0;
  2388. while (sqlite3_step(statement) == SQLITE_ROW)
  2389. {
  2390. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2391. // char *name = (char*)sqlite3_column_text(statement, 1);
  2392. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2393. char *name = (char*)sqlite3_column_text(statement, 0);
  2394. if(name==nil)
  2395. name="";
  2396. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2397. char *description = (char*)sqlite3_column_text(statement, 1);
  2398. if(description==nil)
  2399. description="";
  2400. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2401. int product_id = sqlite3_column_int(statement, 2);
  2402. // char *url = (char*)sqlite3_column_text(statement, 3);
  2403. // if(url==nil)
  2404. // url="";
  2405. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2406. int wid = sqlite3_column_int(statement, 3);
  2407. int closeout = sqlite3_column_int(statement, 4);
  2408. NSMutableDictionary* imgjson=[self category_img:product_id];
  2409. if(wid==0)
  2410. [item setValue:@"false" forKey:@"wish_exists"];
  2411. else
  2412. [item setValue:@"true" forKey:@"wish_exists"];
  2413. if(closeout==0)
  2414. [item setValue:@"false" forKey:@"is_closeout"];
  2415. else
  2416. [item setValue:@"true" forKey:@"is_closeout"];
  2417. [item addEntriesFromDictionary:imgjson];
  2418. // [item setValue:nsurl forKey:@"img"];
  2419. [item setValue:nsname forKey:@"name"];
  2420. [item setValue:nsdescription forKey:@"description"];
  2421. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2422. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2423. i++;
  2424. }
  2425. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2426. [ret setObject:items forKey:@"items"];
  2427. sqlite3_finalize(statement);
  2428. }
  2429. NSLog(@"count:%d",count);
  2430. [iSalesDB close_db:db];
  2431. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2432. return ret;
  2433. }
  2434. @end