OLDataProvider.m 114 KB

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