OLDataProvider.m 116 KB

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