OLDataProvider.m 300 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. @interface OLDataProvider ()
  14. @end
  15. @implementation OLDataProvider
  16. +(void) prepareUpdata:(NSArray*) arr_order
  17. {
  18. NSString* serial= [[NSUUID UUID] UUIDString];
  19. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  20. ret[@"contact"]=[self prepareContact:serial];
  21. ret[@"wishlist"]=[self prepareWishlist:serial];
  22. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  23. }
  24. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  25. {
  26. if(filename.length==0)
  27. return false;
  28. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  29. bool ret=false;
  30. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  31. NSString *cachefolder = [paths objectAtIndex:0];
  32. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  33. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  34. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  35. upfolder = [upfolder stringByAppendingPathComponent:serial];
  36. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  37. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  38. NSFileManager* fileManager = [NSFileManager defaultManager];
  39. BOOL bdir=NO;
  40. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  41. {
  42. NSError *error = nil;
  43. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  44. {
  45. ret=false;
  46. }
  47. else
  48. {
  49. ret=true;
  50. }
  51. }
  52. return ret;
  53. }
  54. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  55. {
  56. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  57. sqlite3 *db = [iSalesDB get_db];
  58. NSString *sqlQuery =@"select _id,product_id,item_id,qty,create_time";
  59. sqlite3_stmt * statement;
  60. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  61. int count=0;
  62. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  63. if ( dbresult== SQLITE_OK)
  64. {
  65. while (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  68. int _id = sqlite3_column_int(statement, 0);
  69. int product_id = sqlite3_column_int(statement, 1);
  70. int item_id = sqlite3_column_int(statement, 2);
  71. int qty = sqlite3_column_int(statement, 3);
  72. char *create_time = (char*)sqlite3_column_text(statement, 4);
  73. NSString* nscreate_time=nil;
  74. if(create_time!=nil)
  75. {
  76. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  77. }
  78. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  79. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  80. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  81. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  82. itemjson[@"create_time"]=nscreate_time;
  83. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  84. count++;
  85. }
  86. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  87. sqlite3_finalize(statement);
  88. }
  89. ret[@"count"]=[NSNumber numberWithInt:count ];
  90. [iSalesDB close_db:db];
  91. return ret;
  92. }
  93. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  94. {
  95. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  96. sqlite3 *db = [iSalesDB get_db];
  97. for(int i=0;i<arr_soid.count;i++)
  98. {
  99. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,sync_data from offline_order where so_id='%@'",arr_soid[i]];
  100. sqlite3_stmt * statement;
  101. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  102. int count=0;
  103. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  104. if ( dbresult== SQLITE_OK)
  105. {
  106. if (sqlite3_step(statement) == SQLITE_ROW)
  107. {
  108. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  109. int _id = sqlite3_column_int(statement, 0);
  110. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  111. NSString* nssync_data=nil;
  112. if(sync_data!=nil)
  113. {
  114. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  115. }
  116. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  117. itemjson[@"json_data"]= nssync_data;
  118. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  119. count++;
  120. }
  121. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  122. sqlite3_finalize(statement);
  123. }
  124. ret[@"count"]=[NSNumber numberWithInt:count ];
  125. }
  126. [iSalesDB close_db:db];
  127. return ret;
  128. }
  129. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  130. {
  131. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  132. sqlite3 *db = [iSalesDB get_db];
  133. // UIApplication * app = [UIApplication sharedApplication];
  134. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  135. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  136. sqlite3_stmt * statement;
  137. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  138. int count=0;
  139. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  140. if ( dbresult== SQLITE_OK)
  141. {
  142. while (sqlite3_step(statement) == SQLITE_ROW)
  143. {
  144. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  145. int _id = sqlite3_column_int(statement, 0);
  146. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  147. NSString* nsimg_0=nil;
  148. if(img_0!=nil)
  149. {
  150. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  151. [self copy_upImg:serial file:nsimg_0];
  152. }
  153. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  154. NSString* nsimg_1=nil;
  155. if(img_1!=nil)
  156. {
  157. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  158. [self copy_upImg:serial file:nsimg_1];
  159. }
  160. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  161. NSString* nsimg_2=nil;
  162. if(img_2!=nil)
  163. {
  164. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  165. [self copy_upImg:serial file:nsimg_2];
  166. }
  167. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  168. NSString* nssync_data=nil;
  169. if(sync_data!=nil)
  170. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  171. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  172. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  173. itemjson[@"json_data"]= nssync_data;
  174. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  175. count++;
  176. }
  177. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  178. sqlite3_finalize(statement);
  179. }
  180. ret[@"count"]=[NSNumber numberWithInt:count ];
  181. [iSalesDB close_db:db];
  182. return ret;
  183. }
  184. +(bool) check_offlinedata
  185. {
  186. UIApplication * app = [UIApplication sharedApplication];
  187. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  188. sqlite3 *db = [iSalesDB get_db];
  189. NSString * where=@"1=1";
  190. // if(appDelegate.user!=nil)
  191. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  192. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  193. [iSalesDB close_db:db];
  194. if(count==0)
  195. {
  196. return false;
  197. }
  198. return true;
  199. //
  200. //[iSalesDB close_db:db];
  201. }
  202. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  203. {
  204. UIApplication * app = [UIApplication sharedApplication];
  205. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  206. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  207. sqlite3 *db = [iSalesDB get_db];
  208. NSString* collectId=params[@"collectId"];
  209. NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  210. [iSalesDB execSql:sqlQuery db:db];
  211. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  212. [iSalesDB close_db:db];
  213. appDelegate.wish_count =count;
  214. [appDelegate update_count_mark];
  215. ret[@"result"]= [NSNumber numberWithInt:2];
  216. return ret;
  217. }
  218. +(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  219. {
  220. UIApplication * app = [UIApplication sharedApplication];
  221. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  222. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  223. sqlite3 *db = [iSalesDB get_db];
  224. NSString* product_id=params[@"product_id"];
  225. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  226. for(int i=0;i<arr.count;i++)
  227. {
  228. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  229. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  230. if(count==0)
  231. {
  232. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  233. [iSalesDB execSql:sqlQuery db:db];
  234. }
  235. }
  236. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  237. [iSalesDB close_db:db];
  238. appDelegate.wish_count =count;
  239. [appDelegate update_count_mark];
  240. ret[@"result"]= [NSNumber numberWithInt:2];
  241. return ret;
  242. //
  243. //return ret;
  244. }
  245. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  246. {
  247. UIApplication * app = [UIApplication sharedApplication];
  248. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  249. NSString* user = appDelegate.user;
  250. sqlite3 *db = [iSalesDB get_db];
  251. NSString* sqlQuery = @"select w.product_id,m.name||'\n'||m.description,w._id from wishlist as w left join product as m on w.product_id=m.product_id order by w.create_time;";
  252. sqlite3_stmt * statement;
  253. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  254. int count=0;
  255. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  256. {
  257. while (sqlite3_step(statement) == SQLITE_ROW)
  258. {
  259. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  260. int product_id = sqlite3_column_double(statement, 0);
  261. char *description = (char*)sqlite3_column_text(statement, 1);
  262. if(description==nil)
  263. description= "";
  264. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  265. int item_id = sqlite3_column_double(statement, 2);
  266. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  267. // char *url = (char*)sqlite3_column_text(statement, 3);
  268. // if(url==nil)
  269. // url="";
  270. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  271. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  272. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  273. item[@"description"]= nsdescription;
  274. item[@"img"]= nsurl;
  275. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  276. count++;
  277. }
  278. ret[@"count"]= [NSNumber numberWithInt:count];
  279. ret[@"total_count"]= [NSNumber numberWithInt:count];
  280. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  281. ret[@"result"]= [NSNumber numberWithInt:2];
  282. appDelegate.wish_count =count;
  283. [appDelegate update_count_mark];
  284. sqlite3_finalize(statement);
  285. }
  286. [iSalesDB close_db:db];
  287. return ret;
  288. }
  289. +(NSDictionary*) offline_notimpl
  290. {
  291. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  292. ret[@"result"]=@"8";
  293. ret[@"err_msg"]=@"offline mode does not support this function.";
  294. return ret;
  295. }
  296. +(NSDictionary*) offline_home
  297. {
  298. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  299. NSString *cachefolder = [paths objectAtIndex:0];
  300. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  301. NSData* json =nil;
  302. json=[NSData dataWithContentsOfFile:img_cache];
  303. NSError *error=nil;
  304. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  305. return menu;
  306. }
  307. +(NSDictionary*) offline_category_menu
  308. {
  309. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  310. NSString *cachefolder = [paths objectAtIndex:0];
  311. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  312. NSData* json =nil;
  313. json=[NSData dataWithContentsOfFile:img_cache];
  314. NSError *error=nil;
  315. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  316. return menu;
  317. }
  318. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  319. {
  320. NSString* offline_command=params[@"offline_Command"];
  321. NSDictionary* ret=nil;
  322. if([offline_command isEqualToString:@"model_NIYMAL"])
  323. {
  324. NSString* category = params[@"category"];
  325. ret = [self refresh_model_NIYMAL:category];
  326. }
  327. return ret;
  328. }
  329. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  330. {
  331. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  332. [ret setValue:@"2" forKey:@"result"];
  333. [ret setValue:@"Regular Mode" forKey:@"mode"];
  334. sqlite3* db= [iSalesDB get_db];
  335. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  336. [iSalesDB close_db:db];
  337. [ret setObject:detail1_section forKey:@"detail_1"];
  338. return ret;
  339. }
  340. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  341. {
  342. UIApplication * app = [UIApplication sharedApplication];
  343. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  344. // NSArray* arr1 = [self get_user_all_price_type];
  345. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  346. // NSSet *set1 = [NSSet setWithArray:arr1];
  347. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  348. // if(appDelegate.contact_id==nil)
  349. // set2=[set1 mutableCopy];
  350. // else
  351. // [set2 intersectsSet:set1];
  352. // NSArray *retarr = [set2 allObjects];
  353. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  354. // sqlite3 *db = [iSalesDB get_db];
  355. NSString* sqlQuery = nil;
  356. if(appDelegate.contact_id==nil)
  357. 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.item_id=%d order by a.order_by;",item_id];
  358. else
  359. 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.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  360. sqlite3_stmt * statement;
  361. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  362. int count=0;
  363. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  364. {
  365. while (sqlite3_step(statement) == SQLITE_ROW)
  366. {
  367. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  368. char *name = (char*)sqlite3_column_text(statement, 0);
  369. if(name==nil)
  370. name="";
  371. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  372. // double price = sqlite3_column_double(statement, 1);
  373. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  374. // if(isnull==nil)
  375. // item[nsname]= @"No Price";
  376. // else
  377. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  378. char *price = (char*)sqlite3_column_text(statement, 1);
  379. if(price!=nil)
  380. {
  381. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  382. nsprice=[AESCrypt fastdecrypt:nsprice];
  383. if(nsprice.length>0)
  384. {
  385. double dp= [nsprice doubleValue];
  386. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  387. }
  388. }
  389. else
  390. {
  391. item[nsname]= @"No Price";
  392. }
  393. // item[nsname]= nsprice;
  394. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  395. count++;
  396. }
  397. ret[@"count"]= [NSNumber numberWithInt:count];
  398. sqlite3_finalize(statement);
  399. }
  400. // [iSalesDB close_db:db];
  401. return ret;
  402. }
  403. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  404. //{
  405. // NSArray* arr1 = [self get_user_all_price_type:db];
  406. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  407. //
  408. // // NSSet *set1 = [NSSet setWithArray:arr1];
  409. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  410. // // [set2 intersectsSet:set1];
  411. // //
  412. // //
  413. // // NSArray *retarr = [set2 allObjects];
  414. //
  415. // NSString* whereprice=nil;
  416. // if(contact_id==nil)
  417. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  418. // else
  419. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  420. //
  421. //
  422. // // sqlite3 *db = [iSalesDB get_db];
  423. //
  424. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  425. // sqlite3_stmt * statement;
  426. //
  427. //
  428. // NSNumber* ret = nil;
  429. // double dprice=DBL_MAX;
  430. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  431. // {
  432. //
  433. //
  434. // while (sqlite3_step(statement) == SQLITE_ROW)
  435. // {
  436. //
  437. // // double val = sqlite3_column_double(statement, 0);
  438. // char *price = (char*)sqlite3_column_text(statement, 0);
  439. // if(price!=nil)
  440. // {
  441. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  442. // nsprice=[AESCrypt fastdecrypt:nsprice];
  443. // if(nsprice.length>0)
  444. // {
  445. // double dp= [nsprice doubleValue];
  446. // if(dp<dprice)
  447. // dprice=dp;
  448. // }
  449. // }
  450. // }
  451. //
  452. //
  453. //
  454. //
  455. // sqlite3_finalize(statement);
  456. //
  457. //
  458. //
  459. //
  460. // }
  461. //
  462. // // [iSalesDB close_db:db];
  463. //
  464. // if(dprice==DBL_MAX)
  465. // ret= nil;
  466. // else
  467. // ret= [NSNumber numberWithDouble:dprice];
  468. // return ret;
  469. //}
  470. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  471. {
  472. NSArray* arr1 = [self get_user_all_price_type:db];
  473. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  474. NSString* whereprice=nil;
  475. if(contact_id==nil)
  476. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  477. else
  478. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  479. // sqlite3 *db = [iSalesDB get_db];
  480. NSString *productIdCondition = @"1 = 1";
  481. if (product_id) {
  482. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  483. }
  484. NSString *itemIdCondition = @"";
  485. if (item_id) {
  486. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  487. }
  488. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  489. sqlite3_stmt * statement;
  490. NSNumber* ret = nil;
  491. double dprice=DBL_MAX;
  492. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  493. {
  494. while (sqlite3_step(statement) == SQLITE_ROW)
  495. {
  496. // double val = sqlite3_column_double(statement, 0);
  497. char *price = (char*)sqlite3_column_text(statement, 0);
  498. if(price!=nil)
  499. {
  500. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  501. nsprice=[AESCrypt fastdecrypt:nsprice];
  502. if(nsprice.length>0)
  503. {
  504. double dp= [nsprice doubleValue];
  505. if(dp<dprice)
  506. dprice=dp;
  507. }
  508. }
  509. }
  510. sqlite3_finalize(statement);
  511. }
  512. // [iSalesDB close_db:db];
  513. if(dprice==DBL_MAX)
  514. ret= nil;
  515. else
  516. ret= [NSNumber numberWithDouble:dprice];
  517. return ret;
  518. }
  519. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  520. {
  521. NSArray* ret=nil;
  522. // sqlite3 *db = [iSalesDB get_db];
  523. // no customer assigned , use login user contact_id
  524. UIApplication * app = [UIApplication sharedApplication];
  525. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  526. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  527. sqlite3_stmt * statement;
  528. // int count=0;
  529. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  530. {
  531. if (sqlite3_step(statement) == SQLITE_ROW)
  532. {
  533. char *val = (char*)sqlite3_column_text(statement, 0);
  534. if(val==nil)
  535. val="";
  536. NSString* price = [[NSString alloc]initWithUTF8String:val];
  537. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  538. }
  539. sqlite3_finalize(statement);
  540. }
  541. // [iSalesDB close_db:db];
  542. return ret;
  543. }
  544. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  545. {
  546. // sqlite3 *db = [iSalesDB get_db];
  547. if(contact_id==nil)
  548. {
  549. // no customer assigned , use login user contact_id
  550. UIApplication * app = [UIApplication sharedApplication];
  551. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  552. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  553. sqlite3_stmt * statement;
  554. // int count=0;
  555. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  556. {
  557. if (sqlite3_step(statement) == SQLITE_ROW)
  558. {
  559. char *val = (char*)sqlite3_column_text(statement, 0);
  560. if(val==nil)
  561. val="";
  562. contact_id = [[NSString alloc]initWithUTF8String:val];
  563. }
  564. sqlite3_finalize(statement);
  565. }
  566. if(contact_id.length<=0)
  567. {
  568. // [iSalesDB close_db:db];
  569. return nil;
  570. }
  571. }
  572. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  573. sqlite3_stmt * statement;
  574. NSArray* ret=nil;
  575. // int count=0;
  576. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  577. {
  578. if (sqlite3_step(statement) == SQLITE_ROW)
  579. {
  580. char *price_type = (char*)sqlite3_column_text(statement, 0);
  581. if(price_type==nil)
  582. price_type="";
  583. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  584. if(nsprice_type.length>0)
  585. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  586. else
  587. ret=nil;
  588. }
  589. sqlite3_finalize(statement);
  590. }
  591. // [iSalesDB close_db:db];
  592. return ret;
  593. }
  594. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  595. {
  596. NSString* ret= nil;
  597. // sqlite3 *db = [iSalesDB get_db];
  598. NSString *sqlQuery = nil;
  599. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  600. if(product_id==nil && model_name)
  601. sqlQuery = [NSString stringWithFormat:@"select i.url from product 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];// 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;
  602. else if (product_id)
  603. sqlQuery = [NSString stringWithFormat:@"select i.url from product 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];// select i.url from model m
  604. sqlite3_stmt * statement;
  605. // int count=0;
  606. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  607. {
  608. if (sqlite3_step(statement) == SQLITE_ROW)
  609. {
  610. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  611. if(imgurl==nil)
  612. imgurl="";
  613. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  614. ret=nsimgurl;
  615. }
  616. sqlite3_finalize(statement);
  617. }
  618. else
  619. {
  620. [ret setValue:@"8" forKey:@"result"];
  621. }
  622. // [iSalesDB close_db:db];
  623. DebugLog(@"data string: %@",ret );
  624. return ret;
  625. }
  626. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  627. {
  628. NSString* ret= nil;
  629. sqlite3 *db = [iSalesDB get_db];
  630. NSString *sqlQuery = nil;
  631. if(product_id==nil)
  632. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  633. else
  634. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  635. sqlite3_stmt * statement;
  636. // int count=0;
  637. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  638. {
  639. if (sqlite3_step(statement) == SQLITE_ROW)
  640. {
  641. char *default_category = (char*)sqlite3_column_text(statement, 0);
  642. if(default_category==nil)
  643. default_category="";
  644. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  645. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  646. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  647. ret=nsdefault_category;
  648. }
  649. sqlite3_finalize(statement);
  650. }
  651. else
  652. {
  653. [ret setValue:@"8" forKey:@"result"];
  654. }
  655. [iSalesDB close_db:db];
  656. DebugLog(@"data string: %@",ret );
  657. return ret;
  658. }
  659. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  660. {
  661. NSString* model_name = [params valueForKey:@"product_name"];
  662. NSString* product_id = [params valueForKey:@"product_id"];
  663. NSString* category = [params valueForKey:@"category"];
  664. if(category==nil)
  665. category = [self model_default_category:product_id model_name:model_name];
  666. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  667. sqlite3 *db = [iSalesDB get_db];
  668. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  669. NSString *sqlQuery = nil;
  670. if(product_id==nil)
  671. 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 product where name='%@';",model_name]; // 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='%@';
  672. else
  673. 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,item_id from product where product_id=%@;",product_id]; // 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=%@;
  674. sqlite3_stmt * statement;
  675. [ret setValue:@"2" forKey:@"result"];
  676. [ret setValue:@"3" forKey:@"detail_section_count"];
  677. // int count=0;
  678. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  679. {
  680. if (sqlite3_step(statement) == SQLITE_ROW)
  681. {
  682. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  683. char *name = (char*)sqlite3_column_text(statement, 0);
  684. if(name==nil)
  685. name="";
  686. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  687. char *description = (char*)sqlite3_column_text(statement, 1);
  688. if(description==nil)
  689. description="";
  690. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  691. int product_id = sqlite3_column_int(statement, 2);
  692. char *color = (char*)sqlite3_column_text(statement, 3);
  693. if(color==nil)
  694. color="";
  695. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  696. //
  697. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  698. // if(legcolor==nil)
  699. // legcolor="";
  700. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  701. //
  702. //
  703. int availability = sqlite3_column_int(statement, 5);
  704. //
  705. // int incoming_stock = sqlite3_column_int(statement, 6);
  706. char *demension = (char*)sqlite3_column_text(statement, 7);
  707. if(demension==nil)
  708. demension="";
  709. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  710. // ,,,,,,,,,
  711. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  712. if(seat_height==nil)
  713. seat_height="";
  714. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  715. char *material = (char*)sqlite3_column_text(statement, 9);
  716. if(material==nil)
  717. material="";
  718. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  719. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  720. if(box_dim==nil)
  721. box_dim="";
  722. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  723. char *volume = (char*)sqlite3_column_text(statement, 11);
  724. if(volume==nil)
  725. volume="";
  726. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  727. double weight = sqlite3_column_double(statement, 12);
  728. char *model_set = (char*)sqlite3_column_text(statement, 13);
  729. if(model_set==nil)
  730. model_set="";
  731. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  732. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  733. if(load_ability==nil)
  734. load_ability="";
  735. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  736. char *default_category = (char*)sqlite3_column_text(statement, 15);
  737. if(default_category==nil)
  738. default_category="";
  739. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  740. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  741. if(fabric_content==nil)
  742. fabric_content="";
  743. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  744. char *assembling = (char*)sqlite3_column_text(statement, 17);
  745. if(assembling==nil)
  746. assembling="";
  747. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  748. char *made_in = (char*)sqlite3_column_text(statement, 18);
  749. if(made_in==nil)
  750. made_in="";
  751. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  752. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  753. if(special_remarks==nil)
  754. special_remarks="";
  755. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  756. int stockUcom = sqlite3_column_double(statement, 20);
  757. char *product_group = (char*)sqlite3_column_text(statement, 21);
  758. if(product_group==nil)
  759. product_group="";
  760. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  761. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  762. // if(fashion_selector==nil)
  763. // fashion_selector="";
  764. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  765. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  766. if(selector_field==nil)
  767. selector_field="";
  768. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  769. char *property_field = (char*)sqlite3_column_text(statement, 23);
  770. if(property_field==nil)
  771. property_field="";
  772. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  773. char *packaging = (char*)sqlite3_column_text(statement, 24);
  774. if(packaging==nil)
  775. packaging="";
  776. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  777. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  778. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  779. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  780. [img_section setValue:model_s_img forKey:@"model_s_img"];
  781. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  782. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  783. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  784. NSString* Availability=nil;
  785. if(availability>0)
  786. Availability=[NSString stringWithFormat:@"%d",availability];
  787. else
  788. Availability = @"In Production";
  789. [img_section setValue:Availability forKey:@"Availability"];
  790. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  791. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  792. int item_id = sqlite3_column_int(statement, 25);
  793. NSString* Price=nil;
  794. if(appDelegate.bLogin==false)
  795. Price=@"Must Sign in.";
  796. else
  797. {
  798. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  799. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  800. if(price==nil)
  801. Price=@"No Price.";
  802. else
  803. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  804. }
  805. [img_section setValue:Price forKey:@"price"];
  806. [img_section setValue:nsname forKey:@"model_name"];
  807. [img_section setValue:nsdescription forKey:@"model_descrition"];
  808. [ret setObject:img_section forKey:@"img_section"];
  809. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  810. int detail0_item_count=0;
  811. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  812. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  813. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  814. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  815. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  816. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  817. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  818. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  819. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  820. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  821. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  822. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  823. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  824. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  825. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  826. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  827. {
  828. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  829. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  830. }
  831. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  832. [detail0_section setValue:@"kv" forKey:@"type"];
  833. [detail0_section setValue:@"Product Information" forKey:@"title"];
  834. [ret setObject:detail0_section forKey:@"detail_0"];
  835. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  836. // [detail1_section setValue:@"detail" forKey:@"target"];
  837. // [detail1_section setValue:@"popup" forKey:@"action"];
  838. // [detail1_section setValue:@"content" forKey:@"type"];
  839. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  840. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  841. // [detail1_section setValue:@"true" forKey:@"single_row"];
  842. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  843. [ret setObject:detail1_section forKey:@"detail_1"];
  844. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  845. [detail2_section setValue:@"detail" forKey:@"target"];
  846. [detail2_section setValue:@"popup" forKey:@"action"];
  847. [detail2_section setValue:@"content" forKey:@"type"];
  848. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  849. [detail2_section setValue:@"true" forKey:@"single_row"];
  850. [detail2_section setValue:@"local" forKey:@"data"];
  851. [ret setObject:detail2_section forKey:@"detail_2"];
  852. }
  853. sqlite3_finalize(statement);
  854. }
  855. else
  856. {
  857. [ret setValue:@"8" forKey:@"result"];
  858. }
  859. NSLog(@"count:%d",count);
  860. [iSalesDB close_db:db];
  861. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  862. return ret;
  863. }
  864. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  865. {
  866. NSString* orderCode = [params valueForKey:@"orderCode"];
  867. NSString* keyword = [params valueForKey:@"keyword"];
  868. keyword=keyword.lowercaseString;
  869. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  870. int limit = [[params valueForKey:@"limit"] intValue];
  871. int offset = [[params valueForKey:@"offset"] intValue];
  872. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  873. sqlite3 *db = [iSalesDB get_db];
  874. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  875. NSString *sqlQuery = nil;
  876. if(exactMatch )
  877. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name limit %d offset %d ;",keyword,limit, offset]; // 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 ;
  878. else
  879. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name limit %d offset %d ;",keyword,keyword,limit, offset];// 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
  880. DebugLog(@"offline_search sql:%@",sqlQuery);
  881. sqlite3_stmt * statement;
  882. [ret setValue:@"2" forKey:@"result"];
  883. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  884. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  885. // int count=0;
  886. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  887. {
  888. int i=0;
  889. while (sqlite3_step(statement) == SQLITE_ROW)
  890. {
  891. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  892. // char *name = (char*)sqlite3_column_text(statement, 1);
  893. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  894. char *name = (char*)sqlite3_column_text(statement, 0);
  895. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  896. char *description = (char*)sqlite3_column_text(statement, 1);
  897. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  898. int product_id = sqlite3_column_int(statement, 2);
  899. // char *url = (char*)sqlite3_column_text(statement, 3);
  900. // if(url==nil)
  901. // url="";
  902. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  903. int wid = sqlite3_column_int(statement, 3);
  904. int closeout = sqlite3_column_int(statement, 4);
  905. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  906. if(wid==0)
  907. [item setValue:@"false" forKey:@"wish_exists"];
  908. else
  909. [item setValue:@"true" forKey:@"wish_exists"];
  910. if(closeout==0)
  911. [item setValue:@"false" forKey:@"is_closeout"];
  912. else
  913. [item setValue:@"true" forKey:@"is_closeout"];
  914. [item addEntriesFromDictionary:imgjson];
  915. // [item setValue:nsurl forKey:@"img"];
  916. [item setValue:nsname forKey:@"fash_name"];
  917. [item setValue:nsdescription forKey:@"description"];
  918. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  919. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  920. i++;
  921. }
  922. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  923. [ret setObject:items forKey:@"items"];
  924. sqlite3_finalize(statement);
  925. }
  926. NSLog(@"count:%d",count);
  927. [iSalesDB close_db:db];
  928. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  929. return ret;
  930. }
  931. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  932. {
  933. //
  934. // NSString* user = [params valueForKey:@"user"];
  935. //
  936. // NSString* password = [params valueForKey:@"password"];
  937. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  938. ret[@"result"]=[NSNumber numberWithInt:2 ];
  939. UIApplication * app = [UIApplication sharedApplication];
  940. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  941. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  942. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  943. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  944. [appDelegate update_count_mark];
  945. appDelegate.can_show_price =false;
  946. appDelegate.can_see_price =false;
  947. appDelegate.can_create_portfolio =false;
  948. appDelegate.can_create_order =false;
  949. appDelegate.can_cancel_order =false;
  950. appDelegate.can_set_cart_price =false;
  951. appDelegate.can_delete_order =false;
  952. appDelegate.can_submit_order =false;
  953. appDelegate.can_set_tearsheet_price =false;
  954. appDelegate.can_update_contact_info = false;
  955. appDelegate.save_order_logout = false;
  956. appDelegate.submit_order_logout = false;
  957. appDelegate.alert_sold_in_quantities = false;
  958. appDelegate.ipad_perm =nil ;
  959. appDelegate.user_type = USER_ROLE_UNKNOWN;
  960. appDelegate.OrderFilter= nil;
  961. [appDelegate SetSo:nil];
  962. [appDelegate set_main_button_panel];
  963. // sqlite3 *db = [iSalesDB get_db];
  964. //
  965. //
  966. //
  967. //
  968. //
  969. // 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"]];
  970. //
  971. //
  972. //
  973. //
  974. // DebugLog(@"offline_login sql:%@",sqlQuery);
  975. // sqlite3_stmt * statement;
  976. //
  977. //
  978. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  979. //
  980. //
  981. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  982. // {
  983. //
  984. //
  985. // if (sqlite3_step(statement) == SQLITE_ROW)
  986. // {
  987. //
  988. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  989. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  990. //
  991. //
  992. //
  993. // int can_show_price = sqlite3_column_int(statement, 0);
  994. // int can_see_price = sqlite3_column_int(statement, 1);
  995. //
  996. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  997. // if(contact_id==nil)
  998. // contact_id="";
  999. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1000. //
  1001. // int user_type = sqlite3_column_int(statement, 3);
  1002. //
  1003. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1004. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1005. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1006. // int can_delete_order = sqlite3_column_int(statement, 7);
  1007. // int can_submit_order = sqlite3_column_int(statement, 8);
  1008. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1009. // int can_create_order = sqlite3_column_int(statement, 10);
  1010. //
  1011. //
  1012. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1013. // if(mode==nil)
  1014. // mode="";
  1015. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1016. //
  1017. //
  1018. // char *username = (char*)sqlite3_column_text(statement, 12);
  1019. // if(username==nil)
  1020. // username="";
  1021. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1022. //
  1023. //
  1024. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1025. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1026. // [header setValue:nscontact_id forKey:@"contact_id"];
  1027. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1028. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1029. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1030. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1031. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1032. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1033. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1034. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1035. //
  1036. // [header setValue:nsusername forKey:@"username"];
  1037. //
  1038. //
  1039. // [ret setObject:header forKey:@"header"];
  1040. // [ret setValue:nsmode forKey:@"mode"];
  1041. //
  1042. //
  1043. // }
  1044. //
  1045. //
  1046. //
  1047. // sqlite3_finalize(statement);
  1048. // }
  1049. //
  1050. //
  1051. //
  1052. // [iSalesDB close_db:db];
  1053. //
  1054. //
  1055. //
  1056. //
  1057. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1058. return ret;
  1059. }
  1060. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1061. {
  1062. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1063. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1064. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1065. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1066. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1067. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1068. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1069. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1070. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1071. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1072. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1073. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1074. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1075. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1076. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1077. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1078. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1079. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1080. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1081. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1082. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1083. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1084. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1085. [arr_name addObject:customer_first_name];
  1086. [arr_name addObject:customer_last_name];
  1087. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1088. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact) values('OFFLINE'||hex(randomblob(16)),0,'%@','%@','%@','%@','%@','%@','%@','%@')",customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name];
  1089. sqlite3 *db = [iSalesDB get_db];
  1090. int result =[iSalesDB execSql:sql_neworder db:db];
  1091. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1092. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  1093. //soId
  1094. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1095. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1096. sqlite3_stmt * statement;
  1097. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1098. {
  1099. if (sqlite3_step(statement) == SQLITE_ROW)
  1100. {
  1101. // char *name = (char*)sqlite3_column_text(statement, 1);
  1102. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1103. //ret = sqlite3_column_int(statement, 0);
  1104. char *soId = (char*)sqlite3_column_text(statement, 0);
  1105. if(soId==nil)
  1106. soId="";
  1107. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1108. [ret setValue:nssoId forKey:@"soId"];
  1109. [ret setValue:nssoId forKey:@"orderCode"];
  1110. }
  1111. sqlite3_finalize(statement);
  1112. }
  1113. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1114. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1115. [iSalesDB close_db:db];
  1116. return [RAUtils dict2data:ret];
  1117. }
  1118. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1119. {
  1120. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1121. NSString* orderCode = [params valueForKey:@"orderCode"];
  1122. sqlite3 *db = [iSalesDB get_db];
  1123. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1124. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1125. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1126. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1127. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1128. [iSalesDB close_db:db];
  1129. return [RAUtils dict2data:ret];
  1130. }
  1131. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1132. {
  1133. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1134. sqlite3 *db = [iSalesDB get_db];
  1135. int count =0;
  1136. if(params[@"count"]!=nil)
  1137. {
  1138. count = [params[@"count"] intValue];
  1139. }
  1140. NSString* product_id=params[@"product_id"];
  1141. NSString* orderCode=params[@"orderCode"];
  1142. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1143. for(int i=0;i<arr_id.count;i++)
  1144. {
  1145. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1146. int item_qty= count;
  1147. if(item_qty==0)
  1148. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1149. NSString* sql=nil;
  1150. if(_id<0)
  1151. {
  1152. // NSString* bundle_serialno= [self model_bundle:[arr_id[i] intValue] db:db];
  1153. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1154. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id) values(%@,'%@',%d,%d)",arr_id[i],orderCode,item_qty,item_id];
  1155. }
  1156. else
  1157. {
  1158. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+%d where _id=%d",item_qty,_id];
  1159. }
  1160. [iSalesDB execSql:sql db:db];
  1161. }
  1162. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1163. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1164. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1165. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1166. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1167. [iSalesDB close_db:db];
  1168. return [RAUtils dict2data:ret];
  1169. }
  1170. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1171. {
  1172. UIApplication * app = [UIApplication sharedApplication];
  1173. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1174. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1175. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  1176. sqlite3_stmt * statement;
  1177. int count=0;
  1178. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1179. {
  1180. while (sqlite3_step(statement) == SQLITE_ROW)
  1181. {
  1182. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1183. int bitem_id = sqlite3_column_int(statement, 0);
  1184. int bitem_qty = sqlite3_column_int(statement, 1);
  1185. char *name = (char*)sqlite3_column_text(statement, 2);
  1186. if(name==nil)
  1187. name="";
  1188. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1189. char *description = (char*)sqlite3_column_text(statement, 3);
  1190. if(description==nil)
  1191. description="";
  1192. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1193. double unit_price = sqlite3_column_double(statement, 4);
  1194. int use_unitprice = sqlite3_column_int(statement, 5);
  1195. if(use_unitprice!=1)
  1196. {
  1197. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1198. }
  1199. itemjson[@"model"]=nsname;
  1200. itemjson[@"description"]=nsdescription;
  1201. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1202. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1203. if(compute)
  1204. {
  1205. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1206. }
  1207. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1208. count++;
  1209. }
  1210. sqlite3_finalize(statement);
  1211. }
  1212. ret[@"count"]=@(count);
  1213. if(count==0)
  1214. return nil;
  1215. else
  1216. return ret;
  1217. }
  1218. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1219. {
  1220. //compute: add part to subtotal;
  1221. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1222. double cuft=0;
  1223. double weight=0;
  1224. int carton=0;
  1225. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  1226. sqlite3_stmt * statement;
  1227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1228. {
  1229. if (sqlite3_step(statement) == SQLITE_ROW)
  1230. {
  1231. double ulength = sqlite3_column_double(statement, 0);
  1232. double uwidth = sqlite3_column_double(statement, 1);
  1233. double uheight = sqlite3_column_double(statement, 2);
  1234. double uweight = sqlite3_column_double(statement, 3);
  1235. double mlength = sqlite3_column_double(statement, 4);
  1236. double mwidth = sqlite3_column_double(statement, 5);
  1237. double mheight = sqlite3_column_double(statement, 6);
  1238. double mweight = sqlite3_column_double(statement, 7);
  1239. double ilength = sqlite3_column_double(statement, 8);
  1240. double iwidth = sqlite3_column_double(statement, 9);
  1241. double iheight = sqlite3_column_double(statement, 10);
  1242. double iweight = sqlite3_column_double(statement, 11);
  1243. int pcs = sqlite3_column_int(statement,12);
  1244. int mpack = sqlite3_column_int(statement, 13);
  1245. int ipack = sqlite3_column_int(statement, 14);
  1246. double ucbf = sqlite3_column_double(statement, 15);
  1247. double icbf = sqlite3_column_double(statement, 16);
  1248. double mcbf = sqlite3_column_double(statement, 17);
  1249. if(ipack==0)
  1250. {
  1251. carton= count/mpack ;
  1252. weight = mweight*carton;
  1253. cuft= carton*(mlength*mwidth*mheight);
  1254. int remain=count%mpack;
  1255. if(remain==0)
  1256. {
  1257. //do nothing;
  1258. }
  1259. else
  1260. {
  1261. carton++;
  1262. weight += uweight*remain;
  1263. cuft += (ulength*uwidth*uheight)*remain;
  1264. }
  1265. }
  1266. else
  1267. {
  1268. carton = count/(mpack*ipack);
  1269. weight = mweight*carton;
  1270. cuft= carton*(mlength*mwidth*mheight);
  1271. int remain=count%(mpack*ipack);
  1272. if(remain==0)
  1273. {
  1274. // do nothing;
  1275. }
  1276. else
  1277. {
  1278. carton++;
  1279. int icarton =remain/ipack;
  1280. int iremain=remain%ipack;
  1281. weight += iweight*icarton;
  1282. cuft += (ilength*iwidth*iheight)*icarton;
  1283. if(iremain==0)
  1284. {
  1285. //do nothing;
  1286. }
  1287. else
  1288. {
  1289. weight += uweight*iremain;
  1290. cuft += (ulength*uwidth*uheight)*iremain;
  1291. }
  1292. }
  1293. }
  1294. #ifdef BUILD_NPD
  1295. cuft=ucbf*count;
  1296. weight= uweight*count;
  1297. #endif
  1298. }
  1299. sqlite3_finalize(statement);
  1300. }
  1301. if(compute)
  1302. {
  1303. NSArray * arr_count=nil;
  1304. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1305. for(int i=0;i<arr_bundle.count;i++)
  1306. {
  1307. [arr_bundle[i] intValue];
  1308. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1309. cuft+=[bundlejson[@"cuft"] doubleValue];
  1310. weight+=[bundlejson[@"weight"] doubleValue];
  1311. carton+=[bundlejson[@"carton"] intValue];
  1312. }
  1313. }
  1314. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1315. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1316. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1317. return ret;
  1318. }
  1319. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1320. {
  1321. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1322. sqlite3 *db = [iSalesDB get_db];
  1323. UIApplication * app = [UIApplication sharedApplication];
  1324. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1325. NSString* orderCode=params[@"orderCode"];
  1326. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  1327. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1328. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1329. DebugLog(@"offline_login sql:%@",sqlQuery);
  1330. sqlite3_stmt * statement;
  1331. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1332. int count=0;
  1333. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1334. if ( dbresult== SQLITE_OK)
  1335. {
  1336. while (sqlite3_step(statement) == SQLITE_ROW)
  1337. {
  1338. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1339. int product_id = sqlite3_column_int(statement, 0);
  1340. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1341. int item_id = sqlite3_column_int(statement, 7);
  1342. NSString* Price=nil;
  1343. if(str_price==nil)
  1344. {
  1345. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1346. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1347. if(price==nil)
  1348. Price=@"No Price.";
  1349. else
  1350. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1351. }
  1352. else
  1353. {
  1354. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1355. }
  1356. double discount = sqlite3_column_double(statement, 2);
  1357. int item_count = sqlite3_column_int(statement, 3);
  1358. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1359. NSString *nsline_note=nil;
  1360. if(line_note!=nil)
  1361. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1362. char *name = (char*)sqlite3_column_text(statement, 5);
  1363. NSString *nsname=nil;
  1364. if(name!=nil)
  1365. nsname= [[NSString alloc]initWithUTF8String:name];
  1366. char *description = (char*)sqlite3_column_text(statement, 6);
  1367. NSString *nsdescription=nil;
  1368. if(description!=nil)
  1369. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1370. int stockUom = sqlite3_column_int(statement, 8);
  1371. int _id = sqlite3_column_int(statement, 9);
  1372. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1373. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1374. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1375. int carton=[bsubtotaljson[@"carton"] intValue];
  1376. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1377. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1378. itemjson[@"model"]=nsname;
  1379. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1380. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1381. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1382. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1383. itemjson[@"check"]=@"true";
  1384. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1385. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1386. itemjson[@"unit_price"]=Price;
  1387. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1388. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1389. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1390. itemjson[@"note"]=nsline_note;
  1391. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1392. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1393. count++;
  1394. }
  1395. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1396. sqlite3_finalize(statement);
  1397. }
  1398. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1399. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1400. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1401. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1402. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1403. ret[@"mode"]=@"Regular Mode";
  1404. [iSalesDB close_db:db];
  1405. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  1406. DebugLog(@"general notes :%@",general_note);
  1407. ret[@"general_note"]= general_note;
  1408. return [RAUtils dict2data:ret];
  1409. }
  1410. +(NSData*) offline_login :(NSMutableDictionary *) params
  1411. {
  1412. NSString* user = [params valueForKey:@"user"];
  1413. NSString* password = [params valueForKey:@"password"];
  1414. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1415. sqlite3 *db = [iSalesDB get_db];
  1416. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1417. DebugLog(@"offline_login sql:%@",sqlQuery);
  1418. sqlite3_stmt * statement;
  1419. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1420. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1421. {
  1422. if (sqlite3_step(statement) == SQLITE_ROW)
  1423. {
  1424. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1425. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1426. int can_show_price = sqlite3_column_int(statement, 0);
  1427. int can_see_price = sqlite3_column_int(statement, 1);
  1428. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1429. if(contact_id==nil)
  1430. contact_id="";
  1431. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1432. int user_type = sqlite3_column_int(statement, 3);
  1433. int can_cancel_order = sqlite3_column_int(statement, 4);
  1434. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1435. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1436. int can_delete_order = sqlite3_column_int(statement, 7);
  1437. int can_submit_order = sqlite3_column_int(statement, 8);
  1438. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1439. int can_create_order = sqlite3_column_int(statement, 10);
  1440. char *mode = (char*)sqlite3_column_text(statement, 11);
  1441. if(mode==nil)
  1442. mode="";
  1443. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1444. char *username = (char*)sqlite3_column_text(statement, 12);
  1445. if(username==nil)
  1446. username="";
  1447. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1448. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1449. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1450. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1451. [header setValue:nscontact_id forKey:@"contact_id"];
  1452. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1453. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1454. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1455. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1456. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1457. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1458. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1459. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1460. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1461. [header setValue:nsusername forKey:@"username"];
  1462. NSError* error=nil;
  1463. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1464. [header setValue:statusFilter forKey:@"statusFilter"];
  1465. [ret setObject:header forKey:@"header"];
  1466. [ret setValue:nsmode forKey:@"mode"];
  1467. }
  1468. sqlite3_finalize(statement);
  1469. }
  1470. [iSalesDB close_db:db];
  1471. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1472. return [RAUtils dict2data:ret];
  1473. }
  1474. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1475. {
  1476. NSString* contactId = [params valueForKey:@"contactId"];
  1477. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1478. sqlite3 *db = [iSalesDB get_db];
  1479. NSString *sqlQuery = nil;
  1480. {
  1481. 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];
  1482. }
  1483. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1484. sqlite3_stmt * statement;
  1485. [ret setValue:@"2" forKey:@"result"];
  1486. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1487. {
  1488. //int i = 0;
  1489. if (sqlite3_step(statement) == SQLITE_ROW)
  1490. {
  1491. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1492. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1493. // int editable = sqlite3_column_int(statement, 0);
  1494. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1495. NSString *nscompany_name =nil;
  1496. if(company_name==nil)
  1497. nscompany_name=@"";
  1498. else
  1499. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1500. char *country = (char*)sqlite3_column_text(statement, 2);
  1501. if(country==nil)
  1502. country="";
  1503. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1504. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1505. // if(addr==nil)
  1506. // addr="";
  1507. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1508. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1509. if(zipcode==nil)
  1510. zipcode="";
  1511. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1512. char *state = (char*)sqlite3_column_text(statement, 5);
  1513. if(state==nil)
  1514. state="";
  1515. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1516. char *city = (char*)sqlite3_column_text(statement, 6);
  1517. if(city==nil)
  1518. city="";
  1519. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1520. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1521. // NSString *nscontact_name = nil;
  1522. // if(contact_name==nil)
  1523. // nscontact_name=@"";
  1524. // else
  1525. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1526. char *phone = (char*)sqlite3_column_text(statement, 8);
  1527. NSString *nsphone = nil;
  1528. if(phone==nil)
  1529. nsphone=@"";
  1530. else
  1531. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1532. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1533. if(contact_id==nil)
  1534. contact_id="";
  1535. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1536. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1537. if(addr_1==nil)
  1538. addr_1="";
  1539. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1540. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1541. if(addr_2==nil)
  1542. addr_2="";
  1543. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1544. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1545. if(addr_3==nil)
  1546. addr_3="";
  1547. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1548. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1549. if(addr_4==nil)
  1550. addr_4="";
  1551. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1552. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1553. if(first_name==nil)
  1554. first_name="";
  1555. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1556. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1557. if(last_name==nil)
  1558. last_name="";
  1559. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1560. char *fax = (char*)sqlite3_column_text(statement, 16);
  1561. NSString *nsfax = nil;
  1562. if(fax==nil)
  1563. nsfax=@"";
  1564. else
  1565. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1566. char *email = (char*)sqlite3_column_text(statement, 17);
  1567. NSString *nsemail = nil;
  1568. if(email==nil)
  1569. nsemail=@"";
  1570. else
  1571. nsemail= [[NSString alloc]initWithUTF8String:email];
  1572. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1573. NSString *nsimg_0 = nil;
  1574. if(img_0==nil)
  1575. nsimg_0=@"";
  1576. else
  1577. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1578. [self copy_bcardImg:nsimg_0];
  1579. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1580. NSString *nsimg_1 = nil;
  1581. if(img_1==nil)
  1582. nsimg_1=@"";
  1583. else
  1584. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1585. [self copy_bcardImg:nsimg_1];
  1586. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1587. NSString *nsimg_2 = nil;
  1588. if(img_2==nil)
  1589. nsimg_2=@"";
  1590. else
  1591. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1592. [self copy_bcardImg:nsimg_2];
  1593. char *price_type = (char*)sqlite3_column_text(statement, 21);
  1594. NSString *nsprice_type = nil;
  1595. if(price_type==nil)
  1596. nsprice_type=@"";
  1597. else
  1598. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  1599. char *notes = (char*)sqlite3_column_text(statement, 22);
  1600. NSString *nsnotes = nil;
  1601. if(notes==nil)
  1602. nsnotes=@"";
  1603. else
  1604. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  1605. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  1606. NSString *nssalesrep = nil;
  1607. if(salesrep==nil)
  1608. nssalesrep=@"";
  1609. else
  1610. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  1611. {
  1612. // decrypt
  1613. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  1614. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  1615. nsphone=[AESCrypt fastdecrypt:nsphone];
  1616. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1617. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  1618. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  1619. }
  1620. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1621. [arr_name addObject:nsfirst_name];
  1622. [arr_name addObject:nslast_name];
  1623. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1624. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  1625. [arr_ext addObject:nsaddr_1];
  1626. [arr_ext addObject:nsaddr_2];
  1627. [arr_ext addObject:nsaddr_3];
  1628. [arr_ext addObject:nsaddr_4];
  1629. [arr_ext addObject:@"\r\n"];
  1630. [arr_ext addObject:nscity];
  1631. [arr_ext addObject:nsstate];
  1632. [arr_ext addObject:nszipcode];
  1633. [arr_ext addObject:nscountry];
  1634. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  1635. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  1636. [item setValue:nsimg_2 forKey:@"business_card_2"];
  1637. [item setValue:nsimg_0 forKey:@"business_card_0"];
  1638. [item setValue:nscountry forKey:@"customer_country"];
  1639. [item setValue:nsphone forKey:@"customer_phone"];
  1640. [item setValue:nsimg_1 forKey:@"business_card_1"];
  1641. [item setValue:nscompany_name forKey:@"customer_name"];
  1642. [item setValue:nsprice_type forKey:@"customer_price_type"];
  1643. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  1644. [item setValue:nsext forKey:@"customer_contact_ext"];
  1645. [item setValue:nszipcode forKey:@"customer_zipcode"];
  1646. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  1647. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  1648. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  1649. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  1650. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  1651. [item setValue:nslast_name forKey:@"customer_last_name"];
  1652. [item setValue:nscity forKey:@"customer_city"];
  1653. [item setValue:nsstate forKey:@"customer_state"];
  1654. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  1655. [item setValue:contactId forKey:@"customer_cid"];
  1656. [item setValue:nscontact_name forKey:@"customer_contact"];
  1657. [item setValue:nsfax forKey:@"customer_fax"];
  1658. [item setValue:nsemail forKey:@"customer_email"];
  1659. [ret setObject:item forKey:@"customerInfo"];
  1660. // i++;
  1661. }
  1662. UIApplication * app = [UIApplication sharedApplication];
  1663. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1664. [ret setValue:appDelegate.mode forKey:@"mode"];
  1665. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  1666. sqlite3_finalize(statement);
  1667. }
  1668. [iSalesDB close_db:db];
  1669. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1670. return ret;
  1671. }
  1672. + (bool) copy_bcardImg:(NSString*) filename
  1673. {
  1674. if(filename.length==0)
  1675. return false;
  1676. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1677. bool ret=false;
  1678. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1679. NSString *cachefolder = [paths objectAtIndex:0];
  1680. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1681. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1682. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1683. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1684. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  1685. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1686. //
  1687. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1688. NSFileManager* fileManager = [NSFileManager defaultManager];
  1689. BOOL bdir=NO;
  1690. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1691. {
  1692. NSError *error = nil;
  1693. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1694. {
  1695. ret=false;
  1696. }
  1697. else
  1698. {
  1699. ret=true;
  1700. }
  1701. // NSError *error = nil;
  1702. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1703. //
  1704. // if(!bsuccess)
  1705. // {
  1706. // DebugLog(@"Create offline_createimg folder failed");
  1707. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1708. // return [RAUtils dict2data:ret];
  1709. // }
  1710. // if(bsuccess)
  1711. // {
  1712. // sqlite3 *db = [self get_db];
  1713. //
  1714. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1715. // [iSalesDB close_db:db];
  1716. // }
  1717. }
  1718. return ret;
  1719. //
  1720. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1721. // if(bsuccess)
  1722. // {
  1723. // NSError *error = nil;
  1724. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1725. // {
  1726. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1727. // }
  1728. // else
  1729. // {
  1730. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1731. //
  1732. // ret[@"img_url_aname"]=filename;
  1733. // ret[@"img_url"]=savedImagePath;
  1734. // }
  1735. // }
  1736. }
  1737. +(NSData *) offline_saveBusinesscard:(NSData *) image
  1738. {
  1739. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1740. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1741. NSString *cachefolder = [paths objectAtIndex:0];
  1742. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1743. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  1744. NSFileManager* fileManager = [NSFileManager defaultManager];
  1745. BOOL bdir=YES;
  1746. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  1747. {
  1748. NSError *error = nil;
  1749. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  1750. if(!bsuccess)
  1751. {
  1752. DebugLog(@"Create offline_createimg folder failed");
  1753. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1754. return [RAUtils dict2data:ret];
  1755. }
  1756. // if(bsuccess)
  1757. // {
  1758. // sqlite3 *db = [self get_db];
  1759. //
  1760. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  1761. // [iSalesDB close_db:db];
  1762. // }
  1763. }
  1764. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  1765. //JEPG格式
  1766. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  1767. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  1768. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  1769. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  1770. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  1771. if(bsuccess)
  1772. {
  1773. NSError *error = nil;
  1774. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  1775. {
  1776. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1777. }
  1778. else
  1779. {
  1780. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  1781. ret[@"img_url_aname"]=filename;
  1782. ret[@"img_url"]=filename;
  1783. }
  1784. }
  1785. else
  1786. {
  1787. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  1788. }
  1789. return [RAUtils dict2data:ret];
  1790. }
  1791. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  1792. {
  1793. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  1794. category = [category substringToIndex:3];
  1795. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1796. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1797. params[@"category"]= category;
  1798. ret[@"params"]= params;
  1799. [ret setValue:@"detail" forKey:@"target"];
  1800. [ret setValue:@"popup" forKey:@"action"];
  1801. [ret setValue:@"content" forKey:@"type"];
  1802. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  1803. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1804. [ret setValue:@"true" forKey:@"single_row"];
  1805. [ret setValue:@"true" forKey:@"partial_refresh"];
  1806. // sqlite3 *db = [iSalesDB get_db];
  1807. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1808. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product 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]; // 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 ;
  1809. sqlite3_stmt * statement;
  1810. int count = 0;
  1811. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  1812. // int count=0;
  1813. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1814. {
  1815. int i=0;
  1816. while (sqlite3_step(statement) == SQLITE_ROW)
  1817. {
  1818. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1819. // char *name = (char*)sqlite3_column_text(statement, 1);
  1820. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1821. char *name = (char*)sqlite3_column_text(statement, 0);
  1822. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1823. int product_id = sqlite3_column_int(statement, 1);
  1824. char *url = (char*)sqlite3_column_text(statement, 2);
  1825. if(url==nil)
  1826. url="";
  1827. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1828. [item setValue:nsurl forKey:@"picture_path"];
  1829. [item setValue:nsname forKey:@"fash_name"];
  1830. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1831. [item setValue:category forKey:@"category"];
  1832. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1833. i++;
  1834. }
  1835. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1836. sqlite3_finalize(statement);
  1837. }
  1838. NSLog(@"count:%d",count);
  1839. // [iSalesDB close_db:db];
  1840. return ret;
  1841. }
  1842. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  1843. {
  1844. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1845. [ret setValue:key forKey:@"key"];
  1846. [ret setValue:value forKey:@"val"];
  1847. return ret;
  1848. }
  1849. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  1850. {
  1851. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1852. [ret setValue:@"0" forKey:@"img_count"];
  1853. // sqlite3 *db = [iSalesDB get_db];
  1854. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  1855. sqlite3_stmt * statement;
  1856. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1857. {
  1858. int i=0;
  1859. if (sqlite3_step(statement) == SQLITE_ROW)
  1860. {
  1861. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1862. // char *name = (char*)sqlite3_column_text(statement, 1);
  1863. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1864. char *value = (char*)sqlite3_column_text(statement, 0);
  1865. if(value==nil)
  1866. value="";
  1867. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1868. char *key = (char*)sqlite3_column_text(statement, 1);
  1869. if(key==nil)
  1870. key="";
  1871. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  1872. [item setValue:nsvalue forKey:@"val"];
  1873. [item setValue:nskey forKey:@"key"];
  1874. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1875. [ret setValue:@"1" forKey:@"count"];
  1876. i++;
  1877. }
  1878. sqlite3_finalize(statement);
  1879. }
  1880. // [iSalesDB close_db:db];
  1881. return ret;
  1882. }
  1883. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  1884. {
  1885. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1886. [ret setValue:@"0" forKey:@"count"];
  1887. // sqlite3 *db = [iSalesDB get_db];
  1888. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  1889. sqlite3_stmt * statement;
  1890. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1891. {
  1892. int i=0;
  1893. while (sqlite3_step(statement) == SQLITE_ROW)
  1894. {
  1895. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1896. // char *name = (char*)sqlite3_column_text(statement, 1);
  1897. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1898. char *value = (char*)sqlite3_column_text(statement, 0);
  1899. if(value==nil)
  1900. value="";
  1901. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  1902. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  1903. if(selector_display==nil)
  1904. selector_display="";
  1905. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  1906. int product_id = sqlite3_column_int(statement, 2);
  1907. char *category = (char*)sqlite3_column_text(statement, 3);
  1908. if(category==nil)
  1909. category="";
  1910. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  1911. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  1912. [item setValue:nsvalue forKey:@"title"];
  1913. [item setValue:url forKey:@"pic_url"];
  1914. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  1915. [params setValue:@"2" forKey:@"count"];
  1916. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  1917. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  1918. [param0 setValue:@"product_id" forKey:@"name"];
  1919. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  1920. [param1 setValue:nscategory forKey:@"val"];
  1921. [param1 setValue:@"category" forKey:@"name"];
  1922. [params setObject:param0 forKey:@"param_0"];
  1923. [params setObject:param1 forKey:@"param_1"];
  1924. [item setObject:params forKey:@"params"];
  1925. [ret setValue:nsselector_display forKey:@"name"];
  1926. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1927. i++;
  1928. }
  1929. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1930. [ret setValue:@"switch" forKey:@"action"];
  1931. sqlite3_finalize(statement);
  1932. }
  1933. // [iSalesDB close_db:db];
  1934. return ret;
  1935. }
  1936. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  1937. {
  1938. // model 在 category search 显示的图片。
  1939. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  1940. // sqlite3 *db = [iSalesDB get_db];
  1941. 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];
  1942. sqlite3_stmt * statement;
  1943. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1944. {
  1945. while (sqlite3_step(statement) == SQLITE_ROW)
  1946. {
  1947. char *url = (char*)sqlite3_column_text(statement, 0);
  1948. if(url==nil)
  1949. url="";
  1950. int type = sqlite3_column_int(statement, 1);
  1951. if(type==0)
  1952. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  1953. else
  1954. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  1955. }
  1956. sqlite3_finalize(statement);
  1957. }
  1958. // [iSalesDB close_db:db];
  1959. return ret;
  1960. }
  1961. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  1962. {
  1963. int item_id=-1;
  1964. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  1965. sqlite3_stmt * statement;
  1966. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1967. {
  1968. if (sqlite3_step(statement) == SQLITE_ROW)
  1969. {
  1970. item_id = sqlite3_column_int(statement, 0);
  1971. }
  1972. sqlite3_finalize(statement);
  1973. }
  1974. return item_id;
  1975. }
  1976. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  1977. {
  1978. // NSString* ret = @"";
  1979. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  1980. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  1981. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  1982. sqlite3_stmt * statement;
  1983. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1984. {
  1985. while (sqlite3_step(statement) == SQLITE_ROW)
  1986. {
  1987. int bitem_id = sqlite3_column_int(statement, 0);
  1988. int bitem_qty = sqlite3_column_int(statement, 1);
  1989. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  1990. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  1991. }
  1992. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  1993. sqlite3_finalize(statement);
  1994. }
  1995. // if(ret==nil)
  1996. // ret=@"";
  1997. *count=arr_count;
  1998. return arr_bundle;
  1999. }
  2000. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2001. {
  2002. // get default sold qty, return -1 if model not found;
  2003. int ret = -1;
  2004. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2005. sqlite3_stmt * statement;
  2006. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2007. {
  2008. if (sqlite3_step(statement) == SQLITE_ROW)
  2009. {
  2010. ret = sqlite3_column_int(statement, 0);
  2011. }
  2012. sqlite3_finalize(statement);
  2013. }
  2014. return ret;
  2015. }
  2016. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2017. {
  2018. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2019. [ret setValue:@"0" forKey:@"img_count"];
  2020. // sqlite3 *db = [iSalesDB get_db];
  2021. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2022. sqlite3_stmt * statement;
  2023. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2024. {
  2025. int i=0;
  2026. while (sqlite3_step(statement) == SQLITE_ROW)
  2027. {
  2028. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2029. // char *name = (char*)sqlite3_column_text(statement, 1);
  2030. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2031. char *url = (char*)sqlite3_column_text(statement, 0);
  2032. if(url==nil)
  2033. url="";
  2034. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2035. [item setValue:nsurl forKey:@"s"];
  2036. [item setValue:nsurl forKey:@"l"];
  2037. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2038. i++;
  2039. }
  2040. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2041. sqlite3_finalize(statement);
  2042. }
  2043. // [iSalesDB close_db:db];
  2044. return ret;
  2045. }
  2046. #pragma mark - Jack
  2047. #warning 做SQL操作时转义!!
  2048. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2049. // "val_227" : {
  2050. // "check" : 1,
  2051. // "value" : "US United States",
  2052. // "value_id" : "228"
  2053. // },
  2054. if (!countryCode) {
  2055. countryCode = @"US";
  2056. }
  2057. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2058. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2059. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2060. int code_id = sqlite3_column_int(stmt, 3); // id
  2061. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2062. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2063. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2064. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2065. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2066. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2067. }
  2068. long n = *count;
  2069. *count = n + 1;
  2070. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2071. [container setValue:countryDic forKey:key];
  2072. }] mutableCopy];
  2073. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2074. return ret;
  2075. }
  2076. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2077. countryCode = [self translateSingleQuote:countryCode];
  2078. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2079. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2080. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2081. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2082. if (name == NULL) {
  2083. name = "";
  2084. }
  2085. if (code == NULL) {
  2086. code = "";
  2087. }
  2088. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2089. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2090. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2091. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2092. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2093. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2094. }
  2095. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2096. [container setValue:stateDic forKey:key];
  2097. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2098. DebugLog(@"query all state error: %@",err_msg);
  2099. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2100. // [stateDic setValue:@"Other" forKey:@"value"];
  2101. // [stateDic setValue:@"" forKey:@"value_id"];
  2102. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2103. //
  2104. // if (state_code && [@"" isEqualToString:state_code]) {
  2105. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2106. // }
  2107. //
  2108. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2109. // [container setValue:stateDic forKey:key];
  2110. }] mutableCopy];
  2111. // failure 可以不用了,一样的
  2112. if (ret.allKeys.count == 0) {
  2113. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2114. [stateDic setValue:@"Other" forKey:@"value"];
  2115. [stateDic setValue:@"" forKey:@"value_id"];
  2116. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2117. if (state_code && [@"" isEqualToString:state_code]) {
  2118. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2119. }
  2120. NSString *key = [NSString stringWithFormat:@"val_0"];
  2121. [ret setValue:stateDic forKey:key];
  2122. }
  2123. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2124. return ret;
  2125. }
  2126. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2127. codeId = [self translateSingleQuote:codeId];
  2128. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  2129. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2130. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2131. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  2132. if (name == NULL) {
  2133. name = "";
  2134. }
  2135. if (code == NULL) {
  2136. code = "";
  2137. }
  2138. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2139. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2140. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2141. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2142. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  2143. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2144. }
  2145. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2146. [container setValue:stateDic forKey:key];
  2147. }] mutableCopy];
  2148. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2149. return ret;
  2150. }
  2151. + (NSDictionary *)offline_getPrice {
  2152. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2153. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2154. char *name = (char *) sqlite3_column_text(stmt, 1);
  2155. int type = sqlite3_column_int(stmt, 2);
  2156. int orderBy = sqlite3_column_int(stmt, 3);
  2157. if (name == NULL) {
  2158. name = "";
  2159. }
  2160. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2161. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2162. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2163. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2164. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2165. if (orderBy == 0) {
  2166. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2167. }
  2168. [container setValue:priceDic forKey:key];
  2169. }] mutableCopy];
  2170. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2171. return ret;
  2172. }
  2173. + (NSDictionary *)offline_getSalesRep {
  2174. // 首先从offline_login表中取出sales_code
  2175. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2176. NSString *user = app.user;
  2177. user = [self translateSingleQuote:user];
  2178. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  2179. __block NSString *user_code = @"";
  2180. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2181. char *code = (char *)sqlite3_column_text(stmt, 0);
  2182. if (code == NULL) {
  2183. code = "";
  2184. }
  2185. user_code = [NSString stringWithUTF8String:code];
  2186. }];
  2187. // 再取所有salesRep
  2188. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2189. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2190. // 1 name 2 code 3 salesrep_id
  2191. char *name = (char *)sqlite3_column_text(stmt, 1);
  2192. char *code = (char *)sqlite3_column_text(stmt, 2);
  2193. int salesrep_id = sqlite3_column_int(stmt, 3);
  2194. if (name == NULL) {
  2195. name = "";
  2196. }
  2197. if (code == NULL) {
  2198. code = "";
  2199. }
  2200. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2201. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2202. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2203. // 比较code 相等则check
  2204. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  2205. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2206. }
  2207. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2208. }] mutableCopy];
  2209. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2210. return ret;
  2211. }
  2212. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2213. zipcode = [self translateSingleQuote:zipcode];
  2214. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2215. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2216. char *country = (char *)sqlite3_column_text(stmt, 0);
  2217. char *state = (char *)sqlite3_column_text(stmt, 1);
  2218. char *city = (char *)sqlite3_column_text(stmt, 2);
  2219. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2220. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2221. if (country == NULL) {
  2222. country = "";
  2223. }
  2224. if (state == NULL) {
  2225. state = "";
  2226. }
  2227. if (city == NULL) {
  2228. city = "";
  2229. }
  2230. if (country_code == NULL) {
  2231. country_code = "";
  2232. }
  2233. if (state_code == NULL) {
  2234. state_code = "";
  2235. }
  2236. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2237. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2238. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2239. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2240. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2241. }] mutableCopy];
  2242. return ret;
  2243. }
  2244. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2245. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2246. [item setValue:value forKey:valueKey];
  2247. [dic setValue:item forKey:itemKey];
  2248. }
  2249. + (NSString *)countryCodeByid:(NSString *)code_id {
  2250. NSString *ret = nil;
  2251. code_id = [self translateSingleQuote:code_id];
  2252. sqlite3 *db = [iSalesDB get_db];
  2253. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2254. sqlite3_stmt * statement;
  2255. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2256. while (sqlite3_step(statement) == SQLITE_ROW) {
  2257. char *code = (char *)sqlite3_column_text(statement, 0);
  2258. if (code == NULL) {
  2259. code = "";
  2260. }
  2261. ret = [NSString stringWithUTF8String:code];
  2262. }
  2263. sqlite3_finalize(statement);
  2264. }
  2265. [iSalesDB close_db:db];
  2266. return ret;
  2267. }
  2268. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2269. NSString *ret = nil;
  2270. code = [self translateSingleQuote:code];
  2271. sqlite3 *db = [iSalesDB get_db];
  2272. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2273. sqlite3_stmt * statement;
  2274. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2275. while (sqlite3_step(statement) == SQLITE_ROW) {
  2276. char *_id = (char *)sqlite3_column_text(statement, 0);
  2277. if (_id == NULL) {
  2278. _id = "";
  2279. }
  2280. ret = [NSString stringWithFormat:@"%s",_id];
  2281. }
  2282. sqlite3_finalize(statement);
  2283. }
  2284. [iSalesDB close_db:db];
  2285. return ret;
  2286. }
  2287. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2288. NSString *name = nil;
  2289. codeId = [self translateSingleQuote:codeId];
  2290. sqlite3 *db = [iSalesDB get_db];
  2291. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2292. sqlite3_stmt * statement;
  2293. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2294. while (sqlite3_step(statement) == SQLITE_ROW) {
  2295. char *value = (char *)sqlite3_column_text(statement, 0);
  2296. if (value == NULL) {
  2297. value = "";
  2298. }
  2299. name = [NSString stringWithUTF8String:value];
  2300. }
  2301. sqlite3_finalize(statement);
  2302. }
  2303. [iSalesDB close_db:db];
  2304. return name;
  2305. }
  2306. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2307. NSString *ret = nil;
  2308. sqlite3 *db = [iSalesDB get_db];
  2309. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  2310. sqlite3_stmt * statement;
  2311. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2312. while (sqlite3_step(statement) == SQLITE_ROW) {
  2313. char *name = (char *)sqlite3_column_text(statement, 0);
  2314. if (name == NULL) {
  2315. name = "";
  2316. }
  2317. ret = [NSString stringWithUTF8String:name];
  2318. }
  2319. sqlite3_finalize(statement);
  2320. }
  2321. [iSalesDB close_db:db];
  2322. return ret;
  2323. }
  2324. + (NSString *)salesRepCodeById:(NSString *)_id {
  2325. NSString *ret = nil;
  2326. _id = [self translateSingleQuote:_id];
  2327. sqlite3 *db = [iSalesDB get_db];
  2328. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2329. sqlite3_stmt * statement;
  2330. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2331. while (sqlite3_step(statement) == SQLITE_ROW) {
  2332. char *rep = (char *)sqlite3_column_text(statement, 0);
  2333. if (rep == NULL) {
  2334. rep = "";
  2335. }
  2336. ret = [NSString stringWithUTF8String:rep];
  2337. }
  2338. sqlite3_finalize(statement);
  2339. }
  2340. [iSalesDB close_db:db];
  2341. return ret;
  2342. }
  2343. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2344. char *tx = (char *)sqlite3_column_text(stmt, col);
  2345. if (tx == NULL) {
  2346. tx = "";
  2347. }
  2348. NSString *text = [NSString stringWithFormat:@"%s",tx];
  2349. if (!text) {
  2350. text = @"";
  2351. }
  2352. // 将字符全部为' '的字符串干掉
  2353. int spaceCount = 0;
  2354. for (int i = 0; i < text.length; i++) {
  2355. if ([text characterAtIndex:i] == ' ') {
  2356. spaceCount++;
  2357. }
  2358. }
  2359. if (spaceCount == text.length) {
  2360. text = @"";
  2361. }
  2362. return text;
  2363. }
  2364. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  2365. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2366. NSData *data = [NSData dataWithContentsOfFile:path];
  2367. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2368. return ret;
  2369. }
  2370. + (NSString *)textFileName:(NSString *)name {
  2371. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2372. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  2373. if (!text) {
  2374. text = @"";
  2375. }
  2376. return text;
  2377. }
  2378. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  2379. return [[dic objectForKey:key] mutableCopy];
  2380. }
  2381. + (id)translateSingleQuote:(NSString *)string {
  2382. if ([string isKindOfClass:[NSString class]])
  2383. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2384. return string;
  2385. }
  2386. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  2387. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  2388. }
  2389. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  2390. NSString* ret= nil;
  2391. NSString *sqlQuery = nil;
  2392. sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  2393. sqlite3_stmt * statement;
  2394. // int count=0;
  2395. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2396. {
  2397. if (sqlite3_step(statement) == SQLITE_ROW)
  2398. {
  2399. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2400. if(imgurl==nil)
  2401. imgurl="";
  2402. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2403. ret=nsimgurl;
  2404. }
  2405. sqlite3_finalize(statement);
  2406. }
  2407. else
  2408. {
  2409. [ret setValue:@"8" forKey:@"result"];
  2410. }
  2411. // [iSalesDB close_db:db];
  2412. DebugLog(@"data string: %@",ret );
  2413. return ret;
  2414. }
  2415. #pragma mark contact list
  2416. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  2417. {
  2418. // contactType = "Sales_Order_Customer";
  2419. // limit = 25;
  2420. // offset = 0;
  2421. // password = 123456;
  2422. // "price_name" = 16;
  2423. // user = EvanK;
  2424. sqlite3 *db = [iSalesDB get_db];
  2425. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  2426. if (contactType) {
  2427. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  2428. } else {
  2429. contactType = @"1 = 1";
  2430. }
  2431. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  2432. DebugLog(@"offline contact list keyword: %@",keyword);
  2433. // advanced search
  2434. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  2435. if (contact_name) {
  2436. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2437. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  2438. } else {
  2439. contact_name = @"";
  2440. }
  2441. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  2442. if (customer_phone) {
  2443. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2444. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  2445. } else {
  2446. customer_phone = @"";
  2447. }
  2448. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  2449. if (customer_fax) {
  2450. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2451. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  2452. } else {
  2453. customer_fax = @"";
  2454. }
  2455. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  2456. if (customer_zipcode) {
  2457. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2458. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  2459. } else {
  2460. customer_zipcode = @"";
  2461. }
  2462. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  2463. if (customer_sales_rep) {
  2464. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2465. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  2466. } else {
  2467. customer_sales_rep = @"";
  2468. }
  2469. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  2470. if (customer_state) {
  2471. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2472. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  2473. } else {
  2474. customer_state = @"";
  2475. }
  2476. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  2477. if (customer_name) {
  2478. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2479. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  2480. } else {
  2481. customer_name = @"";
  2482. }
  2483. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  2484. if (customer_country) {
  2485. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2486. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  2487. } else {
  2488. customer_country = @"";
  2489. }
  2490. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  2491. if (customer_cid) {
  2492. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2493. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  2494. } else {
  2495. customer_cid = @"";
  2496. }
  2497. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  2498. if (customer_city) {
  2499. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2500. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  2501. } else {
  2502. customer_city = @"";
  2503. }
  2504. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  2505. if (customer_address) {
  2506. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2507. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  2508. } else {
  2509. customer_address = @"";
  2510. }
  2511. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  2512. if (customer_email) {
  2513. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2514. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  2515. } else {
  2516. customer_email = @"";
  2517. }
  2518. NSString *price_name = [params valueForKey:@"price_name"];
  2519. if (price_name) {
  2520. if ([price_name containsString:@","]) {
  2521. // 首先从 price表中查处name
  2522. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  2523. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  2524. for (int i = 1;i < pArray.count;i++) {
  2525. NSString *p = pArray[i];
  2526. [mutablePStr appendFormat:@" or type = %@ ",p];
  2527. }
  2528. [mutablePStr appendString:@";"];
  2529. __block NSMutableArray *price_name_array = [NSMutableArray array];
  2530. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2531. char *name = (char *)sqlite3_column_text(stmt, 0);
  2532. if (!name)
  2533. name = "";
  2534. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  2535. }];
  2536. // 再根据name 拼sql
  2537. NSMutableString *mutable_price_name = [NSMutableString string];
  2538. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  2539. for (int i = 1; i < price_name_array.count; i++) {
  2540. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  2541. }
  2542. [mutable_price_name appendString:@")"];
  2543. price_name = mutable_price_name;
  2544. } else {
  2545. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  2546. if ([price_name isEqualToString:@""]) {
  2547. price_name = @"";
  2548. } else {
  2549. __block NSString *price;
  2550. price_name = [self translateSingleQuote:price_name];
  2551. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2552. char *p = (char *)sqlite3_column_text(stmt, 0);
  2553. if (p == NULL) {
  2554. p = "";
  2555. }
  2556. price = [NSString stringWithUTF8String:p];
  2557. }];
  2558. if ([price isEqualToString:@""]) {
  2559. price_name = @"";
  2560. } else {
  2561. price = [self translateSingleQuote:price];
  2562. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  2563. }
  2564. }
  2565. }
  2566. } else {
  2567. price_name = @"";
  2568. }
  2569. int limit = [[params valueForKey:@"limit"] intValue];
  2570. int offset = [[params valueForKey:@"offset"] intValue];
  2571. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2572. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  2573. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  2574. // int result= [iSalesDB AddExFunction:db];
  2575. int count =0;
  2576. NSString *sqlQuery = nil;
  2577. if(keyword.length==0)
  2578. {
  2579. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  2580. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  2581. sqlQuery = sql;
  2582. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  2583. }
  2584. else
  2585. {
  2586. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  2587. keyword = keyword.lowercaseString;
  2588. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2589. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  2590. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  2591. }
  2592. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2593. sqlite3_stmt * statement;
  2594. [ret setValue:@"2" forKey:@"result"];
  2595. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  2596. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2597. {
  2598. int i = 0;
  2599. while (sqlite3_step(statement) == SQLITE_ROW)
  2600. {
  2601. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2602. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2603. int editable = sqlite3_column_int(statement, 0);
  2604. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2605. NSString *nscompany_name =nil;
  2606. if(company_name==nil)
  2607. nscompany_name=@"";
  2608. else
  2609. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  2610. char *country = (char*)sqlite3_column_text(statement, 2);
  2611. if(country==nil)
  2612. country="";
  2613. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2614. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2615. // if(addr==nil)
  2616. // addr="";
  2617. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2618. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2619. if(zipcode==nil)
  2620. zipcode="";
  2621. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2622. char *state = (char*)sqlite3_column_text(statement, 5);
  2623. if(state==nil)
  2624. state="";
  2625. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2626. char *city = (char*)sqlite3_column_text(statement, 6);
  2627. if(city==nil)
  2628. city="";
  2629. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2630. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2631. // NSString *nscontact_name = nil;
  2632. // if(contact_name==nil)
  2633. // nscontact_name=@"";
  2634. // else
  2635. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2636. char *phone = (char*)sqlite3_column_text(statement, 8);
  2637. NSString *nsphone = nil;
  2638. if(phone==nil)
  2639. nsphone=@"";
  2640. else
  2641. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2642. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2643. if(contact_id==nil)
  2644. contact_id="";
  2645. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2646. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2647. if(addr_1==nil)
  2648. addr_1="";
  2649. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2650. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2651. if(addr_2==nil)
  2652. addr_2="";
  2653. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2654. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2655. if(addr_3==nil)
  2656. addr_3="";
  2657. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2658. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2659. if(addr_4==nil)
  2660. addr_4="";
  2661. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2662. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2663. if(first_name==nil)
  2664. first_name="";
  2665. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2666. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2667. if(last_name==nil)
  2668. last_name="";
  2669. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2670. char *fax = (char*)sqlite3_column_text(statement, 16);
  2671. NSString *nsfax = nil;
  2672. if(fax==nil)
  2673. nsfax=@"";
  2674. else
  2675. {
  2676. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2677. if(nsfax.length>0)
  2678. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  2679. }
  2680. char *email = (char*)sqlite3_column_text(statement, 17);
  2681. NSString *nsemail = nil;
  2682. if(email==nil)
  2683. nsemail=@"";
  2684. else
  2685. {
  2686. nsemail= [[NSString alloc]initWithUTF8String:email];
  2687. if(nsemail.length>0)
  2688. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  2689. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  2690. }
  2691. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2692. [arr_name addObject:nsfirst_name];
  2693. [arr_name addObject:nslast_name];
  2694. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2695. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  2696. {
  2697. // decrypt
  2698. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2699. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2700. nsphone=[AESCrypt fastdecrypt:nsphone];
  2701. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2702. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2703. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2704. }
  2705. [arr_addr addObject:nscompany_name];
  2706. [arr_addr addObject:nscontact_name];
  2707. [arr_addr addObject:@"<br>"];
  2708. [arr_addr addObject:nsaddr_1];
  2709. [arr_addr addObject:nsaddr_2];
  2710. [arr_addr addObject:nsaddr_3];
  2711. [arr_addr addObject:nsaddr_4];
  2712. //[arr_addr addObject:nsaddr];
  2713. [arr_addr addObject:nszipcode];
  2714. [arr_addr addObject:nscity];
  2715. [arr_addr addObject:nsstate];
  2716. [arr_addr addObject:nscountry];
  2717. [arr_addr addObject:@"<br>"];
  2718. [arr_addr addObject:nsphone];
  2719. [arr_addr addObject:nsfax];
  2720. [arr_addr addObject:nsemail];
  2721. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  2722. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  2723. [item setValue:name forKey:@"name"];
  2724. [item setValue:nscontact_id forKey:@"contact_id"];
  2725. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  2726. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2727. i++;
  2728. }
  2729. UIApplication * app = [UIApplication sharedApplication];
  2730. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2731. [ret setValue:appDelegate.mode forKey:@"mode"];
  2732. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  2733. sqlite3_finalize(statement);
  2734. }
  2735. [iSalesDB close_db:db];
  2736. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2737. return ret;
  2738. }
  2739. #pragma mark contact Advanced search
  2740. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  2741. {
  2742. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  2743. return [RAUtils dict2data:contactAdvanceDic];
  2744. }
  2745. #pragma mark create new contact
  2746. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  2747. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  2748. NSData *data = [NSData dataWithContentsOfFile:path];
  2749. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2750. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2751. NSString *countryCode = nil;
  2752. NSString *countryCode_id = nil;
  2753. NSString *stateCode = nil;
  2754. NSString *city = nil;
  2755. NSString *zipCode = nil;
  2756. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  2757. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  2758. NSString *code_id = params[@"country"];
  2759. countryCode_id = code_id;
  2760. countryCode = [self countryCodeByid:code_id];
  2761. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  2762. NSString *zip_code = params[@"zipcode"];
  2763. zipCode = zip_code;
  2764. countryCode_id = params[@"country"];
  2765. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  2766. countryCode = [dic valueForKey:@"country_code"];
  2767. if (!countryCode) {
  2768. countryCode = @"US";
  2769. }
  2770. stateCode = [dic valueForKey:@"state_code"];
  2771. city = [dic valueForKey:@"city"];
  2772. // zip code
  2773. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  2774. [zipDic setValue:zipCode forKey:@"value"];
  2775. [section_0 setValue:zipDic forKey:@"item_10"];
  2776. }
  2777. } else {
  2778. // default: US United States
  2779. countryCode = @"US";
  2780. countryCode_id = @"228";
  2781. }
  2782. // country
  2783. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  2784. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  2785. // state
  2786. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  2787. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  2788. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  2789. // city
  2790. if (city) {
  2791. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  2792. [cityDic setValue:city forKey:@"value"];
  2793. [section_0 setValue:cityDic forKey:@"item_12"];
  2794. }
  2795. // price type
  2796. NSDictionary *priceDic = [self offline_getPrice];
  2797. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  2798. // Sales Rep
  2799. NSDictionary *repDic = [self offline_getSalesRep];
  2800. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  2801. [ret setValue:section_0 forKey:@"section_0"];
  2802. return [RAUtils dict2data:ret];
  2803. }
  2804. #pragma mark save
  2805. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update {
  2806. NSString *addr = nil;
  2807. NSString *contact_name = nil;
  2808. NSString *companyName = [params objectForKey:@"company"];
  2809. if (companyName) {
  2810. companyName = [AESCrypt fastencrypt:companyName];
  2811. } else {
  2812. companyName = @"";
  2813. }
  2814. DebugLog(@"company");
  2815. companyName = [self translateSingleQuote:companyName];
  2816. NSString *addr1 = [params objectForKey:@"address"];
  2817. NSString *addr2 = [params objectForKey:@"address2"];
  2818. NSString *addr3 = [params objectForKey:@"address_3"];
  2819. NSString *addr4 = [params objectForKey:@"address_4"];
  2820. if (!addr2) {
  2821. addr2 = @"";
  2822. }
  2823. if (!addr3) {
  2824. addr3 = @"";
  2825. }
  2826. if (!addr4) {
  2827. addr4 = @"";
  2828. }
  2829. if (!addr1) {
  2830. addr1 = @"";
  2831. }
  2832. DebugLog(@"addr");
  2833. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  2834. addr = [AESCrypt fastencrypt:addr];
  2835. addr = [self translateSingleQuote:addr];
  2836. if (addr1 && ![addr1 isEqualToString:@""]) {
  2837. addr1 = [AESCrypt fastencrypt:addr1];
  2838. }
  2839. addr1 = [self translateSingleQuote:addr1];
  2840. addr2 = [self translateSingleQuote:addr2];
  2841. addr3 = [self translateSingleQuote:addr3];
  2842. addr4 = [self translateSingleQuote:addr4];
  2843. NSString *country = [params objectForKey:@"country"];
  2844. if (country) {
  2845. country = [self countryNameByCountryCodeId:country];
  2846. } else {
  2847. country = @"";
  2848. }
  2849. DebugLog(@"country");
  2850. country = [self translateSingleQuote:country];
  2851. NSString *state = [params objectForKey:@"state"];
  2852. if (!state) {
  2853. state = @"";
  2854. }
  2855. DebugLog(@"state");
  2856. state = [self translateSingleQuote:state];
  2857. NSString *city = [params objectForKey:@"city"];
  2858. if (!city) {
  2859. city = @"";
  2860. }
  2861. city = [self translateSingleQuote:city];
  2862. NSString *zipcode = [params objectForKey:@"zipcode"];
  2863. if (!zipcode) {
  2864. zipcode = @"";
  2865. }
  2866. DebugLog(@"zip");
  2867. zipcode = [self translateSingleQuote:zipcode];
  2868. NSString *fistName = [params objectForKey:@"firstname"];
  2869. if (!fistName) {
  2870. fistName = @"";
  2871. }
  2872. NSString *lastName = [params objectForKey:@"lastname"];
  2873. if (!lastName) {
  2874. lastName = @"";
  2875. }
  2876. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  2877. DebugLog(@"contact_name");
  2878. contact_name = [self translateSingleQuote:contact_name];
  2879. fistName = [self translateSingleQuote:fistName];
  2880. lastName = [self translateSingleQuote:lastName];
  2881. NSString *phone = [params objectForKey:@"phone"];
  2882. if (phone) {
  2883. phone = [AESCrypt fastencrypt:phone];
  2884. } else {
  2885. phone = @"";
  2886. }
  2887. DebugLog(@"PHONE");
  2888. phone = [self translateSingleQuote:phone];
  2889. NSString *fax = [params objectForKey:@"fax"];
  2890. if (!fax) {
  2891. fax = @"";
  2892. }
  2893. DebugLog(@"FAX");
  2894. fax = [self translateSingleQuote:fax];
  2895. NSString *email = [params objectForKey:@"email"];
  2896. if (!email) {
  2897. email = @"";
  2898. }
  2899. DebugLog(@"EMAIL:%@",email);
  2900. email = [self translateSingleQuote:email];
  2901. NSString *notes = [params objectForKey:@"contact_notes"];
  2902. if (!notes) {
  2903. notes = @"";
  2904. }
  2905. DebugLog(@"NOTE:%@",notes);
  2906. notes = [self translateSingleQuote:notes];
  2907. NSString *price = [params objectForKey:@"price_name"];
  2908. if (price) {
  2909. price = [self priceNameByPriceId:price];
  2910. } else {
  2911. price = @"";
  2912. }
  2913. DebugLog(@"PRICE");
  2914. price = [self translateSingleQuote:price];
  2915. NSString *salesRep = [params objectForKey:@"sales_rep"];
  2916. if (salesRep) {
  2917. salesRep = [self salesRepCodeById:salesRep];
  2918. } else {
  2919. salesRep = @"";
  2920. }
  2921. salesRep = [self translateSingleQuote:salesRep];
  2922. NSString *img = [params objectForKey:@"business_card"];
  2923. NSArray *array = [img componentsSeparatedByString:@","];
  2924. NSString *img_0 = array[0];
  2925. if (!img_0) {
  2926. img_0 = @"";
  2927. }
  2928. img_0 = [self translateSingleQuote:img_0];
  2929. NSString *img_1 = array[1];
  2930. if (!img_1) {
  2931. img_1 = @"";
  2932. }
  2933. img_1 = [self translateSingleQuote:img_1];
  2934. NSString *img_2 = array[2];
  2935. if (!img_2) {
  2936. img_2 = @"";
  2937. }
  2938. img_2 = [self translateSingleQuote:img_2];
  2939. NSString *contact_id = [NSUUID UUID].UUIDString;
  2940. NSMutableDictionary *sync_dic = [params mutableCopy];
  2941. NSString *sync_data = nil;
  2942. NSString *sql = nil;
  2943. if (update){
  2944. contact_id = [params objectForKey:@"contact_id"];
  2945. if (!contact_id) {
  2946. contact_id = @"";
  2947. }
  2948. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2949. sync_data = [RAUtils dict2string:sync_dic];
  2950. sync_data = [self translateSingleQuote:sync_data];
  2951. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  2952. } else {
  2953. contact_id = [self translateSingleQuote:contact_id];
  2954. [sync_dic setValue:contact_id forKey:@"contact_id"];
  2955. sync_data = [RAUtils dict2string:sync_dic];
  2956. sync_data = [self translateSingleQuote:sync_data];
  2957. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',1,1,0,1,0,1,'%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,sync_data,contact_name,addr];
  2958. }
  2959. int result = [iSalesDB execSql:sql];
  2960. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  2961. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  2962. }
  2963. #pragma mark save new contact
  2964. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  2965. {
  2966. return [self offline_saveContact:params update:NO];
  2967. }
  2968. #pragma mark edit contact
  2969. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  2970. {
  2971. // {
  2972. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  2973. // password = 123456;
  2974. // user = EvanK;
  2975. // }
  2976. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  2977. NSData *data = [NSData dataWithContentsOfFile:path];
  2978. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2979. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  2980. NSString *countryCode = nil;
  2981. NSString *countryCode_id = nil;
  2982. NSString *stateCode = nil;
  2983. /*------contact infor------*/
  2984. __block NSString *country = nil;
  2985. __block NSString *company_name = nil;
  2986. __block NSString *contact_id = params[@"contact_id"];
  2987. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  2988. __block NSString *zipcode = nil;
  2989. __block NSString *state = nil; // state_code
  2990. __block NSString *city = nil; //
  2991. __block NSString *firt_name,*last_name;
  2992. __block NSString *phone,*fax,*email;
  2993. __block NSString *notes,*price_type,*sales_rep;
  2994. __block NSString *img_0,*img_1,*img_2;
  2995. contact_id = [self translateSingleQuote:contact_id];
  2996. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  2997. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2998. country = [self textAtColumn:0 statement:stmt]; // country name
  2999. company_name = [self textAtColumn:1 statement:stmt];
  3000. addr_1 = [self textAtColumn:2 statement:stmt];
  3001. addr_2 = [self textAtColumn:3 statement:stmt];
  3002. addr_3 = [self textAtColumn:4 statement:stmt];
  3003. addr_4 = [self textAtColumn:5 statement:stmt];
  3004. zipcode = [self textAtColumn:6 statement:stmt];
  3005. state = [self textAtColumn:7 statement:stmt]; // state code
  3006. city = [self textAtColumn:8 statement:stmt];
  3007. firt_name = [self textAtColumn:9 statement:stmt];
  3008. last_name = [self textAtColumn:10 statement:stmt];
  3009. phone = [self textAtColumn:11 statement:stmt];
  3010. fax = [self textAtColumn:12 statement:stmt];
  3011. email = [self textAtColumn:13 statement:stmt];
  3012. notes = [self textAtColumn:14 statement:stmt];
  3013. price_type = [self textAtColumn:15 statement:stmt]; // name
  3014. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3015. img_0 = [self textAtColumn:17 statement:stmt];
  3016. img_1 = [self textAtColumn:18 statement:stmt];
  3017. img_2 = [self textAtColumn:19 statement:stmt];
  3018. }];
  3019. // decrypt
  3020. if (company_name) {
  3021. company_name = [AESCrypt fastdecrypt:company_name];
  3022. }
  3023. if (addr_1) {
  3024. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3025. }
  3026. if (phone) {
  3027. phone = [AESCrypt fastdecrypt:phone];
  3028. }
  3029. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3030. countryCode = [iSalesDB jk_queryText:countrySql];
  3031. stateCode = state;
  3032. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3033. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3034. NSString *code_id = params[@"country"];
  3035. countryCode_id = code_id;
  3036. countryCode = [self countryCodeByid:code_id];
  3037. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3038. NSString *zip_code = params[@"zipcode"];
  3039. zipcode = zip_code;
  3040. countryCode_id = params[@"country"];
  3041. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3042. countryCode = [dic valueForKey:@"country_code"];
  3043. stateCode = [dic valueForKey:@"state_code"];
  3044. city = [dic valueForKey:@"city"];
  3045. // zip code
  3046. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3047. [zipDic setValue:zipcode forKey:@"value"];
  3048. [section_0 setValue:zipDic forKey:@"item_8"];
  3049. }
  3050. }
  3051. // 0 Country
  3052. // 1 Company Name
  3053. // 2 Contact ID
  3054. // 3 Picture
  3055. // 4 Address 1
  3056. // 5 Address 2
  3057. // 6 Address 3
  3058. // 7 Address 4
  3059. // 8 Zip Code
  3060. // 9 State/Province
  3061. // 10 City
  3062. // 11 Contact First Name
  3063. // 12 Contact Last Name
  3064. // 13 Phone
  3065. // 14 Fax
  3066. // 15 Email
  3067. // 16 Contact Notes
  3068. // 17 Price Type
  3069. // 18 Sales Rep
  3070. // country
  3071. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3072. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3073. // company
  3074. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3075. // contact_id
  3076. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3077. // picture
  3078. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3079. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3080. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3081. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3082. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3083. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3084. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3085. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  3086. // addr 1 2 3 4
  3087. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  3088. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  3089. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  3090. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  3091. // zip code
  3092. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  3093. // state
  3094. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3095. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  3096. // city
  3097. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  3098. // first last
  3099. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  3100. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  3101. // phone fax email
  3102. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  3103. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  3104. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  3105. // notes
  3106. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  3107. // price
  3108. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  3109. for (NSString *key in priceDic.allKeys) {
  3110. if ([key containsString:@"val_"]) {
  3111. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  3112. if ([dic[@"value"] isEqualToString:price_type]) {
  3113. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3114. [priceDic setValue:dic forKey:key];
  3115. }
  3116. }
  3117. }
  3118. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3119. // Sales Rep
  3120. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  3121. for (NSString *key in repDic.allKeys) {
  3122. if ([key containsString:@"val_"]) {
  3123. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  3124. NSString *value = dic[@"value"];
  3125. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  3126. if (code && [code isEqualToString:sales_rep]) {
  3127. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3128. [repDic setValue:dic forKey:key];
  3129. }
  3130. }
  3131. }
  3132. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  3133. [ret setValue:section_0 forKey:@"section_0"];
  3134. return [RAUtils dict2data:ret];
  3135. }
  3136. #pragma mark save contact
  3137. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  3138. {
  3139. return [self offline_saveContact:params update:YES];
  3140. }
  3141. #pragma mark category
  3142. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3143. if (ck) {
  3144. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  3145. for (NSString *key in res.allKeys) {
  3146. if (![key isEqualToString:@"count"]) {
  3147. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  3148. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3149. if ([val[@"value"] isEqualToString:ck]) {
  3150. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3151. }
  3152. [res setValue:val forKey:key];
  3153. }
  3154. }
  3155. [dic setValue:res forKey:valueKey];
  3156. }
  3157. }
  3158. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  3159. {
  3160. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3161. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3162. NSString* category = [params valueForKey:@"category"];
  3163. if (!category || [category isEqualToString:@""]) {
  3164. category = @"%";
  3165. }
  3166. category = [self translateSingleQuote:category];
  3167. int limit = [[params valueForKey:@"limit"] intValue];
  3168. int offset = [[params valueForKey:@"offset"] intValue];
  3169. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3170. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3171. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3172. sqlite3 *db = [iSalesDB get_db];
  3173. // [iSalesDB AddExFunction:db];
  3174. int count;
  3175. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  3176. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name limit %d offset %d ;",category,limit, offset];
  3177. // {
  3178. // alert = SP;
  3179. // available = "Available Now";
  3180. // bestseller = No;
  3181. // category = 001;
  3182. // "custom_id" = 0;
  3183. // limit = 20;
  3184. // offset = 0;
  3185. // password = 123456;
  3186. // price = "Display All";
  3187. // "price_template" = 0;
  3188. // "sold_by_qty" = "Display All";
  3189. // "sort_by" = 0;
  3190. // user = EvanK;
  3191. // }
  3192. double price_min = 0;
  3193. double price_max = 0;
  3194. if ([params.allKeys containsObject:@"alert"]) {
  3195. // alert
  3196. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3197. NSString *alert = params[@"alert"];
  3198. if ([alert isEqualToString:@"Display All"]) {
  3199. alert = [NSString stringWithFormat:@""];
  3200. } else {
  3201. alert = [self translateSingleQuote:alert];
  3202. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3203. }
  3204. // available
  3205. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3206. NSString *available = params[@"available"];
  3207. NSString *available_condition;
  3208. if ([available isEqualToString:@"Display All"]) {
  3209. available_condition = @"";
  3210. } else if ([available isEqualToString:@"Available Now"]) {
  3211. available_condition = @"and availability > 0";
  3212. } else {
  3213. available_condition = @"and availability == 0";
  3214. }
  3215. // best seller
  3216. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3217. NSString *best_seller = @"";
  3218. NSString *order_best_seller = @"m.name asc";
  3219. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3220. best_seller = @"and best_seller > 0";
  3221. order_best_seller = @"m.best_seller desc,m.name asc";
  3222. }
  3223. // price
  3224. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3225. NSString *price = params[@"price"];
  3226. price_min = 0;
  3227. price_max = MAXFLOAT;
  3228. if (appDelegate.user) {
  3229. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3230. NSMutableString *priceName = [NSMutableString string];
  3231. for (int i = 0; i < priceTypeArray.count; i++) {
  3232. NSString *pricetype = priceTypeArray[i];
  3233. pricetype = [self translateSingleQuote:pricetype];
  3234. if (i == 0) {
  3235. [priceName appendFormat:@"'%@'",pricetype];
  3236. } else {
  3237. [priceName appendFormat:@",'%@'",pricetype];
  3238. }
  3239. }
  3240. if ([price isEqualToString:@"Display All"]) {
  3241. price = [NSString stringWithFormat:@""];
  3242. } else if([price containsString:@"+"]){
  3243. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3244. price_min = [price doubleValue];
  3245. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3246. } else {
  3247. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3248. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3249. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3250. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3251. }
  3252. } else {
  3253. price = @"";
  3254. }
  3255. // sold_by_qty : Sold in quantities of %@
  3256. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3257. NSString *qty = params[@"sold_by_qty"];
  3258. if ([qty isEqualToString:@"Display All"]) {
  3259. qty = @"";
  3260. } else {
  3261. qty = [self translateSingleQuote:qty];
  3262. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3263. }
  3264. // cate
  3265. category = [self translateSingleQuote:category];
  3266. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  3267. // where bestseller > 0 order by bestseller desc
  3268. // sql query: alert availability(int) best_seller(int) price qty
  3269. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  3270. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3271. }
  3272. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3273. if (!appDelegate.user) {
  3274. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3275. }
  3276. [ret setValue:filter forKey:@"filter"];
  3277. DebugLog(@"offline_category sql:%@",sqlQuery);
  3278. sqlite3_stmt * statement;
  3279. [ret setValue:@"2" forKey:@"result"];
  3280. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3281. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3282. // int count=0;
  3283. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3284. {
  3285. int i=0;
  3286. while (sqlite3_step(statement) == SQLITE_ROW)
  3287. {
  3288. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3289. char *name = (char*)sqlite3_column_text(statement, 0);
  3290. if(name==nil)
  3291. name="";
  3292. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3293. char *description = (char*)sqlite3_column_text(statement, 1);
  3294. if(description==nil)
  3295. description="";
  3296. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3297. int product_id = sqlite3_column_int(statement, 2);
  3298. int wid = sqlite3_column_int(statement, 3);
  3299. int closeout = sqlite3_column_int(statement, 4);
  3300. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3301. if(wid==0)
  3302. [item setValue:@"false" forKey:@"wish_exists"];
  3303. else
  3304. [item setValue:@"true" forKey:@"wish_exists"];
  3305. if(closeout==0)
  3306. [item setValue:@"false" forKey:@"is_closeout"];
  3307. else
  3308. [item setValue:@"true" forKey:@"is_closeout"];
  3309. [item addEntriesFromDictionary:imgjson];
  3310. // [item setValue:nsurl forKey:@"img"];
  3311. [item setValue:nsname forKey:@"name"];
  3312. [item setValue:nsdescription forKey:@"description"];
  3313. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3314. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3315. i++;
  3316. }
  3317. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3318. [ret setObject:items forKey:@"items"];
  3319. sqlite3_finalize(statement);
  3320. } else {
  3321. DebugLog(@"nothing...");
  3322. }
  3323. DebugLog(@"count:%d",count);
  3324. [iSalesDB close_db:db];
  3325. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3326. return ret;
  3327. }
  3328. # pragma mark item search
  3329. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  3330. {
  3331. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3332. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  3333. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3334. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3335. // category
  3336. NSDictionary *category_menu = [self offline_category_menu];
  3337. [filter setValue:category_menu forKey:@"category"];
  3338. NSString* where= nil;
  3339. NSString* orderby= @"m.name";
  3340. if (!filterSearch) {
  3341. int covertype = [[params valueForKey:@"covertype"] intValue];
  3342. switch (covertype) {
  3343. case 0:
  3344. {
  3345. where=@"m.category like'%%#005#%%'";
  3346. break;
  3347. }
  3348. case 1:
  3349. {
  3350. where=@"m.alert like '%QS%'";
  3351. break;
  3352. }
  3353. case 2:
  3354. {
  3355. where=@"m.availability>0";
  3356. break;
  3357. }
  3358. case 3:
  3359. {
  3360. where=@"m.best_seller>0";
  3361. orderby=@"m.best_seller desc,m.name asc";
  3362. break;
  3363. }
  3364. default:
  3365. where=@"1=1";
  3366. break;
  3367. }
  3368. }
  3369. int limit = [[params valueForKey:@"limit"] intValue];
  3370. int offset = [[params valueForKey:@"offset"] intValue];
  3371. sqlite3 *db = [iSalesDB get_db];
  3372. // [iSalesDB AddExFunction:db];
  3373. int count;
  3374. NSString *sqlQuery = nil;
  3375. where = [where stringByAppendingString:@" and m.is_active = 1"];
  3376. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from product m left join wishlist w on m.product_id=w.product_id where %@ order by %@ limit %d offset %d ;",where,orderby,limit, offset];
  3377. double price_min = 0;
  3378. double price_max = 0;
  3379. if (filterSearch) {
  3380. // alert
  3381. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3382. NSString *alert = params[@"alert"];
  3383. if ([alert isEqualToString:@"Display All"]) {
  3384. alert = [NSString stringWithFormat:@""];
  3385. } else {
  3386. alert = [self translateSingleQuote:alert];
  3387. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3388. }
  3389. // available
  3390. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3391. NSString *available = params[@"available"];
  3392. NSString *available_condition;
  3393. if ([available isEqualToString:@"Display All"]) {
  3394. available_condition = @"";
  3395. } else if ([available isEqualToString:@"Available Now"]) {
  3396. available_condition = @"and availability > 0";
  3397. } else {
  3398. available_condition = @"and availability == 0";
  3399. }
  3400. // best seller
  3401. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3402. NSString *best_seller = @"";
  3403. NSString *order_best_seller = @"m.name asc";
  3404. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3405. best_seller = @"and best_seller > 0";
  3406. order_best_seller = @"m.best_seller desc,m.name asc";
  3407. }
  3408. // price
  3409. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3410. NSString *price = params[@"price"];
  3411. price_min = 0;
  3412. price_max = MAXFLOAT;
  3413. if (appDelegate.user) {
  3414. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3415. NSMutableString *priceName = [NSMutableString string];
  3416. for (int i = 0; i < priceTypeArray.count; i++) {
  3417. NSString *pricetype = priceTypeArray[i];
  3418. pricetype = [self translateSingleQuote:pricetype];
  3419. if (i == 0) {
  3420. [priceName appendFormat:@"'%@'",pricetype];
  3421. } else {
  3422. [priceName appendFormat:@",'%@'",pricetype];
  3423. }
  3424. }
  3425. if ([price isEqualToString:@"Display All"]) {
  3426. price = [NSString stringWithFormat:@""];
  3427. } else if([price containsString:@"+"]){
  3428. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3429. price_min = [price doubleValue];
  3430. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3431. } else {
  3432. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3433. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3434. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3435. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3436. }
  3437. } else {
  3438. price = @"";
  3439. }
  3440. // sold_by_qty : Sold in quantities of %@
  3441. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3442. NSString *qty = params[@"sold_by_qty"];
  3443. if ([qty isEqualToString:@"Display All"]) {
  3444. qty = @"";
  3445. } else {
  3446. qty = [self translateSingleQuote:qty];
  3447. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3448. }
  3449. // category
  3450. NSString *category_id = params[@"ctgId"];
  3451. NSMutableArray *cate_id_array = nil;
  3452. NSMutableString *cateWhere = [NSMutableString string];
  3453. if ([category_id isEqualToString:@""] || !category_id) {
  3454. [cateWhere appendString:@"1 = 1"];
  3455. } else {
  3456. if ([category_id containsString:@","]) {
  3457. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  3458. } else {
  3459. cate_id_array = [@[category_id] mutableCopy];
  3460. }
  3461. /*-----------*/
  3462. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  3463. for (int i = 0; i < cate_id_array.count; i++) {
  3464. for (NSString *key0 in cateDic.allKeys) {
  3465. if ([key0 containsString:@"category_"]) {
  3466. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  3467. for (NSString *key1 in category0.allKeys) {
  3468. if ([key1 containsString:@"category_"]) {
  3469. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  3470. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3471. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  3472. cate_id_array[i] = [category1 objectForKey:@"id"];
  3473. if (i == 0) {
  3474. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  3475. } else {
  3476. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  3477. }
  3478. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3479. [category0 setValue:category1 forKey:key1];
  3480. [cateDic setValue:category0 forKey:key0];
  3481. }
  3482. }
  3483. }
  3484. }
  3485. }
  3486. }
  3487. [filter setValue:cateDic forKey:@"category"];
  3488. }
  3489. // where bestseller > 0 order by bestseller desc
  3490. // sql query: alert availability(int) best_seller(int) price qty
  3491. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id order by %@ limit %d offset %d;",cateWhere,best_seller,alert,available_condition,qty,price,order_best_seller,limit,offset];
  3492. // count
  3493. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3494. }
  3495. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  3496. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3497. if (!appDelegate.user) {
  3498. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3499. }
  3500. [ret setValue:filter forKey:@"filter"];
  3501. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  3502. sqlite3_stmt * statement;
  3503. [ret setValue:@"2" forKey:@"result"];
  3504. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3505. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3506. // int count=0;
  3507. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3508. {
  3509. int i=0;
  3510. while (sqlite3_step(statement) == SQLITE_ROW)
  3511. {
  3512. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3513. char *name = (char*)sqlite3_column_text(statement, 0);
  3514. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3515. char *description = (char*)sqlite3_column_text(statement, 1);
  3516. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3517. int product_id = sqlite3_column_int(statement, 2);
  3518. int wid = sqlite3_column_int(statement, 3);
  3519. int closeout = sqlite3_column_int(statement, 4);
  3520. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3521. if(wid==0)
  3522. [item setValue:@"false" forKey:@"wish_exists"];
  3523. else
  3524. [item setValue:@"true" forKey:@"wish_exists"];
  3525. if(closeout==0)
  3526. [item setValue:@"false" forKey:@"is_closeout"];
  3527. else
  3528. [item setValue:@"true" forKey:@"is_closeout"];
  3529. [item addEntriesFromDictionary:imgjson];
  3530. // [item setValue:nsurl forKey:@"img"];
  3531. [item setValue:nsname forKey:@"fash_name"];
  3532. [item setValue:nsdescription forKey:@"description"];
  3533. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3534. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3535. i++;
  3536. }
  3537. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3538. [ret setObject:items forKey:@"items"];
  3539. sqlite3_finalize(statement);
  3540. }
  3541. [iSalesDB close_db:db];
  3542. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3543. return ret;
  3544. }
  3545. #pragma mark order detail
  3546. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  3547. if (str1.length == 0) {
  3548. str1 = @"&nbsp";
  3549. }
  3550. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  3551. return str;
  3552. }
  3553. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  3554. {
  3555. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  3556. [fromformatter setDateFormat:from];
  3557. NSDate *date = [fromformatter dateFromString:datetime];
  3558. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  3559. [toformatter setDateFormat:to];
  3560. NSString * ret = [toformatter stringFromDate:date];
  3561. return ret;
  3562. }
  3563. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  3564. // 把毫秒去掉
  3565. if ([dateTime containsString:@"."]) {
  3566. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  3567. }
  3568. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  3569. formatter.dateFormat = formate;
  3570. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  3571. NSDate *date = [formatter dateFromString:dateTime];
  3572. formatter.dateFormat = newFormate;
  3573. return [formatter stringFromDate:date];
  3574. }
  3575. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  3576. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  3577. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  3578. }
  3579. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  3580. {
  3581. DebugLog(@"offline oderdetail params: %@",params);
  3582. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3583. int orderId = [params[@"orderId"] intValue];
  3584. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  3585. // decrypt card number and card security code
  3586. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  3587. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  3588. sqlite3 *db = [iSalesDB get_db];
  3589. sqlite3_stmt * statement;
  3590. NSString *customerID = nil; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  3591. NSString *nssoid = nil;
  3592. NSString* orderinfo = nil;
  3593. NSString *moreInfo = nil;
  3594. double handlingFee = 0;
  3595. double payments_and_credist = 0;
  3596. double totalPrice = 0;
  3597. double shippingFee = 0;
  3598. double lift_gate = 0;
  3599. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  3600. {
  3601. if (sqlite3_step(statement) == SQLITE_ROW)
  3602. {
  3603. int order_id = sqlite3_column_int(statement, 0);
  3604. NSString *sign_url = [self textAtColumn:51 statement:statement];
  3605. ret[@"sign_url"] = sign_url;
  3606. char *soid = (char*)sqlite3_column_text(statement, 1);
  3607. if(soid==nil)
  3608. soid= "";
  3609. nssoid= [[NSString alloc]initWithUTF8String:soid];
  3610. // so#
  3611. ret[@"so#"] = nssoid;
  3612. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  3613. if(poNumber==nil)
  3614. poNumber= "";
  3615. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  3616. char *create_time = (char*)sqlite3_column_text(statement, 3);
  3617. if(create_time==nil)
  3618. create_time= "";
  3619. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  3620. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  3621. int status = sqlite3_column_int(statement, 4);
  3622. int erpStatus = sqlite3_column_int(statement, 49);
  3623. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  3624. // status
  3625. if (status > 1 && status != 3) {
  3626. status = erpStatus;
  3627. } else if (status == 3) {
  3628. status = 15;
  3629. }
  3630. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  3631. ret[@"order_status"] = nsstatus;
  3632. char *company_name = (char*)sqlite3_column_text(statement, 5);
  3633. if(company_name==nil)
  3634. company_name= "";
  3635. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  3636. // company name
  3637. ret[@"company_name"] = nscompany_name;
  3638. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  3639. if(customer_contact==nil)
  3640. customer_contact= "";
  3641. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  3642. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  3643. if(addr_1==nil)
  3644. addr_1="";
  3645. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3646. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  3647. if(addr_2==nil)
  3648. addr_2="";
  3649. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3650. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  3651. if(addr_3==nil)
  3652. addr_3="";
  3653. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3654. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  3655. if(addr_4==nil)
  3656. addr_4="";
  3657. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3658. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3659. [arr_addr addObject:nsaddr_1];
  3660. [arr_addr addObject:nsaddr_2];
  3661. [arr_addr addObject:nsaddr_3];
  3662. [arr_addr addObject:nsaddr_4];
  3663. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  3664. char *logist = (char*)sqlite3_column_text(statement, 11);
  3665. if(logist==nil)
  3666. logist= "";
  3667. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  3668. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  3669. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  3670. shipping = @"Shipping To Be Quoted";
  3671. } else {
  3672. shippingFee = sqlite3_column_double(statement, 12);
  3673. }
  3674. // Shipping
  3675. ret[@"Shipping"] = shipping;
  3676. int have_lift_gate = sqlite3_column_int(statement, 17);
  3677. lift_gate = sqlite3_column_double(statement, 13);
  3678. // Liftgate Fee(No loading dock)
  3679. if (!have_lift_gate) {
  3680. lift_gate = 0;
  3681. }
  3682. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  3683. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  3684. if(general_notes==nil)
  3685. general_notes= "";
  3686. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  3687. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  3688. if(internal_notes==nil)
  3689. internal_notes= "";
  3690. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  3691. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  3692. if(payment_type==nil)
  3693. payment_type= "";
  3694. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  3695. // order info
  3696. orderinfo = [self textFileName:@"order_info.html"];
  3697. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  3698. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  3699. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  3700. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  3701. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  3702. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  3703. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  3704. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  3705. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  3706. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  3707. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  3708. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  3709. NSString *payment = nil;
  3710. if([nspayment_type isEqualToString:@"Credit Card"])
  3711. {
  3712. payment = [self textFileName:@"creditcardpayment.html"];
  3713. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  3714. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  3715. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  3716. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  3717. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  3718. NSString *card_type = [self textAtColumn:42 statement:statement];
  3719. if (card_type.length > 0) { // 显示星号
  3720. card_type = @"****";
  3721. }
  3722. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  3723. if (card_number.length > 0 && card_number.length > 4) {
  3724. for (int i = 0; i < card_number.length - 4; i++) {
  3725. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  3726. }
  3727. } else {
  3728. card_number = @"";
  3729. }
  3730. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  3731. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  3732. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  3733. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  3734. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  3735. card_expiration = @"****";
  3736. }
  3737. NSString *card_city = [self textAtColumn:46 statement:statement];
  3738. NSString *card_state = [self textAtColumn:47 statement:statement];
  3739. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  3740. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  3741. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  3742. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  3743. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  3744. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  3745. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  3746. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  3747. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  3748. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  3749. }
  3750. else
  3751. {
  3752. payment=[self textFileName:@"normalpayment.html"];
  3753. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  3754. }
  3755. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  3756. ret[@"result"]= [NSNumber numberWithInt:2];
  3757. // more info
  3758. moreInfo = [self textFileName:@"more_info.html"];
  3759. /*****ship to******/
  3760. // ShipToCompany_or_&nbsp
  3761. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  3762. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  3763. NSString *shipToName = [self textAtColumn:19 statement:statement];
  3764. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  3765. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  3766. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  3767. /*****ship from******/
  3768. // ShipFromCompany_or_&nbsp
  3769. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  3770. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  3771. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  3772. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  3773. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  3774. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  3775. /*****freight to******/
  3776. // FreightBillToCompany_or_&nbsp
  3777. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  3778. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  3779. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  3780. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  3781. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  3782. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  3783. /*****merchandise to******/
  3784. // MerchandiseBillToCompany_or_&nbsp
  3785. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  3786. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  3787. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  3788. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  3789. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  3790. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  3791. /*****return to******/
  3792. // ReturnToCompany_or_&nbsp
  3793. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  3794. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  3795. NSString *returnToName = [self textAtColumn:31 statement:statement];
  3796. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  3797. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  3798. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  3799. //
  3800. DebugLog(@"more info : %@",moreInfo);
  3801. // handling fee
  3802. handlingFee = sqlite3_column_double(statement, 33);
  3803. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  3804. // customer info
  3805. customerID = [self textAtColumn:36 statement:statement];
  3806. // mode
  3807. ret[@"mode"] = appDelegate.mode;
  3808. // model_count
  3809. ret[@"model_count"] = @(0);
  3810. }
  3811. sqlite3_finalize(statement);
  3812. }
  3813. [iSalesDB close_db:db];
  3814. NSDictionary *contactInfo = [self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]];
  3815. ret[@"customerInfo"] = contactInfo;
  3816. // models
  3817. if (nssoid) {
  3818. __block double TotalCuft = 0;
  3819. __block double TotalWeight = 0;
  3820. __block int TotalCarton = 0;
  3821. __block double allItemPrice = 0;
  3822. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  3823. sqlite3 *db1 = [iSalesDB get_db];
  3824. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3825. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3826. int product_id = sqlite3_column_int(stmt, 0);
  3827. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  3828. int item_id = sqlite3_column_int(stmt, 7);
  3829. NSString* Price=nil;
  3830. if(str_price==nil)
  3831. {
  3832. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  3833. if(price==nil)
  3834. Price=@"No Price.";
  3835. else
  3836. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3837. }
  3838. else
  3839. {
  3840. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3841. }
  3842. double discount = sqlite3_column_double(stmt, 2);
  3843. int item_count = sqlite3_column_int(stmt, 3);
  3844. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  3845. NSString *nsline_note=nil;
  3846. if(line_note!=nil)
  3847. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3848. // char *name = (char*)sqlite3_column_text(stmt, 5);
  3849. // NSString *nsname = nil;
  3850. // if(name!=nil)
  3851. // nsname= [[NSString alloc]initWithUTF8String:name];
  3852. NSString *nsname = [self textAtColumn:5 statement:stmt];
  3853. // char *description = (char*)sqlite3_column_text(stmt, 6);
  3854. // NSString *nsdescription=nil;
  3855. // if(description!=nil)
  3856. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  3857. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  3858. // int stockUom = sqlite3_column_int(stmt, 8);
  3859. // int _id = sqlite3_column_int(stmt, 9);
  3860. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  3861. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3862. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3863. int carton=[bsubtotaljson[@"carton"] intValue];
  3864. TotalCuft += cuft;
  3865. TotalWeight += weight;
  3866. TotalCarton += carton;
  3867. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  3868. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  3869. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  3870. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3871. itemjson[@"note"]=nsline_note;
  3872. itemjson[@"origin_price"] = Price;
  3873. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  3874. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  3875. itemjson[@"order_item_status"] = @""; // 暂时不处理
  3876. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  3877. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  3878. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  3879. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  3880. if(itemjson[@"combine"] != nil)
  3881. {
  3882. // int citem=0;
  3883. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  3884. for(int bc=0;bc<bcount;bc++)
  3885. {
  3886. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  3887. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  3888. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  3889. subTotal += uprice * modulus * item_count;
  3890. }
  3891. }
  3892. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  3893. NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  3894. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  3895. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  3896. allItemPrice += subTotal;
  3897. }];
  3898. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  3899. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  3900. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  3901. // payments/Credits
  3902. // payments_and_credist = sqlite3_column_double(statement, 34);
  3903. payments_and_credist = allItemPrice;
  3904. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  3905. // // total
  3906. // totalPrice = sqlite3_column_double(statement, 35);
  3907. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  3908. } else {
  3909. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  3910. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  3911. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  3912. // payments/Credits
  3913. payments_and_credist = 0;
  3914. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  3915. }
  3916. // total
  3917. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  3918. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  3919. ret[@"order_info"]= orderinfo;
  3920. ret[@"more_order_info"] = moreInfo;
  3921. return [RAUtils dict2data:ret];
  3922. }
  3923. #pragma mark order list
  3924. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  3925. NSString *nsstatus = @"";
  3926. switch (status) {
  3927. case 0:
  3928. {
  3929. nsstatus=@"Temp Order";
  3930. break;
  3931. }
  3932. case 1:
  3933. {
  3934. nsstatus=@"Saved Order";
  3935. break;
  3936. }
  3937. case 2: {
  3938. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  3939. break;
  3940. }
  3941. case 3:
  3942. case 15:
  3943. {
  3944. nsstatus=@"Cancelled";
  3945. break;
  3946. }
  3947. case 10:
  3948. {
  3949. nsstatus=@"Quote Submitted";
  3950. break;
  3951. }
  3952. case 11:
  3953. {
  3954. nsstatus=@"Sales Order Submitted";
  3955. break;
  3956. }
  3957. case 12:
  3958. {
  3959. nsstatus=@"Processing";
  3960. break;
  3961. }
  3962. case 13:
  3963. {
  3964. nsstatus=@"Shipped";
  3965. break;
  3966. }
  3967. case 14:
  3968. {
  3969. nsstatus=@"Closed";
  3970. break;
  3971. }
  3972. default:
  3973. break;
  3974. }
  3975. return nsstatus;
  3976. }
  3977. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  3978. double total = 0;
  3979. __block double payments_and_credist = 0;
  3980. __block double allItemPrice = 0;
  3981. // sqlite3 *db1 = [iSalesDB get_db];
  3982. if (so_id) {
  3983. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  3984. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3985. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3986. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3987. int product_id = sqlite3_column_int(stmt, 0);
  3988. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  3989. int item_id = sqlite3_column_int(stmt, 7);
  3990. NSString* Price=nil;
  3991. if(str_price==nil)
  3992. {
  3993. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  3994. if(price==nil)
  3995. Price=@"No Price.";
  3996. else
  3997. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3998. }
  3999. else
  4000. {
  4001. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4002. }
  4003. int discount = sqlite3_column_int(stmt, 2);
  4004. int item_count = sqlite3_column_int(stmt, 3);
  4005. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4006. itemjson[@"The unit price"]=Price;
  4007. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4008. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4009. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4010. if(itemjson[@"combine"] != nil)
  4011. {
  4012. // int citem=0;
  4013. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4014. for(int bc=0;bc<bcount;bc++)
  4015. {
  4016. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4017. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4018. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4019. subTotal += uprice * modulus * item_count;
  4020. }
  4021. }
  4022. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4023. allItemPrice += subTotal;
  4024. }];
  4025. payments_and_credist = allItemPrice;
  4026. } else {
  4027. // payments/Credits
  4028. payments_and_credist = 0;
  4029. }
  4030. // lift_gate handlingFee shippingFee
  4031. __block double lift_gate = 0;
  4032. __block double handlingFee = 0;
  4033. __block double shippingFee = 0;
  4034. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  4035. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4036. int have_lift_gate = sqlite3_column_int(stmt, 0);
  4037. if (have_lift_gate) {
  4038. lift_gate = sqlite3_column_double(stmt, 1);
  4039. }
  4040. handlingFee = sqlite3_column_double(stmt, 2);
  4041. shippingFee = sqlite3_column_double(stmt, 3);
  4042. }];
  4043. // total
  4044. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4045. if (close) {
  4046. [iSalesDB close_db:db1];
  4047. }
  4048. return total;
  4049. }
  4050. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  4051. {
  4052. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4053. int limit = [[params valueForKey:@"limit"] intValue];
  4054. int offset = [[params valueForKey:@"offset"] intValue];
  4055. NSString* keyword = [params valueForKey:@"keyWord"];
  4056. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  4057. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  4058. NSString* where=@"1 = 1";
  4059. if(keyword.length>0)
  4060. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  4061. if (orderStatus.length > 0) {
  4062. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  4063. if (order_status_array.count == 1) {
  4064. int status_value = [[order_status_array firstObject] integerValue];
  4065. if (status_value <= 1 || status_value == 3) {
  4066. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  4067. } else {
  4068. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  4069. }
  4070. } else if (order_status_array.count > 1) {
  4071. for (int i = 0; i < order_status_array.count;i++) {
  4072. NSString *status = order_status_array[i];
  4073. NSString *condition = @" or";
  4074. if (i == 0) {
  4075. condition = @" and (";
  4076. }
  4077. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  4078. int status_value = [status integerValue];
  4079. if (status_value <= 1 || status_value == 3) {
  4080. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  4081. } else {
  4082. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  4083. }
  4084. }
  4085. where = [where stringByAppendingString:@" )"];
  4086. }
  4087. }
  4088. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  4089. DebugLog(@"order list sql: %@",sqlQuery);
  4090. sqlite3 *db = [iSalesDB get_db];
  4091. sqlite3_stmt * statement;
  4092. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4093. {
  4094. int count=0;
  4095. while (sqlite3_step(statement) == SQLITE_ROW)
  4096. {
  4097. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  4098. int order_id = sqlite3_column_double(statement, 0);
  4099. char *soid = (char*)sqlite3_column_text(statement, 1);
  4100. if(soid==nil)
  4101. soid= "";
  4102. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  4103. int status = sqlite3_column_double(statement, 2);
  4104. int erpStatus = sqlite3_column_double(statement, 9);
  4105. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  4106. if(sales_rep==nil)
  4107. sales_rep= "";
  4108. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  4109. char *create_by = (char*)sqlite3_column_text(statement, 4);
  4110. if(create_by==nil)
  4111. create_by= "";
  4112. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  4113. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4114. if(company_name==nil)
  4115. company_name= "";
  4116. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4117. char *create_time = (char*)sqlite3_column_text(statement, 6);
  4118. if(create_time==nil)
  4119. create_time= "";
  4120. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4121. DebugLog(@"time: %@",nscreate_time);
  4122. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  4123. // double total_price = sqlite3_column_double(statement, 7);
  4124. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid]db:db close:NO];
  4125. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  4126. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4127. // ": "JH",
  4128. // "": "$8307.00",
  4129. // "": "MOB1608050001",
  4130. // "": "ArpithaT",
  4131. // "": "1st Stage Property Transformations",
  4132. // "": "JANICE SUTTON",
  4133. // "": 2255,
  4134. // "": "08/02/2016 09:49:18",
  4135. // "": 1,
  4136. // "": "Saved Order"
  4137. // "": "1470384050483",
  4138. // "model_count": "6 / 28"
  4139. item[@"sales_rep"]= nssales_rep;
  4140. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  4141. item[@"so#"]= nssoid;
  4142. item[@"create_by"]= nscreate_by;
  4143. item[@"customer_name"]= nscompany_name;
  4144. item[@"customer_contact"] = customer_contact;
  4145. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  4146. item[@"purchase_time"]= nscreate_time;
  4147. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  4148. item[@"order_status"]= nsstatus;
  4149. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  4150. // item[@"model_count"]
  4151. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  4152. count++;
  4153. }
  4154. ret[@"count"]= [NSNumber numberWithInt:count];
  4155. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  4156. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  4157. ret[@"result"]= [NSNumber numberWithInt:2];
  4158. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  4159. ret[@"time_zone"] = @"PST";
  4160. sqlite3_finalize(statement);
  4161. }
  4162. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  4163. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  4164. [iSalesDB close_db:db];
  4165. return [RAUtils dict2data:ret];
  4166. }
  4167. #pragma mark update gnotes
  4168. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  4169. {
  4170. DebugLog(@"params: %@",params);
  4171. // comments = Meyoyoyoyoyoyoy;
  4172. // orderCode = MOB1608110001;
  4173. // password = 123456;
  4174. // user = EvanK;
  4175. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments"],[self valueInParams:params key:@"orderCode"]];
  4176. int ret = [iSalesDB execSql:sql];
  4177. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4178. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4179. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4180. [dic setValue:@"160409" forKey:@"min_ver"];
  4181. return [RAUtils dict2data:dic];
  4182. }
  4183. #pragma mark move to wishlist
  4184. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  4185. {
  4186. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4187. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4188. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id = %@;",orderCode,_id];
  4189. sqlite3 *db = [iSalesDB get_db];
  4190. __block NSString *product_id = nil;
  4191. __block int item_count = 0;
  4192. __block NSString *item_id = nil;
  4193. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4194. product_id = [self textAtColumn:0 statement:stmt];
  4195. item_count = sqlite3_column_int(stmt, 1);
  4196. item_id = [self textAtColumn:2 statement:stmt];
  4197. }];
  4198. //
  4199. // NSString *contactIdSql = [NSString stringWithFormat:@"select customer_cid from offline_order where so_id = '%@';",orderCode];
  4200. // __block NSString *contact_id = nil;
  4201. // [iSalesDB jk_query:contactIdSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4202. // contact_id = [self textAtColumn:0 statement:stmt];
  4203. // }];
  4204. NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  4205. int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  4206. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:nil];
  4207. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4208. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4209. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4210. [dic setValue:@"160409" forKey:@"min_ver"];
  4211. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  4212. // 删除
  4213. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id = %@;",_id];
  4214. [iSalesDB execSql:deleteSql db:db];
  4215. [iSalesDB close_db:db];
  4216. return [RAUtils dict2data:dic];
  4217. }
  4218. #pragma mark cart delete
  4219. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  4220. {
  4221. // cartItemId = 548;
  4222. // orderCode = MOB1608110001;
  4223. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4224. NSString *orderCode = [self valueInParams:params key:@"MOB1608110001"];
  4225. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where _id = %@ and so_no = '%@';",_id,orderCode];
  4226. int ret = [iSalesDB execSql:sql];
  4227. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4228. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4229. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4230. [dic setValue:@"160409" forKey:@"min_ver"];
  4231. return [RAUtils dict2data:dic];
  4232. }
  4233. #pragma mark set price
  4234. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  4235. {
  4236. DebugLog(@"cart set price params: %@",params);
  4237. // "cartitem_id" = 1;
  4238. // discount = "0.000000";
  4239. // "item_note" = "";
  4240. // price = "269.000000";
  4241. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4242. NSString *notes = [self valueInParams:params key:@"item_note"];
  4243. NSString *discount = [self valueInParams:params key:@"discount"];
  4244. NSString *price = [self valueInParams:params key:@"price"];
  4245. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  4246. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f where _id = %@",price,discount.doubleValue,_id];
  4247. int ret = [iSalesDB execSql:sql];
  4248. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4249. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4250. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4251. [dic setValue:@"160409" forKey:@"min_ver"];
  4252. return [RAUtils dict2data:dic];
  4253. }
  4254. #pragma mark set line notes
  4255. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  4256. {
  4257. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4258. NSString *notes = [self valueInParams:params key:@"notes"];
  4259. notes = [self translateSingleQuote:notes];
  4260. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  4261. int ret = [iSalesDB execSql:sql];
  4262. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4263. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4264. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4265. [dic setValue:@"160409" forKey:@"min_ver"];
  4266. return [RAUtils dict2data:dic];
  4267. }
  4268. #pragma mark set qty
  4269. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  4270. {
  4271. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4272. int item_count = [params[@"inputInt"] integerValue];
  4273. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  4274. int ret = [iSalesDB execSql:sql];
  4275. __block int item_id = 0;
  4276. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4277. item_id = sqlite3_column_int(stmt, 0);
  4278. }];
  4279. sqlite3 *db = [iSalesDB get_db];
  4280. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4281. [iSalesDB close_db:db];
  4282. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4283. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4284. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4285. [dic setValue:@"160409" forKey:@"min_ver"];
  4286. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  4287. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  4288. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  4289. return [RAUtils dict2data:dic];
  4290. }
  4291. #pragma mark place order
  4292. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  4293. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4294. if (aname) {
  4295. [dic setValue:aname forKey:@"aname"];
  4296. }
  4297. if (control) {
  4298. [dic setValue:control forKey:@"control"];
  4299. }
  4300. if (keyboard) {
  4301. [dic setValue:keyboard forKey:@"keyboard"];
  4302. }
  4303. if (name) {
  4304. [dic setValue:name forKey:@"name"];
  4305. }
  4306. if (value) {
  4307. [dic setValue:value forKey:@"value"];
  4308. }
  4309. return dic;
  4310. }
  4311. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  4312. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4313. orderCode = [self translateSingleQuote:orderCode];
  4314. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  4315. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,c.email,decrypt(c.phone),c.fax,c.zipcode,c.city,c.state,c.country from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  4316. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4317. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  4318. NSString *img0 = [self textAtColumn:1 statement:stmt];
  4319. NSString *img1 = [self textAtColumn:2 statement:stmt];
  4320. NSString *img2 = [self textAtColumn:3 statement:stmt];
  4321. NSString *company_name = [self textAtColumn:4 statement:stmt];
  4322. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  4323. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  4324. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  4325. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  4326. NSString *first_name = [self textAtColumn:9 statement:stmt];
  4327. NSString *last_name = [self textAtColumn:10 statement:stmt];
  4328. NSString *email = [self textAtColumn:11 statement:stmt];
  4329. NSString *phone = [self textAtColumn:12 statement:stmt];
  4330. NSString *fax = [self textAtColumn:13 statement:stmt];
  4331. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  4332. NSString *city = [self textAtColumn:15 statement:stmt];
  4333. NSString *state = [self textAtColumn:16 statement:stmt];
  4334. NSString *country = [self textAtColumn:17 statement:stmt];
  4335. NSString *name = [first_name stringByAppendingFormat:@" %@",last_name];
  4336. // contact id
  4337. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  4338. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  4339. [contact_id_dic setValue:@"text" forKey:@"control"];
  4340. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  4341. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  4342. [contact_id_dic setValue:@"true" forKey:@"required"];
  4343. [contact_id_dic setValue:contact_id forKey:@"value"];
  4344. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  4345. // business card
  4346. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  4347. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  4348. [business_card_dic setValue:@"img" forKey:@"control"];
  4349. [business_card_dic setValue:@"1" forKey:@"disable"];
  4350. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  4351. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  4352. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  4353. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  4354. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  4355. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  4356. [business_card_dic setValue:@"business_card" forKey:@"name"];
  4357. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  4358. // fax
  4359. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4360. [customer_dic setValue:fax_dic forKey:@"item_10"];
  4361. // zipcode
  4362. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  4363. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  4364. // city
  4365. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  4366. [customer_dic setValue:city_dic forKey:@"item_12"];
  4367. // state
  4368. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  4369. [customer_dic setValue:state_dic forKey:@"item_13"];
  4370. // country
  4371. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  4372. [customer_dic setValue:country_dic forKey:@"item_14"];
  4373. // company name
  4374. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  4375. [customer_dic setValue:company_dic forKey:@"item_2"];
  4376. // addr_1
  4377. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  4378. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  4379. // addr_2
  4380. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  4381. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  4382. // addr_3
  4383. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  4384. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  4385. // addr_4
  4386. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  4387. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  4388. // Contact
  4389. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  4390. [customer_dic setValue:contact_dic forKey:@"item_7"];
  4391. // email
  4392. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4393. [customer_dic setValue:email_dic forKey:@"item_8"];
  4394. // phone
  4395. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4396. [customer_dic setValue:phone_dic forKey:@"item_9"];
  4397. // title
  4398. [customer_dic setValue:@"Customer" forKey:@"title"];
  4399. // count
  4400. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  4401. }];
  4402. return customer_dic;
  4403. }
  4404. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4405. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4406. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4407. orderCode = [self translateSingleQuote:orderCode];
  4408. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  4409. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4410. NSString *cid = [self textAtColumn:0 statement:stmt];
  4411. NSString *name = [self textAtColumn:1 statement:stmt];
  4412. NSString *ext = [self textAtColumn:2 statement:stmt];
  4413. NSString *contact = [self textAtColumn:3 statement:stmt];
  4414. NSString *email = [self textAtColumn:4 statement:stmt];
  4415. NSString *fax = [self textAtColumn:5 statement:stmt];
  4416. NSString *phone = [self textAtColumn:6 statement:stmt];
  4417. // count
  4418. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4419. // title
  4420. [dic setValue:@"Ship To" forKey:@"title"];
  4421. // choose
  4422. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4423. [choose_dic setValue:@"choose" forKey:@"aname"];
  4424. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4425. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  4426. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4427. @"key_map" : @{@"receive_cid" : @"customer_cid",
  4428. @"receive_contact" : @"customer_contact",
  4429. @"receive_email" : @"customer_email",
  4430. @"receive_ext" : @"customer_contact_ext",
  4431. @"receive_fax" : @"customer_fax",
  4432. @"receive_name" : @"customer_name",
  4433. @"receive_phone" : @"customer_phone"},
  4434. @"refresh" : [NSNumber numberWithInteger:1],
  4435. @"type" : @"pull"};
  4436. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  4437. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  4438. @"name" : @"Add new address",
  4439. @"refresh" : [NSNumber numberWithInteger:1],
  4440. @"value" : @"new_addr"
  4441. };
  4442. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  4443. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  4444. @"key_map" : @{@"receive_cid" : @"customer_cid",
  4445. @"receive_contact" : @"customer_contact",
  4446. @"receive_email" : @"customer_email",
  4447. @"receive_ext" : @"customer_contact_ext",
  4448. @"receive_fax" : @"customer_fax",
  4449. @"receive_name" : @"customer_name",
  4450. @"receive_phone" : @"customer_phone"},
  4451. @"name" : @"select_ship_to",
  4452. @"refresh" : [NSNumber numberWithInteger:1],
  4453. @"value" : @"Sales_Order_Ship_To"};
  4454. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  4455. [dic setValue:choose_dic forKey:@"item_0"];
  4456. // contact id
  4457. NSDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4458. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4459. [contact_id_dic setValue:@"true" forKey:@"required"];
  4460. [dic setValue:contact_id_dic forKey:@"item_1"];
  4461. // company name
  4462. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4463. [dic setValue:company_name_dic forKey:@"item_2"];
  4464. // address
  4465. NSDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4466. [dic setValue:ext_dic forKey:@"item_3"];
  4467. // contact
  4468. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4469. [dic setValue:contact_dic forKey:@"item_4"];
  4470. // phone
  4471. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4472. [dic setValue:phone_dic forKey:@"item_5"];
  4473. // fax
  4474. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4475. [dic setValue:fax_dic forKey:@"item_6"];
  4476. // email
  4477. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4478. [dic setValue:email_dic forKey:@"item_7"];
  4479. }];
  4480. return dic;
  4481. }
  4482. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  4483. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4484. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4485. orderCode = [self translateSingleQuote:orderCode];
  4486. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  4487. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4488. NSString *cid = [self textAtColumn:0 statement:stmt];
  4489. NSString *name = [self textAtColumn:1 statement:stmt];
  4490. NSString *ext = [self textAtColumn:2 statement:stmt];
  4491. NSString *contact = [self textAtColumn:3 statement:stmt];
  4492. NSString *email = [self textAtColumn:4 statement:stmt];
  4493. NSString *fax = [self textAtColumn:5 statement:stmt];
  4494. NSString *phone = [self textAtColumn:6 statement:stmt];
  4495. // count
  4496. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4497. // title
  4498. [dic setValue:@"Ship From" forKey:@"title"];
  4499. // hide
  4500. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4501. // choose
  4502. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4503. [choose_dic setValue:@"choose" forKey:@"aname"];
  4504. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4505. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  4506. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  4507. @"key_map" : @{@"sender_cid" : @"customer_cid",
  4508. @"sender_contact" : @"customer_contact",
  4509. @"sender_email" : @"customer_email",
  4510. @"sender_ext" : @"customer_contact_ext",
  4511. @"sender_fax" : @"customer_fax",
  4512. @"sender_name" : @"customer_name",
  4513. @"sender_phone" : @"customer_phone"},
  4514. @"name" : @"select_cid",
  4515. @"refresh" : [NSNumber numberWithInteger:0],
  4516. @"value" : @"Sales_Order_Ship_From"};
  4517. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  4518. [dic setValue:choose_dic forKey:@"item_0"];
  4519. // contact id
  4520. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4521. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4522. [contact_id_dic setValue:@"true" forKey:@"required"];
  4523. [dic setValue:contact_id_dic forKey:@"item_1"];
  4524. // company name
  4525. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4526. [dic setValue:company_name_dic forKey:@"item_2"];
  4527. // address
  4528. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4529. [dic setValue:ext_dic forKey:@"item_3"];
  4530. // contact
  4531. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4532. [dic setValue:contact_dic forKey:@"item_4"];
  4533. // phone
  4534. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4535. [dic setValue:phone_dic forKey:@"item_5"];
  4536. // fax
  4537. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4538. [dic setValue:fax_dic forKey:@"item_6"];
  4539. // email
  4540. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4541. [dic setValue:email_dic forKey:@"item_7"];
  4542. }];
  4543. return dic;
  4544. }
  4545. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  4546. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4547. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4548. orderCode = [self translateSingleQuote:orderCode];
  4549. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  4550. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4551. NSString *cid = [self textAtColumn:0 statement:stmt];
  4552. NSString *name = [self textAtColumn:1 statement:stmt];
  4553. NSString *ext = [self textAtColumn:2 statement:stmt];
  4554. NSString *contact = [self textAtColumn:3 statement:stmt];
  4555. NSString *email = [self textAtColumn:4 statement:stmt];
  4556. NSString *fax = [self textAtColumn:5 statement:stmt];
  4557. NSString *phone = [self textAtColumn:6 statement:stmt];
  4558. // count
  4559. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4560. // title
  4561. [dic setValue:@"Freight Bill To" forKey:@"title"];
  4562. // hide
  4563. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4564. // choose
  4565. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4566. [choose_dic setValue:@"choose" forKey:@"aname"];
  4567. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4568. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  4569. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  4570. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  4571. @"shipping_billto_contact" : @"receive_contact",
  4572. @"shipping_billto_email" : @"receive_email",
  4573. @"shipping_billto_ext" : @"receive_ext",
  4574. @"shipping_billto_fax" : @"receive_fax",
  4575. @"shipping_billto_name" : @"receive_name",
  4576. @"shipping_billto_phone" : @"receive_phone"},
  4577. @"type" : @"pull"};
  4578. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  4579. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4580. @"type" : @"pull",
  4581. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  4582. @"shipping_billto_contact" : @"customer_contact",
  4583. @"shipping_billto_email" : @"customer_email",
  4584. @"shipping_billto_ext" : @"customer_contact_ext",
  4585. @"shipping_billto_fax" : @"customer_fax",
  4586. @"shipping_billto_name" : @"customer_name",
  4587. @"shipping_billto_phone" : @"customer_phone"}
  4588. };
  4589. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  4590. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  4591. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  4592. @"shipping_billto_contact" : @"sender_contact",
  4593. @"shipping_billto_email" : @"sender_email",
  4594. @"shipping_billto_ext" : @"sender_ext",
  4595. @"shipping_billto_fax" : @"sender_fax",
  4596. @"shipping_billto_name" : @"sender_name",
  4597. @"shipping_billto_phone" : @"sender_phone"},
  4598. @"type" : @"pull"
  4599. };
  4600. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  4601. NSDictionary *select_freight_bill_to_dic = @{
  4602. @"aname" : @"Select freight bill to",
  4603. @"name" : @"select_cid",
  4604. @"refresh" : [NSNumber numberWithInteger:0],
  4605. @"value" : @"Sales_Order_Freight_Bill_To",
  4606. @"key_map" : @{
  4607. @"shipping_billto_cid" : @"customer_cid",
  4608. @"shipping_billto_contact" : @"customer_contact",
  4609. @"shipping_billto_email" : @"customer_email",
  4610. @"shipping_billto_ext" : @"customer_contact_ext",
  4611. @"shipping_billto_fax" : @"customer_fax",
  4612. @"shipping_billto_name" : @"customer_name",
  4613. @"shipping_billto_phone" : @"customer_phone"
  4614. }
  4615. };
  4616. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  4617. [dic setValue:choose_dic forKey:@"item_0"];
  4618. // contact id
  4619. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4620. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4621. [contact_id_dic setValue:@"true" forKey:@"required"];
  4622. [dic setValue:contact_id_dic forKey:@"item_1"];
  4623. // company name
  4624. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4625. [dic setValue:company_name_dic forKey:@"item_2"];
  4626. // address
  4627. NSDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4628. [dic setValue:ext_dic forKey:@"item_3"];
  4629. // contact
  4630. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4631. [dic setValue:contact_dic forKey:@"item_4"];
  4632. // phone
  4633. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4634. [dic setValue:phone_dic forKey:@"item_5"];
  4635. // fax
  4636. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4637. [dic setValue:fax_dic forKey:@"item_6"];
  4638. // email
  4639. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4640. [dic setValue:email_dic forKey:@"item_7"];
  4641. }];
  4642. return dic;
  4643. }
  4644. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4645. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4646. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4647. orderCode = [self translateSingleQuote:orderCode];
  4648. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  4649. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4650. NSString *cid = [self textAtColumn:0 statement:stmt];
  4651. NSString *name = [self textAtColumn:1 statement:stmt];
  4652. NSString *ext = [self textAtColumn:2 statement:stmt];
  4653. NSString *contact = [self textAtColumn:3 statement:stmt];
  4654. NSString *email = [self textAtColumn:4 statement:stmt];
  4655. NSString *fax = [self textAtColumn:5 statement:stmt];
  4656. NSString *phone = [self textAtColumn:6 statement:stmt];
  4657. // count
  4658. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4659. // title
  4660. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  4661. // hide
  4662. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4663. // choose
  4664. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4665. [choose_dic setValue:@"choose" forKey:@"aname"];
  4666. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4667. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  4668. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  4669. @"key_map" : @{@"billing_cid" : @"receive_cid",
  4670. @"billing_contact" : @"receive_contact",
  4671. @"billing_email" : @"receive_email",
  4672. @"billing_ext" : @"receive_ext",
  4673. @"billing_fax" : @"receive_fax",
  4674. @"billing_name" : @"receive_name",
  4675. @"billing_phone" : @"receive_phone"},
  4676. @"type" : @"pull"};
  4677. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  4678. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  4679. @"type" : @"pull",
  4680. @"key_map" : @{@"billing_cid" : @"customer_cid",
  4681. @"billing_contact" : @"customer_contact",
  4682. @"billing_email" : @"customer_email",
  4683. @"billing_ext" : @"customer_contact_ext",
  4684. @"billing_fax" : @"customer_fax",
  4685. @"billing_name" : @"customer_name",
  4686. @"billing_phone" : @"customer_phone"}
  4687. };
  4688. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  4689. NSDictionary *select_bill_to_dic = @{
  4690. @"aname" : @"Select bill to",
  4691. @"name" : @"select_cid",
  4692. @"refresh" : [NSNumber numberWithInteger:0],
  4693. @"value" : @"Sales_Order_Merchandise_Bill_To",
  4694. @"key_map" : @{
  4695. @"billing_cid" : @"customer_cid",
  4696. @"billing_contact" : @"customer_contact",
  4697. @"billing_email" : @"customer_email",
  4698. @"billing_ext" : @"customer_contact_ext",
  4699. @"billing_fax" : @"customer_fax",
  4700. @"billing_name" : @"customer_name",
  4701. @"billing_phone" : @"customer_phone"
  4702. }
  4703. };
  4704. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  4705. [dic setValue:choose_dic forKey:@"item_0"];
  4706. // contact id
  4707. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4708. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4709. [contact_id_dic setValue:@"true" forKey:@"required"];
  4710. [dic setValue:contact_id_dic forKey:@"item_1"];
  4711. // company name
  4712. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4713. [dic setValue:company_name_dic forKey:@"item_2"];
  4714. // address
  4715. NSDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4716. [dic setValue:ext_dic forKey:@"item_3"];
  4717. // contact
  4718. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4719. [dic setValue:contact_dic forKey:@"item_4"];
  4720. // phone
  4721. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4722. [dic setValue:phone_dic forKey:@"item_5"];
  4723. // fax
  4724. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4725. [dic setValue:fax_dic forKey:@"item_6"];
  4726. // email
  4727. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4728. [dic setValue:email_dic forKey:@"item_7"];
  4729. }];
  4730. return dic;
  4731. }
  4732. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  4733. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4734. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4735. orderCode = [self translateSingleQuote:orderCode];
  4736. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  4737. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4738. NSString *cid = [self textAtColumn:0 statement:stmt];
  4739. NSString *name = [self textAtColumn:1 statement:stmt];
  4740. NSString *ext = [self textAtColumn:2 statement:stmt];
  4741. NSString *contact = [self textAtColumn:3 statement:stmt];
  4742. NSString *email = [self textAtColumn:4 statement:stmt];
  4743. NSString *fax = [self textAtColumn:5 statement:stmt];
  4744. NSString *phone = [self textAtColumn:6 statement:stmt];
  4745. // count
  4746. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  4747. // title
  4748. [dic setValue:@"Return To" forKey:@"title"];
  4749. // hide
  4750. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  4751. // choose
  4752. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  4753. [choose_dic setValue:@"choose" forKey:@"aname"];
  4754. [choose_dic setValue:@"multi_action" forKey:@"control"];
  4755. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  4756. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  4757. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  4758. @"returnto_contact" : @"sender_contact",
  4759. @"returnto_email" : @"sender_email",
  4760. @"returnto_ext" : @"sender_ext",
  4761. @"returnto_fax" : @"sender_fax",
  4762. @"returnto_name" : @"sender_name",
  4763. @"returnto_phone" : @"sender_phone"},
  4764. @"type" : @"pull"};
  4765. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  4766. NSDictionary *select_return_to_dic = @{
  4767. @"aname" : @"Select return to",
  4768. @"name" : @"select_cid",
  4769. @"refresh" : [NSNumber numberWithInteger:0],
  4770. @"value" : @"Contact_Return_To",
  4771. @"key_map" : @{
  4772. @"returnto_cid" : @"customer_cid",
  4773. @"returnto_contact" : @"customer_contact",
  4774. @"returnto_email" : @"customer_email",
  4775. @"returnto_ext" : @"customer_contact_ext",
  4776. @"returnto_fax" : @"customer_fax",
  4777. @"returnto_name" : @"customer_name",
  4778. @"returnto_phone" : @"customer_phone"
  4779. }
  4780. };
  4781. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  4782. [dic setValue:choose_dic forKey:@"item_0"];
  4783. // contact id
  4784. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  4785. [contact_id_dic setValue:@"1" forKey:@"disable"];
  4786. [contact_id_dic setValue:@"true" forKey:@"required"];
  4787. [dic setValue:contact_id_dic forKey:@"item_1"];
  4788. // company name
  4789. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  4790. [dic setValue:company_name_dic forKey:@"item_2"];
  4791. // address
  4792. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  4793. [dic setValue:ext_dic forKey:@"item_3"];
  4794. // contact
  4795. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  4796. [dic setValue:contact_dic forKey:@"item_4"];
  4797. // phone
  4798. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  4799. [dic setValue:phone_dic forKey:@"item_5"];
  4800. // fax
  4801. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  4802. [dic setValue:fax_dic forKey:@"item_6"];
  4803. // email
  4804. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  4805. [dic setValue:email_dic forKey:@"item_7"];
  4806. }];
  4807. return dic;
  4808. }
  4809. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  4810. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4811. orderCode = [self translateSingleQuote:orderCode];
  4812. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4813. __block double TotalCuft = 0;
  4814. __block double TotalWeight = 0;
  4815. __block int TotalCarton = 0;
  4816. __block double payments = 0;
  4817. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  4818. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4819. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4820. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  4821. // item id
  4822. int item_id = sqlite3_column_int(stmt, 0);
  4823. // count
  4824. int item_count = sqlite3_column_int(stmt, 1);
  4825. // stockUom
  4826. int stockUom = sqlite3_column_int(stmt, 2);
  4827. // unit price
  4828. NSString *str_price = [self textAtColumn:3 statement:stmt];
  4829. NSString* Price=nil;
  4830. if(str_price==nil)
  4831. {
  4832. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  4833. if(price==nil)
  4834. Price=@"No Price.";
  4835. else
  4836. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4837. }
  4838. else
  4839. {
  4840. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  4841. }
  4842. // discount
  4843. double discount = sqlite3_column_double(stmt, 4);
  4844. // name
  4845. NSString *name = [self textAtColumn:5 statement:stmt];
  4846. // description
  4847. NSString *description = [self textAtColumn:6 statement:stmt];
  4848. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  4849. // img
  4850. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  4851. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  4852. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4853. if(combine != nil)
  4854. {
  4855. // int citem=0;
  4856. int bcount=[[combine valueForKey:@"count"] intValue];
  4857. for(int bc=0;bc<bcount;bc++)
  4858. {
  4859. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4860. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4861. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4862. subTotal += uprice * modulus * item_count;
  4863. }
  4864. }
  4865. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  4866. [model_dic setValue:@"model" forKey:@"control"];
  4867. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  4868. [model_dic setValue:description forKey:@"description"];
  4869. [model_dic setValue:img forKey:@"img_url"];
  4870. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  4871. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  4872. [model_dic setValue:Price forKey:@"unit_price"];
  4873. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  4874. if (combine) {
  4875. [model_dic setValue:combine forKey:@"combine"];
  4876. }
  4877. // well,what under the row is the info for total
  4878. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4879. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4880. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4881. int carton=[bsubtotaljson[@"carton"] intValue];
  4882. TotalCuft += cuft;
  4883. TotalWeight += weight;
  4884. TotalCarton += carton;
  4885. payments += subTotal;
  4886. //---------------------------
  4887. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  4888. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  4889. }];
  4890. [dic setValue:@"Model Information" forKey:@"title"];
  4891. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  4892. return dic;
  4893. }
  4894. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  4895. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4896. [dic setValue:@"Remarks Content" forKey:@"title"];
  4897. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  4898. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4899. orderCode = [self translateSingleQuote:orderCode];
  4900. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  4901. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4902. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  4903. int mustCall = sqlite3_column_int(stmt, 1);
  4904. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  4905. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  4906. NSDictionary *po_dic = @{
  4907. @"aname" : @"PO#",
  4908. @"control" : @"edit",
  4909. @"keyboard" : @"text",
  4910. @"name" : @"poNumber",
  4911. @"value" : poNumber
  4912. };
  4913. NSDictionary *must_call_dic = @{
  4914. @"aname" : @"MUST CALL BEFORE DELIVERY",
  4915. @"control" : @"switch",
  4916. @"name" : @"must_call",
  4917. @"value" : mustCall ? @"true" : @"false"
  4918. };
  4919. NSDictionary *general_notes_dic = @{
  4920. @"aname" : @"General notes",
  4921. @"control" : @"text_view",
  4922. @"keyboard" : @"text",
  4923. @"name" : @"comments",
  4924. @"value" : generalNotes
  4925. };
  4926. NSDictionary *internal_notes_dic = @{
  4927. @"aname" : @"Internal notes",
  4928. @"control" : @"text_view",
  4929. @"keyboard" : @"text",
  4930. @"name" : @"internal_notes",
  4931. @"value" : internalNotes
  4932. };
  4933. [dic setValue:po_dic forKey:@"item_0"];
  4934. [dic setValue:must_call_dic forKey:@"item_1"];
  4935. [dic setValue:general_notes_dic forKey:@"item_2"];
  4936. [dic setValue:internal_notes_dic forKey:@"item_3"];
  4937. }];
  4938. return dic;
  4939. }
  4940. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  4941. // params
  4942. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4943. orderCode = [self translateSingleQuote:orderCode];
  4944. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4945. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  4946. __block double lift_gate_value = 0;
  4947. __block double handling_fee = 0;
  4948. __block double shipping = 0;
  4949. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4950. int lift_gate = sqlite3_column_int(stmt, 0);
  4951. lift_gate_value = sqlite3_column_double(stmt, 1);
  4952. shipping = sqlite3_column_double(stmt, 2);
  4953. handling_fee = sqlite3_column_double(stmt, 3);
  4954. if (!lift_gate) {
  4955. lift_gate_value = 0;
  4956. }
  4957. }];
  4958. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  4959. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  4960. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  4961. double payments = [[total valueForKey:@"payments"] doubleValue];
  4962. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  4963. double totalPrice = payments;
  4964. [dic setValue:@"Order Total" forKey:@"title"];
  4965. NSDictionary *payments_dic = @{
  4966. @"align" : @"right",
  4967. @"aname" : @"Payments/Credits",
  4968. @"control" : @"text",
  4969. @"name" : @"paymentsAndCredits",
  4970. @"type" : @"price",
  4971. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  4972. };
  4973. [dic setValue:payments_dic forKey:@"item_0"];
  4974. NSDictionary *handling_fee_dic = @{
  4975. @"align" : @"right",
  4976. @"aname" : @"Handling Fee",
  4977. @"control" : @"text",
  4978. @"name" : @"handling_fee_value",
  4979. @"required" : @"true",
  4980. @"type" : @"price",
  4981. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  4982. };
  4983. NSDictionary *shipping_dic = @{
  4984. @"align" : @"right",
  4985. @"aname" : @"Shipping*",
  4986. @"control" : @"text",
  4987. @"name" : @"shipping",
  4988. @"required" : @"true",
  4989. @"type" : @"price",
  4990. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  4991. };
  4992. NSDictionary *lift_gate_dic = @{
  4993. @"align" : @"right",
  4994. @"aname" : @"Liftgate Fee(No Loading Dock)",
  4995. @"control" : @"text",
  4996. @"name" : @"lift_gate_value",
  4997. @"required" : @"true",
  4998. @"type" : @"price",
  4999. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  5000. };
  5001. int item_count = 1;
  5002. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  5003. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  5004. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5005. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5006. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  5007. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5008. } else {
  5009. }
  5010. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5011. NSDictionary *total_price_dic = @{
  5012. @"align" : @"right",
  5013. @"aname" : @"Total",
  5014. @"control" : @"text",
  5015. @"name" : @"totalPrice",
  5016. @"type" : @"price",
  5017. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  5018. };
  5019. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5020. NSDictionary *total_cuft_dic = @{
  5021. @"align" : @"right",
  5022. @"aname" : @"Total Cuft",
  5023. @"control" : @"text",
  5024. @"name" : @"",
  5025. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  5026. };
  5027. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5028. NSDictionary *total_weight_dic = @{
  5029. @"align" : @"right",
  5030. @"aname" : @"Total Weight",
  5031. @"control" : @"text",
  5032. @"name" : @"",
  5033. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  5034. };
  5035. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5036. NSDictionary *total_carton_dic = @{
  5037. @"align" : @"right",
  5038. @"aname" : @"Total Carton",
  5039. @"control" : @"text",
  5040. @"name" : @"",
  5041. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  5042. };
  5043. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5044. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  5045. return dic;
  5046. }
  5047. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  5048. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5049. orderCode = [self translateSingleQuote:orderCode];
  5050. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5051. [dic setValue:@"Signature" forKey:@"title"];
  5052. __block NSString *pic_path = @"";
  5053. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  5054. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5055. pic_path = [self textAtColumn:0 statement:stmt];
  5056. }];
  5057. NSDictionary *pic_dic = @{
  5058. @"aname" : @"Signature",
  5059. @"avalue" :pic_path,
  5060. @"control" : @"signature",
  5061. @"name" : @"sign_picpath",
  5062. @"value" : pic_path
  5063. };
  5064. [dic setValue:pic_dic forKey:@"item_0"];
  5065. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5066. return dic;
  5067. }
  5068. + (NSDictionary *)shippingMethodDic:(NSDictionary *)params db:(sqlite3 *)db {
  5069. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5070. // refresh_trigger logist
  5071. NSString *refresh_trigger = [self valueInParams:params key:@"refresh_trigger"];
  5072. NSString *logist = nil;
  5073. if ([refresh_trigger isEqualToString:@"logist"]) {
  5074. logist = [self valueInParams:params key:@"logist"];
  5075. }
  5076. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5077. [dic setValue:@"Shipping Method" forKey:@"title"];
  5078. // val_0
  5079. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  5080. }
  5081. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  5082. [val_0 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5083. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  5084. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  5085. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  5086. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5087. NSDictionary *val0_subItem_item0 = @{
  5088. @"aname" : @"Option",
  5089. @"cadedate" : @{
  5090. @"count" : [NSNumber numberWithInteger:2],
  5091. @"val_0" : @{
  5092. @"check" : [NSNumber numberWithInteger:0],
  5093. @"refresh" : [NSNumber numberWithInteger:0],
  5094. @"value" : @"PERSONAL PICK UP",
  5095. @"value_id" : @"PERSONAL PICK UP"
  5096. },
  5097. @"val_1" : @{
  5098. @"check" : [NSNumber numberWithInteger:0],
  5099. @"refresh" : [NSNumber numberWithInteger:0],
  5100. @"sub_item" : @{
  5101. @"count" : [NSNumber numberWithInteger:1],
  5102. @"item_0" : @{
  5103. @"aname" : @"BOL",
  5104. @"control" : @"edit",
  5105. @"keyboard" : @"text",
  5106. @"name" : @"logist_note_text",
  5107. @"refresh" : [NSNumber numberWithInteger:0],
  5108. @"required" : @"false",
  5109. @"value" : @""
  5110. }
  5111. },
  5112. @"value" : @"USE MY CARRIER",
  5113. @"value_id" : @"USE MY CARRIER"
  5114. }
  5115. },
  5116. @"control" : @"enum",
  5117. @"name" : @"logistic_note",
  5118. @"required" : @"true",
  5119. @"single_select" : @"true"
  5120. };
  5121. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  5122. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  5123. // val_1
  5124. NSString *lift_gate = [self valueInParams:params key:@"lift_gate"];
  5125. if (lift_gate && [lift_gate isEqualToString:@"true"]) {
  5126. } else {
  5127. lift_gate = @"false";
  5128. }
  5129. NSDictionary *val_1 = @{
  5130. @"check" : [NSNumber numberWithInteger:0],
  5131. @"sub_item" : @{
  5132. @"count" : [NSNumber numberWithInteger:1],
  5133. @"item_0" : @{
  5134. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  5135. @"control" : @"switch",
  5136. @"name" : @"lift_gate",
  5137. @"refresh" : [NSNumber numberWithInteger:1],
  5138. @"required" : @"true",
  5139. @"value" : lift_gate
  5140. }
  5141. },
  5142. @"value" : @"COMMON CARRIER",
  5143. @"value_id" : @"COMMON CARRIER"
  5144. };
  5145. // cadedate
  5146. NSDictionary *cadedate = @{
  5147. @"count" : [NSNumber numberWithInteger:2],
  5148. @"val_0" : val_0,
  5149. @"val_1" : val_1
  5150. };
  5151. // item_0
  5152. NSDictionary *item_0 = @{
  5153. @"aname" : @"Shipping",
  5154. @"cadedate" : cadedate,
  5155. @"control" : @"enum",
  5156. @"name" : @"logist",
  5157. @"refresh" : [NSNumber numberWithInteger:1],
  5158. @"single_select" : @"true",
  5159. };
  5160. [dic setValue:item_0 forKey:@"item_0"];
  5161. return dic;
  5162. }
  5163. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  5164. {
  5165. sqlite3 *db = [iSalesDB get_db];
  5166. // params
  5167. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5168. orderCode = [self translateSingleQuote:orderCode];
  5169. // 0 Order Type 1 Shipping Method 2 Payment Information
  5170. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  5171. // 1 Shipping Method
  5172. NSDictionary *shipping_method_dic = [self shippingMethodDic:params db:db];
  5173. [ret setValue:shipping_method_dic forKey:@"section_1"];
  5174. // 3 Customer
  5175. NSDictionary *customer_dic = [self customerDic:params db:db];
  5176. [ret setValue:customer_dic forKey:@"section_3"];
  5177. // 4 Ship To
  5178. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  5179. [ret setValue:ship_to_dic forKey:@"section_4"];
  5180. // 5 Ship From
  5181. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  5182. [ret setValue:ship_from_dic forKey:@"section_5"];
  5183. // 6 Freight Bill To
  5184. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  5185. [ret setValue:freight_bill_to forKey:@"section_6"];
  5186. // 7 Merchandise Bill To
  5187. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  5188. [ret setValue:merchandise_bill_to_dic forKey:@"section_7"];
  5189. // 8 Return To
  5190. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  5191. [ret setValue:return_to_dic forKey:@"section_8"];
  5192. // 9 Model Information
  5193. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  5194. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  5195. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  5196. [ret setValue:model_info_dic forKey:@"section_9"];
  5197. // 10 Remarks Content
  5198. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  5199. [ret setValue:remarks_content_dic forKey:@"section_10"];
  5200. // 11 Order Total
  5201. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  5202. [ret setValue:order_total_dic forKey:@"section_11"];
  5203. // 12 Signature
  5204. NSDictionary *sign_dic = [self signatureDic:params db:db];
  5205. [ret setValue:sign_dic forKey:@"section_12"];
  5206. [iSalesDB close_db:db];
  5207. return [RAUtils dict2data:ret];
  5208. // return nil;
  5209. }
  5210. #pragma mark addr editor
  5211. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  5212. NSMutableDictionary *new_item = [item mutableCopy];
  5213. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  5214. return new_item;
  5215. }
  5216. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  5217. {
  5218. // "is_subaction" = true;
  5219. // orderCode = MOB1608240002;
  5220. // password = 123456;
  5221. // "subaction_tag" = 1;
  5222. // user = EvanK;
  5223. // {
  5224. // "is_subaction" = true;
  5225. // orderCode = MOB1608240002;
  5226. // password = 123456;
  5227. // "refresh_trigger" = zipcode;
  5228. // "subaction_tag" = 1;
  5229. // user = EvanK;
  5230. // }
  5231. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  5232. [ret removeObjectForKey:@"up_params"];
  5233. [ret setObject:@"New Address" forKey:@"title"];
  5234. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  5235. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  5236. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  5237. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  5238. [country_dic removeObjectForKey:@"refresh"];
  5239. [country_dic removeObjectForKey:@"restore"];
  5240. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  5241. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  5242. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  5243. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  5244. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  5245. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  5246. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  5247. [zip_code_dic removeObjectForKey:@"refresh"];
  5248. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  5249. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  5250. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  5251. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  5252. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  5253. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  5254. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  5255. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  5256. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  5257. // country
  5258. NSString *countryCode = @"US";
  5259. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5260. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  5261. // state
  5262. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  5263. NSDictionary *tmpDic = @{
  5264. @"value" : @"Other",
  5265. @"value_id" : @"",
  5266. @"check" : [NSNumber numberWithInteger:0]
  5267. };
  5268. for (int i = 0; i < allState.allKeys.count; i++) {
  5269. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  5270. NSDictionary *tmp = allState[key];
  5271. [allState setValue:tmpDic forKey:key];
  5272. tmpDic = tmp;
  5273. }
  5274. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  5275. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  5276. [ret setValue:new_section_0 forKey:@"section_0"];
  5277. return [RAUtils dict2data:ret];
  5278. }
  5279. #pragma mark save addr
  5280. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  5281. {
  5282. // NSString *companyName = [self valueInParams:params key:@"company"];
  5283. // NSString *addr1 = [self valueInParams:params key:@"address"];
  5284. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  5285. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  5286. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  5287. // NSString *countryId = [self valueInParams:params key:@"country"];
  5288. // NSString *stateCode = [self valueInParams:params key:@"state"];
  5289. // NSString *city = [self valueInParams:params key:@"city"];
  5290. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  5291. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  5292. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  5293. // NSString *phone = [self valueInParams:params key:@"phone"];
  5294. // NSString *fax = [self valueInParams:params key:@"fax"];
  5295. // NSString *email = [self valueInParams:params key:@"email"];
  5296. return [self offline_saveContact:params update:NO];
  5297. }
  5298. #pragma mark cancel order
  5299. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  5300. {
  5301. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5302. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  5303. int ret = [iSalesDB execSql:sql];
  5304. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5305. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5306. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5307. [dic setValue:@"160409" forKey:@"min_ver"];
  5308. return [RAUtils dict2data:dic];
  5309. }
  5310. #pragma mark sign order
  5311. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  5312. {
  5313. //参考 offline_saveBusinesscard
  5314. NSLog(@"sign order params: %@",params);
  5315. // orderCode = MOB1608240002;
  5316. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  5317. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5318. orderCode = [self translateSingleQuote:orderCode];
  5319. NSString *picPath = [self valueInParams:params key:@"picpath"];
  5320. picPath = [self translateSingleQuote:picPath];
  5321. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  5322. int ret = [iSalesDB execSql:sql];
  5323. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5324. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5325. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5326. [dic setValue:@"160409" forKey:@"min_ver"];
  5327. return [RAUtils dict2data:dic];
  5328. }
  5329. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  5330. {
  5331. return nil;
  5332. }
  5333. @end