OLDataProvider.m 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447
  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. //soId
  853. return [RAUtils dict2data:ret];
  854. }
  855. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  856. {
  857. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  858. int limit = [[params valueForKey:@"limit"] intValue];
  859. int offset = [[params valueForKey:@"offset"] intValue];
  860. NSString* keyword = [params valueForKey:@"keyWord"];
  861. NSString* where=@"1=1";
  862. if(keyword.length>0)
  863. 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];
  864. 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];
  865. sqlite3 *db = [iSalesDB get_db];
  866. sqlite3_stmt * statement;
  867. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  868. {
  869. int count=0;
  870. while (sqlite3_step(statement) == SQLITE_ROW)
  871. {
  872. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  873. int order_id = sqlite3_column_double(statement, 0);
  874. char *soid = (char*)sqlite3_column_text(statement, 1);
  875. if(soid==nil)
  876. soid= "";
  877. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  878. int status = sqlite3_column_double(statement, 2);
  879. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  880. if(sales_rep==nil)
  881. sales_rep= "";
  882. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  883. char *create_by = (char*)sqlite3_column_text(statement, 4);
  884. if(create_by==nil)
  885. create_by= "";
  886. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  887. char *company_name = (char*)sqlite3_column_text(statement, 5);
  888. if(company_name==nil)
  889. company_name= "";
  890. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  891. char *create_time = (char*)sqlite3_column_text(statement, 6);
  892. if(create_time==nil)
  893. create_time= "";
  894. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  895. double total_price = sqlite3_column_double(statement, 7);
  896. NSString* nsstatus=@"";
  897. switch (status) {
  898. case 0:
  899. {
  900. nsstatus=@"Temp Order";
  901. break;
  902. }
  903. case 1:
  904. {
  905. nsstatus=@"Saved Order";
  906. break;
  907. }
  908. case 10:
  909. {
  910. nsstatus=@"Quote Submitted";
  911. break;
  912. }
  913. case 11:
  914. {
  915. nsstatus=@"Sales Order Submitted";
  916. break;
  917. }
  918. case 12:
  919. {
  920. nsstatus=@"Processing";
  921. break;
  922. }
  923. case 13:
  924. {
  925. nsstatus=@"Shipped";
  926. break;
  927. }
  928. case 14:
  929. {
  930. nsstatus=@"Closed";
  931. break;
  932. }
  933. case 15:
  934. {
  935. nsstatus=@"Cancelled";
  936. break;
  937. }
  938. default:
  939. break;
  940. }
  941. item[@"sales_rep"]= nssales_rep;
  942. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  943. item[@"order_status"]= nsstatus;
  944. item[@"so#"]= nssoid;
  945. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  946. item[@"create_by"]= nscreate_by;
  947. item[@"customer_name"]= nscompany_name;
  948. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  949. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  950. item[@"purchase_time"]= nscreate_time;
  951. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  952. count++;
  953. }
  954. ret[@"count"]= [NSNumber numberWithInt:count];
  955. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  956. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  957. ret[@"result"]= [NSNumber numberWithInt:2];
  958. sqlite3_finalize(statement);
  959. }
  960. [iSalesDB close_db:db];
  961. return [RAUtils dict2data:ret];
  962. }
  963. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  964. {
  965. int orderId = [params[@"orderId"] intValue];
  966. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  967. // [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"debug_category_filter" ofType:@"json" ]];
  968. // NSString* content = [NSString stringWithContentsOfFile:thepath encoding:NSUTF8StringEncoding error:nil];
  969. 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 ];
  970. sqlite3 *db = [iSalesDB get_db];
  971. sqlite3_stmt * statement;
  972. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  973. {
  974. if (sqlite3_step(statement) == SQLITE_ROW)
  975. {
  976. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  977. int order_id = sqlite3_column_int(statement, 0);
  978. char *soid = (char*)sqlite3_column_text(statement, 1);
  979. if(soid==nil)
  980. soid= "";
  981. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  982. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  983. if(poNumber==nil)
  984. poNumber= "";
  985. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  986. char *create_time = (char*)sqlite3_column_text(statement, 3);
  987. if(create_time==nil)
  988. create_time= "";
  989. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  990. int status = sqlite3_column_int(statement, 4);
  991. NSString* nsstatus=@"";
  992. switch (status) {
  993. case 0:
  994. {
  995. nsstatus=@"Temp Order";
  996. break;
  997. }
  998. case 1:
  999. {
  1000. nsstatus=@"Saved Order";
  1001. break;
  1002. }
  1003. case 10:
  1004. {
  1005. nsstatus=@"Quote Submitted";
  1006. break;
  1007. }
  1008. case 11:
  1009. {
  1010. nsstatus=@"Sales Order Submitted";
  1011. break;
  1012. }
  1013. case 12:
  1014. {
  1015. nsstatus=@"Processing";
  1016. break;
  1017. }
  1018. case 13:
  1019. {
  1020. nsstatus=@"Shipped";
  1021. break;
  1022. }
  1023. case 14:
  1024. {
  1025. nsstatus=@"Closed";
  1026. break;
  1027. }
  1028. case 15:
  1029. {
  1030. nsstatus=@"Cancelled";
  1031. break;
  1032. }
  1033. default:
  1034. break;
  1035. }
  1036. double TotalCuft = sqlite3_column_double(statement, 5);
  1037. double TotalWeight = sqlite3_column_double(statement, 6);
  1038. double TotalCarton = sqlite3_column_double(statement, 7);
  1039. char *company_name = (char*)sqlite3_column_text(statement, 8);
  1040. if(company_name==nil)
  1041. company_name= "";
  1042. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  1043. char *customer_contact = (char*)sqlite3_column_text(statement, 9);
  1044. if(customer_contact==nil)
  1045. customer_contact= "";
  1046. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  1047. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1048. if(addr_1==nil)
  1049. addr_1="";
  1050. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1051. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1052. if(addr_2==nil)
  1053. addr_2="";
  1054. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1055. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1056. if(addr_3==nil)
  1057. addr_3="";
  1058. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1059. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1060. if(addr_4==nil)
  1061. addr_4="";
  1062. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1063. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  1064. [arr_addr addObject:nsaddr_1];
  1065. [arr_addr addObject:nsaddr_2];
  1066. [arr_addr addObject:nsaddr_3];
  1067. [arr_addr addObject:nsaddr_4];
  1068. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  1069. char *logist = (char*)sqlite3_column_text(statement, 14);
  1070. if(logist==nil)
  1071. logist= "";
  1072. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  1073. double shipping = sqlite3_column_double(statement, 15);
  1074. double lift_gate = sqlite3_column_double(statement, 16);
  1075. char *general_notes = (char*)sqlite3_column_text(statement, 17);
  1076. if(general_notes==nil)
  1077. general_notes= "";
  1078. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  1079. char *internal_notes = (char*)sqlite3_column_text(statement, 18);
  1080. if(internal_notes==nil)
  1081. internal_notes= "";
  1082. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  1083. char *payment_type = (char*)sqlite3_column_text(statement, 19);
  1084. if(payment_type==nil)
  1085. payment_type= "";
  1086. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  1087. int have_lift_gate = sqlite3_column_int(statement, 20);
  1088. NSError *error = nil;
  1089. NSString* orderinfo = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"orderinfo" ofType:@"html"] encoding:NSUTF8StringEncoding error:&error];
  1090. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SO_PLACEHOLDER" withString:nssoid];
  1091. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"PO_PLACEHOLDER" withString:nspoNumber];
  1092. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ORDERDATE_PLACEHOLDER" withString:nscreate_time];
  1093. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ORDERSTATUS_PLACEHOLDER" withString:nsstatus];
  1094. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CUFT_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalCuft]];
  1095. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"WEIGHT_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalWeight]];
  1096. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CARTON_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",TotalCarton]];
  1097. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"COMPANY_PLACEHOLDER" withString:nscompany_name];
  1098. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"CONTACT_PLACEHOLDER" withString:nscustomer_contact];
  1099. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"ADDRESS_PLACEHOLDER" withString:customer_address];
  1100. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SHIPPINGMETHOD_PLACEHOLDER" withString:nslogist];
  1101. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"SHIPPING_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",shipping]];
  1102. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"LIFTGATE_PLACEHOLDER" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  1103. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"GENERALNOTES_PLACEHOLDER" withString:nsgeneral_notes];
  1104. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"INTERNALNOTES_PLACEHOLDER" withString:nsinternal_notes];
  1105. NSString *payment = nil;
  1106. if([nspayment_type isEqualToString:@"Credit Card"])
  1107. {
  1108. payment=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"creditcardpayment" ofType:@"html"] encoding:NSUTF8StringEncoding error:&error];
  1109. //payment=[payment stringByReplacingOccurrencesOfString:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1110. }
  1111. else
  1112. {
  1113. payment=[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"normalpayment" ofType:@"html"] encoding:NSUTF8StringEncoding error:&error];
  1114. payment=[payment stringByReplacingOccurrencesOfString:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1115. }
  1116. orderinfo=[orderinfo stringByReplacingOccurrencesOfString:@"PAYMENT_PLACEHOLDER" withString:payment];
  1117. ret[@"order_info"]= orderinfo;
  1118. ret[@"result"]= [NSNumber numberWithInt:2];
  1119. ret[@"result"]= [NSNumber numberWithInt:2];
  1120. }
  1121. sqlite3_finalize(statement);
  1122. }
  1123. [iSalesDB close_db:db];
  1124. return [RAUtils dict2data:ret];
  1125. }
  1126. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1127. {
  1128. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1129. NSString* orderCode = [params valueForKey:@"orderCode"];
  1130. sqlite3 *db = [iSalesDB get_db];
  1131. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1132. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1133. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1134. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1135. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1136. [iSalesDB close_db:db];
  1137. return [RAUtils dict2data:ret];
  1138. }
  1139. +(NSData*) offline_login :(NSMutableDictionary *) params
  1140. {
  1141. NSString* user = [params valueForKey:@"user"];
  1142. NSString* password = [params valueForKey:@"password"];
  1143. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1144. sqlite3 *db = [iSalesDB get_db];
  1145. 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"]];
  1146. DebugLog(@"offline_login sql:%@",sqlQuery);
  1147. sqlite3_stmt * statement;
  1148. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1149. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1150. {
  1151. if (sqlite3_step(statement) == SQLITE_ROW)
  1152. {
  1153. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1154. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1155. int can_show_price = sqlite3_column_int(statement, 0);
  1156. int can_see_price = sqlite3_column_int(statement, 1);
  1157. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1158. if(contact_id==nil)
  1159. contact_id="";
  1160. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1161. int user_type = sqlite3_column_int(statement, 3);
  1162. int can_cancel_order = sqlite3_column_int(statement, 4);
  1163. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1164. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1165. int can_delete_order = sqlite3_column_int(statement, 7);
  1166. int can_submit_order = sqlite3_column_int(statement, 8);
  1167. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1168. int can_create_order = sqlite3_column_int(statement, 10);
  1169. char *mode = (char*)sqlite3_column_text(statement, 11);
  1170. if(mode==nil)
  1171. mode="";
  1172. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1173. char *username = (char*)sqlite3_column_text(statement, 12);
  1174. if(username==nil)
  1175. username="";
  1176. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1177. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1178. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1179. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1180. [header setValue:nscontact_id forKey:@"contact_id"];
  1181. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1182. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1183. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1184. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1185. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1186. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1187. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1188. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1189. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1190. [header setValue:nsusername forKey:@"username"];
  1191. NSError* error=nil;
  1192. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1193. [header setValue:statusFilter forKey:@"statusFilter"];
  1194. [ret setObject:header forKey:@"header"];
  1195. [ret setValue:nsmode forKey:@"mode"];
  1196. }
  1197. sqlite3_finalize(statement);
  1198. }
  1199. [iSalesDB close_db:db];
  1200. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1201. return [RAUtils dict2data:ret];
  1202. }
  1203. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1204. {
  1205. NSString* contactId = [params valueForKey:@"contactId"];
  1206. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1207. sqlite3 *db = [iSalesDB get_db];
  1208. NSString *sqlQuery = nil;
  1209. {
  1210. 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];
  1211. }
  1212. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1213. sqlite3_stmt * statement;
  1214. [ret setValue:@"2" forKey:@"result"];
  1215. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1216. {
  1217. //int i = 0;
  1218. if (sqlite3_step(statement) == SQLITE_ROW)
  1219. {
  1220. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1221. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1222. // int editable = sqlite3_column_int(statement, 0);
  1223. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1224. NSString *nscompany_name =nil;
  1225. if(company_name==nil)
  1226. nscompany_name=@"";
  1227. else
  1228. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1229. char *country = (char*)sqlite3_column_text(statement, 2);
  1230. if(country==nil)
  1231. country="";
  1232. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1233. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1234. // if(addr==nil)
  1235. // addr="";
  1236. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1237. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1238. if(zipcode==nil)
  1239. zipcode="";
  1240. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1241. char *state = (char*)sqlite3_column_text(statement, 5);
  1242. if(state==nil)
  1243. state="";
  1244. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1245. char *city = (char*)sqlite3_column_text(statement, 6);
  1246. if(city==nil)
  1247. city="";
  1248. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1249. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1250. // NSString *nscontact_name = nil;
  1251. // if(contact_name==nil)
  1252. // nscontact_name=@"";
  1253. // else
  1254. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1255. char *phone = (char*)sqlite3_column_text(statement, 8);
  1256. NSString *nsphone = nil;
  1257. if(phone==nil)
  1258. nsphone=@"";
  1259. else
  1260. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1261. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1262. if(contact_id==nil)
  1263. contact_id="";
  1264. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1265. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1266. if(addr_1==nil)
  1267. addr_1="";
  1268. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1269. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1270. if(addr_2==nil)
  1271. addr_2="";
  1272. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1273. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1274. if(addr_3==nil)
  1275. addr_3="";
  1276. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1277. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1278. if(addr_4==nil)
  1279. addr_4="";
  1280. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1281. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1282. if(first_name==nil)
  1283. first_name="";
  1284. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1285. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1286. if(last_name==nil)
  1287. last_name="";
  1288. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1289. char *fax = (char*)sqlite3_column_text(statement, 16);
  1290. NSString *nsfax = nil;
  1291. if(fax==nil)
  1292. nsfax=@"";
  1293. else
  1294. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1295. char *email = (char*)sqlite3_column_text(statement, 17);
  1296. NSString *nsemail = nil;
  1297. if(email==nil)
  1298. nsemail=@"";
  1299. else
  1300. nsemail= [[NSString alloc]initWithUTF8String:email];
  1301. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1302. NSString *nsimg_0 = nil;
  1303. if(img_0==nil)
  1304. nsimg_0=@"";
  1305. else
  1306. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1307. [self copy_bcardImg:nsimg_0];
  1308. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1309. NSString *nsimg_1 = nil;
  1310. if(img_1==nil)
  1311. nsimg_1=@"";
  1312. else
  1313. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1314. [self copy_bcardImg:nsimg_1];
  1315. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1316. NSString *nsimg_2 = nil;
  1317. if(img_2==nil)
  1318. nsimg_2=@"";
  1319. else
  1320. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1321. [self copy_bcardImg:nsimg_2];
  1322. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1323. NSString *nsprice_type = nil;
  1324. if(price_type==nil)
  1325. nsprice_type=@"";
  1326. else
  1327. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1328. char *notes = (char*)sqlite3_column_text(statement, 22);
  1329. NSString *nsnotes = nil;
  1330. if(notes==nil)
  1331. nsnotes=@"";
  1332. else
  1333. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1334. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1335. NSString *nssalesrep = nil;
  1336. if(salesrep==nil)
  1337. nssalesrep=@"";
  1338. else
  1339. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1340. {
  1341. // decrypt
  1342. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1343. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1344. nsphone=[AESCrypt fastdecrypt:nsphone];
  1345. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1346. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1347. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1348. }
  1349. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1350. [arr_name addObject:nsfirst_name];
  1351. [arr_name addObject:nslast_name];
  1352. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1353. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1354. [arr_ext addObject:nsaddr_1];
  1355. [arr_ext addObject:nsaddr_2];
  1356. [arr_ext addObject:nsaddr_3];
  1357. [arr_ext addObject:nsaddr_4];
  1358. [arr_ext addObject:@"\r\n"];
  1359. [arr_ext addObject:nscity];
  1360. [arr_ext addObject:nsstate];
  1361. [arr_ext addObject:nszipcode];
  1362. [arr_ext addObject:nscountry];
  1363. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1364. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1365. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1366. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1367. [item setValue:nscountry forKey:@"customer_country"];
  1368. [item setValue:nsphone forKey:@"customer_phone"];
  1369. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1370. [item setValue:nscompany_name forKey:@"customer_name"];
  1371. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1372. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1373. [item setValue:nsext forKey:@"customer_contact_ext"];
  1374. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1375. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1376. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1377. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1378. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1379. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1380. [item setValue:nslast_name forKey:@"customer_last_name"];
  1381. [item setValue:nscity forKey:@"customer_city"];
  1382. [item setValue:nsstate forKey:@"customer_state"];
  1383. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1384. [item setValue:contactId forKey:@"customer_cid"];
  1385. [item setValue:nscontact_name forKey:@"customer_contact"];
  1386. [item setValue:nsfax forKey:@"customer_fax"];
  1387. [item setValue:nsemail forKey:@"customer_email"];
  1388. [ret setObject:item forKey:@"customerInfo"];
  1389. // i++;
  1390. }
  1391. UIApplication * app = [UIApplication sharedApplication];
  1392. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1393. [ret setValue:appDelegate.mode forKey:@"mode"];
  1394. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1395. sqlite3_finalize(statement);
  1396. }
  1397. [iSalesDB close_db:db];
  1398. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1399. return ret;
  1400. }
  1401. + (bool) copy_bcardImg:(NSString*) filename
  1402. {
  1403. if(filename.length==0)
  1404. return false;
  1405. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1406. bool ret=false;
  1407. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1408. NSString *cachefolder = [paths objectAtIndex:0];
  1409. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1410. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1411. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1412. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1413. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1414. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1415. //
  1416. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1417. NSFileManager* fileManager = [NSFileManager defaultManager];
  1418. BOOL bdir=NO;
  1419. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1420. {
  1421. NSError *error = nil;
  1422. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1423. {
  1424. ret=false;
  1425. }
  1426. else
  1427. {
  1428. ret=true;
  1429. }
  1430. // NSError *error = nil;
  1431. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1432. //
  1433. // if(!bsuccess)
  1434. // {
  1435. // DebugLog(@"Create offline_createimg folder failed");
  1436. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1437. // return [RAUtils dict2data:ret];
  1438. // }
  1439. // if(bsuccess)
  1440. // {
  1441. // sqlite3 *db = [self get_db];
  1442. //
  1443. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1444. // [iSalesDB close_db:db];
  1445. // }
  1446. }
  1447. return ret;
  1448. //
  1449. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1450. // if(bsuccess)
  1451. // {
  1452. // NSError *error = nil;
  1453. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1454. // {
  1455. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1456. // }
  1457. // else
  1458. // {
  1459. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1460. //
  1461. // ret[@"img_url_aname"]=filename;
  1462. // ret[@"img_url"]=savedImagePath;
  1463. // }
  1464. // }
  1465. }
  1466. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1467. {
  1468. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1469. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1470. NSString *cachefolder = [paths objectAtIndex:0];
  1471. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1472. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1473. NSFileManager* fileManager = [NSFileManager defaultManager];
  1474. BOOL bdir=YES;
  1475. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1476. {
  1477. NSError *error = nil;
  1478. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1479. if(!bsuccess)
  1480. {
  1481. DebugLog(@"Create offline_createimg folder failed");
  1482. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1483. return [RAUtils dict2data:ret];
  1484. }
  1485. // if(bsuccess)
  1486. // {
  1487. // sqlite3 *db = [self get_db];
  1488. //
  1489. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1490. // [iSalesDB close_db:db];
  1491. // }
  1492. }
  1493. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1494. //JEPG格式
  1495. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1496. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1497. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1498. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1499. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1500. if(bsuccess)
  1501. {
  1502. NSError *error = nil;
  1503. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1504. {
  1505. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1506. }
  1507. else
  1508. {
  1509. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1510. ret[@"img_url_aname"]=filename;
  1511. ret[@"img_url"]=filename;
  1512. }
  1513. }
  1514. else
  1515. {
  1516. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1517. }
  1518. return [RAUtils dict2data:ret];
  1519. }
  1520. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  1521. {
  1522. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1523. category = [category substringToIndex:3];
  1524. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1525. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1526. params[@"category"]= category;
  1527. ret[@"params"]= params;
  1528. [ret setValue:@"detail" forKey:@"target"];
  1529. [ret setValue:@"popup" forKey:@"action"];
  1530. [ret setValue:@"content" forKey:@"type"];
  1531. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1532. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1533. [ret setValue:@"true" forKey:@"single_row"];
  1534. [ret setValue:@"true" forKey:@"partial_refresh"];
  1535. // sqlite3 *db = [iSalesDB get_db];
  1536. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1537. 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];
  1538. sqlite3_stmt * statement;
  1539. int count = 0;
  1540. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1541. // int count=0;
  1542. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1543. {
  1544. int i=0;
  1545. while (sqlite3_step(statement) == SQLITE_ROW)
  1546. {
  1547. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1548. // char *name = (char*)sqlite3_column_text(statement, 1);
  1549. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1550. char *name = (char*)sqlite3_column_text(statement, 0);
  1551. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1552. int product_id = sqlite3_column_int(statement, 1);
  1553. char *url = (char*)sqlite3_column_text(statement, 2);
  1554. if(url==nil)
  1555. url="";
  1556. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1557. [item setValue:nsurl forKey:@"picture_path"];
  1558. [item setValue:nsname forKey:@"fash_name"];
  1559. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1560. [item setValue:category forKey:@"category"];
  1561. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1562. i++;
  1563. }
  1564. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1565. sqlite3_finalize(statement);
  1566. }
  1567. NSLog(@"count:%d",count);
  1568. // [iSalesDB close_db:db];
  1569. return ret;
  1570. }
  1571. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1572. {
  1573. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1574. [ret setValue:key forKey:@"key"];
  1575. [ret setValue:value forKey:@"val"];
  1576. return ret;
  1577. }
  1578. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  1579. {
  1580. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1581. [ret setValue:@"0" forKey:@"img_count"];
  1582. // sqlite3 *db = [iSalesDB get_db];
  1583. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from model where product_id=%d ;",field,product_id];
  1584. sqlite3_stmt * statement;
  1585. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1586. {
  1587. int i=0;
  1588. if (sqlite3_step(statement) == SQLITE_ROW)
  1589. {
  1590. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1591. // char *name = (char*)sqlite3_column_text(statement, 1);
  1592. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1593. char *value = (char*)sqlite3_column_text(statement, 0);
  1594. if(value==nil)
  1595. value="";
  1596. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1597. char *key = (char*)sqlite3_column_text(statement, 1);
  1598. if(key==nil)
  1599. key="";
  1600. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1601. [item setValue:nsvalue forKey:@"val"];
  1602. [item setValue:nskey forKey:@"key"];
  1603. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1604. [ret setValue:@"1" forKey:@"count"];
  1605. i++;
  1606. }
  1607. sqlite3_finalize(statement);
  1608. }
  1609. // [iSalesDB close_db:db];
  1610. return ret;
  1611. }
  1612. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  1613. {
  1614. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1615. [ret setValue:@"0" forKey:@"count"];
  1616. // sqlite3 *db = [iSalesDB get_db];
  1617. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from model where product_group='%@' order by name;",field,product_group];
  1618. sqlite3_stmt * statement;
  1619. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1620. {
  1621. int i=0;
  1622. while (sqlite3_step(statement) == SQLITE_ROW)
  1623. {
  1624. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1625. // char *name = (char*)sqlite3_column_text(statement, 1);
  1626. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1627. char *value = (char*)sqlite3_column_text(statement, 0);
  1628. if(value==nil)
  1629. value="";
  1630. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1631. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1632. if(selector_display==nil)
  1633. selector_display="";
  1634. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1635. int product_id = sqlite3_column_int(statement, 2);
  1636. char *category = (char*)sqlite3_column_text(statement, 3);
  1637. if(category==nil)
  1638. category="";
  1639. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1640. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  1641. [item setValue:nsvalue forKey:@"title"];
  1642. [item setValue:url forKey:@"pic_url"];
  1643. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1644. [params setValue:@"2" forKey:@"count"];
  1645. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1646. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1647. [param0 setValue:@"product_id" forKey:@"name"];
  1648. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1649. [param1 setValue:nscategory forKey:@"val"];
  1650. [param1 setValue:@"category" forKey:@"name"];
  1651. [params setObject:param0 forKey:@"param_0"];
  1652. [params setObject:param1 forKey:@"param_1"];
  1653. [item setObject:params forKey:@"params"];
  1654. [ret setValue:nsselector_display forKey:@"name"];
  1655. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1656. i++;
  1657. }
  1658. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1659. [ret setValue:@"switch" forKey:@"action"];
  1660. sqlite3_finalize(statement);
  1661. }
  1662. // [iSalesDB close_db:db];
  1663. return ret;
  1664. }
  1665. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  1666. {
  1667. // model 在 category search 显示的图片。
  1668. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1669. // sqlite3 *db = [iSalesDB get_db];
  1670. 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];
  1671. sqlite3_stmt * statement;
  1672. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1673. {
  1674. while (sqlite3_step(statement) == SQLITE_ROW)
  1675. {
  1676. char *url = (char*)sqlite3_column_text(statement, 0);
  1677. if(url==nil)
  1678. url="";
  1679. int type = sqlite3_column_int(statement, 1);
  1680. if(type==0)
  1681. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1682. else
  1683. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1684. }
  1685. sqlite3_finalize(statement);
  1686. }
  1687. // [iSalesDB close_db:db];
  1688. return ret;
  1689. }
  1690. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  1691. {
  1692. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1693. [ret setValue:@"0" forKey:@"img_count"];
  1694. // sqlite3 *db = [iSalesDB get_db];
  1695. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  1696. sqlite3_stmt * statement;
  1697. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1698. {
  1699. int i=0;
  1700. while (sqlite3_step(statement) == SQLITE_ROW)
  1701. {
  1702. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1703. // char *name = (char*)sqlite3_column_text(statement, 1);
  1704. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1705. char *url = (char*)sqlite3_column_text(statement, 0);
  1706. if(url==nil)
  1707. url="";
  1708. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1709. [item setValue:nsurl forKey:@"s"];
  1710. [item setValue:nsurl forKey:@"l"];
  1711. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  1712. i++;
  1713. }
  1714. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  1715. sqlite3_finalize(statement);
  1716. }
  1717. // [iSalesDB close_db:db];
  1718. return ret;
  1719. }
  1720. #pragma mark - Jack
  1721. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  1722. // "val_227" : {
  1723. // "check" : 1,
  1724. // "value" : "US United States",
  1725. // "value_id" : "228"
  1726. // },
  1727. if (!countryCode) {
  1728. countryCode = @"US";
  1729. }
  1730. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  1731. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  1732. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  1733. int code_id = sqlite3_column_int(stmt, 3); // id
  1734. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1735. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  1736. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  1737. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1738. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  1739. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1740. }
  1741. long n = *count;
  1742. *count = n + 1;
  1743. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  1744. [container setValue:countryDic forKey:key];
  1745. }] mutableCopy];
  1746. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1747. return ret;
  1748. }
  1749. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  1750. countryCode = [self translateSingleQuote:countryCode];
  1751. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  1752. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1753. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  1754. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  1755. if (name == NULL) {
  1756. name = "";
  1757. }
  1758. if (code == NULL) {
  1759. code = "";
  1760. }
  1761. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1762. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1763. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1764. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1765. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  1766. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1767. }
  1768. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1769. [container setValue:stateDic forKey:key];
  1770. }] mutableCopy];
  1771. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1772. return ret;
  1773. }
  1774. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  1775. codeId = [self translateSingleQuote:codeId];
  1776. 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];
  1777. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1778. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  1779. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  1780. if (name == NULL) {
  1781. name = "";
  1782. }
  1783. if (code == NULL) {
  1784. code = "";
  1785. }
  1786. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  1787. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1788. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  1789. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1790. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  1791. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1792. }
  1793. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  1794. [container setValue:stateDic forKey:key];
  1795. }] mutableCopy];
  1796. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1797. return ret;
  1798. }
  1799. + (NSDictionary *)offline_getPrice {
  1800. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  1801. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1802. char *name = (char *) sqlite3_column_text(stmt, 1);
  1803. int type = sqlite3_column_int(stmt, 2);
  1804. int orderBy = sqlite3_column_int(stmt, 3);
  1805. if (name == NULL) {
  1806. name = "";
  1807. }
  1808. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  1809. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  1810. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  1811. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  1812. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  1813. if (orderBy == 0) {
  1814. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1815. }
  1816. [container setValue:priceDic forKey:key];
  1817. }] mutableCopy];
  1818. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1819. return ret;
  1820. }
  1821. + (NSDictionary *)offline_getSalesRep {
  1822. // 首先从offline_login表中取出sales_code
  1823. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  1824. NSString *user = app.user;
  1825. user = [self translateSingleQuote:user];
  1826. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  1827. __block NSString *user_code = @"";
  1828. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1829. char *code = (char *)sqlite3_column_text(stmt, 0);
  1830. if (code == NULL) {
  1831. code = "";
  1832. }
  1833. user_code = [NSString stringWithUTF8String:code];
  1834. }];
  1835. // 再取所有salesRep
  1836. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  1837. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1838. // 1 name 2 code 3 salesrep_id
  1839. char *name = (char *)sqlite3_column_text(stmt, 1);
  1840. char *code = (char *)sqlite3_column_text(stmt, 2);
  1841. int salesrep_id = sqlite3_column_int(stmt, 3);
  1842. if (name == NULL) {
  1843. name = "";
  1844. }
  1845. if (code == NULL) {
  1846. code = "";
  1847. }
  1848. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  1849. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  1850. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  1851. // 比较code 相等则check
  1852. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  1853. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  1854. }
  1855. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  1856. }] mutableCopy];
  1857. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  1858. return ret;
  1859. }
  1860. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  1861. zipcode = [self translateSingleQuote:zipcode];
  1862. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  1863. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1864. char *country = (char *)sqlite3_column_text(stmt, 0);
  1865. char *state = (char *)sqlite3_column_text(stmt, 1);
  1866. char *city = (char *)sqlite3_column_text(stmt, 2);
  1867. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  1868. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  1869. if (country == NULL) {
  1870. country = "";
  1871. }
  1872. if (state == NULL) {
  1873. state = "";
  1874. }
  1875. if (city == NULL) {
  1876. city = "";
  1877. }
  1878. if (country_code == NULL) {
  1879. country_code = "";
  1880. }
  1881. if (state_code == NULL) {
  1882. state_code = "";
  1883. }
  1884. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  1885. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  1886. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  1887. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  1888. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  1889. }] mutableCopy];
  1890. return ret;
  1891. }
  1892. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  1893. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  1894. [item setValue:value forKey:valueKey];
  1895. [dic setValue:item forKey:itemKey];
  1896. }
  1897. + (NSString *)countryCodeByid:(NSString *)code_id {
  1898. NSString *ret = nil;
  1899. code_id = [self translateSingleQuote:code_id];
  1900. sqlite3 *db = [iSalesDB get_db];
  1901. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  1902. sqlite3_stmt * statement;
  1903. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1904. while (sqlite3_step(statement) == SQLITE_ROW) {
  1905. char *code = (char *)sqlite3_column_text(statement, 0);
  1906. if (code == NULL) {
  1907. code = "";
  1908. }
  1909. ret = [NSString stringWithUTF8String:code];
  1910. }
  1911. sqlite3_finalize(statement);
  1912. }
  1913. [iSalesDB close_db:db];
  1914. return ret;
  1915. }
  1916. + (NSString *)countryCodeIdByCode:(NSString *)code {
  1917. NSString *ret = nil;
  1918. code = [self translateSingleQuote:code];
  1919. sqlite3 *db = [iSalesDB get_db];
  1920. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  1921. sqlite3_stmt * statement;
  1922. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1923. while (sqlite3_step(statement) == SQLITE_ROW) {
  1924. char *_id = (char *)sqlite3_column_text(statement, 0);
  1925. if (_id == NULL) {
  1926. _id = "";
  1927. }
  1928. ret = [NSString stringWithFormat:@"%s",_id];
  1929. }
  1930. sqlite3_finalize(statement);
  1931. }
  1932. [iSalesDB close_db:db];
  1933. return ret;
  1934. }
  1935. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  1936. NSString *name = nil;
  1937. codeId = [self translateSingleQuote:codeId];
  1938. sqlite3 *db = [iSalesDB get_db];
  1939. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  1940. sqlite3_stmt * statement;
  1941. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1942. while (sqlite3_step(statement) == SQLITE_ROW) {
  1943. char *value = (char *)sqlite3_column_text(statement, 0);
  1944. if (value == NULL) {
  1945. value = "";
  1946. }
  1947. name = [NSString stringWithUTF8String:value];
  1948. }
  1949. sqlite3_finalize(statement);
  1950. }
  1951. [iSalesDB close_db:db];
  1952. return name;
  1953. }
  1954. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  1955. NSString *ret = nil;
  1956. priceId = [self translateSingleQuote:priceId];
  1957. sqlite3 *db = [iSalesDB get_db];
  1958. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  1959. sqlite3_stmt * statement;
  1960. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1961. while (sqlite3_step(statement) == SQLITE_ROW) {
  1962. char *name = (char *)sqlite3_column_text(statement, 0);
  1963. if (name == NULL) {
  1964. name = "";
  1965. }
  1966. ret = [NSString stringWithUTF8String:name];
  1967. }
  1968. sqlite3_finalize(statement);
  1969. }
  1970. [iSalesDB close_db:db];
  1971. return ret;
  1972. }
  1973. + (NSString *)salesRepCodeById:(NSString *)_id {
  1974. NSString *ret = nil;
  1975. _id = [self translateSingleQuote:_id];
  1976. sqlite3 *db = [iSalesDB get_db];
  1977. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  1978. sqlite3_stmt * statement;
  1979. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  1980. while (sqlite3_step(statement) == SQLITE_ROW) {
  1981. char *rep = (char *)sqlite3_column_text(statement, 0);
  1982. if (rep == NULL) {
  1983. rep = "";
  1984. }
  1985. ret = [NSString stringWithUTF8String:rep];
  1986. }
  1987. sqlite3_finalize(statement);
  1988. }
  1989. [iSalesDB close_db:db];
  1990. return ret;
  1991. }
  1992. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  1993. char *tx = (char *)sqlite3_column_text(stmt, col);
  1994. if (tx == NULL) {
  1995. tx = "";
  1996. }
  1997. NSString *text = [NSString stringWithFormat:@"%s",tx];
  1998. if (!text) {
  1999. text = @"";
  2000. }
  2001. return text;
  2002. }
  2003. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  2004. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2005. NSData *data = [NSData dataWithContentsOfFile:path];
  2006. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2007. return ret;
  2008. }
  2009. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  2010. return [[dic objectForKey:key] mutableCopy];
  2011. }
  2012. + (NSString *)translateSingleQuote:(NSString *)string {
  2013. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2014. }
  2015. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  2016. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  2017. }
  2018. #pragma mark contact list
  2019. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  2020. {
  2021. // contactType = "Sales_Order_Customer";
  2022. // limit = 25;
  2023. // offset = 0;
  2024. // password = 123456;
  2025. // "price_name" = 16;
  2026. // user = EvanK;
  2027. sqlite3 *db = [iSalesDB get_db];
  2028. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  2029. if (contactType) {
  2030. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  2031. } else {
  2032. contactType = @"1 = 1";
  2033. }
  2034. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  2035. DebugLog(@"offline contact list keyword: %@",keyword);
  2036. // advanced search
  2037. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  2038. if (contact_name) {
  2039. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2040. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  2041. } else {
  2042. contact_name = @"";
  2043. }
  2044. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  2045. if (customer_phone) {
  2046. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2047. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  2048. } else {
  2049. customer_phone = @"";
  2050. }
  2051. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  2052. if (customer_fax) {
  2053. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2054. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  2055. } else {
  2056. customer_fax = @"";
  2057. }
  2058. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  2059. if (customer_zipcode) {
  2060. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2061. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  2062. } else {
  2063. customer_zipcode = @"";
  2064. }
  2065. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  2066. if (customer_sales_rep) {
  2067. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2068. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  2069. } else {
  2070. customer_sales_rep = @"";
  2071. }
  2072. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  2073. if (customer_state) {
  2074. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2075. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  2076. } else {
  2077. customer_state = @"";
  2078. }
  2079. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  2080. if (customer_name) {
  2081. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2082. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  2083. } else {
  2084. customer_name = @"";
  2085. }
  2086. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  2087. if (customer_country) {
  2088. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2089. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  2090. } else {
  2091. customer_country = @"";
  2092. }
  2093. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  2094. if (customer_cid) {
  2095. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2096. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  2097. } else {
  2098. customer_cid = @"";
  2099. }
  2100. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  2101. if (customer_city) {
  2102. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2103. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  2104. } else {
  2105. customer_city = @"";
  2106. }
  2107. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  2108. if (customer_address) {
  2109. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2110. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  2111. } else {
  2112. customer_address = @"";
  2113. }
  2114. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  2115. if (customer_email) {
  2116. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2117. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  2118. } else {
  2119. customer_email = @"";
  2120. }
  2121. NSString *price_name = [params valueForKey:@"price_name"];
  2122. if (price_name) {
  2123. if ([price_name containsString:@","]) {
  2124. // 首先从 price表中查处name
  2125. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  2126. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  2127. for (int i = 1;i < pArray.count;i++) {
  2128. NSString *p = pArray[i];
  2129. [mutablePStr appendFormat:@" or type = %@ ",p];
  2130. }
  2131. [mutablePStr appendString:@";"];
  2132. __block NSMutableArray *price_name_array = [NSMutableArray array];
  2133. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2134. char *name = (char *)sqlite3_column_text(stmt, 0);
  2135. if (!name)
  2136. name = "";
  2137. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  2138. }];
  2139. // 再根据name 拼sql
  2140. NSMutableString *mutable_price_name = [NSMutableString string];
  2141. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  2142. for (int i = 1; i < price_name_array.count; i++) {
  2143. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  2144. }
  2145. [mutable_price_name appendString:@")"];
  2146. price_name = mutable_price_name;
  2147. } else {
  2148. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  2149. if ([price_name isEqualToString:@""]) {
  2150. price_name = @"";
  2151. } else {
  2152. __block NSString *price;
  2153. price_name = [self translateSingleQuote:price_name];
  2154. [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) {
  2155. char *p = (char *)sqlite3_column_text(stmt, 0);
  2156. if (p == NULL) {
  2157. p = "";
  2158. }
  2159. price = [NSString stringWithUTF8String:p];
  2160. }];
  2161. if ([price isEqualToString:@""]) {
  2162. price_name = @"";
  2163. } else {
  2164. price = [self translateSingleQuote:price];
  2165. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  2166. }
  2167. }
  2168. }
  2169. } else {
  2170. price_name = @"";
  2171. }
  2172. int limit = [[params valueForKey:@"limit"] intValue];
  2173. int offset = [[params valueForKey:@"offset"] intValue];
  2174. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2175. 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];
  2176. 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];
  2177. // int result= [iSalesDB AddExFunction:db];
  2178. int count =0;
  2179. NSString *sqlQuery = nil;
  2180. if(keyword.length==0)
  2181. {
  2182. // 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];
  2183. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  2184. sqlQuery = sql;
  2185. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  2186. }
  2187. else
  2188. {
  2189. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  2190. keyword = keyword.lowercaseString;
  2191. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2192. 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];
  2193. 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]];
  2194. }
  2195. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2196. sqlite3_stmt * statement;
  2197. [ret setValue:@"2" forKey:@"result"];
  2198. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  2199. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2200. {
  2201. int i = 0;
  2202. while (sqlite3_step(statement) == SQLITE_ROW)
  2203. {
  2204. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2205. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2206. int editable = sqlite3_column_int(statement, 0);
  2207. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2208. NSString *nscompany_name =nil;
  2209. if(company_name==nil)
  2210. nscompany_name=@"";
  2211. else
  2212. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  2213. char *country = (char*)sqlite3_column_text(statement, 2);
  2214. if(country==nil)
  2215. country="";
  2216. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2217. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2218. // if(addr==nil)
  2219. // addr="";
  2220. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2221. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2222. if(zipcode==nil)
  2223. zipcode="";
  2224. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2225. char *state = (char*)sqlite3_column_text(statement, 5);
  2226. if(state==nil)
  2227. state="";
  2228. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2229. char *city = (char*)sqlite3_column_text(statement, 6);
  2230. if(city==nil)
  2231. city="";
  2232. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2233. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2234. // NSString *nscontact_name = nil;
  2235. // if(contact_name==nil)
  2236. // nscontact_name=@"";
  2237. // else
  2238. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2239. char *phone = (char*)sqlite3_column_text(statement, 8);
  2240. NSString *nsphone = nil;
  2241. if(phone==nil)
  2242. nsphone=@"";
  2243. else
  2244. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2245. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2246. if(contact_id==nil)
  2247. contact_id="";
  2248. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2249. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2250. if(addr_1==nil)
  2251. addr_1="";
  2252. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2253. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2254. if(addr_2==nil)
  2255. addr_2="";
  2256. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2257. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2258. if(addr_3==nil)
  2259. addr_3="";
  2260. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2261. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2262. if(addr_4==nil)
  2263. addr_4="";
  2264. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2265. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2266. if(first_name==nil)
  2267. first_name="";
  2268. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2269. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2270. if(last_name==nil)
  2271. last_name="";
  2272. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2273. char *fax = (char*)sqlite3_column_text(statement, 16);
  2274. NSString *nsfax = nil;
  2275. if(fax==nil)
  2276. nsfax=@"";
  2277. else
  2278. {
  2279. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2280. if(nsfax.length>0)
  2281. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  2282. }
  2283. char *email = (char*)sqlite3_column_text(statement, 17);
  2284. NSString *nsemail = nil;
  2285. if(email==nil)
  2286. nsemail=@"";
  2287. else
  2288. {
  2289. nsemail= [[NSString alloc]initWithUTF8String:email];
  2290. if(nsemail.length>0)
  2291. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  2292. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  2293. }
  2294. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2295. [arr_name addObject:nsfirst_name];
  2296. [arr_name addObject:nslast_name];
  2297. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2298. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  2299. {
  2300. // decrypt
  2301. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2302. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2303. nsphone=[AESCrypt fastdecrypt:nsphone];
  2304. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2305. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2306. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2307. }
  2308. [arr_addr addObject:nscompany_name];
  2309. [arr_addr addObject:nscontact_name];
  2310. [arr_addr addObject:@"<br>"];
  2311. [arr_addr addObject:nsaddr_1];
  2312. [arr_addr addObject:nsaddr_2];
  2313. [arr_addr addObject:nsaddr_3];
  2314. [arr_addr addObject:nsaddr_4];
  2315. //[arr_addr addObject:nsaddr];
  2316. [arr_addr addObject:nszipcode];
  2317. [arr_addr addObject:nscity];
  2318. [arr_addr addObject:nsstate];
  2319. [arr_addr addObject:nscountry];
  2320. [arr_addr addObject:@"<br>"];
  2321. [arr_addr addObject:nsphone];
  2322. [arr_addr addObject:nsfax];
  2323. [arr_addr addObject:nsemail];
  2324. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  2325. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  2326. [item setValue:name forKey:@"name"];
  2327. [item setValue:nscontact_id forKey:@"contact_id"];
  2328. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  2329. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2330. i++;
  2331. }
  2332. UIApplication * app = [UIApplication sharedApplication];
  2333. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2334. [ret setValue:appDelegate.mode forKey:@"mode"];
  2335. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  2336. sqlite3_finalize(statement);
  2337. }
  2338. [iSalesDB close_db:db];
  2339. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2340. return ret;
  2341. }
  2342. #pragma mark contact Advanced search
  2343. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  2344. {
  2345. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  2346. return [RAUtils dict2data:contactAdvanceDic];
  2347. }
  2348. #pragma mark create new contact
  2349. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  2350. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  2351. NSData *data = [NSData dataWithContentsOfFile:path];
  2352. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2353. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2354. NSString *countryCode = nil;
  2355. NSString *countryCode_id = nil;
  2356. NSString *stateCode = nil;
  2357. NSString *city = nil;
  2358. NSString *zipCode = nil;
  2359. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2360. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  2361. NSString *code_id = params[@"country"];
  2362. countryCode_id = code_id;
  2363. countryCode = [self countryCodeByid:code_id];
  2364. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  2365. NSString *zip_code = params[@"zipcode"];
  2366. zipCode = zip_code;
  2367. countryCode_id = params[@"country"];
  2368. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2369. countryCode = [dic valueForKey:@"country_code"];
  2370. stateCode = [dic valueForKey:@"state_code"];
  2371. city = [dic valueForKey:@"city"];
  2372. // zip code
  2373. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  2374. [zipDic setValue:zipCode forKey:@"value"];
  2375. [section_0 setValue:zipDic forKey:@"item_10"];
  2376. }
  2377. } else {
  2378. // default: US United States
  2379. countryCode = @"US";
  2380. countryCode_id = @"228";
  2381. }
  2382. // country
  2383. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2384. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  2385. // state
  2386. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2387. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  2388. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  2389. // city
  2390. if (city) {
  2391. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  2392. [cityDic setValue:city forKey:@"value"];
  2393. [section_0 setValue:cityDic forKey:@"item_12"];
  2394. }
  2395. // price type
  2396. NSDictionary *priceDic = [self offline_getPrice];
  2397. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  2398. // Sales Rep
  2399. NSDictionary *repDic = [self offline_getSalesRep];
  2400. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2401. [ret setValue:section_0 forKey:@"section_0"];
  2402. return [RAUtils dict2data:ret];
  2403. }
  2404. #pragma mark save
  2405. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update {
  2406. NSString *addr = nil;
  2407. NSString *contact_name = nil;
  2408. NSString *companyName = [params objectForKey:@"company"];
  2409. if (companyName) {
  2410. companyName = [AESCrypt fastencrypt:companyName];
  2411. } else {
  2412. companyName = @"";
  2413. }
  2414. companyName = [self translateSingleQuote:companyName];
  2415. NSString *addr1 = [params objectForKey:@"address"];
  2416. NSString *addr2 = [params objectForKey:@"address2"];
  2417. NSString *addr3 = [params objectForKey:@"address_3"];
  2418. NSString *addr4 = [params objectForKey:@"address_4"];
  2419. if (!addr2) {
  2420. addr2 = @"";
  2421. }
  2422. if (!addr3) {
  2423. addr3 = @"";
  2424. }
  2425. if (!addr4) {
  2426. addr4 = @"";
  2427. }
  2428. if (!addr1) {
  2429. addr1 = @"";
  2430. }
  2431. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  2432. addr = [AESCrypt fastencrypt:addr];
  2433. addr = [self translateSingleQuote:addr];
  2434. if (addr1 && ![addr1 isEqualToString:@""]) {
  2435. addr1 = [AESCrypt fastencrypt:addr1];
  2436. }
  2437. addr1 = [self translateSingleQuote:addr1];
  2438. addr2 = [self translateSingleQuote:addr2];
  2439. addr3 = [self translateSingleQuote:addr3];
  2440. addr4 = [self translateSingleQuote:addr4];
  2441. NSString *country = [params objectForKey:@"country"];
  2442. if (country) {
  2443. country = [self countryNameByCountryCodeId:country];
  2444. } else {
  2445. country = @"";
  2446. }
  2447. country = [self translateSingleQuote:country];
  2448. NSString *state = [params objectForKey:@"state"];
  2449. if (!state) {
  2450. state = @"";
  2451. }
  2452. state = [self translateSingleQuote:state];
  2453. NSString *city = [params objectForKey:@"city"];
  2454. if (!city) {
  2455. city = @"";
  2456. }
  2457. city = [self translateSingleQuote:city];
  2458. NSString *zipcode = [params objectForKey:@"zipcode"];
  2459. if (!zipcode) {
  2460. zipcode = @"";
  2461. }
  2462. zipcode = [self translateSingleQuote:zipcode];
  2463. NSString *fistName = [params objectForKey:@"firstname"];
  2464. if (!fistName) {
  2465. fistName = @"";
  2466. }
  2467. NSString *lastName = [params objectForKey:@"lastname"];
  2468. if (!lastName) {
  2469. lastName = @"";
  2470. }
  2471. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  2472. contact_name = [self translateSingleQuote:contact_name];
  2473. fistName = [self translateSingleQuote:fistName];
  2474. lastName = [self translateSingleQuote:lastName];
  2475. NSString *phone = [params objectForKey:@"phone"];
  2476. if (phone) {
  2477. phone = [AESCrypt fastencrypt:phone];
  2478. } else {
  2479. phone = @"";
  2480. }
  2481. phone = [self translateSingleQuote:phone];
  2482. NSString *fax = [params objectForKey:@"fax"];
  2483. if (!fax) {
  2484. fax = @"";
  2485. }
  2486. fax = [self translateSingleQuote:fax];
  2487. NSString *email = [params objectForKey:@"email"];
  2488. if (!email) {
  2489. email = @"";
  2490. }
  2491. email = [self translateSingleQuote:email];
  2492. NSString *notes = [params objectForKey:@"contact_notes"];
  2493. if (!notes) {
  2494. notes = @"";
  2495. }
  2496. notes = [self translateSingleQuote:notes];
  2497. NSString *price = [params objectForKey:@"price_name"];
  2498. if (price) {
  2499. price = [self priceNameByPriceId:price];
  2500. } else {
  2501. price = @"";
  2502. }
  2503. price = [self translateSingleQuote:price];
  2504. NSString *salesRep = [params objectForKey:@"sales_rep"];
  2505. if (salesRep) {
  2506. salesRep = [self salesRepCodeById:salesRep];
  2507. } else {
  2508. salesRep = @"";
  2509. }
  2510. salesRep = [self translateSingleQuote:salesRep];
  2511. NSString *img = [params objectForKey:@"business_card"];
  2512. NSArray *array = [img componentsSeparatedByString:@","];
  2513. NSString *img_0 = array[0];
  2514. if (!img_0) {
  2515. img_0 = @"";
  2516. }
  2517. img_0 = [self translateSingleQuote:img_0];
  2518. NSString *img_1 = array[1];
  2519. if (!img_1) {
  2520. img_1 = @"";
  2521. }
  2522. img_1 = [self translateSingleQuote:img_1];
  2523. NSString *img_2 = array[2];
  2524. if (!img_2) {
  2525. img_2 = @"";
  2526. }
  2527. img_2 = [self translateSingleQuote:img_2];
  2528. NSString *contact_id = [NSUUID UUID].UUIDString;
  2529. NSMutableDictionary *sync_dic = [params mutableCopy];
  2530. NSString *sync_data = nil;
  2531. NSString *sql = nil;
  2532. if (update){
  2533. contact_id = [params objectForKey:@"contact_id"];
  2534. if (!contact_id) {
  2535. contact_id = @"";
  2536. }
  2537. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2538. sync_data = [RAUtils dict2string:sync_dic];
  2539. sync_data = [self translateSingleQuote:sync_data];
  2540. 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];
  2541. } else {
  2542. contact_id = [self translateSingleQuote:contact_id];
  2543. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2544. sync_data = [RAUtils dict2string:sync_dic];
  2545. sync_data = [self translateSingleQuote:sync_data];
  2546. 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];
  2547. }
  2548. int result = [iSalesDB execSql:sql];
  2549. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2550. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2551. }
  2552. #pragma mark save new contact
  2553. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  2554. {
  2555. return [self offline_saveContact:params update:NO];
  2556. }
  2557. #pragma mark edit contact
  2558. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  2559. {
  2560. // {
  2561. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  2562. // password = 123456;
  2563. // user = EvanK;
  2564. // }
  2565. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  2566. NSData *data = [NSData dataWithContentsOfFile:path];
  2567. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2568. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2569. NSString *countryCode = nil;
  2570. NSString *countryCode_id = nil;
  2571. NSString *stateCode = nil;
  2572. /*------contact infor------*/
  2573. __block NSString *country = nil;
  2574. __block NSString *company_name = nil;
  2575. __block NSString *contact_id = params[@"contact_id"];
  2576. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  2577. __block NSString *zipcode = nil;
  2578. __block NSString *state = nil; // state_code
  2579. __block NSString *city = nil; //
  2580. __block NSString *firt_name,*last_name;
  2581. __block NSString *phone,*fax,*email;
  2582. __block NSString *notes,*price_type,*sales_rep;
  2583. __block NSString *img_0,*img_1,*img_2;
  2584. contact_id = [self translateSingleQuote:contact_id];
  2585. 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];
  2586. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2587. country = [self textAtColumn:0 statement:stmt]; // country name
  2588. company_name = [self textAtColumn:1 statement:stmt];
  2589. addr_1 = [self textAtColumn:2 statement:stmt];
  2590. addr_2 = [self textAtColumn:3 statement:stmt];
  2591. addr_3 = [self textAtColumn:4 statement:stmt];
  2592. addr_4 = [self textAtColumn:5 statement:stmt];
  2593. zipcode = [self textAtColumn:6 statement:stmt];
  2594. state = [self textAtColumn:7 statement:stmt]; // state code
  2595. city = [self textAtColumn:8 statement:stmt];
  2596. firt_name = [self textAtColumn:9 statement:stmt];
  2597. last_name = [self textAtColumn:10 statement:stmt];
  2598. phone = [self textAtColumn:11 statement:stmt];
  2599. fax = [self textAtColumn:12 statement:stmt];
  2600. email = [self textAtColumn:13 statement:stmt];
  2601. notes = [self textAtColumn:14 statement:stmt];
  2602. price_type = [self textAtColumn:15 statement:stmt]; // name
  2603. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  2604. img_0 = [self textAtColumn:17 statement:stmt];
  2605. img_1 = [self textAtColumn:18 statement:stmt];
  2606. img_2 = [self textAtColumn:19 statement:stmt];
  2607. }];
  2608. // decrypt
  2609. if (company_name) {
  2610. company_name = [AESCrypt fastdecrypt:company_name];
  2611. }
  2612. if (addr_1) {
  2613. addr_1 = [AESCrypt fastdecrypt:addr_1];
  2614. }
  2615. if (phone) {
  2616. phone = [AESCrypt fastdecrypt:phone];
  2617. }
  2618. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  2619. countryCode = [iSalesDB jk_queryText:countrySql];
  2620. stateCode = state;
  2621. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2622. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  2623. NSString *code_id = params[@"country"];
  2624. countryCode_id = code_id;
  2625. countryCode = [self countryCodeByid:code_id];
  2626. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  2627. NSString *zip_code = params[@"zipcode"];
  2628. zipcode = zip_code;
  2629. countryCode_id = params[@"country"];
  2630. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2631. countryCode = [dic valueForKey:@"country_code"];
  2632. stateCode = [dic valueForKey:@"state_code"];
  2633. city = [dic valueForKey:@"city"];
  2634. // zip code
  2635. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  2636. [zipDic setValue:zipcode forKey:@"value"];
  2637. [section_0 setValue:zipDic forKey:@"item_8"];
  2638. }
  2639. }
  2640. // 0 Country
  2641. // 1 Company Name
  2642. // 2 Contact ID
  2643. // 3 Picture
  2644. // 4 Address 1
  2645. // 5 Address 2
  2646. // 6 Address 3
  2647. // 7 Address 4
  2648. // 8 Zip Code
  2649. // 9 State/Province
  2650. // 10 City
  2651. // 11 Contact First Name
  2652. // 12 Contact Last Name
  2653. // 13 Phone
  2654. // 14 Fax
  2655. // 15 Email
  2656. // 16 Contact Notes
  2657. // 17 Price Type
  2658. // 18 Sales Rep
  2659. // country
  2660. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2661. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  2662. // company
  2663. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  2664. // contact_id
  2665. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  2666. // picture
  2667. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  2668. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  2669. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  2670. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  2671. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  2672. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  2673. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  2674. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  2675. // addr 1 2 3 4
  2676. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  2677. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  2678. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  2679. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  2680. // zip code
  2681. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  2682. // state
  2683. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2684. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  2685. // city
  2686. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  2687. // first last
  2688. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  2689. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  2690. // phone fax email
  2691. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  2692. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  2693. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  2694. // notes
  2695. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  2696. // price
  2697. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  2698. for (NSString *key in priceDic.allKeys) {
  2699. if ([key containsString:@"val_"]) {
  2700. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  2701. if ([dic[@"value"] isEqualToString:price_type]) {
  2702. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2703. [priceDic setValue:dic forKey:key];
  2704. }
  2705. }
  2706. }
  2707. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2708. // Sales Rep
  2709. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  2710. for (NSString *key in repDic.allKeys) {
  2711. if ([key containsString:@"val_"]) {
  2712. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  2713. NSString *value = dic[@"value"];
  2714. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  2715. if (code && [code isEqualToString:sales_rep]) {
  2716. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2717. [repDic setValue:dic forKey:key];
  2718. }
  2719. }
  2720. }
  2721. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  2722. [ret setValue:section_0 forKey:@"section_0"];
  2723. return [RAUtils dict2data:ret];
  2724. }
  2725. #pragma mark save contact
  2726. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  2727. {
  2728. return [self offline_saveContact:params update:YES];
  2729. }
  2730. #pragma mark category
  2731. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2732. if (ck) {
  2733. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  2734. for (NSString *key in res.allKeys) {
  2735. if (![key isEqualToString:@"count"]) {
  2736. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  2737. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  2738. if ([val[@"value"] isEqualToString:ck]) {
  2739. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  2740. }
  2741. [res setValue:val forKey:key];
  2742. }
  2743. }
  2744. [dic setValue:res forKey:valueKey];
  2745. }
  2746. }
  2747. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  2748. {
  2749. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2750. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2751. NSString* category = [params valueForKey:@"category"];
  2752. if (!category || [category isEqualToString:@""]) {
  2753. category = @"%";
  2754. }
  2755. category = [self translateSingleQuote:category];
  2756. int limit = [[params valueForKey:@"limit"] intValue];
  2757. int offset = [[params valueForKey:@"offset"] intValue];
  2758. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2759. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  2760. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  2761. sqlite3 *db = [iSalesDB get_db];
  2762. // [iSalesDB AddExFunction:db];
  2763. int count;
  2764. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  2765. 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];
  2766. // {
  2767. // alert = SP;
  2768. // available = "Available Now";
  2769. // bestseller = No;
  2770. // category = 001;
  2771. // "custom_id" = 0;
  2772. // limit = 20;
  2773. // offset = 0;
  2774. // password = 123456;
  2775. // price = "Display All";
  2776. // "price_template" = 0;
  2777. // "sold_by_qty" = "Display All";
  2778. // "sort_by" = 0;
  2779. // user = EvanK;
  2780. // }
  2781. double price_min = 0;
  2782. double price_max = 0;
  2783. if ([params.allKeys containsObject:@"alert"]) {
  2784. // alert
  2785. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  2786. NSString *alert = params[@"alert"];
  2787. if ([alert isEqualToString:@"Display All"]) {
  2788. alert = [NSString stringWithFormat:@""];
  2789. } else {
  2790. alert = [self translateSingleQuote:alert];
  2791. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  2792. }
  2793. // available
  2794. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  2795. NSString *available = params[@"available"];
  2796. NSString *available_condition;
  2797. if ([available isEqualToString:@"Display All"]) {
  2798. available_condition = @"";
  2799. } else if ([available isEqualToString:@"Available Now"]) {
  2800. available_condition = @"and availability > 0";
  2801. } else {
  2802. available_condition = @"and availability == 0";
  2803. }
  2804. // best seller
  2805. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  2806. NSString *best_seller = @"";
  2807. NSString *order_best_seller = @"m.name asc";
  2808. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  2809. best_seller = @"and best_seller > 0";
  2810. order_best_seller = @"m.best_seller desc";
  2811. }
  2812. // price
  2813. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  2814. NSString *price = params[@"price"];
  2815. price_min = 0;
  2816. price_max = MAXFLOAT;
  2817. if (appDelegate.user) {
  2818. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  2819. NSMutableString *priceName = [NSMutableString string];
  2820. for (int i = 0; i < priceTypeArray.count; i++) {
  2821. NSString *pricetype = priceTypeArray[i];
  2822. pricetype = [self translateSingleQuote:pricetype];
  2823. if (i == 0) {
  2824. [priceName appendFormat:@"'%@'",pricetype];
  2825. } else {
  2826. [priceName appendFormat:@",'%@'",pricetype];
  2827. }
  2828. }
  2829. if ([price isEqualToString:@"Display All"]) {
  2830. price = [NSString stringWithFormat:@""];
  2831. } else if([price containsString:@"+"]){
  2832. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  2833. price_min = [price doubleValue];
  2834. 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];
  2835. } else {
  2836. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  2837. price_min = [[priceArray objectAtIndex:0] doubleValue];
  2838. price_max = [[priceArray objectAtIndex:1] doubleValue];
  2839. 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];
  2840. }
  2841. } else {
  2842. price = @"";
  2843. }
  2844. // sold_by_qty : Sold in quantities of %@
  2845. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  2846. NSString *qty = params[@"sold_by_qty"];
  2847. if ([qty isEqualToString:@"Display All"]) {
  2848. qty = @"";
  2849. } else {
  2850. qty = [self translateSingleQuote:qty];
  2851. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  2852. }
  2853. // cate
  2854. category = [self translateSingleQuote:category];
  2855. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  2856. // where bestseller > 0 order by bestseller desc
  2857. // sql query: alert availability(int) best_seller(int) price qty
  2858. 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];
  2859. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",cateWhere,best_seller,alert,available_condition,qty,price];
  2860. }
  2861. count = [iSalesDB get_recordcount:db table:@"model" where:where];
  2862. if (!appDelegate.user) {
  2863. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  2864. }
  2865. [ret setValue:filter forKey:@"filter"];
  2866. DebugLog(@"offline_category sql:%@",sqlQuery);
  2867. sqlite3_stmt * statement;
  2868. [ret setValue:@"2" forKey:@"result"];
  2869. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2870. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2871. // int count=0;
  2872. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2873. {
  2874. int i=0;
  2875. while (sqlite3_step(statement) == SQLITE_ROW)
  2876. {
  2877. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2878. char *name = (char*)sqlite3_column_text(statement, 0);
  2879. if(name==nil)
  2880. name="";
  2881. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2882. char *description = (char*)sqlite3_column_text(statement, 1);
  2883. if(description==nil)
  2884. description="";
  2885. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2886. int product_id = sqlite3_column_int(statement, 2);
  2887. int wid = sqlite3_column_int(statement, 3);
  2888. int closeout = sqlite3_column_int(statement, 4);
  2889. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2890. if(wid==0)
  2891. [item setValue:@"false" forKey:@"wish_exists"];
  2892. else
  2893. [item setValue:@"true" forKey:@"wish_exists"];
  2894. if(closeout==0)
  2895. [item setValue:@"false" forKey:@"is_closeout"];
  2896. else
  2897. [item setValue:@"true" forKey:@"is_closeout"];
  2898. [item addEntriesFromDictionary:imgjson];
  2899. // [item setValue:nsurl forKey:@"img"];
  2900. [item setValue:nsname forKey:@"name"];
  2901. [item setValue:nsdescription forKey:@"description"];
  2902. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2903. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2904. i++;
  2905. }
  2906. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2907. [ret setObject:items forKey:@"items"];
  2908. sqlite3_finalize(statement);
  2909. } else {
  2910. DebugLog(@"nothing...");
  2911. }
  2912. DebugLog(@"count:%d",count);
  2913. [iSalesDB close_db:db];
  2914. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2915. return ret;
  2916. }
  2917. # pragma mark item search
  2918. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  2919. {
  2920. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2921. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  2922. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  2923. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  2924. // category
  2925. NSDictionary *category_menu = [self offline_category_menu];
  2926. [filter setValue:category_menu forKey:@"category"];
  2927. NSString* where= nil;
  2928. NSString* orderby= @"m.name";
  2929. if (!filterSearch) {
  2930. int covertype = [[params valueForKey:@"covertype"] intValue];
  2931. switch (covertype) {
  2932. case 0:
  2933. {
  2934. where=@"m.category like'%%#005#%%'";
  2935. break;
  2936. }
  2937. case 1:
  2938. {
  2939. where=@"m.alert like '%QS%'";
  2940. break;
  2941. }
  2942. case 2:
  2943. {
  2944. where=@"m.availability>0";
  2945. break;
  2946. }
  2947. case 3:
  2948. {
  2949. where=@"m.best_seller>0";
  2950. orderby=@"m.best_seller desc";
  2951. break;
  2952. }
  2953. default:
  2954. where=@"1=1";
  2955. break;
  2956. }
  2957. }
  2958. int limit = [[params valueForKey:@"limit"] intValue];
  2959. int offset = [[params valueForKey:@"offset"] intValue];
  2960. sqlite3 *db = [iSalesDB get_db];
  2961. // [iSalesDB AddExFunction:db];
  2962. int count;
  2963. NSString *sqlQuery = nil;
  2964. 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];
  2965. double price_min = 0;
  2966. double price_max = 0;
  2967. if (filterSearch) {
  2968. // alert
  2969. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  2970. NSString *alert = params[@"alert"];
  2971. if ([alert isEqualToString:@"Display All"]) {
  2972. alert = [NSString stringWithFormat:@""];
  2973. } else {
  2974. alert = [self translateSingleQuote:alert];
  2975. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  2976. }
  2977. // available
  2978. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  2979. NSString *available = params[@"available"];
  2980. NSString *available_condition;
  2981. if ([available isEqualToString:@"Display All"]) {
  2982. available_condition = @"";
  2983. } else if ([available isEqualToString:@"Available Now"]) {
  2984. available_condition = @"and availability > 0";
  2985. } else {
  2986. available_condition = @"and availability == 0";
  2987. }
  2988. // best seller
  2989. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  2990. NSString *best_seller = @"";
  2991. NSString *order_best_seller = @"m.name asc";
  2992. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  2993. best_seller = @"and best_seller > 0";
  2994. order_best_seller = @"m.best_seller desc";
  2995. }
  2996. // price
  2997. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  2998. NSString *price = params[@"price"];
  2999. price_min = 0;
  3000. price_max = MAXFLOAT;
  3001. if (appDelegate.user) {
  3002. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3003. NSMutableString *priceName = [NSMutableString string];
  3004. for (int i = 0; i < priceTypeArray.count; i++) {
  3005. NSString *pricetype = priceTypeArray[i];
  3006. pricetype = [self translateSingleQuote:pricetype];
  3007. if (i == 0) {
  3008. [priceName appendFormat:@"'%@'",pricetype];
  3009. } else {
  3010. [priceName appendFormat:@",'%@'",pricetype];
  3011. }
  3012. }
  3013. if ([price isEqualToString:@"Display All"]) {
  3014. price = [NSString stringWithFormat:@""];
  3015. } else if([price containsString:@"+"]){
  3016. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3017. price_min = [price doubleValue];
  3018. 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];
  3019. } else {
  3020. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3021. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3022. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3023. 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];
  3024. }
  3025. } else {
  3026. price = @"";
  3027. }
  3028. // sold_by_qty : Sold in quantities of %@
  3029. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3030. NSString *qty = params[@"sold_by_qty"];
  3031. if ([qty isEqualToString:@"Display All"]) {
  3032. qty = @"";
  3033. } else {
  3034. qty = [self translateSingleQuote:qty];
  3035. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3036. }
  3037. // category
  3038. NSString *category_id = params[@"ctgId"];
  3039. NSMutableArray *cate_id_array = nil;
  3040. NSMutableString *cateWhere = [NSMutableString string];
  3041. if ([category_id isEqualToString:@""] || !category_id) {
  3042. [cateWhere appendString:@"1 = 1"];
  3043. } else {
  3044. if ([category_id containsString:@","]) {
  3045. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  3046. } else {
  3047. cate_id_array = [@[category_id] mutableCopy];
  3048. }
  3049. /*-----------*/
  3050. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  3051. for (int i = 0; i < cate_id_array.count; i++) {
  3052. for (NSString *key0 in cateDic.allKeys) {
  3053. if ([key0 containsString:@"category_"]) {
  3054. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  3055. for (NSString *key1 in category0.allKeys) {
  3056. if ([key1 containsString:@"category_"]) {
  3057. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  3058. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3059. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  3060. cate_id_array[i] = [category1 objectForKey:@"id"];
  3061. if (i == 0) {
  3062. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  3063. } else {
  3064. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  3065. }
  3066. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3067. [category0 setValue:category1 forKey:key1];
  3068. [cateDic setValue:category0 forKey:key0];
  3069. }
  3070. }
  3071. }
  3072. }
  3073. }
  3074. }
  3075. [filter setValue:cateDic forKey:@"category"];
  3076. }
  3077. // where bestseller > 0 order by bestseller desc
  3078. // sql query: alert availability(int) best_seller(int) price qty
  3079. 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];
  3080. // count
  3081. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@",cateWhere,best_seller,alert,available_condition,qty,price];
  3082. }
  3083. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  3084. count = [iSalesDB get_recordcount:db table:@"model" where:where];
  3085. if (!appDelegate.user) {
  3086. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3087. }
  3088. [ret setValue:filter forKey:@"filter"];
  3089. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  3090. sqlite3_stmt * statement;
  3091. [ret setValue:@"2" forKey:@"result"];
  3092. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3093. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3094. // int count=0;
  3095. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3096. {
  3097. int i=0;
  3098. while (sqlite3_step(statement) == SQLITE_ROW)
  3099. {
  3100. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3101. char *name = (char*)sqlite3_column_text(statement, 0);
  3102. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3103. char *description = (char*)sqlite3_column_text(statement, 1);
  3104. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3105. int product_id = sqlite3_column_int(statement, 2);
  3106. int wid = sqlite3_column_int(statement, 3);
  3107. int closeout = sqlite3_column_int(statement, 4);
  3108. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3109. if(wid==0)
  3110. [item setValue:@"false" forKey:@"wish_exists"];
  3111. else
  3112. [item setValue:@"true" forKey:@"wish_exists"];
  3113. if(closeout==0)
  3114. [item setValue:@"false" forKey:@"is_closeout"];
  3115. else
  3116. [item setValue:@"true" forKey:@"is_closeout"];
  3117. [item addEntriesFromDictionary:imgjson];
  3118. // [item setValue:nsurl forKey:@"img"];
  3119. [item setValue:nsname forKey:@"fash_name"];
  3120. [item setValue:nsdescription forKey:@"description"];
  3121. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3122. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3123. i++;
  3124. }
  3125. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3126. [ret setObject:items forKey:@"items"];
  3127. sqlite3_finalize(statement);
  3128. }
  3129. [iSalesDB close_db:db];
  3130. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3131. return ret;
  3132. }
  3133. @end