OLDataProvider.m 345 KB

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