OLDataProvider.m 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364
  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. [iSalesDB close_db:db];
  26. if(count==0)
  27. {
  28. return false;
  29. }
  30. return true;
  31. //
  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 db:db];
  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 db:db];
  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 db:db];
  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. sqlite3* db= [iSalesDB get_db];
  167. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  168. [iSalesDB close_db:db];
  169. [ret setObject:detail1_section forKey:@"detail_1"];
  170. return ret;
  171. }
  172. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  173. {
  174. UIApplication * app = [UIApplication sharedApplication];
  175. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  176. // NSArray* arr1 = [self get_user_all_price_type];
  177. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  178. // NSSet *set1 = [NSSet setWithArray:arr1];
  179. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  180. // if(appDelegate.contact_id==nil)
  181. // set2=[set1 mutableCopy];
  182. // else
  183. // [set2 intersectsSet:set1];
  184. // NSArray *retarr = [set2 allObjects];
  185. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  186. // sqlite3 *db = [iSalesDB get_db];
  187. NSString* sqlQuery = nil;
  188. if(appDelegate.contact_id==nil)
  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 order by a.order_by;",product_id];
  190. else
  191. 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];
  192. sqlite3_stmt * statement;
  193. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  194. int count=0;
  195. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  196. {
  197. while (sqlite3_step(statement) == SQLITE_ROW)
  198. {
  199. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  200. char *name = (char*)sqlite3_column_text(statement, 0);
  201. if(name==nil)
  202. name="";
  203. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  204. // double price = sqlite3_column_double(statement, 1);
  205. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  206. // if(isnull==nil)
  207. // item[nsname]= @"No Price";
  208. // else
  209. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  210. char *price = (char*)sqlite3_column_text(statement, 1);
  211. if(price!=nil)
  212. {
  213. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  214. nsprice=[AESCrypt fastdecrypt:nsprice];
  215. if(nsprice.length>0)
  216. {
  217. double dp= [nsprice doubleValue];
  218. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  219. }
  220. }
  221. else
  222. {
  223. item[nsname]= @"No Price";
  224. }
  225. // item[nsname]= nsprice;
  226. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  227. count++;
  228. }
  229. ret[@"count"]= [NSNumber numberWithInt:count];
  230. sqlite3_finalize(statement);
  231. }
  232. // [iSalesDB close_db:db];
  233. return ret;
  234. }
  235. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  236. {
  237. NSArray* arr1 = [self get_user_all_price_type:db];
  238. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  239. // NSSet *set1 = [NSSet setWithArray:arr1];
  240. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  241. // [set2 intersectsSet:set1];
  242. //
  243. //
  244. // NSArray *retarr = [set2 allObjects];
  245. NSString* whereprice=nil;
  246. if(contact_id==nil)
  247. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  248. else
  249. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  250. // sqlite3 *db = [iSalesDB get_db];
  251. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  252. sqlite3_stmt * statement;
  253. NSNumber* ret = nil;
  254. double dprice=DBL_MAX;
  255. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  256. {
  257. while (sqlite3_step(statement) == SQLITE_ROW)
  258. {
  259. // double val = sqlite3_column_double(statement, 0);
  260. char *price = (char*)sqlite3_column_text(statement, 0);
  261. if(price!=nil)
  262. {
  263. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  264. nsprice=[AESCrypt fastdecrypt:nsprice];
  265. if(nsprice.length>0)
  266. {
  267. double dp= [nsprice doubleValue];
  268. if(dp<dprice)
  269. dprice=dp;
  270. }
  271. }
  272. }
  273. sqlite3_finalize(statement);
  274. }
  275. // [iSalesDB close_db:db];
  276. if(dprice==DBL_MAX)
  277. ret= nil;
  278. else
  279. ret= [NSNumber numberWithDouble:dprice];
  280. return ret;
  281. }
  282. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  283. {
  284. NSArray* ret=nil;
  285. // sqlite3 *db = [iSalesDB get_db];
  286. // no customer assigned , use login user contact_id
  287. UIApplication * app = [UIApplication sharedApplication];
  288. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  289. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  290. sqlite3_stmt * statement;
  291. // int count=0;
  292. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  293. {
  294. if (sqlite3_step(statement) == SQLITE_ROW)
  295. {
  296. char *val = (char*)sqlite3_column_text(statement, 0);
  297. if(val==nil)
  298. val="";
  299. NSString* price = [[NSString alloc]initWithUTF8String:val];
  300. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  301. }
  302. sqlite3_finalize(statement);
  303. }
  304. // [iSalesDB close_db:db];
  305. return ret;
  306. }
  307. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  308. {
  309. // sqlite3 *db = [iSalesDB get_db];
  310. if(contact_id==nil)
  311. {
  312. // no customer assigned , use login user contact_id
  313. UIApplication * app = [UIApplication sharedApplication];
  314. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  315. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  316. sqlite3_stmt * statement;
  317. // int count=0;
  318. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  319. {
  320. if (sqlite3_step(statement) == SQLITE_ROW)
  321. {
  322. char *val = (char*)sqlite3_column_text(statement, 0);
  323. if(val==nil)
  324. val="";
  325. contact_id = [[NSString alloc]initWithUTF8String:val];
  326. }
  327. sqlite3_finalize(statement);
  328. }
  329. if(contact_id.length<=0)
  330. {
  331. // [iSalesDB close_db:db];
  332. return nil;
  333. }
  334. }
  335. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  336. sqlite3_stmt * statement;
  337. NSArray* ret=nil;
  338. // int count=0;
  339. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  340. {
  341. if (sqlite3_step(statement) == SQLITE_ROW)
  342. {
  343. char *price_type = (char*)sqlite3_column_text(statement, 0);
  344. if(price_type==nil)
  345. price_type="";
  346. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  347. if(nsprice_type.length>0)
  348. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  349. else
  350. ret=nil;
  351. }
  352. sqlite3_finalize(statement);
  353. }
  354. // [iSalesDB close_db:db];
  355. return ret;
  356. }
  357. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  358. {
  359. NSString* ret= nil;
  360. // sqlite3 *db = [iSalesDB get_db];
  361. NSString *sqlQuery = nil;
  362. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  363. if(product_id==nil)
  364. 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];
  365. else
  366. 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];
  367. sqlite3_stmt * statement;
  368. // int count=0;
  369. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  370. {
  371. if (sqlite3_step(statement) == SQLITE_ROW)
  372. {
  373. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  374. if(imgurl==nil)
  375. imgurl="";
  376. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  377. ret=nsimgurl;
  378. }
  379. sqlite3_finalize(statement);
  380. }
  381. else
  382. {
  383. [ret setValue:@"8" forKey:@"result"];
  384. }
  385. // [iSalesDB close_db:db];
  386. DebugLog(@"data string: %@",ret );
  387. return ret;
  388. }
  389. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  390. {
  391. NSString* ret= nil;
  392. sqlite3 *db = [iSalesDB get_db];
  393. NSString *sqlQuery = nil;
  394. if(product_id==nil)
  395. sqlQuery = [NSString stringWithFormat:@"select default_category from model where name='%@';",model_name];
  396. else
  397. sqlQuery = [NSString stringWithFormat:@"select default_category from model where product_id=%@;",product_id];
  398. sqlite3_stmt * statement;
  399. // int count=0;
  400. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  401. {
  402. if (sqlite3_step(statement) == SQLITE_ROW)
  403. {
  404. char *default_category = (char*)sqlite3_column_text(statement, 0);
  405. if(default_category==nil)
  406. default_category="";
  407. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  408. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  409. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  410. ret=nsdefault_category;
  411. }
  412. sqlite3_finalize(statement);
  413. }
  414. else
  415. {
  416. [ret setValue:@"8" forKey:@"result"];
  417. }
  418. [iSalesDB close_db:db];
  419. DebugLog(@"data string: %@",ret );
  420. return ret;
  421. }
  422. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  423. {
  424. NSString* model_name = [params valueForKey:@"product_name"];
  425. NSString* product_id = [params valueForKey:@"product_id"];
  426. NSString* category = [params valueForKey:@"category"];
  427. if(category==nil)
  428. category = [self model_default_category:product_id model_name:model_name];
  429. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  430. sqlite3 *db = [iSalesDB get_db];
  431. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  432. NSString *sqlQuery = nil;
  433. if(product_id==nil)
  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 name='%@';",model_name];
  435. else
  436. 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];
  437. sqlite3_stmt * statement;
  438. [ret setValue:@"2" forKey:@"result"];
  439. [ret setValue:@"3" forKey:@"detail_section_count"];
  440. // int count=0;
  441. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  442. {
  443. if (sqlite3_step(statement) == SQLITE_ROW)
  444. {
  445. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  446. char *name = (char*)sqlite3_column_text(statement, 0);
  447. if(name==nil)
  448. name="";
  449. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  450. char *description = (char*)sqlite3_column_text(statement, 1);
  451. if(description==nil)
  452. description="";
  453. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  454. int product_id = sqlite3_column_int(statement, 2);
  455. char *color = (char*)sqlite3_column_text(statement, 3);
  456. if(color==nil)
  457. color="";
  458. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  459. //
  460. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  461. // if(legcolor==nil)
  462. // legcolor="";
  463. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  464. //
  465. //
  466. int availability = sqlite3_column_int(statement, 5);
  467. //
  468. // int incoming_stock = sqlite3_column_int(statement, 6);
  469. char *demension = (char*)sqlite3_column_text(statement, 7);
  470. if(demension==nil)
  471. demension="";
  472. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  473. // ,,,,,,,,,
  474. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  475. if(seat_height==nil)
  476. seat_height="";
  477. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  478. char *material = (char*)sqlite3_column_text(statement, 9);
  479. if(material==nil)
  480. material="";
  481. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  482. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  483. if(box_dim==nil)
  484. box_dim="";
  485. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  486. char *volume = (char*)sqlite3_column_text(statement, 11);
  487. if(volume==nil)
  488. volume="";
  489. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  490. double weight = sqlite3_column_double(statement, 12);
  491. char *model_set = (char*)sqlite3_column_text(statement, 13);
  492. if(model_set==nil)
  493. model_set="";
  494. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  495. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  496. if(load_ability==nil)
  497. load_ability="";
  498. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  499. char *default_category = (char*)sqlite3_column_text(statement, 15);
  500. if(default_category==nil)
  501. default_category="";
  502. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  503. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  504. if(fabric_content==nil)
  505. fabric_content="";
  506. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  507. char *assembling = (char*)sqlite3_column_text(statement, 17);
  508. if(assembling==nil)
  509. assembling="";
  510. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  511. char *made_in = (char*)sqlite3_column_text(statement, 18);
  512. if(made_in==nil)
  513. made_in="";
  514. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  515. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  516. if(special_remarks==nil)
  517. special_remarks="";
  518. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  519. int stockUcom = sqlite3_column_double(statement, 20);
  520. char *product_group = (char*)sqlite3_column_text(statement, 21);
  521. if(product_group==nil)
  522. product_group="";
  523. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  524. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  525. // if(fashion_selector==nil)
  526. // fashion_selector="";
  527. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  528. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  529. if(selector_field==nil)
  530. selector_field="";
  531. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  532. char *property_field = (char*)sqlite3_column_text(statement, 23);
  533. if(property_field==nil)
  534. property_field="";
  535. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  536. char *packaging = (char*)sqlite3_column_text(statement, 24);
  537. if(packaging==nil)
  538. packaging="";
  539. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  540. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  541. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  542. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  543. [img_section setValue:model_s_img forKey:@"model_s_img"];
  544. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  545. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  546. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  547. NSString* Availability=nil;
  548. if(availability>0)
  549. Availability=[NSString stringWithFormat:@"%d",availability];
  550. else
  551. Availability = @"In Production";
  552. [img_section setValue:Availability forKey:@"Availability"];
  553. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  554. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  555. NSString* Price=nil;
  556. if(appDelegate.bLogin==false)
  557. Price=@"Must Sign in.";
  558. else
  559. {
  560. NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  561. if(price==nil)
  562. Price=@"No Price.";
  563. else
  564. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  565. }
  566. [img_section setValue:Price forKey:@"price"];
  567. [img_section setValue:nsname forKey:@"model_name"];
  568. [img_section setValue:nsdescription forKey:@"model_descrition"];
  569. [ret setObject:img_section forKey:@"img_section"];
  570. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  571. int detail0_item_count=0;
  572. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  573. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  574. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  575. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  576. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  577. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  578. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  579. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  580. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  581. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  582. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  583. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  584. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  585. NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  586. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  587. {
  588. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  589. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  590. }
  591. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  592. [detail0_section setValue:@"kv" forKey:@"type"];
  593. [detail0_section setValue:@"Product Information" forKey:@"title"];
  594. [ret setObject:detail0_section forKey:@"detail_0"];
  595. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  596. // [detail1_section setValue:@"detail" forKey:@"target"];
  597. // [detail1_section setValue:@"popup" forKey:@"action"];
  598. // [detail1_section setValue:@"content" forKey:@"type"];
  599. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  600. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  601. // [detail1_section setValue:@"true" forKey:@"single_row"];
  602. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  603. [ret setObject:detail1_section forKey:@"detail_1"];
  604. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  605. [detail2_section setValue:@"detail" forKey:@"target"];
  606. [detail2_section setValue:@"popup" forKey:@"action"];
  607. [detail2_section setValue:@"content" forKey:@"type"];
  608. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  609. [detail2_section setValue:@"true" forKey:@"single_row"];
  610. [detail2_section setValue:@"local" forKey:@"data"];
  611. [ret setObject:detail2_section forKey:@"detail_2"];
  612. }
  613. sqlite3_finalize(statement);
  614. }
  615. else
  616. {
  617. [ret setValue:@"8" forKey:@"result"];
  618. }
  619. NSLog(@"count:%d",count);
  620. [iSalesDB close_db:db];
  621. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  622. return ret;
  623. }
  624. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  625. {
  626. NSString* orderCode = [params valueForKey:@"orderCode"];
  627. NSString* keyword = [params valueForKey:@"keyword"];
  628. keyword=keyword.lowercaseString;
  629. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  630. int limit = [[params valueForKey:@"limit"] intValue];
  631. int offset = [[params valueForKey:@"offset"] intValue];
  632. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  633. sqlite3 *db = [iSalesDB get_db];
  634. int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"lower(name) like'%%%@%%' or lower(description) like'%%%@%%'",keyword,keyword]];
  635. NSString *sqlQuery = nil;
  636. if(exactMatch )
  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'%@%%' order by m.name limit %d offset %d ;",keyword,limit, offset];
  638. else
  639. 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];
  640. DebugLog(@"offline_search sql:%@",sqlQuery);
  641. sqlite3_stmt * statement;
  642. [ret setValue:@"2" forKey:@"result"];
  643. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  644. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  645. // int count=0;
  646. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  647. {
  648. int i=0;
  649. while (sqlite3_step(statement) == SQLITE_ROW)
  650. {
  651. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  652. // char *name = (char*)sqlite3_column_text(statement, 1);
  653. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  654. char *name = (char*)sqlite3_column_text(statement, 0);
  655. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  656. char *description = (char*)sqlite3_column_text(statement, 1);
  657. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  658. int product_id = sqlite3_column_int(statement, 2);
  659. // char *url = (char*)sqlite3_column_text(statement, 3);
  660. // if(url==nil)
  661. // url="";
  662. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  663. int wid = sqlite3_column_int(statement, 3);
  664. int closeout = sqlite3_column_int(statement, 4);
  665. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  666. if(wid==0)
  667. [item setValue:@"false" forKey:@"wish_exists"];
  668. else
  669. [item setValue:@"true" forKey:@"wish_exists"];
  670. if(closeout==0)
  671. [item setValue:@"false" forKey:@"is_closeout"];
  672. else
  673. [item setValue:@"true" forKey:@"is_closeout"];
  674. [item addEntriesFromDictionary:imgjson];
  675. // [item setValue:nsurl forKey:@"img"];
  676. [item setValue:nsname forKey:@"fash_name"];
  677. [item setValue:nsdescription forKey:@"description"];
  678. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  679. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  680. i++;
  681. }
  682. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  683. [ret setObject:items forKey:@"items"];
  684. sqlite3_finalize(statement);
  685. }
  686. NSLog(@"count:%d",count);
  687. [iSalesDB close_db:db];
  688. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  689. return ret;
  690. }
  691. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  692. {
  693. //
  694. // NSString* user = [params valueForKey:@"user"];
  695. //
  696. // NSString* password = [params valueForKey:@"password"];
  697. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  698. ret[@"result"]=[NSNumber numberWithInt:2 ];
  699. UIApplication * app = [UIApplication sharedApplication];
  700. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  701. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  702. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  703. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  704. [appDelegate update_count_mark];
  705. appDelegate.can_show_price =false;
  706. appDelegate.can_see_price =false;
  707. appDelegate.can_create_portfolio =false;
  708. appDelegate.can_create_order =false;
  709. appDelegate.can_cancel_order =false;
  710. appDelegate.can_set_cart_price =false;
  711. appDelegate.can_delete_order =false;
  712. appDelegate.can_submit_order =false;
  713. appDelegate.can_set_tearsheet_price =false;
  714. appDelegate.can_update_contact_info = false;
  715. appDelegate.save_order_logout = false;
  716. appDelegate.submit_order_logout = false;
  717. appDelegate.alert_sold_in_quantities = false;
  718. appDelegate.ipad_perm =nil ;
  719. appDelegate.user_type = USER_ROLE_UNKNOWN;
  720. appDelegate.OrderFilter= nil;
  721. [appDelegate SetSo:nil];
  722. [appDelegate set_main_button_panel];
  723. // sqlite3 *db = [iSalesDB get_db];
  724. //
  725. //
  726. //
  727. //
  728. //
  729. // 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"]];
  730. //
  731. //
  732. //
  733. //
  734. // DebugLog(@"offline_login sql:%@",sqlQuery);
  735. // sqlite3_stmt * statement;
  736. //
  737. //
  738. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  739. //
  740. //
  741. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  742. // {
  743. //
  744. //
  745. // if (sqlite3_step(statement) == SQLITE_ROW)
  746. // {
  747. //
  748. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  749. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  750. //
  751. //
  752. //
  753. // int can_show_price = sqlite3_column_int(statement, 0);
  754. // int can_see_price = sqlite3_column_int(statement, 1);
  755. //
  756. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  757. // if(contact_id==nil)
  758. // contact_id="";
  759. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  760. //
  761. // int user_type = sqlite3_column_int(statement, 3);
  762. //
  763. // int can_cancel_order = sqlite3_column_int(statement, 4);
  764. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  765. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  766. // int can_delete_order = sqlite3_column_int(statement, 7);
  767. // int can_submit_order = sqlite3_column_int(statement, 8);
  768. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  769. // int can_create_order = sqlite3_column_int(statement, 10);
  770. //
  771. //
  772. // char *mode = (char*)sqlite3_column_text(statement, 11);
  773. // if(mode==nil)
  774. // mode="";
  775. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  776. //
  777. //
  778. // char *username = (char*)sqlite3_column_text(statement, 12);
  779. // if(username==nil)
  780. // username="";
  781. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  782. //
  783. //
  784. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  785. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  786. // [header setValue:nscontact_id forKey:@"contact_id"];
  787. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  788. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  789. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  790. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  791. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  792. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  793. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  794. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  795. //
  796. // [header setValue:nsusername forKey:@"username"];
  797. //
  798. //
  799. // [ret setObject:header forKey:@"header"];
  800. // [ret setValue:nsmode forKey:@"mode"];
  801. //
  802. //
  803. // }
  804. //
  805. //
  806. //
  807. // sqlite3_finalize(statement);
  808. // }
  809. //
  810. //
  811. //
  812. // [iSalesDB close_db:db];
  813. //
  814. //
  815. //
  816. //
  817. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  818. return ret;
  819. }
  820. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  821. {
  822. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  823. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  824. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  825. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  826. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  827. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  828. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  829. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  830. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  831. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  832. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  833. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  834. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  835. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  836. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  837. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  838. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  839. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  840. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  841. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  842. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  843. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  844. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  845. [arr_name addObject:customer_first_name];
  846. [arr_name addObject:customer_last_name];
  847. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  848. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact) values('OFFLINE'||hex(randomblob(16)),0,'%@','%@','%@','%@','%@','%@','%@','%@')",customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name];
  849. int result =[iSalesDB execSql:sql_neworder ];
  850. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  851. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  852. return [RAUtils dict2data:ret];
  853. }
  854. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  855. {
  856. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  857. int limit = [[params valueForKey:@"limit"] intValue];
  858. int offset = [[params valueForKey:@"offset"] intValue];
  859. NSString* keyword = [params valueForKey:@"keyWord"];
  860. NSString* where=@"1=1";
  861. if(keyword.length>0)
  862. 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];
  863. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time limit %d offset %d" ,where, limit, offset];
  864. sqlite3 *db = [iSalesDB get_db];
  865. sqlite3_stmt * statement;
  866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  867. {
  868. int count=0;
  869. while (sqlite3_step(statement) == SQLITE_ROW)
  870. {
  871. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  872. int order_id = sqlite3_column_double(statement, 0);
  873. char *soid = (char*)sqlite3_column_text(statement, 1);
  874. if(soid==nil)
  875. soid= "";
  876. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  877. int status = sqlite3_column_double(statement, 2);
  878. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  879. if(sales_rep==nil)
  880. sales_rep= "";
  881. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  882. char *create_by = (char*)sqlite3_column_text(statement, 4);
  883. if(create_by==nil)
  884. create_by= "";
  885. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  886. char *company_name = (char*)sqlite3_column_text(statement, 5);
  887. if(company_name==nil)
  888. company_name= "";
  889. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  890. char *create_time = (char*)sqlite3_column_text(statement, 6);
  891. if(create_time==nil)
  892. create_time= "";
  893. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  894. double total_price = sqlite3_column_double(statement, 7);
  895. item[@"sales_rep"]= nssales_rep;
  896. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  897. item[@"so#"]= nssoid;
  898. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];;
  899. item[@"create_by"]= nscreate_by;
  900. item[@"customer_name"]= nscompany_name;
  901. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];;
  902. item[@"purchase_time"]= nscreate_time;
  903. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  904. count++;
  905. }
  906. ret[@"count"]= [NSNumber numberWithInt:count];
  907. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  908. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  909. ret[@"result"]= [NSNumber numberWithInt:2];
  910. sqlite3_finalize(statement);
  911. }
  912. [iSalesDB close_db:db];
  913. return [RAUtils dict2data:ret];
  914. }
  915. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  916. {
  917. int orderId = [params[@"orderId"] intValue];
  918. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  919. // [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"debug_category_filter" ofType:@"json" ]];
  920. // NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil];
  921. NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  922. sqlite3 *db = [iSalesDB get_db];
  923. sqlite3_stmt * statement;
  924. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  925. {
  926. if (sqlite3_step(statement) == SQLITE_ROW)
  927. {
  928. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  929. int order_id = sqlite3_column_int(statement, 0);
  930. char *soid = (char*)sqlite3_column_text(statement, 1);
  931. if(soid==nil)
  932. soid= "";
  933. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  934. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  935. if(poNumber==nil)
  936. poNumber= "";
  937. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  938. char *create_time = (char*)sqlite3_column_text(statement, 3);
  939. if(create_time==nil)
  940. create_time= "";
  941. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  942. int status = sqlite3_column_int(statement, 4);
  943. NSString* nsstatus=@"";
  944. switch (status) {
  945. case 0:
  946. {
  947. nsstatus=@"Temp Order";
  948. break;
  949. }
  950. case 1:
  951. {
  952. nsstatus=@"Saved Order";
  953. break;
  954. }
  955. case 10:
  956. {
  957. nsstatus=@"Quote Submitted";
  958. break;
  959. }
  960. case 11:
  961. {
  962. nsstatus=@"Sales Order Submitted";
  963. break;
  964. }
  965. case 12:
  966. {
  967. nsstatus=@"Processing";
  968. break;
  969. }
  970. case 13:
  971. {
  972. nsstatus=@"Shipped";
  973. break;
  974. }
  975. case 14:
  976. {
  977. nsstatus=@"Closed";
  978. break;
  979. }
  980. case 15:
  981. {
  982. nsstatus=@"Cancelled";
  983. break;
  984. }
  985. default:
  986. break;
  987. }
  988. double TotalCuft = sqlite3_column_double(statement, 5);
  989. double TotalWeight = sqlite3_column_double(statement, 6);
  990. double TotalCarton = sqlite3_column_double(statement, 7);
  991. char *company_name = (char*)sqlite3_column_text(statement, 8);
  992. if(company_name==nil)
  993. company_name= "";
  994. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  995. char *customer_contact = (char*)sqlite3_column_text(statement, 9);
  996. if(customer_contact==nil)
  997. customer_contact= "";
  998. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  999. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1000. if(addr_1==nil)
  1001. addr_1="";
  1002. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1003. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1004. if(addr_2==nil)
  1005. addr_2="";
  1006. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1007. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1008. if(addr_3==nil)
  1009. addr_3="";
  1010. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1011. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1012. if(addr_4==nil)
  1013. addr_4="";
  1014. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1015. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  1016. [arr_addr addObject:nsaddr_1];
  1017. [arr_addr addObject:nsaddr_2];
  1018. [arr_addr addObject:nsaddr_3];
  1019. [arr_addr addObject:nsaddr_4];
  1020. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  1021. char *logist = (char*)sqlite3_column_text(statement, 14);
  1022. if(logist==nil)
  1023. logist= "";
  1024. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  1025. double shipping = sqlite3_column_double(statement, 15);
  1026. double lift_gate = sqlite3_column_double(statement, 16);
  1027. char *general_notes = (char*)sqlite3_column_text(statement, 17);
  1028. if(general_notes==nil)
  1029. general_notes= "";
  1030. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  1031. char *internal_notes = (char*)sqlite3_column_text(statement, 18);
  1032. if(internal_notes==nil)
  1033. internal_notes= "";
  1034. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  1035. char *payment_type = (char*)sqlite3_column_text(statement, 19);
  1036. if(payment_type==nil)
  1037. payment_type= "";
  1038. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  1039. int have_lift_gate = sqlite3_column_int(statement, 20);
  1040. NSError *error = nil;
  1041. NSString* orderinfo = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"orderinfo" ofType:@"template"] encoding:NSUTF8StringEncoding error:&error];
  1042. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SO_PLACEHOLDER" withString:nssoid];
  1043. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"PO_PLACEHOLDER" withString:nspoNumber];
  1044. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ORDERDATE_PLACEHOLDER" withString:nscreate_time];
  1045. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ORDERSTATUS_PLACEHOLDER" withString:nsstatus];
  1046. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CUFT_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalCuft]];
  1047. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"WEIGHT_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalWeight]];
  1048. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CARTON_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalCarton]];
  1049. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"COMPANY_PLACEHOLDER" withString:nscompany_name];
  1050. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CONTACT_PLACEHOLDER" withString:nscustomer_contact];
  1051. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ADDRESS_PLACEHOLDER" withString:customer_address];
  1052. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SHIPPINGMETHOD_PLACEHOLDER" withString:nslogist];
  1053. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SHIPPING_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",shipping]];
  1054. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"LIFTGATE_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  1055. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"GENERALNOTES_PLACEHOLDER" withString:nsgeneral_notes];
  1056. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"INTERNALNOTES_PLACEHOLDER" withString:nsinternal_notes];
  1057. NSString *payment = nil;
  1058. if([nspayment_type isEqualToString:@"Credit Card"])
  1059. {
  1060. payment=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"creditcardpayment" ofType:@"template"] encoding:NSUTF8StringEncoding error:&error];
  1061. //payment=[payment stringByReplacingOccurrencesOfString:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1062. }
  1063. else
  1064. {
  1065. payment=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"normalpayment" ofType:@"template"] encoding:NSUTF8StringEncoding error:&error];
  1066. payment=[payment stringByReplacingOccurrencesOfString:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1067. }
  1068. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"PAYMENT_PLACEHOLDER" withString:payment];
  1069. ret[@"order_info"]= orderinfo;
  1070. ret[@"result"]= [NSNumber numberWithInt:2];
  1071. ret[@"result"]= [NSNumber numberWithInt:2];
  1072. }
  1073. sqlite3_finalize(statement);
  1074. }
  1075. [iSalesDB close_db:db];
  1076. return [RAUtils dict2data:ret];
  1077. }
  1078. +(NSDictionary*) offline_login :(NSMutableDictionary *) params
  1079. {
  1080. NSString* user = [params valueForKey:@"user"];
  1081. NSString* password = [params valueForKey:@"password"];
  1082. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1083. sqlite3 *db = [iSalesDB get_db];
  1084. 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"]];
  1085. DebugLog(@"offline_login sql:%@",sqlQuery);
  1086. sqlite3_stmt * statement;
  1087. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1088. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1089. {
  1090. if (sqlite3_step(statement) == SQLITE_ROW)
  1091. {
  1092. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1093. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1094. int can_show_price = sqlite3_column_int(statement, 0);
  1095. int can_see_price = sqlite3_column_int(statement, 1);
  1096. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1097. if(contact_id==nil)
  1098. contact_id="";
  1099. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1100. int user_type = sqlite3_column_int(statement, 3);
  1101. int can_cancel_order = sqlite3_column_int(statement, 4);
  1102. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1103. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1104. int can_delete_order = sqlite3_column_int(statement, 7);
  1105. int can_submit_order = sqlite3_column_int(statement, 8);
  1106. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1107. int can_create_order = sqlite3_column_int(statement, 10);
  1108. char *mode = (char*)sqlite3_column_text(statement, 11);
  1109. if(mode==nil)
  1110. mode="";
  1111. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1112. char *username = (char*)sqlite3_column_text(statement, 12);
  1113. if(username==nil)
  1114. username="";
  1115. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1116. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1117. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1118. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1119. [header setValue:nscontact_id forKey:@"contact_id"];
  1120. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1121. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1122. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1123. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1124. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1125. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1126. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1127. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1128. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1129. [header setValue:nsusername forKey:@"username"];
  1130. [ret setObject:header forKey:@"header"];
  1131. [ret setValue:nsmode forKey:@"mode"];
  1132. }
  1133. sqlite3_finalize(statement);
  1134. }
  1135. [iSalesDB close_db:db];
  1136. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1137. return ret;
  1138. }
  1139. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1140. {
  1141. NSString* contactId = [params valueForKey:@"contactId"];
  1142. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1143. sqlite3 *db = [iSalesDB get_db];
  1144. NSString *sqlQuery = nil;
  1145. {
  1146. 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];
  1147. }
  1148. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1149. sqlite3_stmt * statement;
  1150. [ret setValue:@"2" forKey:@"result"];
  1151. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1152. {
  1153. //int i = 0;
  1154. if (sqlite3_step(statement) == SQLITE_ROW)
  1155. {
  1156. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1157. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1158. // int editable = sqlite3_column_int(statement, 0);
  1159. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1160. NSString *nscompany_name =nil;
  1161. if(company_name==nil)
  1162. nscompany_name=@"";
  1163. else
  1164. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1165. char *country = (char*)sqlite3_column_text(statement, 2);
  1166. if(country==nil)
  1167. country="";
  1168. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1169. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1170. // if(addr==nil)
  1171. // addr="";
  1172. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1173. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1174. if(zipcode==nil)
  1175. zipcode="";
  1176. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1177. char *state = (char*)sqlite3_column_text(statement, 5);
  1178. if(state==nil)
  1179. state="";
  1180. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1181. char *city = (char*)sqlite3_column_text(statement, 6);
  1182. if(city==nil)
  1183. city="";
  1184. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1185. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1186. // NSString *nscontact_name = nil;
  1187. // if(contact_name==nil)
  1188. // nscontact_name=@"";
  1189. // else
  1190. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1191. char *phone = (char*)sqlite3_column_text(statement, 8);
  1192. NSString *nsphone = nil;
  1193. if(phone==nil)
  1194. nsphone=@"";
  1195. else
  1196. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1197. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1198. if(contact_id==nil)
  1199. contact_id="";
  1200. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1201. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1202. if(addr_1==nil)
  1203. addr_1="";
  1204. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1205. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1206. if(addr_2==nil)
  1207. addr_2="";
  1208. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1209. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1210. if(addr_3==nil)
  1211. addr_3="";
  1212. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1213. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1214. if(addr_4==nil)
  1215. addr_4="";
  1216. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1217. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1218. if(first_name==nil)
  1219. first_name="";
  1220. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1221. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1222. if(last_name==nil)
  1223. last_name="";
  1224. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1225. char *fax = (char*)sqlite3_column_text(statement, 16);
  1226. NSString *nsfax = nil;
  1227. if(fax==nil)
  1228. nsfax=@"";
  1229. else
  1230. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1231. char *email = (char*)sqlite3_column_text(statement, 17);
  1232. NSString *nsemail = nil;
  1233. if(email==nil)
  1234. nsemail=@"";
  1235. else
  1236. nsemail= [[NSString alloc]initWithUTF8String:email];
  1237. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1238. NSString *nsimg_0 = nil;
  1239. if(img_0==nil)
  1240. nsimg_0=@"";
  1241. else
  1242. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1243. [self copy_bcardImg:nsimg_0];
  1244. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1245. NSString *nsimg_1 = nil;
  1246. if(img_1==nil)
  1247. nsimg_1=@"";
  1248. else
  1249. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1250. [self copy_bcardImg:nsimg_1];
  1251. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1252. NSString *nsimg_2 = nil;
  1253. if(img_2==nil)
  1254. nsimg_2=@"";
  1255. else
  1256. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1257. [self copy_bcardImg:nsimg_2];
  1258. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1259. NSString *nsprice_type = nil;
  1260. if(price_type==nil)
  1261. nsprice_type=@"";
  1262. else
  1263. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1264. char *notes = (char*)sqlite3_column_text(statement, 22);
  1265. NSString *nsnotes = nil;
  1266. if(notes==nil)
  1267. nsnotes=@"";
  1268. else
  1269. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1270. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1271. NSString *nssalesrep = nil;
  1272. if(salesrep==nil)
  1273. nssalesrep=@"";
  1274. else
  1275. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1276. {
  1277. // decrypt
  1278. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1279. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1280. nsphone=[AESCrypt fastdecrypt:nsphone];
  1281. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1282. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1283. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1284. }
  1285. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1286. [arr_name addObject:nsfirst_name];
  1287. [arr_name addObject:nslast_name];
  1288. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1289. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1290. [arr_ext addObject:nsaddr_1];
  1291. [arr_ext addObject:nsaddr_2];
  1292. [arr_ext addObject:nsaddr_3];
  1293. [arr_ext addObject:nsaddr_4];
  1294. [arr_ext addObject:@"\r\n"];
  1295. [arr_ext addObject:nscity];
  1296. [arr_ext addObject:nsstate];
  1297. [arr_ext addObject:nszipcode];
  1298. [arr_ext addObject:nscountry];
  1299. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1300. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1301. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1302. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1303. [item setValue:nscountry forKey:@"customer_country"];
  1304. [item setValue:nsphone forKey:@"customer_phone"];
  1305. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1306. [item setValue:nscompany_name forKey:@"customer_name"];
  1307. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1308. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1309. [item setValue:nsext forKey:@"customer_contact_ext"];
  1310. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1311. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1312. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1313. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1314. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1315. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1316. [item setValue:nslast_name forKey:@"customer_last_name"];
  1317. [item setValue:nscity forKey:@"customer_city"];
  1318. [item setValue:nsstate forKey:@"customer_state"];
  1319. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1320. [item setValue:contactId forKey:@"customer_cid"];
  1321. [item setValue:nscontact_name forKey:@"customer_contact"];
  1322. [item setValue:nsfax forKey:@"customer_fax"];
  1323. [item setValue:nsemail forKey:@"customer_email"];
  1324. [ret setObject:item forKey:@"customerInfo"];
  1325. // i++;
  1326. }
  1327. UIApplication * app = [UIApplication sharedApplication];
  1328. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1329. [ret setValue:appDelegate.mode forKey:@"mode"];
  1330. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1331. sqlite3_finalize(statement);
  1332. }
  1333. [iSalesDB close_db:db];
  1334. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1335. return ret;
  1336. }
  1337. + (bool) copy_bcardImg:(NSString*) filename
  1338. {
  1339. if(filename.length==0)
  1340. return false;
  1341. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1342. bool ret=false;
  1343. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1344. NSString *cachefolder = [paths objectAtIndex:0];
  1345. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1346. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1347. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1348. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1349. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1350. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1351. //
  1352. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1353. NSFileManager* fileManager = [NSFileManager defaultManager];
  1354. BOOL bdir=NO;
  1355. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1356. {
  1357. NSError *error = nil;
  1358. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1359. {
  1360. ret=false;
  1361. }
  1362. else
  1363. {
  1364. ret=true;
  1365. }
  1366. // NSError *error = nil;
  1367. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1368. //
  1369. // if(!bsuccess)
  1370. // {
  1371. // DebugLog(@"Create offline_createimg folder failed");
  1372. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1373. // return [RAUtils dict2data:ret];
  1374. // }
  1375. // if(bsuccess)
  1376. // {
  1377. // sqlite3 *db = [self get_db];
  1378. //
  1379. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1380. // [iSalesDB close_db:db];
  1381. // }
  1382. }
  1383. return ret;
  1384. //
  1385. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1386. // if(bsuccess)
  1387. // {
  1388. // NSError *error = nil;
  1389. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1390. // {
  1391. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1392. // }
  1393. // else
  1394. // {
  1395. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1396. //
  1397. // ret[@"img_url_aname"]=filename;
  1398. // ret[@"img_url"]=savedImagePath;
  1399. // }
  1400. // }
  1401. }
  1402. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1403. {
  1404. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1405. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1406. NSString *cachefolder = [paths objectAtIndex:0];
  1407. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1408. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1409. NSFileManager* fileManager = [NSFileManager defaultManager];
  1410. BOOL bdir=YES;
  1411. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1412. {
  1413. NSError *error = nil;
  1414. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1415. if(!bsuccess)
  1416. {
  1417. DebugLog(@"Create offline_createimg folder failed");
  1418. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1419. return [RAUtils dict2data:ret];
  1420. }
  1421. // if(bsuccess)
  1422. // {
  1423. // sqlite3 *db = [self get_db];
  1424. //
  1425. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1426. // [iSalesDB close_db:db];
  1427. // }
  1428. }
  1429. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1430. //JEPG格式
  1431. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1432. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1433. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1434. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1435. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1436. if(bsuccess)
  1437. {
  1438. NSError *error = nil;
  1439. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1440. {
  1441. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1442. }
  1443. else
  1444. {
  1445. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1446. ret[@"img_url_aname"]=filename;
  1447. ret[@"img_url"]=filename;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1453. }
  1454. return [RAUtils dict2data:ret];
  1455. }
  1456. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  1457. {
  1458. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1459. category = [category substringToIndex:3];
  1460. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1461. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1462. params[@"category"]= category;
  1463. ret[@"params"]= params;
  1464. [ret setValue:@"detail" forKey:@"target"];
  1465. [ret setValue:@"popup" forKey:@"action"];
  1466. [ret setValue:@"content" forKey:@"type"];
  1467. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1468. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1469. [ret setValue:@"true" forKey:@"single_row"];
  1470. [ret setValue:@"true" forKey:@"partial_refresh"];
  1471. // sqlite3 *db = [iSalesDB get_db];
  1472. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1473. 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];
  1474. sqlite3_stmt * statement;
  1475. int count = 0;
  1476. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1477. // int count=0;
  1478. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1479. {
  1480. int i=0;
  1481. while (sqlite3_step(statement) == SQLITE_ROW)
  1482. {
  1483. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1484. // char *name = (char*)sqlite3_column_text(statement, 1);
  1485. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1486. char *name = (char*)sqlite3_column_text(statement, 0);
  1487. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1488. int product_id = sqlite3_column_int(statement, 1);
  1489. char *url = (char*)sqlite3_column_text(statement, 2);
  1490. if(url==nil)
  1491. url="";
  1492. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1493. [item setValue:nsurl forKey:@"picture_path"];
  1494. [item setValue:nsname forKey:@"fash_name"];
  1495. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1496. [item setValue:category forKey:@"category"];
  1497. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1498. i++;
  1499. }
  1500. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1501. sqlite3_finalize(statement);
  1502. }
  1503. NSLog(@"count:%d",count);
  1504. // [iSalesDB close_db:db];
  1505. return ret;
  1506. }
  1507. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1508. {
  1509. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1510. [ret setValue:key forKey:@"key"];
  1511. [ret setValue:value forKey:@"val"];
  1512. return ret;
  1513. }
  1514. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  1515. {
  1516. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1517. [ret setValue:@"0" forKey:@"img_count"];
  1518. // sqlite3 *db = [iSalesDB get_db];
  1519. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from model where product_id=%d ;",field,product_id];
  1520. sqlite3_stmt * statement;
  1521. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1522. {
  1523. int i=0;
  1524. if (sqlite3_step(statement) == SQLITE_ROW)
  1525. {
  1526. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1527. // char *name = (char*)sqlite3_column_text(statement, 1);
  1528. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1529. char *value = (char*)sqlite3_column_text(statement, 0);
  1530. if(value==nil)
  1531. value="";
  1532. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1533. char *key = (char*)sqlite3_column_text(statement, 1);
  1534. if(key==nil)
  1535. key="";
  1536. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1537. [item setValue:nsvalue forKey:@"val"];
  1538. [item setValue:nskey forKey:@"key"];
  1539. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1540. [ret setValue:@"1" forKey:@"count"];
  1541. i++;
  1542. }
  1543. sqlite3_finalize(statement);
  1544. }
  1545. // [iSalesDB close_db:db];
  1546. return ret;
  1547. }
  1548. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  1549. {
  1550. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1551. [ret setValue:@"0" forKey:@"count"];
  1552. // sqlite3 *db = [iSalesDB get_db];
  1553. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from model where product_group='%@' order by name;",field,product_group];
  1554. sqlite3_stmt * statement;
  1555. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1556. {
  1557. int i=0;
  1558. while (sqlite3_step(statement) == SQLITE_ROW)
  1559. {
  1560. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1561. // char *name = (char*)sqlite3_column_text(statement, 1);
  1562. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1563. char *value = (char*)sqlite3_column_text(statement, 0);
  1564. if(value==nil)
  1565. value="";
  1566. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1567. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1568. if(selector_display==nil)
  1569. selector_display="";
  1570. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1571. int product_id = sqlite3_column_int(statement, 2);
  1572. char *category = (char*)sqlite3_column_text(statement, 3);
  1573. if(category==nil)
  1574. category="";
  1575. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1576. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  1577. [item setValue:nsvalue forKey:@"title"];
  1578. [item setValue:url forKey:@"pic_url"];
  1579. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1580. [params setValue:@"2" forKey:@"count"];
  1581. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1582. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1583. [param0 setValue:@"product_id" forKey:@"name"];
  1584. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1585. [param1 setValue:nscategory forKey:@"val"];
  1586. [param1 setValue:@"category" forKey:@"name"];
  1587. [params setObject:param0 forKey:@"param_0"];
  1588. [params setObject:param1 forKey:@"param_1"];
  1589. [item setObject:params forKey:@"params"];
  1590. [ret setValue:nsselector_display forKey:@"name"];
  1591. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1592. i++;
  1593. }
  1594. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1595. [ret setValue:@"switch" forKey:@"action"];
  1596. sqlite3_finalize(statement);
  1597. }
  1598. // [iSalesDB close_db:db];
  1599. return ret;
  1600. }
  1601. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  1602. {
  1603. // model 在 category search 显示的图片。
  1604. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1605. // sqlite3 *db = [iSalesDB get_db];
  1606. 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];
  1607. sqlite3_stmt * statement;
  1608. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1609. {
  1610. while (sqlite3_step(statement) == SQLITE_ROW)
  1611. {
  1612. char *url = (char*)sqlite3_column_text(statement, 0);
  1613. if(url==nil)
  1614. url="";
  1615. int type = sqlite3_column_int(statement, 1);
  1616. if(type==0)
  1617. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1618. else
  1619. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1620. }
  1621. sqlite3_finalize(statement);
  1622. }
  1623. // [iSalesDB close_db:db];
  1624. return ret;
  1625. }
  1626. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  1627. {
  1628. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1629. [ret setValue:@"0" forKey:@"img_count"];
  1630. // sqlite3 *db = [iSalesDB get_db];
  1631. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  1632. sqlite3_stmt * statement;
  1633. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1634. {
  1635. int i=0;
  1636. while (sqlite3_step(statement) == SQLITE_ROW)
  1637. {
  1638. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1639. // char *name = (char*)sqlite3_column_text(statement, 1);
  1640. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1641. char *url = (char*)sqlite3_column_text(statement, 0);
  1642. if(url==nil)
  1643. url="";
  1644. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1645. [item setValue:nsurl forKey:@"s"];
  1646. [item setValue:nsurl forKey:@"l"];
  1647. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  1648. i++;
  1649. }
  1650. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  1651. sqlite3_finalize(statement);
  1652. }
  1653. // [iSalesDB close_db:db];
  1654. return ret;
  1655. }
  1656. #pragma mark - Jack
  1657. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  1658. // "val_227" : {
  1659. // "check" : 1,
  1660. // "value" : "US United States",
  1661. // "value_id" : "228"
  1662. // },
  1663. if (!countryCode) {
  1664. countryCode = @"US";
  1665. }
  1666. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  1667. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  1668. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  1669. int code_id = sqlite3_column_int(stmt, 3); // id
  1670. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1671. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  1672. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  1673. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1674. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  1675. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1676. }
  1677. long n = *count;
  1678. *count = n + 1;
  1679. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  1680. [container setValue:countryDic forKey:key];
  1681. }] mutableCopy];
  1682. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1683. return ret;
  1684. }
  1685. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  1686. countryCode = [self translateSingleQuote:countryCode];
  1687. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  1688. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1689. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  1690. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  1691. if (name == NULL) {
  1692. name = "";
  1693. }
  1694. if (code == NULL) {
  1695. code = "";
  1696. }
  1697. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1698. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1699. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1700. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1701. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  1702. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1703. }
  1704. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1705. [container setValue:stateDic forKey:key];
  1706. }] mutableCopy];
  1707. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1708. return ret;
  1709. }
  1710. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  1711. codeId = [self translateSingleQuote:codeId];
  1712. 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];
  1713. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1714. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  1715. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  1716. if (name == NULL) {
  1717. name = "";
  1718. }
  1719. if (code == NULL) {
  1720. code = "";
  1721. }
  1722. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1723. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1724. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1725. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1726. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  1727. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1728. }
  1729. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1730. [container setValue:stateDic forKey:key];
  1731. }] mutableCopy];
  1732. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1733. return ret;
  1734. }
  1735. + (NSDictionary *)offline_getPrice {
  1736. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  1737. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1738. char *name = (char *) sqlite3_column_text(stmt, 1);
  1739. int type = sqlite3_column_int(stmt, 2);
  1740. int orderBy = sqlite3_column_int(stmt, 3);
  1741. if (name == NULL) {
  1742. name = "";
  1743. }
  1744. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  1745. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1746. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  1747. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  1748. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1749. if (orderBy == 0) {
  1750. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1751. }
  1752. [container setValue:priceDic forKey:key];
  1753. }] mutableCopy];
  1754. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1755. return ret;
  1756. }
  1757. + (NSDictionary *)offline_getSalesRep {
  1758. // 首先从offline_login表中取出sales_code
  1759. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1760. NSString *user = app.user;
  1761. user = [self translateSingleQuote:user];
  1762. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  1763. __block NSString *user_code = @"";
  1764. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1765. char *code = (char *)sqlite3_column_text(stmt, 0);
  1766. if (code == NULL) {
  1767. code = "";
  1768. }
  1769. user_code = [NSString stringWithUTF8String:code];
  1770. }];
  1771. // 再取所有salesRep
  1772. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  1773. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1774. // 1 name 2 code 3 salesrep_id
  1775. char *name = (char *)sqlite3_column_text(stmt, 1);
  1776. char *code = (char *)sqlite3_column_text(stmt, 2);
  1777. int salesrep_id = sqlite3_column_int(stmt, 3);
  1778. if (name == NULL) {
  1779. name = "";
  1780. }
  1781. if (code == NULL) {
  1782. code = "";
  1783. }
  1784. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  1785. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  1786. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  1787. // 比较code 相等则check
  1788. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  1789. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1790. }
  1791. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  1792. }] mutableCopy];
  1793. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1794. return ret;
  1795. }
  1796. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  1797. zipcode = [self translateSingleQuote:zipcode];
  1798. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  1799. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1800. char *country = (char *)sqlite3_column_text(stmt, 0);
  1801. char *state = (char *)sqlite3_column_text(stmt, 1);
  1802. char *city = (char *)sqlite3_column_text(stmt, 2);
  1803. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  1804. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  1805. if (country == NULL) {
  1806. country = "";
  1807. }
  1808. if (state == NULL) {
  1809. state = "";
  1810. }
  1811. if (city == NULL) {
  1812. city = "";
  1813. }
  1814. if (country_code == NULL) {
  1815. country_code = "";
  1816. }
  1817. if (state_code == NULL) {
  1818. state_code = "";
  1819. }
  1820. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  1821. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  1822. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  1823. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  1824. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  1825. }] mutableCopy];
  1826. return ret;
  1827. }
  1828. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  1829. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  1830. [item setValue:value forKey:valueKey];
  1831. [dic setValue:item forKey:itemKey];
  1832. }
  1833. + (NSString *)countryCodeByid:(NSString *)code_id {
  1834. NSString *ret = nil;
  1835. code_id = [self translateSingleQuote:code_id];
  1836. sqlite3 *db = [iSalesDB get_db];
  1837. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  1838. sqlite3_stmt * statement;
  1839. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1840. while (sqlite3_step(statement) == SQLITE_ROW) {
  1841. char *code = (char *)sqlite3_column_text(statement, 0);
  1842. if (code == NULL) {
  1843. code = "";
  1844. }
  1845. ret = [NSString stringWithUTF8String:code];
  1846. }
  1847. sqlite3_finalize(statement);
  1848. }
  1849. [iSalesDB close_db:db];
  1850. return ret;
  1851. }
  1852. + (NSString *)countryCodeIdByCode:(NSString *)code {
  1853. NSString *ret = nil;
  1854. code = [self translateSingleQuote:code];
  1855. sqlite3 *db = [iSalesDB get_db];
  1856. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  1857. sqlite3_stmt * statement;
  1858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1859. while (sqlite3_step(statement) == SQLITE_ROW) {
  1860. char *_id = (char *)sqlite3_column_text(statement, 0);
  1861. if (_id == NULL) {
  1862. _id = "";
  1863. }
  1864. ret = [NSString stringWithFormat:@"%s",_id];
  1865. }
  1866. sqlite3_finalize(statement);
  1867. }
  1868. [iSalesDB close_db:db];
  1869. return ret;
  1870. }
  1871. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  1872. NSString *name = nil;
  1873. codeId = [self translateSingleQuote:codeId];
  1874. sqlite3 *db = [iSalesDB get_db];
  1875. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  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 *value = (char *)sqlite3_column_text(statement, 0);
  1880. if (value == NULL) {
  1881. value = "";
  1882. }
  1883. name = [NSString stringWithUTF8String:value];
  1884. }
  1885. sqlite3_finalize(statement);
  1886. }
  1887. [iSalesDB close_db:db];
  1888. return name;
  1889. }
  1890. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  1891. NSString *ret = nil;
  1892. priceId = [self translateSingleQuote:priceId];
  1893. sqlite3 *db = [iSalesDB get_db];
  1894. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  1895. sqlite3_stmt * statement;
  1896. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1897. while (sqlite3_step(statement) == SQLITE_ROW) {
  1898. char *name = (char *)sqlite3_column_text(statement, 0);
  1899. if (name == NULL) {
  1900. name = "";
  1901. }
  1902. ret = [NSString stringWithUTF8String:name];
  1903. }
  1904. sqlite3_finalize(statement);
  1905. }
  1906. [iSalesDB close_db:db];
  1907. return ret;
  1908. }
  1909. + (NSString *)salesRepCodeById:(NSString *)_id {
  1910. NSString *ret = nil;
  1911. _id = [self translateSingleQuote:_id];
  1912. sqlite3 *db = [iSalesDB get_db];
  1913. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  1914. sqlite3_stmt * statement;
  1915. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1916. while (sqlite3_step(statement) == SQLITE_ROW) {
  1917. char *rep = (char *)sqlite3_column_text(statement, 0);
  1918. if (rep == NULL) {
  1919. rep = "";
  1920. }
  1921. ret = [NSString stringWithUTF8String:rep];
  1922. }
  1923. sqlite3_finalize(statement);
  1924. }
  1925. [iSalesDB close_db:db];
  1926. return ret;
  1927. }
  1928. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  1929. char *tx = (char *)sqlite3_column_text(stmt, col);
  1930. if (tx == NULL) {
  1931. tx = "";
  1932. }
  1933. NSString *text = [NSString stringWithFormat:@"%s",tx];
  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. + (NSString *)translateSingleQuote:(NSString *)string {
  1949. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  1950. }
  1951. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  1952. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  1953. }
  1954. #pragma mark contact list
  1955. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  1956. {
  1957. // contactType = "Sales_Order_Customer";
  1958. // limit = 25;
  1959. // offset = 0;
  1960. // password = 123456;
  1961. // "price_name" = 16;
  1962. // user = EvanK;
  1963. sqlite3 *db = [iSalesDB get_db];
  1964. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  1965. if (contactType) {
  1966. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  1967. } else {
  1968. contactType = @"1 = 1";
  1969. }
  1970. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  1971. DebugLog(@"offline contact list keyword: %@",keyword);
  1972. // advanced search
  1973. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  1974. if (contact_name) {
  1975. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  1976. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  1977. } else {
  1978. contact_name = @"";
  1979. }
  1980. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  1981. if (customer_phone) {
  1982. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  1983. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  1984. } else {
  1985. customer_phone = @"";
  1986. }
  1987. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  1988. if (customer_fax) {
  1989. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  1990. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  1991. } else {
  1992. customer_fax = @"";
  1993. }
  1994. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  1995. if (customer_zipcode) {
  1996. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  1997. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  1998. } else {
  1999. customer_zipcode = @"";
  2000. }
  2001. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  2002. if (customer_sales_rep) {
  2003. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2004. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  2005. } else {
  2006. customer_sales_rep = @"";
  2007. }
  2008. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  2009. if (customer_state) {
  2010. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2011. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  2012. } else {
  2013. customer_state = @"";
  2014. }
  2015. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  2016. if (customer_name) {
  2017. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2018. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  2019. } else {
  2020. customer_name = @"";
  2021. }
  2022. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  2023. if (customer_country) {
  2024. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2025. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  2026. } else {
  2027. customer_country = @"";
  2028. }
  2029. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  2030. if (customer_cid) {
  2031. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2032. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  2033. } else {
  2034. customer_cid = @"";
  2035. }
  2036. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  2037. if (customer_city) {
  2038. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2039. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  2040. } else {
  2041. customer_city = @"";
  2042. }
  2043. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  2044. if (customer_address) {
  2045. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2046. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  2047. } else {
  2048. customer_address = @"";
  2049. }
  2050. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  2051. if (customer_email) {
  2052. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2053. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  2054. } else {
  2055. customer_email = @"";
  2056. }
  2057. NSString *price_name = [params valueForKey:@"price_name"];
  2058. if (price_name) {
  2059. if ([price_name containsString:@","]) {
  2060. // 首先从 price表中查处name
  2061. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  2062. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  2063. for (int i = 1;i < pArray.count;i++) {
  2064. NSString *p = pArray[i];
  2065. [mutablePStr appendFormat:@" or type = %@ ",p];
  2066. }
  2067. [mutablePStr appendString:@";"];
  2068. __block NSMutableArray *price_name_array = [NSMutableArray array];
  2069. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2070. char *name = (char *)sqlite3_column_text(stmt, 0);
  2071. if (!name)
  2072. name = "";
  2073. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  2074. }];
  2075. // 再根据name 拼sql
  2076. NSMutableString *mutable_price_name = [NSMutableString string];
  2077. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  2078. for (int i = 1; i < price_name_array.count; i++) {
  2079. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  2080. }
  2081. [mutable_price_name appendString:@")"];
  2082. price_name = mutable_price_name;
  2083. } else {
  2084. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  2085. if ([price_name isEqualToString:@""]) {
  2086. price_name = @"";
  2087. } else {
  2088. __block NSString *price;
  2089. price_name = [self translateSingleQuote:price_name];
  2090. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2091. char *p = (char *)sqlite3_column_text(stmt, 0);
  2092. if (p == NULL) {
  2093. p = "";
  2094. }
  2095. price = [NSString stringWithUTF8String:p];
  2096. }];
  2097. if ([price isEqualToString:@""]) {
  2098. price_name = @"";
  2099. } else {
  2100. price = [self translateSingleQuote:price];
  2101. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  2102. }
  2103. }
  2104. }
  2105. } else {
  2106. price_name = @"";
  2107. }
  2108. int limit = [[params valueForKey:@"limit"] intValue];
  2109. int offset = [[params valueForKey:@"offset"] intValue];
  2110. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2111. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  2112. NSString *sql = [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 %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  2113. // int result= [iSalesDB AddExFunction:db];
  2114. int count =0;
  2115. NSString *sqlQuery = nil;
  2116. if(keyword.length==0)
  2117. {
  2118. // 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];
  2119. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  2120. sqlQuery = sql;
  2121. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  2122. }
  2123. else
  2124. {
  2125. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  2126. keyword = keyword.lowercaseString;
  2127. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2128. 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 %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  2129. 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]];
  2130. }
  2131. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2132. sqlite3_stmt * statement;
  2133. [ret setValue:@"2" forKey:@"result"];
  2134. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  2135. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2136. {
  2137. int i = 0;
  2138. while (sqlite3_step(statement) == SQLITE_ROW)
  2139. {
  2140. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2141. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2142. int editable = sqlite3_column_int(statement, 0);
  2143. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2144. NSString *nscompany_name =nil;
  2145. if(company_name==nil)
  2146. nscompany_name=@"";
  2147. else
  2148. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  2149. char *country = (char*)sqlite3_column_text(statement, 2);
  2150. if(country==nil)
  2151. country="";
  2152. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2153. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2154. // if(addr==nil)
  2155. // addr="";
  2156. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2157. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2158. if(zipcode==nil)
  2159. zipcode="";
  2160. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2161. char *state = (char*)sqlite3_column_text(statement, 5);
  2162. if(state==nil)
  2163. state="";
  2164. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2165. char *city = (char*)sqlite3_column_text(statement, 6);
  2166. if(city==nil)
  2167. city="";
  2168. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2169. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2170. // NSString *nscontact_name = nil;
  2171. // if(contact_name==nil)
  2172. // nscontact_name=@"";
  2173. // else
  2174. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2175. char *phone = (char*)sqlite3_column_text(statement, 8);
  2176. NSString *nsphone = nil;
  2177. if(phone==nil)
  2178. nsphone=@"";
  2179. else
  2180. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2181. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2182. if(contact_id==nil)
  2183. contact_id="";
  2184. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2185. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2186. if(addr_1==nil)
  2187. addr_1="";
  2188. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2189. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2190. if(addr_2==nil)
  2191. addr_2="";
  2192. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2193. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2194. if(addr_3==nil)
  2195. addr_3="";
  2196. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2197. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2198. if(addr_4==nil)
  2199. addr_4="";
  2200. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2201. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2202. if(first_name==nil)
  2203. first_name="";
  2204. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2205. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2206. if(last_name==nil)
  2207. last_name="";
  2208. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2209. char *fax = (char*)sqlite3_column_text(statement, 16);
  2210. NSString *nsfax = nil;
  2211. if(fax==nil)
  2212. nsfax=@"";
  2213. else
  2214. {
  2215. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2216. if(nsfax.length>0)
  2217. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  2218. }
  2219. char *email = (char*)sqlite3_column_text(statement, 17);
  2220. NSString *nsemail = nil;
  2221. if(email==nil)
  2222. nsemail=@"";
  2223. else
  2224. {
  2225. nsemail= [[NSString alloc]initWithUTF8String:email];
  2226. if(nsemail.length>0)
  2227. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  2228. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  2229. }
  2230. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2231. [arr_name addObject:nsfirst_name];
  2232. [arr_name addObject:nslast_name];
  2233. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2234. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  2235. {
  2236. // decrypt
  2237. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2238. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2239. nsphone=[AESCrypt fastdecrypt:nsphone];
  2240. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2241. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2242. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2243. }
  2244. [arr_addr addObject:nscompany_name];
  2245. [arr_addr addObject:nscontact_name];
  2246. [arr_addr addObject:@"<br>"];
  2247. [arr_addr addObject:nsaddr_1];
  2248. [arr_addr addObject:nsaddr_2];
  2249. [arr_addr addObject:nsaddr_3];
  2250. [arr_addr addObject:nsaddr_4];
  2251. //[arr_addr addObject:nsaddr];
  2252. [arr_addr addObject:nszipcode];
  2253. [arr_addr addObject:nscity];
  2254. [arr_addr addObject:nsstate];
  2255. [arr_addr addObject:nscountry];
  2256. [arr_addr addObject:@"<br>"];
  2257. [arr_addr addObject:nsphone];
  2258. [arr_addr addObject:nsfax];
  2259. [arr_addr addObject:nsemail];
  2260. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  2261. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  2262. [item setValue:name forKey:@"name"];
  2263. [item setValue:nscontact_id forKey:@"contact_id"];
  2264. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  2265. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2266. i++;
  2267. }
  2268. UIApplication * app = [UIApplication sharedApplication];
  2269. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2270. [ret setValue:appDelegate.mode forKey:@"mode"];
  2271. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  2272. sqlite3_finalize(statement);
  2273. }
  2274. [iSalesDB close_db:db];
  2275. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2276. return ret;
  2277. }
  2278. #pragma mark contact Advanced search
  2279. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  2280. {
  2281. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  2282. return [RAUtils dict2data:contactAdvanceDic];
  2283. }
  2284. #pragma mark create new contact
  2285. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  2286. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  2287. NSData *data = [NSData dataWithContentsOfFile:path];
  2288. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2289. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2290. NSString *countryCode = nil;
  2291. NSString *countryCode_id = nil;
  2292. NSString *stateCode = nil;
  2293. NSString *city = nil;
  2294. NSString *zipCode = nil;
  2295. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2296. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  2297. NSString *code_id = params[@"country"];
  2298. countryCode_id = code_id;
  2299. countryCode = [self countryCodeByid:code_id];
  2300. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  2301. NSString *zip_code = params[@"zipcode"];
  2302. zipCode = zip_code;
  2303. countryCode_id = params[@"country"];
  2304. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2305. countryCode = [dic valueForKey:@"country_code"];
  2306. stateCode = [dic valueForKey:@"state_code"];
  2307. city = [dic valueForKey:@"city"];
  2308. // zip code
  2309. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  2310. [zipDic setValue:zipCode forKey:@"value"];
  2311. [section_0 setValue:zipDic forKey:@"item_10"];
  2312. }
  2313. } else {
  2314. // default: US United States
  2315. countryCode = @"US";
  2316. countryCode_id = @"228";
  2317. }
  2318. // country
  2319. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2320. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  2321. // state
  2322. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2323. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  2324. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  2325. // city
  2326. if (city) {
  2327. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  2328. [cityDic setValue:city forKey:@"value"];
  2329. [section_0 setValue:cityDic forKey:@"item_12"];
  2330. }
  2331. // price type
  2332. NSDictionary *priceDic = [self offline_getPrice];
  2333. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  2334. // Sales Rep
  2335. NSDictionary *repDic = [self offline_getSalesRep];
  2336. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2337. [ret setValue:section_0 forKey:@"section_0"];
  2338. return [RAUtils dict2data:ret];
  2339. }
  2340. #pragma mark save
  2341. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update {
  2342. NSString *addr = nil;
  2343. NSString *contact_name = nil;
  2344. NSString *companyName = [params objectForKey:@"company"];
  2345. if (companyName) {
  2346. companyName = [AESCrypt fastencrypt:companyName];
  2347. } else {
  2348. companyName = @"";
  2349. }
  2350. companyName = [self translateSingleQuote:companyName];
  2351. NSString *addr1 = [params objectForKey:@"address"];
  2352. NSString *addr2 = [params objectForKey:@"address2"];
  2353. NSString *addr3 = [params objectForKey:@"address_3"];
  2354. NSString *addr4 = [params objectForKey:@"address_4"];
  2355. if (!addr2) {
  2356. addr2 = @"";
  2357. }
  2358. if (!addr3) {
  2359. addr3 = @"";
  2360. }
  2361. if (!addr4) {
  2362. addr4 = @"";
  2363. }
  2364. if (!addr1) {
  2365. addr1 = @"";
  2366. }
  2367. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  2368. addr = [AESCrypt fastencrypt:addr];
  2369. addr = [self translateSingleQuote:addr];
  2370. if (addr1 && ![addr1 isEqualToString:@""]) {
  2371. addr1 = [AESCrypt fastencrypt:addr1];
  2372. }
  2373. addr1 = [self translateSingleQuote:addr1];
  2374. addr2 = [self translateSingleQuote:addr2];
  2375. addr3 = [self translateSingleQuote:addr3];
  2376. addr4 = [self translateSingleQuote:addr4];
  2377. NSString *country = [params objectForKey:@"country"];
  2378. if (country) {
  2379. country = [self countryNameByCountryCodeId:country];
  2380. } else {
  2381. country = @"";
  2382. }
  2383. country = [self translateSingleQuote:country];
  2384. NSString *state = [params objectForKey:@"state"];
  2385. if (!state) {
  2386. state = @"";
  2387. }
  2388. state = [self translateSingleQuote:state];
  2389. NSString *city = [params objectForKey:@"city"];
  2390. if (!city) {
  2391. city = @"";
  2392. }
  2393. city = [self translateSingleQuote:city];
  2394. NSString *zipcode = [params objectForKey:@"zipcode"];
  2395. if (!zipcode) {
  2396. zipcode = @"";
  2397. }
  2398. zipcode = [self translateSingleQuote:zipcode];
  2399. NSString *fistName = [params objectForKey:@"firstname"];
  2400. if (!fistName) {
  2401. fistName = @"";
  2402. }
  2403. NSString *lastName = [params objectForKey:@"lastname"];
  2404. if (!lastName) {
  2405. lastName = @"";
  2406. }
  2407. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  2408. contact_name = [self translateSingleQuote:contact_name];
  2409. fistName = [self translateSingleQuote:fistName];
  2410. lastName = [self translateSingleQuote:lastName];
  2411. NSString *phone = [params objectForKey:@"phone"];
  2412. if (phone) {
  2413. phone = [AESCrypt fastencrypt:phone];
  2414. } else {
  2415. phone = @"";
  2416. }
  2417. phone = [self translateSingleQuote:phone];
  2418. NSString *fax = [params objectForKey:@"fax"];
  2419. if (!fax) {
  2420. fax = @"";
  2421. }
  2422. fax = [self translateSingleQuote:fax];
  2423. NSString *email = [params objectForKey:@"email"];
  2424. if (!email) {
  2425. email = @"";
  2426. }
  2427. email = [self translateSingleQuote:email];
  2428. NSString *notes = [params objectForKey:@"contact_notes"];
  2429. if (!notes) {
  2430. notes = @"";
  2431. }
  2432. notes = [self translateSingleQuote:notes];
  2433. NSString *price = [params objectForKey:@"price_name"];
  2434. if (price) {
  2435. price = [self priceNameByPriceId:price];
  2436. } else {
  2437. price = @"";
  2438. }
  2439. price = [self translateSingleQuote:price];
  2440. NSString *salesRep = [params objectForKey:@"sales_rep"];
  2441. if (salesRep) {
  2442. salesRep = [self salesRepCodeById:salesRep];
  2443. } else {
  2444. salesRep = @"";
  2445. }
  2446. salesRep = [self translateSingleQuote:salesRep];
  2447. NSString *img = [params objectForKey:@"business_card"];
  2448. NSArray *array = [img componentsSeparatedByString:@","];
  2449. NSString *img_0 = array[0];
  2450. if (!img_0) {
  2451. img_0 = @"";
  2452. }
  2453. img_0 = [self translateSingleQuote:img_0];
  2454. NSString *img_1 = array[1];
  2455. if (!img_1) {
  2456. img_1 = @"";
  2457. }
  2458. img_1 = [self translateSingleQuote:img_1];
  2459. NSString *img_2 = array[2];
  2460. if (!img_2) {
  2461. img_2 = @"";
  2462. }
  2463. img_2 = [self translateSingleQuote:img_2];
  2464. NSString *contact_id = [NSUUID UUID].UUIDString;
  2465. NSMutableDictionary *sync_dic = [params mutableCopy];
  2466. NSString *sync_data = nil;
  2467. NSString *sql = nil;
  2468. if (update){
  2469. contact_id = [params objectForKey:@"contact_id"];
  2470. if (!contact_id) {
  2471. contact_id = @"";
  2472. }
  2473. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2474. sync_data = [RAUtils dict2string:sync_dic];
  2475. sync_data = [self translateSingleQuote:sync_data];
  2476. 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 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  2477. } else {
  2478. contact_id = [self translateSingleQuote:contact_id];
  2479. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2480. sync_data = [RAUtils dict2string:sync_dic];
  2481. sync_data = [self translateSingleQuote:sync_data];
  2482. 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,contact_name,addr) 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,contact_name,addr];
  2483. }
  2484. int result = [iSalesDB execSql:sql];
  2485. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2486. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2487. }
  2488. #pragma mark save new contact
  2489. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  2490. {
  2491. return [self offline_saveContact:params update:NO];
  2492. }
  2493. #pragma mark edit contact
  2494. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  2495. {
  2496. // {
  2497. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  2498. // password = 123456;
  2499. // user = EvanK;
  2500. // }
  2501. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  2502. NSData *data = [NSData dataWithContentsOfFile:path];
  2503. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2504. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2505. NSString *countryCode = nil;
  2506. NSString *countryCode_id = nil;
  2507. NSString *stateCode = nil;
  2508. /*------contact infor------*/
  2509. __block NSString *country = nil;
  2510. __block NSString *company_name = nil;
  2511. __block NSString *contact_id = params[@"contact_id"];
  2512. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  2513. __block NSString *zipcode = nil;
  2514. __block NSString *state = nil; // state_code
  2515. __block NSString *city = nil; //
  2516. __block NSString *firt_name,*last_name;
  2517. __block NSString *phone,*fax,*email;
  2518. __block NSString *notes,*price_type,*sales_rep;
  2519. __block NSString *img_0,*img_1,*img_2;
  2520. contact_id = [self translateSingleQuote:contact_id];
  2521. 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];
  2522. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2523. country = [self textAtColumn:0 statement:stmt]; // country name
  2524. company_name = [self textAtColumn:1 statement:stmt];
  2525. addr_1 = [self textAtColumn:2 statement:stmt];
  2526. addr_2 = [self textAtColumn:3 statement:stmt];
  2527. addr_3 = [self textAtColumn:4 statement:stmt];
  2528. addr_4 = [self textAtColumn:5 statement:stmt];
  2529. zipcode = [self textAtColumn:6 statement:stmt];
  2530. state = [self textAtColumn:7 statement:stmt]; // state code
  2531. city = [self textAtColumn:8 statement:stmt];
  2532. firt_name = [self textAtColumn:9 statement:stmt];
  2533. last_name = [self textAtColumn:10 statement:stmt];
  2534. phone = [self textAtColumn:11 statement:stmt];
  2535. fax = [self textAtColumn:12 statement:stmt];
  2536. email = [self textAtColumn:13 statement:stmt];
  2537. notes = [self textAtColumn:14 statement:stmt];
  2538. price_type = [self textAtColumn:15 statement:stmt]; // name
  2539. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  2540. img_0 = [self textAtColumn:17 statement:stmt];
  2541. img_1 = [self textAtColumn:18 statement:stmt];
  2542. img_2 = [self textAtColumn:19 statement:stmt];
  2543. }];
  2544. // decrypt
  2545. if (company_name) {
  2546. company_name = [AESCrypt fastdecrypt:company_name];
  2547. }
  2548. if (addr_1) {
  2549. addr_1 = [AESCrypt fastdecrypt:addr_1];
  2550. }
  2551. if (phone) {
  2552. phone = [AESCrypt fastdecrypt:phone];
  2553. }
  2554. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  2555. countryCode = [iSalesDB jk_queryText:countrySql];
  2556. stateCode = state;
  2557. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2558. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  2559. NSString *code_id = params[@"country"];
  2560. countryCode_id = code_id;
  2561. countryCode = [self countryCodeByid:code_id];
  2562. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  2563. NSString *zip_code = params[@"zipcode"];
  2564. zipcode = zip_code;
  2565. countryCode_id = params[@"country"];
  2566. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2567. countryCode = [dic valueForKey:@"country_code"];
  2568. stateCode = [dic valueForKey:@"state_code"];
  2569. city = [dic valueForKey:@"city"];
  2570. // zip code
  2571. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  2572. [zipDic setValue:zipcode forKey:@"value"];
  2573. [section_0 setValue:zipDic forKey:@"item_8"];
  2574. }
  2575. }
  2576. // 0 Country
  2577. // 1 Company Name
  2578. // 2 Contact ID
  2579. // 3 Picture
  2580. // 4 Address 1
  2581. // 5 Address 2
  2582. // 6 Address 3
  2583. // 7 Address 4
  2584. // 8 Zip Code
  2585. // 9 State/Province
  2586. // 10 City
  2587. // 11 Contact First Name
  2588. // 12 Contact Last Name
  2589. // 13 Phone
  2590. // 14 Fax
  2591. // 15 Email
  2592. // 16 Contact Notes
  2593. // 17 Price Type
  2594. // 18 Sales Rep
  2595. // country
  2596. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2597. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  2598. // company
  2599. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  2600. // contact_id
  2601. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  2602. // picture
  2603. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  2604. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  2605. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  2606. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  2607. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  2608. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  2609. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  2610. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  2611. // addr 1 2 3 4
  2612. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  2613. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  2614. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  2615. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  2616. // zip code
  2617. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  2618. // state
  2619. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2620. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  2621. // city
  2622. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  2623. // first last
  2624. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  2625. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  2626. // phone fax email
  2627. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  2628. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  2629. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  2630. // notes
  2631. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  2632. // price
  2633. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  2634. for (NSString *key in priceDic.allKeys) {
  2635. if ([key containsString:@"val_"]) {
  2636. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  2637. if ([dic[@"value"] isEqualToString:price_type]) {
  2638. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2639. [priceDic setValue:dic forKey:key];
  2640. }
  2641. }
  2642. }
  2643. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2644. // Sales Rep
  2645. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  2646. for (NSString *key in repDic.allKeys) {
  2647. if ([key containsString:@"val_"]) {
  2648. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  2649. NSString *value = dic[@"value"];
  2650. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  2651. if (code && [code isEqualToString:sales_rep]) {
  2652. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2653. [repDic setValue:dic forKey:key];
  2654. }
  2655. }
  2656. }
  2657. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  2658. [ret setValue:section_0 forKey:@"section_0"];
  2659. return [RAUtils dict2data:ret];
  2660. }
  2661. #pragma mark save contact
  2662. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  2663. {
  2664. return [self offline_saveContact:params update:YES];
  2665. }
  2666. #pragma mark category
  2667. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2668. if (ck) {
  2669. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  2670. for (NSString *key in res.allKeys) {
  2671. if (![key isEqualToString:@"count"]) {
  2672. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  2673. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  2674. if ([val[@"value"] isEqualToString:ck]) {
  2675. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2676. }
  2677. [res setValue:val forKey:key];
  2678. }
  2679. }
  2680. [dic setValue:res forKey:valueKey];
  2681. }
  2682. }
  2683. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  2684. {
  2685. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2686. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2687. NSString* category = [params valueForKey:@"category"];
  2688. if (!category || [category isEqualToString:@""]) {
  2689. category = @"%";
  2690. }
  2691. category = [self translateSingleQuote:category];
  2692. int limit = [[params valueForKey:@"limit"] intValue];
  2693. int offset = [[params valueForKey:@"offset"] intValue];
  2694. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2695. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  2696. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  2697. sqlite3 *db = [iSalesDB get_db];
  2698. // [iSalesDB AddExFunction:db];
  2699. int count;
  2700. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  2701. 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];
  2702. // {
  2703. // alert = SP;
  2704. // available = "Available Now";
  2705. // bestseller = No;
  2706. // category = 001;
  2707. // "custom_id" = 0;
  2708. // limit = 20;
  2709. // offset = 0;
  2710. // password = 123456;
  2711. // price = "Display All";
  2712. // "price_template" = 0;
  2713. // "sold_by_qty" = "Display All";
  2714. // "sort_by" = 0;
  2715. // user = EvanK;
  2716. // }
  2717. double price_min = 0;
  2718. double price_max = 0;
  2719. if ([params.allKeys containsObject:@"alert"]) {
  2720. // alert
  2721. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  2722. NSString *alert = params[@"alert"];
  2723. if ([alert isEqualToString:@"Display All"]) {
  2724. alert = [NSString stringWithFormat:@""];
  2725. } else {
  2726. alert = [self translateSingleQuote:alert];
  2727. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  2728. }
  2729. // available
  2730. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  2731. NSString *available = params[@"available"];
  2732. NSString *available_condition;
  2733. if ([available isEqualToString:@"Display All"]) {
  2734. available_condition = @"";
  2735. } else if ([available isEqualToString:@"Available Now"]) {
  2736. available_condition = @"and availability > 0";
  2737. } else {
  2738. available_condition = @"and availability == 0";
  2739. }
  2740. // best seller
  2741. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  2742. NSString *best_seller = @"";
  2743. NSString *order_best_seller = @"m.name asc";
  2744. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  2745. best_seller = @"and best_seller > 0";
  2746. order_best_seller = @"m.best_seller desc";
  2747. }
  2748. // price
  2749. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  2750. NSString *price = params[@"price"];
  2751. price_min = 0;
  2752. price_max = MAXFLOAT;
  2753. if (appDelegate.user) {
  2754. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  2755. NSMutableString *priceName = [NSMutableString string];
  2756. for (int i = 0; i < priceTypeArray.count; i++) {
  2757. NSString *pricetype = priceTypeArray[i];
  2758. pricetype = [self translateSingleQuote:pricetype];
  2759. if (i == 0) {
  2760. [priceName appendFormat:@"'%@'",pricetype];
  2761. } else {
  2762. [priceName appendFormat:@",'%@'",pricetype];
  2763. }
  2764. }
  2765. if ([price isEqualToString:@"Display All"]) {
  2766. price = [NSString stringWithFormat:@""];
  2767. } else if([price containsString:@"+"]){
  2768. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  2769. price_min = [price doubleValue];
  2770. price = [NSString stringWithFormat:@"and product_id in (select DISTINCT product_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  2771. } else {
  2772. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  2773. price_min = [[priceArray objectAtIndex:0] doubleValue];
  2774. price_max = [[priceArray objectAtIndex:1] doubleValue];
  2775. price = [NSString stringWithFormat:@"and product_id in (select DISTINCT product_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  2776. }
  2777. } else {
  2778. price = @"";
  2779. }
  2780. // sold_by_qty : Sold in quantities of %@
  2781. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  2782. NSString *qty = params[@"sold_by_qty"];
  2783. if ([qty isEqualToString:@"Display All"]) {
  2784. qty = @"";
  2785. } else {
  2786. qty = [self translateSingleQuote:qty];
  2787. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  2788. }
  2789. // cate
  2790. category = [self translateSingleQuote:category];
  2791. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  2792. // where bestseller > 0 order by bestseller desc
  2793. // sql query: alert availability(int) best_seller(int) price qty
  2794. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from model where %@ %@ %@ %@ %@ %@) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  2795. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",cateWhere,best_seller,alert,available_condition,qty,price];
  2796. }
  2797. count = [iSalesDB get_recordcount:db table:@"model" where:where];
  2798. if (!appDelegate.user) {
  2799. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  2800. }
  2801. [ret setValue:filter forKey:@"filter"];
  2802. DebugLog(@"offline_category sql:%@",sqlQuery);
  2803. sqlite3_stmt * statement;
  2804. [ret setValue:@"2" forKey:@"result"];
  2805. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2806. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2807. // int count=0;
  2808. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2809. {
  2810. int i=0;
  2811. while (sqlite3_step(statement) == SQLITE_ROW)
  2812. {
  2813. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2814. char *name = (char*)sqlite3_column_text(statement, 0);
  2815. if(name==nil)
  2816. name="";
  2817. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2818. char *description = (char*)sqlite3_column_text(statement, 1);
  2819. if(description==nil)
  2820. description="";
  2821. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2822. int product_id = sqlite3_column_int(statement, 2);
  2823. int wid = sqlite3_column_int(statement, 3);
  2824. int closeout = sqlite3_column_int(statement, 4);
  2825. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2826. if(wid==0)
  2827. [item setValue:@"false" forKey:@"wish_exists"];
  2828. else
  2829. [item setValue:@"true" forKey:@"wish_exists"];
  2830. if(closeout==0)
  2831. [item setValue:@"false" forKey:@"is_closeout"];
  2832. else
  2833. [item setValue:@"true" forKey:@"is_closeout"];
  2834. [item addEntriesFromDictionary:imgjson];
  2835. // [item setValue:nsurl forKey:@"img"];
  2836. [item setValue:nsname forKey:@"name"];
  2837. [item setValue:nsdescription forKey:@"description"];
  2838. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2839. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2840. i++;
  2841. }
  2842. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2843. [ret setObject:items forKey:@"items"];
  2844. sqlite3_finalize(statement);
  2845. } else {
  2846. DebugLog(@"nothing...");
  2847. }
  2848. DebugLog(@"count:%d",count);
  2849. [iSalesDB close_db:db];
  2850. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2851. return ret;
  2852. }
  2853. # pragma mark item search
  2854. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  2855. {
  2856. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2857. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  2858. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  2859. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  2860. // category
  2861. NSDictionary *category_menu = [self offline_category_menu];
  2862. [filter setValue:category_menu forKey:@"category"];
  2863. NSString* where= nil;
  2864. NSString* orderby= @"m.name";
  2865. if (!filterSearch) {
  2866. int covertype = [[params valueForKey:@"covertype"] intValue];
  2867. switch (covertype) {
  2868. case 0:
  2869. {
  2870. where=@"m.category like'%%#005#%%'";
  2871. break;
  2872. }
  2873. case 1:
  2874. {
  2875. where=@"m.alert like '%QS%'";
  2876. break;
  2877. }
  2878. case 2:
  2879. {
  2880. where=@"m.availability>0";
  2881. break;
  2882. }
  2883. case 3:
  2884. {
  2885. where=@"m.best_seller>0";
  2886. orderby=@"m.best_seller desc";
  2887. break;
  2888. }
  2889. default:
  2890. where=@"1=1";
  2891. break;
  2892. }
  2893. }
  2894. int limit = [[params valueForKey:@"limit"] intValue];
  2895. int offset = [[params valueForKey:@"offset"] intValue];
  2896. sqlite3 *db = [iSalesDB get_db];
  2897. // [iSalesDB AddExFunction:db];
  2898. int count;
  2899. NSString *sqlQuery = nil;
  2900. 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];
  2901. double price_min = 0;
  2902. double price_max = 0;
  2903. if (filterSearch) {
  2904. // alert
  2905. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  2906. NSString *alert = params[@"alert"];
  2907. if ([alert isEqualToString:@"Display All"]) {
  2908. alert = [NSString stringWithFormat:@""];
  2909. } else {
  2910. alert = [self translateSingleQuote:alert];
  2911. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  2912. }
  2913. // available
  2914. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  2915. NSString *available = params[@"available"];
  2916. NSString *available_condition;
  2917. if ([available isEqualToString:@"Display All"]) {
  2918. available_condition = @"";
  2919. } else if ([available isEqualToString:@"Available Now"]) {
  2920. available_condition = @"and availability > 0";
  2921. } else {
  2922. available_condition = @"and availability == 0";
  2923. }
  2924. // best seller
  2925. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  2926. NSString *best_seller = @"";
  2927. NSString *order_best_seller = @"m.name asc";
  2928. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  2929. best_seller = @"and best_seller > 0";
  2930. order_best_seller = @"m.best_seller desc";
  2931. }
  2932. // price
  2933. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  2934. NSString *price = params[@"price"];
  2935. price_min = 0;
  2936. price_max = MAXFLOAT;
  2937. if (appDelegate.user) {
  2938. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  2939. NSMutableString *priceName = [NSMutableString string];
  2940. for (int i = 0; i < priceTypeArray.count; i++) {
  2941. NSString *pricetype = priceTypeArray[i];
  2942. pricetype = [self translateSingleQuote:pricetype];
  2943. if (i == 0) {
  2944. [priceName appendFormat:@"'%@'",pricetype];
  2945. } else {
  2946. [priceName appendFormat:@",'%@'",pricetype];
  2947. }
  2948. }
  2949. if ([price isEqualToString:@"Display All"]) {
  2950. price = [NSString stringWithFormat:@""];
  2951. } else if([price containsString:@"+"]){
  2952. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  2953. price_min = [price doubleValue];
  2954. price = [NSString stringWithFormat:@"and product_id in (select DISTINCT product_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  2955. } else {
  2956. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  2957. price_min = [[priceArray objectAtIndex:0] doubleValue];
  2958. price_max = [[priceArray objectAtIndex:1] doubleValue];
  2959. price = [NSString stringWithFormat:@"and product_id in (select DISTINCT product_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  2960. }
  2961. } else {
  2962. price = @"";
  2963. }
  2964. // sold_by_qty : Sold in quantities of %@
  2965. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  2966. NSString *qty = params[@"sold_by_qty"];
  2967. if ([qty isEqualToString:@"Display All"]) {
  2968. qty = @"";
  2969. } else {
  2970. qty = [self translateSingleQuote:qty];
  2971. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  2972. }
  2973. // category
  2974. NSString *category_id = params[@"ctgId"];
  2975. NSMutableArray *cate_id_array = nil;
  2976. NSMutableString *cateWhere = [NSMutableString string];
  2977. if ([category_id isEqualToString:@""] || !category_id) {
  2978. [cateWhere appendString:@"1 = 1"];
  2979. } else {
  2980. if ([category_id containsString:@","]) {
  2981. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  2982. } else {
  2983. cate_id_array = [@[category_id] mutableCopy];
  2984. }
  2985. /*-----------*/
  2986. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  2987. for (int i = 0; i < cate_id_array.count; i++) {
  2988. for (NSString *key0 in cateDic.allKeys) {
  2989. if ([key0 containsString:@"category_"]) {
  2990. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  2991. for (NSString *key1 in category0.allKeys) {
  2992. if ([key1 containsString:@"category_"]) {
  2993. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  2994. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  2995. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  2996. cate_id_array[i] = [category1 objectForKey:@"id"];
  2997. if (i == 0) {
  2998. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  2999. } else {
  3000. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  3001. }
  3002. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3003. [category0 setValue:category1 forKey:key1];
  3004. [cateDic setValue:category0 forKey:key0];
  3005. }
  3006. }
  3007. }
  3008. }
  3009. }
  3010. }
  3011. [filter setValue:cateDic forKey:@"category"];
  3012. }
  3013. // where bestseller > 0 order by bestseller desc
  3014. // sql query: alert availability(int) best_seller(int) price qty
  3015. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from model where %@ %@ %@ %@ %@ %@) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  3016. // count
  3017. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",cateWhere,best_seller,alert,available_condition,qty,price];
  3018. }
  3019. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  3020. count = [iSalesDB get_recordcount:db table:@"model" where:where];
  3021. if (!appDelegate.user) {
  3022. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3023. }
  3024. [ret setValue:filter forKey:@"filter"];
  3025. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  3026. sqlite3_stmt * statement;
  3027. [ret setValue:@"2" forKey:@"result"];
  3028. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3029. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3030. // int count=0;
  3031. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3032. {
  3033. int i=0;
  3034. while (sqlite3_step(statement) == SQLITE_ROW)
  3035. {
  3036. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3037. char *name = (char*)sqlite3_column_text(statement, 0);
  3038. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3039. char *description = (char*)sqlite3_column_text(statement, 1);
  3040. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3041. int product_id = sqlite3_column_int(statement, 2);
  3042. int wid = sqlite3_column_int(statement, 3);
  3043. int closeout = sqlite3_column_int(statement, 4);
  3044. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3045. if(wid==0)
  3046. [item setValue:@"false" forKey:@"wish_exists"];
  3047. else
  3048. [item setValue:@"true" forKey:@"wish_exists"];
  3049. if(closeout==0)
  3050. [item setValue:@"false" forKey:@"is_closeout"];
  3051. else
  3052. [item setValue:@"true" forKey:@"is_closeout"];
  3053. [item addEntriesFromDictionary:imgjson];
  3054. // [item setValue:nsurl forKey:@"img"];
  3055. [item setValue:nsname forKey:@"fash_name"];
  3056. [item setValue:nsdescription forKey:@"description"];
  3057. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3058. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3059. i++;
  3060. }
  3061. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3062. [ret setObject:items forKey:@"items"];
  3063. sqlite3_finalize(statement);
  3064. }
  3065. [iSalesDB close_db:db];
  3066. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3067. return ret;
  3068. }
  3069. @end