OLDataProvider.m 376 KB

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