OLDataProvider.m 451 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992
  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. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // 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
  57. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. NSMutableDictionary* values=params[@"replaceValue"];
  83. //生成portfolio pdf需要的数据
  84. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  85. data[@"npd_url"]=@"www.newpacificdirect.com";
  86. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  87. [formatter setDateFormat:@"MMMM yyyy"];
  88. NSString* date = [formatter stringFromDate:[NSDate date]];
  89. data[@"create_month"]=date;
  90. data[@"company_name"]=@"New Pacific Direct,Inc.";
  91. data[@"catalog_name"]=params[@"catalog_name"];
  92. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  93. data[@"TOTAL_PAGE"]=0;
  94. data[@"CURRENT_PAGE"]=0;
  95. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  96. bool show_stockout=[params[@"show_stockout"] boolValue];
  97. NSString *sql = [NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  98. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  99. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  100. sqlite3 *db = [iSalesDB get_db];
  101. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  102. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  103. int product_id = sqlite3_column_int(stmt, 0);
  104. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  105. NSString *name = [self textAtColumn:1 statement:stmt];
  106. NSString *description = [self textAtColumn:2 statement:stmt];
  107. double price = sqlite3_column_double(stmt, 3);
  108. NSString *s_price = [self textAtColumn:3 statement:stmt];
  109. double discount = sqlite3_column_double(stmt,4);
  110. int qty = sqlite3_column_int(stmt, 5);
  111. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  112. int is_percentage = sqlite3_column_int(stmt, 6);
  113. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  114. int item_id = sqlite3_column_int(stmt, 7);
  115. // int fashion_id = sqlite3_column_int(stmt, 8);
  116. NSString *line_note = [self textAtColumn:9 statement:stmt];
  117. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  118. double percent = sqlite3_column_double(stmt, 10);
  119. // int portfolio_id = sqlite3_column_int(stmt, 11);
  120. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  121. int availability = sqlite3_column_int(stmt, 12);
  122. NSString *color = [self textAtColumn:13 statement:stmt];
  123. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  124. NSString *demension = [self textAtColumn:15 statement:stmt];
  125. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  126. NSString *material = [self textAtColumn:17 statement:stmt];
  127. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  128. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  129. double volume = sqlite3_column_double(stmt, 19);
  130. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  131. double weight = sqlite3_column_double(stmt, 20);
  132. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  133. int model_set = sqlite3_column_int(stmt, 21);
  134. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  135. double load_ability = sqlite3_column_double(stmt, 22);
  136. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  137. NSString *assembling = [self textAtColumn:24 statement:stmt];
  138. NSString *made_in = [self textAtColumn:25 statement:stmt];
  139. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  140. if(is_percentage==0&&qty==0&&!show_stockout)
  141. {
  142. return;
  143. }
  144. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  145. {
  146. return;
  147. }
  148. if(is_percentage)
  149. {
  150. qty=availability;
  151. }
  152. else
  153. {
  154. percent=1;
  155. }
  156. qty=qty*percent+0.5;
  157. for(int i=0;i<[values[@"count"] intValue];i++)
  158. {
  159. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  160. if([valueitem[@"product_id"] intValue]==product_id)
  161. {
  162. line_note=valueitem[@"linenotes"];
  163. qty=[valueitem[@"available_qty"] intValue];
  164. s_qty=valueitem[@"available_qty"] ;
  165. if(valueitem[@"regular_price"]!=nil)
  166. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  167. s_price=valueitem[@"special_price"];
  168. price=[valueitem[@"special_price"] floatValue];
  169. discount=0;
  170. }
  171. }
  172. NSString* set_price=@"";
  173. if([params[@"entered_price"] boolValue])
  174. {
  175. if (s_price==nil ) {
  176. set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  177. }
  178. else
  179. {
  180. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  181. }
  182. }
  183. NSString* get_price=@"";
  184. {
  185. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  186. // DebugLog(@"price time interval");
  187. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  188. if(gprice==nil)
  189. get_price=@"Price:No Price.";
  190. else
  191. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  192. }
  193. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  194. // if ([qty_null isEqualToString:@"null"]) {
  195. // qty=availability;
  196. // }
  197. //
  198. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  199. // if ([qty_percent_null isEqualToString:@"null"]) {
  200. // percentage=1;
  201. // }
  202. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  203. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  204. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  205. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  206. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  207. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  208. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  209. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  210. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  211. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  212. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  213. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  214. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  215. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  216. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  217. if([params[@"availability"] boolValue]==false || (s_qty.length==0 && s_availability.length==0))
  218. str_availability=@"";
  219. if([params[@"color"] boolValue]==false || color.length==0 )
  220. str_color=@"";
  221. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  222. str_model_set=@"";
  223. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  224. str_legcolor=@"";
  225. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  226. str_demension=@"";
  227. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  228. str_seat_height=@"";
  229. if([params[@"material"] boolValue]==false || material.length==0 )
  230. str_meterial=@"";
  231. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  232. str_box_dim=@"";
  233. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  234. str_volume=@"";
  235. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  236. str_weight=@"";
  237. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  238. str_load_ability=@"";
  239. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  240. str_fabric_content=@"";
  241. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  242. str_assembling=@"";
  243. if([params[@"made"] boolValue]==false || made_in.length==0 )
  244. str_made_in=@"";
  245. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  246. str_line_note=@"";
  247. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  248. [arr_detail addObject:str_color];
  249. [arr_detail addObject:str_model_set];
  250. [arr_detail addObject:str_legcolor];
  251. [arr_detail addObject:str_demension];
  252. [arr_detail addObject:str_seat_height];
  253. [arr_detail addObject:str_meterial];
  254. [arr_detail addObject:str_box_dim];
  255. [arr_detail addObject:str_volume];
  256. [arr_detail addObject:str_weight];
  257. [arr_detail addObject:str_load_ability];
  258. [arr_detail addObject:str_fabric_content];
  259. [arr_detail addObject:str_assembling];
  260. [arr_detail addObject:str_made_in];
  261. [arr_detail addObject:str_line_note];
  262. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  263. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  264. //model image;
  265. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  266. // qr image
  267. NSString* qrpath=nil;
  268. if([params[@"show_barcode"] boolValue])
  269. {
  270. NSString* temp = NSTemporaryDirectory();
  271. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  272. qrpath=[temp stringByAppendingPathComponent:filename];
  273. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  274. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  275. }
  276. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  277. item[@"img"]=img_path;
  278. item[@"img_qr"]=qrpath;
  279. item[@"special_price"]=set_price;
  280. item[@"price"]=get_price;
  281. item[@"name"]=name;
  282. item[@"description"]=description;
  283. item[@"detail"]=detail;
  284. item[@"img"]=img_path;
  285. // @{
  286. // //@"linenotes": line_note,
  287. //// @"product_id": product_id_string,
  288. //// @"available_qty": @(qty),
  289. //// @"available_percent" : @(percent),
  290. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  291. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  292. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  293. // @"": ,
  294. // @"": ,
  295. // @"": ,
  296. // @"": ,
  297. // @"": ,
  298. // @"": ,
  299. // @"":
  300. // }.mutableCopy;
  301. // if (percentage) {
  302. // [item removeObjectForKey:@"available_qty"];
  303. // } else {
  304. // [item removeObjectForKey:@"available_percent"];
  305. // }
  306. //
  307. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  308. // if ([qty_null isEqualToString:@"null"]) {
  309. // [item removeObjectForKey:@"available_qty"];
  310. // }
  311. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  312. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  313. }];
  314. [iSalesDB close_db:db];
  315. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  316. // [dic setValue:@"" forKey:@"email_content"];
  317. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  318. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  319. //
  320. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  321. // cell[@"count"]=[NSNumber numberWithInt:10];
  322. grid[@"cell0"]=dic;
  323. data[@"grid0"]=grid;
  324. return data;
  325. }
  326. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  327. {
  328. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  329. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  330. NSString *cache_folder=[paths objectAtIndex:0];
  331. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  332. BOOL bdir=NO;
  333. NSFileManager* fileManager = [NSFileManager defaultManager];
  334. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir])
  335. {
  336. NSError * error=nil;
  337. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  338. {
  339. return nil;
  340. }
  341. }
  342. NSData *data = [NSData dataWithContentsOfFile:template_path];
  343. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  344. return ret;
  345. }
  346. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  347. {
  348. NSString* template_file=nil;
  349. switch([params[@"pdf_style"] intValue])
  350. {
  351. case 0:
  352. template_file= @"portfolio_2x3.json";
  353. break;
  354. case 1:
  355. template_file= @"portfolio_3x2.json";
  356. break;
  357. default:
  358. template_file= @"portfolio_3x2.json";
  359. }
  360. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  361. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  362. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  363. DebugLog(@"%@",file);
  364. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  365. [dic setObject:@"Regular Mode" forKey:@"mode"];
  366. if (file) {
  367. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  368. [dic setObject:file forKey:@"pdf_path"];
  369. dic[@"isLocalFile"]=@"true";
  370. return [RAUtils dict2data:dic];
  371. }
  372. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  373. dic[@"isLocalFile"]=@"true";
  374. return [RAUtils dict2data:dic];
  375. }
  376. +(NSString*) get_offline_soid:(sqlite3*)db
  377. {
  378. NSString* soid=nil;
  379. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  380. [formatter setDateFormat:@"yyMMdd"];
  381. NSString* date = [formatter stringFromDate:[NSDate date]];
  382. for(int i=1;i<999;i++)
  383. {
  384. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  385. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  386. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  387. if(count==0)
  388. return soid;
  389. }
  390. return nil;
  391. }
  392. +(NSArray*) enumOfflineOrder
  393. {
  394. NSMutableArray* ret = [[NSMutableArray alloc] init];
  395. sqlite3 *db = [iSalesDB get_db];
  396. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  397. sqlite3_stmt * statement;
  398. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  399. if ( dbresult== SQLITE_OK)
  400. {
  401. while (sqlite3_step(statement) == SQLITE_ROW)
  402. {
  403. char *so_id = (char*)sqlite3_column_text(statement, 0);
  404. NSString* nsso_id=nil;
  405. if(so_id!=nil)
  406. {
  407. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  408. }
  409. [ret addObject:nsso_id];
  410. }
  411. sqlite3_finalize(statement);
  412. }
  413. [iSalesDB close_db:db];
  414. return ret;
  415. }
  416. +(void) uploadFile:(NSString*) file
  417. {
  418. NSData* data = [NSData dataWithContentsOfFile: file];
  419. UIApplication * app = [UIApplication sharedApplication];
  420. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  421. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  422. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  423. if(appDelegate.user!=nil)
  424. [params setValue:appDelegate.user forKey:@"user"];
  425. // if(appDelegate.contact_id!=nil)
  426. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  427. if(appDelegate.password!=nil)
  428. [params setValue:appDelegate.password forKey:@"password"];
  429. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  430. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  431. } error:nil];
  432. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  433. NSProgress *progress = nil;
  434. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  435. if (error) {
  436. NSString* err_msg = [error localizedDescription];
  437. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  438. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  439. NSLog(@"data string: %@",str);
  440. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  441. } else {
  442. NSLog(@"response ");
  443. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  444. // 再将NSData转为字符串
  445. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  446. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  447. NSLog(@"data string: %@",jsonStr);
  448. NSDictionary* json = responseObject;
  449. if([[json valueForKey:@"result"] intValue]==2)
  450. {
  451. // NSString* img_url_down = json[@"img_url_aname"];
  452. // NSString* img_url_up = json[@"img_url"];
  453. }
  454. else
  455. {
  456. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  457. }
  458. }
  459. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  460. }];
  461. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  462. //
  463. [uploadTask resume];
  464. }
  465. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  466. {
  467. NSString* serial= [[NSUUID UUID] UUIDString];
  468. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  469. NSString *cachefolder = [paths objectAtIndex:0];
  470. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  471. upfolder = [upfolder stringByAppendingPathComponent:serial];
  472. NSFileManager* fileManager = [NSFileManager defaultManager];
  473. BOOL bdir=YES;
  474. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  475. {
  476. NSError *error = nil;
  477. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  478. if(!bsuccess)
  479. DebugLog(@"Create UPLOAD folder failed");
  480. }
  481. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  482. ret[@"contact"]=[self prepareContact:serial];
  483. ret[@"wishlist"]=[self prepareWishlist:serial];
  484. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  485. ret[@"portfolio"] = [self preparePortfolio:serial];
  486. ret[@"view_portfolio"] = [self preparePDF:serial];
  487. NSString* str= [RAUtils dict2string:ret];
  488. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  489. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  490. NSError *error=nil;
  491. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  492. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  493. ZipArchive* zip = [[ZipArchive alloc] init];
  494. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  495. for(int i=0;i<arr_files.count;i++)
  496. {
  497. NSString* file=arr_files[i];
  498. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  499. }
  500. if( ![zip CloseZipFile2] )
  501. {
  502. zippath = @"";
  503. }
  504. ret[@"file"]=zippath;
  505. return ret;
  506. }
  507. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  508. {
  509. if(filename.length==0)
  510. return false;
  511. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  512. bool ret=false;
  513. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  514. NSString *cachefolder = [paths objectAtIndex:0];
  515. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  516. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  517. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  518. upfolder = [upfolder stringByAppendingPathComponent:serial];
  519. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  520. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  521. NSFileManager* fileManager = [NSFileManager defaultManager];
  522. BOOL bdir=NO;
  523. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  524. {
  525. NSError *error = nil;
  526. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  527. {
  528. ret=false;
  529. }
  530. else
  531. {
  532. ret=true;
  533. }
  534. }
  535. return ret;
  536. }
  537. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  538. {
  539. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  540. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  541. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  542. NSString* where=@" 1=1";
  543. if (ver!=nil) {
  544. where=@"is_dirty=1";
  545. }
  546. sqlite3 *db = [iSalesDB get_db];
  547. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  548. sqlite3_stmt * statement;
  549. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  550. int count=0;
  551. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  552. if ( dbresult== SQLITE_OK)
  553. {
  554. while (sqlite3_step(statement) == SQLITE_ROW)
  555. {
  556. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  557. int _id = sqlite3_column_int(statement, 0);
  558. int product_id = sqlite3_column_int(statement, 1);
  559. int item_id = sqlite3_column_int(statement, 2);
  560. int qty = sqlite3_column_int(statement, 3);
  561. char *create_time = (char*)sqlite3_column_text(statement, 4);
  562. NSString* nscreate_time=nil;
  563. if(create_time!=nil)
  564. {
  565. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  566. }
  567. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  568. NSString* nsmodify_time=nil;
  569. if(modify_time!=nil)
  570. {
  571. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  572. }
  573. int is_delete = sqlite3_column_int(statement, 6);
  574. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  575. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  576. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  577. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  578. itemjson[@"create_time"]=nscreate_time;
  579. itemjson[@"modify_time"]=nsmodify_time;
  580. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  581. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  582. count++;
  583. }
  584. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  585. sqlite3_finalize(statement);
  586. }
  587. ret[@"count"]=[NSNumber numberWithInt:count ];
  588. [iSalesDB close_db:db];
  589. return ret;
  590. }
  591. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  592. {
  593. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  594. UIApplication * app = [UIApplication sharedApplication];
  595. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  596. 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 ];
  597. // 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 ];
  598. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  599. DebugLog(@"offline_login sql:%@",sqlQuery);
  600. sqlite3_stmt * statement;
  601. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  602. int count=0;
  603. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  604. if ( dbresult== SQLITE_OK)
  605. {
  606. while (sqlite3_step(statement) == SQLITE_ROW)
  607. {
  608. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  609. int product_id = sqlite3_column_int(statement, 0);
  610. char *str_price = (char*)sqlite3_column_text(statement, 1);
  611. int item_id = sqlite3_column_int(statement, 7);
  612. NSString* Price=nil;
  613. if(str_price==nil)
  614. {
  615. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  616. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  617. if(price==nil)
  618. Price=@"No Price.";
  619. else
  620. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  621. }
  622. else
  623. {
  624. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  625. }
  626. double discount = sqlite3_column_double(statement, 2);
  627. int item_count = sqlite3_column_int(statement, 3);
  628. char *line_note = (char*)sqlite3_column_text(statement, 4);
  629. NSString *nsline_note=nil;
  630. if(line_note!=nil)
  631. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  632. char *name = (char*)sqlite3_column_text(statement, 5);
  633. NSString *nsname=nil;
  634. if(name!=nil)
  635. nsname= [[NSString alloc]initWithUTF8String:name];
  636. char *description = (char*)sqlite3_column_text(statement, 6);
  637. NSString *nsdescription=nil;
  638. if(description!=nil)
  639. nsdescription= [[NSString alloc]initWithUTF8String:description];
  640. // int stockUom = sqlite3_column_int(statement, 8);
  641. // int _id = sqlite3_column_int(statement, 9);
  642. //
  643. //
  644. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  645. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  646. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  647. // int carton=[bsubtotaljson[@"carton"] intValue];
  648. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  649. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  650. itemjson[@"model"]=nsname;
  651. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  652. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  653. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  654. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  655. // itemjson[@"check"]=@"true";
  656. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  657. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  658. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  659. itemjson[@"unit_price"]=Price;
  660. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  661. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  662. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  663. itemjson[@"note"]=nsline_note;
  664. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  665. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  666. count++;
  667. }
  668. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  669. sqlite3_finalize(statement);
  670. }
  671. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  672. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  673. return ret;
  674. }
  675. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  676. {
  677. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  678. sqlite3 *db = [iSalesDB get_db];
  679. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  680. // for(int i=0;i<arr_soid.count;i++)
  681. sqlite3_stmt * statement;
  682. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  683. //int count=0;
  684. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  685. if ( dbresult== SQLITE_OK)
  686. {
  687. int count=0;
  688. while (sqlite3_step(statement) == SQLITE_ROW)
  689. {
  690. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  691. int _id = sqlite3_column_int(statement, 0);
  692. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  693. NSString* nssync_data=nil;
  694. if(sync_data!=nil)
  695. {
  696. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  697. }
  698. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  699. NSString* nsimg_1=nil;
  700. if(img_1!=nil)
  701. {
  702. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  703. [self copy_upImg:serial file:nsimg_1];
  704. }
  705. char *so_no = (char*)sqlite3_column_text(statement, 3);
  706. NSString* nsso_no=nil;
  707. if(so_no!=nil)
  708. {
  709. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  710. }
  711. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  712. itemjson[@"order_type"]=@"submit order";
  713. else
  714. itemjson[@"order_type"]=@"archive order";
  715. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  716. itemjson[@"json_data"]= nssync_data;
  717. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  718. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  719. count++;
  720. }
  721. ret[@"count"]=[NSNumber numberWithInt:count ];
  722. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  723. }
  724. sqlite3_finalize(statement);
  725. [iSalesDB close_db:db];
  726. return ret;
  727. }
  728. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  729. {
  730. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  731. sqlite3 *db = [iSalesDB get_db];
  732. // UIApplication * app = [UIApplication sharedApplication];
  733. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  734. 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";
  735. sqlite3_stmt * statement;
  736. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  737. int count=0;
  738. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  739. if ( dbresult== SQLITE_OK)
  740. {
  741. while (sqlite3_step(statement) == SQLITE_ROW)
  742. {
  743. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  744. int _id = sqlite3_column_int(statement, 0);
  745. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  746. NSString* nsimg_0=nil;
  747. if(img_0!=nil)
  748. {
  749. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  750. [self copy_upImg:serial file:nsimg_0];
  751. }
  752. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  753. NSString* nsimg_1=nil;
  754. if(img_1!=nil)
  755. {
  756. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  757. [self copy_upImg:serial file:nsimg_1];
  758. }
  759. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  760. NSString* nsimg_2=nil;
  761. if(img_2!=nil)
  762. {
  763. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  764. [self copy_upImg:serial file:nsimg_2];
  765. }
  766. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  767. NSString* nssync_data=nil;
  768. if(sync_data!=nil)
  769. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  770. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  771. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  772. itemjson[@"json_data"]= nssync_data;
  773. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  774. count++;
  775. }
  776. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  777. sqlite3_finalize(statement);
  778. }
  779. ret[@"count"]=[NSNumber numberWithInt:count ];
  780. [iSalesDB close_db:db];
  781. return ret;
  782. }
  783. +(bool) check_offlinedata
  784. {
  785. UIApplication * app = [UIApplication sharedApplication];
  786. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  787. sqlite3 *db = [iSalesDB get_db];
  788. NSString * where=@"1=1";
  789. // if(appDelegate.user!=nil)
  790. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  791. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  792. [iSalesDB close_db:db];
  793. if(count==0)
  794. {
  795. return false;
  796. }
  797. return true;
  798. //
  799. //[iSalesDB close_db:db];
  800. }
  801. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  802. {
  803. UIApplication * app = [UIApplication sharedApplication];
  804. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  805. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  806. sqlite3 *db = [iSalesDB get_db];
  807. NSString* collectId=params[@"collectId"];
  808. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  809. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  810. [iSalesDB execSql:sqlQuery db:db];
  811. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  812. [iSalesDB close_db:db];
  813. appDelegate.wish_count =count;
  814. [appDelegate update_count_mark];
  815. ret[@"result"]= [NSNumber numberWithInt:2];
  816. return ret;
  817. }
  818. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  819. //{
  820. //
  821. // UIApplication * app = [UIApplication sharedApplication];
  822. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  823. //
  824. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  825. // sqlite3 *db = [iSalesDB get_db];
  826. // NSString* product_id=params[@"product_id"];
  827. //
  828. //
  829. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  830. //
  831. //
  832. // for(int i=0;i<arr.count;i++)
  833. // {
  834. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  835. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  836. // if(count==0)
  837. // {
  838. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  839. // [iSalesDB execSql:sqlQuery db:db];
  840. // }
  841. // }
  842. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  843. // [iSalesDB close_db:db];
  844. //
  845. // appDelegate.wish_count =count;
  846. //
  847. // [appDelegate update_count_mark];
  848. // ret[@"result"]= [NSNumber numberWithInt:2];
  849. // return ret;
  850. // //
  851. // //return ret;
  852. //}
  853. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  854. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  855. DebugLog(@"time interval: %lf",interval);
  856. }
  857. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  858. {
  859. UIApplication * app = [UIApplication sharedApplication];
  860. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  861. int sort = [[params objectForKey:@"sort"] intValue];
  862. NSString *sort_str = @"";
  863. switch (sort) {
  864. case 0:{
  865. sort_str = @"order by w.modify_time desc";
  866. }
  867. break;
  868. case 1:{
  869. sort_str = @"order by w.modify_time asc";
  870. }
  871. break;
  872. case 2:{
  873. sort_str = @"order by m.name asc";
  874. }
  875. break;
  876. case 3:{
  877. sort_str = @"order by m.name desc";
  878. }
  879. break;
  880. case 4:{
  881. sort_str = @"order by m.description asc";
  882. }
  883. break;
  884. default:
  885. break;
  886. }
  887. NSString* user = appDelegate.user;
  888. sqlite3 *db = [iSalesDB get_db];
  889. // order by w.create_time
  890. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  891. // 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];
  892. sqlite3_stmt * statement;
  893. NSDate *date1 = [NSDate date];
  894. NSDate *date2 = nil;
  895. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  896. int count=0;
  897. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  898. {
  899. while (sqlite3_step(statement) == SQLITE_ROW)
  900. {
  901. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  902. int product_id = sqlite3_column_double(statement, 0);
  903. char *description = (char*)sqlite3_column_text(statement, 1);
  904. if(description==nil)
  905. description= "";
  906. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  907. int item_id = sqlite3_column_double(statement, 2);
  908. NSDate *date_image = [NSDate date];
  909. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  910. printf("image : ");
  911. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  912. // char *url = (char*)sqlite3_column_text(statement, 3);
  913. // if(url==nil)
  914. // url="";
  915. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  916. int qty = sqlite3_column_int(statement, 3);
  917. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  918. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  919. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  920. item[@"description"]= nsdescription;
  921. item[@"img"]= nsurl;
  922. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  923. count++;
  924. }
  925. printf("total time:");
  926. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  927. ret[@"count"]= [NSNumber numberWithInt:count];
  928. ret[@"total_count"]= [NSNumber numberWithInt:count];
  929. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  930. ret[@"result"]= [NSNumber numberWithInt:2];
  931. appDelegate.wish_count =count;
  932. [appDelegate update_count_mark];
  933. sqlite3_finalize(statement);
  934. }
  935. [iSalesDB close_db:db];
  936. return ret;
  937. }
  938. +(NSDictionary*) offline_notimpl
  939. {
  940. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  941. ret[@"result"]=@"8";
  942. ret[@"err_msg"]=@"offline mode does not support this function.";
  943. return ret;
  944. }
  945. +(NSDictionary*) offline_home
  946. {
  947. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  948. NSString *cachefolder = [paths objectAtIndex:0];
  949. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  950. NSData* json =nil;
  951. json=[NSData dataWithContentsOfFile:img_cache];
  952. NSError *error=nil;
  953. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  954. return menu;
  955. }
  956. +(NSDictionary*) offline_category_menu
  957. {
  958. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  959. NSString *cachefolder = [paths objectAtIndex:0];
  960. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  961. NSData* json =nil;
  962. json=[NSData dataWithContentsOfFile:img_cache];
  963. NSError *error=nil;
  964. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  965. return menu;
  966. }
  967. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  968. {
  969. NSString* offline_command=params[@"offline_Command"];
  970. NSDictionary* ret=nil;
  971. if([offline_command isEqualToString:@"model_NIYMAL"])
  972. {
  973. NSString* category = params[@"category"];
  974. ret = [self refresh_model_NIYMAL:category];
  975. }
  976. return ret;
  977. }
  978. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  979. {
  980. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  981. [ret setValue:@"2" forKey:@"result"];
  982. [ret setValue:@"Regular Mode" forKey:@"mode"];
  983. sqlite3* db= [iSalesDB get_db];
  984. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  985. [iSalesDB close_db:db];
  986. [ret setObject:detail1_section forKey:@"detail_1"];
  987. return ret;
  988. }
  989. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  990. {
  991. UIApplication * app = [UIApplication sharedApplication];
  992. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  993. // NSArray* arr1 = [self get_user_all_price_type];
  994. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  995. // NSSet *set1 = [NSSet setWithArray:arr1];
  996. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  997. // if(appDelegate.contact_id==nil)
  998. // set2=[set1 mutableCopy];
  999. // else
  1000. // [set2 intersectsSet:set1];
  1001. // NSArray *retarr = [set2 allObjects];
  1002. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1003. // sqlite3 *db = [iSalesDB get_db];
  1004. NSString* sqlQuery = nil;
  1005. if(appDelegate.contact_id==nil)
  1006. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1007. else
  1008. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1009. sqlite3_stmt * statement;
  1010. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1011. int count=0;
  1012. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1013. {
  1014. while (sqlite3_step(statement) == SQLITE_ROW)
  1015. {
  1016. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1017. char *name = (char*)sqlite3_column_text(statement, 0);
  1018. if(name==nil)
  1019. name="";
  1020. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1021. // double price = sqlite3_column_double(statement, 1);
  1022. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1023. // if(isnull==nil)
  1024. // item[nsname]= @"No Price";
  1025. // else
  1026. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1027. char *price = (char*)sqlite3_column_text(statement, 1);
  1028. if(price!=nil)
  1029. {
  1030. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1031. nsprice=[AESCrypt fastdecrypt:nsprice];
  1032. if(nsprice.length>0)
  1033. {
  1034. double dp= [nsprice doubleValue];
  1035. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1036. }
  1037. }
  1038. else
  1039. {
  1040. item[nsname]= @"No Price";
  1041. }
  1042. // int type= sqlite3_column_int(statement, 2);
  1043. // item[@"type"]=[NSNumber numberWithInt:type ];
  1044. // item[nsname]= nsprice;
  1045. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1046. count++;
  1047. }
  1048. ret[@"count"]= [NSNumber numberWithInt:count];
  1049. sqlite3_finalize(statement);
  1050. }
  1051. // [iSalesDB close_db:db];
  1052. return ret;
  1053. }
  1054. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1055. //{
  1056. // NSArray* arr1 = [self get_user_all_price_type:db];
  1057. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1058. //
  1059. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1060. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1061. // // [set2 intersectsSet:set1];
  1062. // //
  1063. // //
  1064. // // NSArray *retarr = [set2 allObjects];
  1065. //
  1066. // NSString* whereprice=nil;
  1067. // if(contact_id==nil)
  1068. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1069. // else
  1070. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1071. //
  1072. //
  1073. // // sqlite3 *db = [iSalesDB get_db];
  1074. //
  1075. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1076. // sqlite3_stmt * statement;
  1077. //
  1078. //
  1079. // NSNumber* ret = nil;
  1080. // double dprice=DBL_MAX;
  1081. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1082. // {
  1083. //
  1084. //
  1085. // while (sqlite3_step(statement) == SQLITE_ROW)
  1086. // {
  1087. //
  1088. // // double val = sqlite3_column_double(statement, 0);
  1089. // char *price = (char*)sqlite3_column_text(statement, 0);
  1090. // if(price!=nil)
  1091. // {
  1092. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1093. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1094. // if(nsprice.length>0)
  1095. // {
  1096. // double dp= [nsprice doubleValue];
  1097. // if(dp<dprice)
  1098. // dprice=dp;
  1099. // }
  1100. // }
  1101. // }
  1102. //
  1103. //
  1104. //
  1105. //
  1106. // sqlite3_finalize(statement);
  1107. //
  1108. //
  1109. //
  1110. //
  1111. // }
  1112. //
  1113. // // [iSalesDB close_db:db];
  1114. //
  1115. // if(dprice==DBL_MAX)
  1116. // ret= nil;
  1117. // else
  1118. // ret= [NSNumber numberWithDouble:dprice];
  1119. // return ret;
  1120. //}
  1121. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1122. {
  1123. NSArray* arr1 = [self get_user_all_price_type:db];
  1124. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1125. NSString* whereprice=nil;
  1126. if(contact_id==nil)
  1127. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1128. else
  1129. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1130. // sqlite3 *db = [iSalesDB get_db];
  1131. NSString *productIdCondition = @"1 = 1";
  1132. if (product_id) {
  1133. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1134. }
  1135. NSString *itemIdCondition = @"";
  1136. if (item_id) {
  1137. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1138. }
  1139. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1140. sqlite3_stmt * statement;
  1141. NSNumber* ret = nil;
  1142. double dprice=DBL_MAX;
  1143. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1144. {
  1145. while (sqlite3_step(statement) == SQLITE_ROW)
  1146. {
  1147. // double val = sqlite3_column_double(statement, 0);
  1148. char *price = (char*)sqlite3_column_text(statement, 0);
  1149. if(price!=nil)
  1150. {
  1151. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1152. nsprice=[AESCrypt fastdecrypt:nsprice];
  1153. if(nsprice.length>0)
  1154. {
  1155. double dp= [nsprice doubleValue];
  1156. if(dp<dprice)
  1157. dprice=dp;
  1158. }
  1159. }
  1160. }
  1161. sqlite3_finalize(statement);
  1162. }
  1163. // [iSalesDB close_db:db];
  1164. if(dprice==DBL_MAX)
  1165. ret= nil;
  1166. else
  1167. ret= [NSNumber numberWithDouble:dprice];
  1168. return ret;
  1169. }
  1170. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1171. {
  1172. NSArray* ret=nil;
  1173. // sqlite3 *db = [iSalesDB get_db];
  1174. // no customer assigned , use login user contact_id
  1175. UIApplication * app = [UIApplication sharedApplication];
  1176. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1177. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1178. sqlite3_stmt * statement;
  1179. // int count=0;
  1180. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1181. {
  1182. if (sqlite3_step(statement) == SQLITE_ROW)
  1183. {
  1184. char *val = (char*)sqlite3_column_text(statement, 0);
  1185. if(val==nil)
  1186. val="";
  1187. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1188. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1189. }
  1190. sqlite3_finalize(statement);
  1191. }
  1192. // [iSalesDB close_db:db];
  1193. return ret;
  1194. }
  1195. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1196. {
  1197. // sqlite3 *db = [iSalesDB get_db];
  1198. if(contact_id==nil)
  1199. {
  1200. // no customer assigned , use login user contact_id
  1201. UIApplication * app = [UIApplication sharedApplication];
  1202. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1203. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1204. sqlite3_stmt * statement;
  1205. // int count=0;
  1206. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1207. {
  1208. if (sqlite3_step(statement) == SQLITE_ROW)
  1209. {
  1210. char *val = (char*)sqlite3_column_text(statement, 0);
  1211. if(val==nil)
  1212. val="";
  1213. contact_id = [[NSString alloc]initWithUTF8String:val];
  1214. }
  1215. sqlite3_finalize(statement);
  1216. }
  1217. if(contact_id.length<=0)
  1218. {
  1219. // [iSalesDB close_db:db];
  1220. return nil;
  1221. }
  1222. }
  1223. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1224. sqlite3_stmt * statement;
  1225. NSArray* ret=nil;
  1226. // int count=0;
  1227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1228. {
  1229. if (sqlite3_step(statement) == SQLITE_ROW)
  1230. {
  1231. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1232. if(price_type==nil)
  1233. price_type="";
  1234. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1235. if(nsprice_type.length>0)
  1236. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1237. else
  1238. ret=nil;
  1239. }
  1240. sqlite3_finalize(statement);
  1241. }
  1242. // [iSalesDB close_db:db];
  1243. return ret;
  1244. }
  1245. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1246. {
  1247. NSString* ret= nil;
  1248. // sqlite3 *db = [iSalesDB get_db];
  1249. NSString *sqlQuery = nil;
  1250. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1251. // 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;
  1252. // 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
  1253. if(product_id==nil && model_name)
  1254. 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;
  1255. else if (product_id)
  1256. 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
  1257. sqlite3_stmt * statement;
  1258. // int count=0;
  1259. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1260. {
  1261. if (sqlite3_step(statement) == SQLITE_ROW)
  1262. {
  1263. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1264. if(imgurl==nil)
  1265. imgurl="";
  1266. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1267. ret=nsimgurl;
  1268. }
  1269. sqlite3_finalize(statement);
  1270. }
  1271. else
  1272. {
  1273. [ret setValue:@"8" forKey:@"result"];
  1274. }
  1275. // [iSalesDB close_db:db];
  1276. DebugLog(@"data string: %@",ret );
  1277. return ret;
  1278. }
  1279. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1280. {
  1281. NSString* ret= nil;
  1282. sqlite3 *db = [iSalesDB get_db];
  1283. NSString *sqlQuery = nil;
  1284. if(product_id==nil)
  1285. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1286. else
  1287. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1288. sqlite3_stmt * statement;
  1289. // int count=0;
  1290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1291. {
  1292. if (sqlite3_step(statement) == SQLITE_ROW)
  1293. {
  1294. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1295. if(default_category==nil)
  1296. default_category="";
  1297. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1298. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1299. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1300. ret=nsdefault_category;
  1301. }
  1302. sqlite3_finalize(statement);
  1303. }
  1304. else
  1305. {
  1306. [ret setValue:@"8" forKey:@"result"];
  1307. }
  1308. [iSalesDB close_db:db];
  1309. DebugLog(@"data string: %@",ret );
  1310. return ret;
  1311. }
  1312. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1313. {
  1314. NSString* model_name = [params valueForKey:@"product_name"];
  1315. NSString* product_id = [params valueForKey:@"product_id"];
  1316. NSString* category = [params valueForKey:@"category"];
  1317. if(category==nil)
  1318. category = [self model_default_category:product_id model_name:model_name];
  1319. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1320. sqlite3 *db = [iSalesDB get_db];
  1321. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1322. NSString *sqlQuery = nil;
  1323. if(product_id==nil)
  1324. 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,ETA,item_id 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='%@';
  1325. else
  1326. 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,ETA,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=%@;
  1327. sqlite3_stmt * statement;
  1328. [ret setValue:@"2" forKey:@"result"];
  1329. [ret setValue:@"3" forKey:@"detail_section_count"];
  1330. // int count=0;
  1331. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1332. {
  1333. if (sqlite3_step(statement) == SQLITE_ROW)
  1334. {
  1335. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1336. char *name = (char*)sqlite3_column_text(statement, 0);
  1337. if(name==nil)
  1338. name="";
  1339. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1340. char *description = (char*)sqlite3_column_text(statement, 1);
  1341. if(description==nil)
  1342. description="";
  1343. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1344. int product_id = sqlite3_column_int(statement, 2);
  1345. char *color = (char*)sqlite3_column_text(statement, 3);
  1346. if(color==nil)
  1347. color="";
  1348. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1349. //
  1350. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1351. // if(legcolor==nil)
  1352. // legcolor="";
  1353. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1354. //
  1355. //
  1356. int availability = sqlite3_column_int(statement, 5);
  1357. //
  1358. int incoming_stock = sqlite3_column_int(statement, 6);
  1359. char *demension = (char*)sqlite3_column_text(statement, 7);
  1360. if(demension==nil)
  1361. demension="";
  1362. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1363. // ,,,,,,,,,
  1364. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1365. if(seat_height==nil)
  1366. seat_height="";
  1367. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1368. char *material = (char*)sqlite3_column_text(statement, 9);
  1369. if(material==nil)
  1370. material="";
  1371. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1372. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1373. if(box_dim==nil)
  1374. box_dim="";
  1375. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1376. char *volume = (char*)sqlite3_column_text(statement, 11);
  1377. if(volume==nil)
  1378. volume="";
  1379. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1380. double weight = sqlite3_column_double(statement, 12);
  1381. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1382. if(model_set==nil)
  1383. model_set="";
  1384. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1385. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1386. if(load_ability==nil)
  1387. load_ability="";
  1388. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1389. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1390. if(default_category==nil)
  1391. default_category="";
  1392. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1393. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1394. if(fabric_content==nil)
  1395. fabric_content="";
  1396. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1397. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1398. if(assembling==nil)
  1399. assembling="";
  1400. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1401. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1402. if(made_in==nil)
  1403. made_in="";
  1404. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1405. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1406. if(special_remarks==nil)
  1407. special_remarks="";
  1408. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1409. int stockUcom = sqlite3_column_double(statement, 20);
  1410. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1411. if(product_group==nil)
  1412. product_group="";
  1413. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1414. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1415. // if(fashion_selector==nil)
  1416. // fashion_selector="";
  1417. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1418. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1419. if(selector_field==nil)
  1420. selector_field="";
  1421. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1422. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1423. if(property_field==nil)
  1424. property_field="";
  1425. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1426. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1427. if(packaging==nil)
  1428. packaging="";
  1429. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1430. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1431. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1432. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1433. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1434. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1435. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1436. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1437. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1438. NSString* Availability=nil;
  1439. if(availability>0)
  1440. Availability=[NSString stringWithFormat:@"%d",availability];
  1441. else
  1442. Availability = @"In Production";
  1443. [img_section setValue:Availability forKey:@"Availability"];
  1444. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1445. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1446. char *eta = (char*)sqlite3_column_text(statement, 25);
  1447. if(eta==nil)
  1448. eta="";
  1449. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1450. [img_section setValue:nseta forKey:@"ETA"];
  1451. int item_id = sqlite3_column_int(statement, 26);
  1452. NSString* Price=nil;
  1453. if(appDelegate.bLogin==false)
  1454. Price=@"Must Sign in.";
  1455. else
  1456. {
  1457. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1458. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1459. if(price==nil)
  1460. Price=@"No Price.";
  1461. else
  1462. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1463. }
  1464. [img_section setValue:Price forKey:@"price"];
  1465. [img_section setValue:nsname forKey:@"model_name"];
  1466. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1467. [ret setObject:img_section forKey:@"img_section"];
  1468. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1469. int detail0_item_count=0;
  1470. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1471. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1472. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1473. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1474. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1475. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1476. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1477. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1478. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1479. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1480. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1481. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1482. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1483. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1484. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1485. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1486. {
  1487. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1488. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1489. }
  1490. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1491. [detail0_section setValue:@"kv" forKey:@"type"];
  1492. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1493. [ret setObject:detail0_section forKey:@"detail_0"];
  1494. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1495. // [detail1_section setValue:@"detail" forKey:@"target"];
  1496. // [detail1_section setValue:@"popup" forKey:@"action"];
  1497. // [detail1_section setValue:@"content" forKey:@"type"];
  1498. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1499. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1500. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1501. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1502. [ret setObject:detail1_section forKey:@"detail_1"];
  1503. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1504. [detail2_section setValue:@"detail" forKey:@"target"];
  1505. [detail2_section setValue:@"popup" forKey:@"action"];
  1506. [detail2_section setValue:@"content" forKey:@"type"];
  1507. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1508. [detail2_section setValue:@"true" forKey:@"single_row"];
  1509. [detail2_section setValue:@"local" forKey:@"data"];
  1510. [ret setObject:detail2_section forKey:@"detail_2"];
  1511. }
  1512. sqlite3_finalize(statement);
  1513. }
  1514. else
  1515. {
  1516. [ret setValue:@"8" forKey:@"result"];
  1517. }
  1518. NSLog(@"count:%d",count);
  1519. [iSalesDB close_db:db];
  1520. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1521. return ret;
  1522. }
  1523. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1524. NSString* orderCode = [params valueForKey:@"orderCode"];
  1525. NSString* keyword = [params valueForKey:@"keyword"];
  1526. keyword=keyword.lowercaseString;
  1527. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1528. int limit = [[params valueForKey:@"limit"] intValue];
  1529. int offset = [[params valueForKey:@"offset"] intValue];
  1530. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1531. NSString *limit_str = @"";
  1532. if (limited) {
  1533. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1534. }
  1535. sqlite3 *db = [iSalesDB get_db];
  1536. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1537. UIApplication * app = [UIApplication sharedApplication];
  1538. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1539. NSString *sqlQuery = nil;
  1540. if(exactMatch )
  1541. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, 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 ;
  1542. else
  1543. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, 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
  1544. DebugLog(@"offline_search sql:%@",sqlQuery);
  1545. sqlite3_stmt * statement;
  1546. [ret setValue:@"2" forKey:@"result"];
  1547. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1548. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1549. // int count=0;
  1550. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1551. {
  1552. int i=0;
  1553. while (sqlite3_step(statement) == SQLITE_ROW)
  1554. {
  1555. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1556. // char *name = (char*)sqlite3_column_text(statement, 1);
  1557. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1558. char *name = (char*)sqlite3_column_text(statement, 0);
  1559. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1560. char *description = (char*)sqlite3_column_text(statement, 1);
  1561. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1562. int product_id = sqlite3_column_int(statement, 2);
  1563. // char *url = (char*)sqlite3_column_text(statement, 3);
  1564. // if(url==nil)
  1565. // url="";
  1566. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1567. int wid = sqlite3_column_int(statement, 3);
  1568. int closeout = sqlite3_column_int(statement, 4);
  1569. int cid = sqlite3_column_int(statement, 5);
  1570. int wisdelete = sqlite3_column_int(statement, 6);
  1571. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1572. if(wid !=0 && wisdelete != 1)
  1573. [item setValue:@"true" forKey:@"wish_exists"];
  1574. else
  1575. [item setValue:@"false" forKey:@"wish_exists"];
  1576. if(closeout==0)
  1577. [item setValue:@"false" forKey:@"is_closeout"];
  1578. else
  1579. [item setValue:@"true" forKey:@"is_closeout"];
  1580. if(cid==0)
  1581. [item setValue:@"false" forKey:@"cart_exists"];
  1582. else
  1583. [item setValue:@"true" forKey:@"cart_exists"];
  1584. [item addEntriesFromDictionary:imgjson];
  1585. // [item setValue:nsurl forKey:@"img"];
  1586. [item setValue:nsname forKey:@"fash_name"];
  1587. [item setValue:nsdescription forKey:@"description"];
  1588. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1589. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1590. i++;
  1591. }
  1592. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1593. [ret setObject:items forKey:@"items"];
  1594. sqlite3_finalize(statement);
  1595. }
  1596. NSLog(@"count:%d",count);
  1597. [iSalesDB close_db:db];
  1598. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1599. return ret;
  1600. }
  1601. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1602. {
  1603. return [self search:params limited:YES];
  1604. }
  1605. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1606. {
  1607. UIApplication * app = [UIApplication sharedApplication];
  1608. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1609. appDelegate.disable_trigger=true;
  1610. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1611. appDelegate.disable_trigger=false;
  1612. //
  1613. // NSString* user = [params valueForKey:@"user"];
  1614. //
  1615. // NSString* password = [params valueForKey:@"password"];
  1616. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1617. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1618. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1619. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1620. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1621. [appDelegate update_count_mark];
  1622. appDelegate.can_show_price =false;
  1623. appDelegate.can_see_price =false;
  1624. appDelegate.can_create_portfolio =false;
  1625. appDelegate.can_create_order =false;
  1626. appDelegate.can_cancel_order =false;
  1627. appDelegate.can_set_cart_price =false;
  1628. appDelegate.can_delete_order =false;
  1629. appDelegate.can_submit_order =false;
  1630. appDelegate.can_set_tearsheet_price =false;
  1631. appDelegate.can_update_contact_info = false;
  1632. appDelegate.save_order_logout = false;
  1633. appDelegate.submit_order_logout = false;
  1634. appDelegate.alert_sold_in_quantities = false;
  1635. appDelegate.ipad_perm =nil ;
  1636. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1637. appDelegate.OrderFilter= nil;
  1638. [appDelegate SetSo:nil];
  1639. [appDelegate set_main_button_panel];
  1640. // sqlite3 *db = [iSalesDB get_db];
  1641. //
  1642. //
  1643. //
  1644. //
  1645. //
  1646. // 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"]];
  1647. //
  1648. //
  1649. //
  1650. //
  1651. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1652. // sqlite3_stmt * statement;
  1653. //
  1654. //
  1655. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1656. //
  1657. //
  1658. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1659. // {
  1660. //
  1661. //
  1662. // if (sqlite3_step(statement) == SQLITE_ROW)
  1663. // {
  1664. //
  1665. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1666. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1667. //
  1668. //
  1669. //
  1670. // int can_show_price = sqlite3_column_int(statement, 0);
  1671. // int can_see_price = sqlite3_column_int(statement, 1);
  1672. //
  1673. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1674. // if(contact_id==nil)
  1675. // contact_id="";
  1676. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1677. //
  1678. // int user_type = sqlite3_column_int(statement, 3);
  1679. //
  1680. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1681. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1682. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1683. // int can_delete_order = sqlite3_column_int(statement, 7);
  1684. // int can_submit_order = sqlite3_column_int(statement, 8);
  1685. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1686. // int can_create_order = sqlite3_column_int(statement, 10);
  1687. //
  1688. //
  1689. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1690. // if(mode==nil)
  1691. // mode="";
  1692. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1693. //
  1694. //
  1695. // char *username = (char*)sqlite3_column_text(statement, 12);
  1696. // if(username==nil)
  1697. // username="";
  1698. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1699. //
  1700. //
  1701. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1702. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1703. // [header setValue:nscontact_id forKey:@"contact_id"];
  1704. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1705. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1706. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1707. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1708. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1709. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1710. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1711. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1712. //
  1713. // [header setValue:nsusername forKey:@"username"];
  1714. //
  1715. //
  1716. // [ret setObject:header forKey:@"header"];
  1717. // [ret setValue:nsmode forKey:@"mode"];
  1718. //
  1719. //
  1720. // }
  1721. //
  1722. //
  1723. //
  1724. // sqlite3_finalize(statement);
  1725. // }
  1726. //
  1727. //
  1728. //
  1729. // [iSalesDB close_db:db];
  1730. //
  1731. //
  1732. //
  1733. //
  1734. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1735. return ret;
  1736. }
  1737. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1738. {
  1739. UIApplication * app = [UIApplication sharedApplication];
  1740. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1741. appDelegate.disable_trigger=true;
  1742. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1743. appDelegate.disable_trigger=false;
  1744. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1745. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1746. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1747. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1748. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1749. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1750. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1751. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1752. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1753. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1754. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1755. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1756. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1757. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1758. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1759. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1760. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1761. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1762. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1763. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1764. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1765. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1766. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1767. [arr_name addObject:customer_first_name];
  1768. [arr_name addObject:customer_last_name];
  1769. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1770. // default ship from
  1771. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  1772. __block NSString *cid = @"";
  1773. __block NSString *name = @"";
  1774. __block NSString *ext = @"";
  1775. __block NSString *contact = @"";
  1776. __block NSString *email = @"";
  1777. __block NSString *fax = @"";
  1778. __block NSString *phone = @"";
  1779. sqlite3 *db = [iSalesDB get_db];
  1780. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1781. cid = [self textAtColumn:0 statement:statment];
  1782. name = [self textAtColumn:1 statement:statment];
  1783. ext = [self textAtColumn:2 statement:statment];
  1784. contact = [self textAtColumn:3 statement:statment];
  1785. email = [self textAtColumn:4 statement:statment];
  1786. fax = [self textAtColumn:5 statement:statment];
  1787. phone = [self textAtColumn:6 statement:statment];
  1788. }];
  1789. NSString* so_id = [self get_offline_soid:db];
  1790. if(so_id==nil)
  1791. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1792. 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,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email];
  1793. int result =[iSalesDB execSql:sql_neworder db:db];
  1794. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1795. //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'
  1796. //soId
  1797. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1798. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1799. sqlite3_stmt * statement;
  1800. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1801. {
  1802. if (sqlite3_step(statement) == SQLITE_ROW)
  1803. {
  1804. // char *name = (char*)sqlite3_column_text(statement, 1);
  1805. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1806. //ret = sqlite3_column_int(statement, 0);
  1807. char *soId = (char*)sqlite3_column_text(statement, 0);
  1808. if(soId==nil)
  1809. soId="";
  1810. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1811. [ret setValue:nssoId forKey:@"soId"];
  1812. [ret setValue:nssoId forKey:@"orderCode"];
  1813. }
  1814. sqlite3_finalize(statement);
  1815. }
  1816. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1817. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1818. [iSalesDB close_db:db];
  1819. return [RAUtils dict2data:ret];
  1820. }
  1821. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1822. {
  1823. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1824. NSString* orderCode = [params valueForKey:@"orderCode"];
  1825. UIApplication * app = [UIApplication sharedApplication];
  1826. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1827. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1828. {
  1829. appDelegate.disable_trigger=true;
  1830. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1831. appDelegate.disable_trigger=false;
  1832. }
  1833. sqlite3 *db = [iSalesDB get_db];
  1834. int cart_count=[self query_ordercartcount:orderCode db:db];
  1835. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1836. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1837. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1838. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1839. [iSalesDB close_db:db];
  1840. return [RAUtils dict2data:ret];
  1841. }
  1842. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1843. {
  1844. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1845. sqlite3 *db = [iSalesDB get_db];
  1846. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1847. int count =0;
  1848. if(params[@"count"]!=nil)
  1849. {
  1850. count = [params[@"count"] intValue];
  1851. }
  1852. NSString* product_id=params[@"product_id"];
  1853. NSString* orderCode=params[@"orderCode"];
  1854. NSString *qty = params[@"qty"];
  1855. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1856. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1857. for(int i=0;i<arr_id.count;i++)
  1858. {
  1859. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1860. int item_qty= count;
  1861. if (qty) {
  1862. item_qty = [qty_arr[i] integerValue];
  1863. }
  1864. if(item_qty==0)
  1865. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1866. NSString* sql=nil;
  1867. sqlite3_stmt *stmt;
  1868. if(_id<0)
  1869. {
  1870. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1871. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1872. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1873. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1874. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1875. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1876. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1877. }
  1878. else
  1879. {
  1880. if (qty) {
  1881. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1882. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1883. sqlite3_bind_int(stmt, 1, _id);
  1884. } else {
  1885. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1886. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1887. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1888. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1889. }
  1890. }
  1891. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1892. [iSalesDB execSql:@"ROLLBACK" db:db];
  1893. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1894. [iSalesDB close_db:db];
  1895. DebugLog(@"add to cart error");
  1896. return [RAUtils dict2data:ret];
  1897. }
  1898. }
  1899. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1900. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1901. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1902. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1903. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1904. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1905. [iSalesDB close_db:db];
  1906. return [RAUtils dict2data:ret];
  1907. }
  1908. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1909. {
  1910. UIApplication * app = [UIApplication sharedApplication];
  1911. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1912. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1913. 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];
  1914. sqlite3_stmt * statement;
  1915. int count=0;
  1916. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1917. {
  1918. while (sqlite3_step(statement) == SQLITE_ROW)
  1919. {
  1920. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1921. int bitem_id = sqlite3_column_int(statement, 0);
  1922. int bitem_qty = sqlite3_column_int(statement, 1);
  1923. char *name = (char*)sqlite3_column_text(statement, 2);
  1924. if(name==nil)
  1925. name="";
  1926. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1927. char *description = (char*)sqlite3_column_text(statement, 3);
  1928. if(description==nil)
  1929. description="";
  1930. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1931. double unit_price = sqlite3_column_double(statement, 4);
  1932. int use_unitprice = sqlite3_column_int(statement, 5);
  1933. if(use_unitprice!=1)
  1934. {
  1935. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1936. }
  1937. itemjson[@"model"]=nsname;
  1938. itemjson[@"description"]=nsdescription;
  1939. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1940. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1941. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1942. if(compute)
  1943. {
  1944. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1945. }
  1946. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1947. count++;
  1948. }
  1949. sqlite3_finalize(statement);
  1950. }
  1951. ret[@"count"]=@(count);
  1952. if(count==0)
  1953. return nil;
  1954. else
  1955. return ret;
  1956. }
  1957. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1958. {
  1959. //compute: add part to subtotal;
  1960. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1961. dict_item[@(item_id)]=@"1";
  1962. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1963. double cuft=0;
  1964. double weight=0;
  1965. int carton=0;
  1966. 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];
  1967. sqlite3_stmt * statement;
  1968. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1969. {
  1970. if (sqlite3_step(statement) == SQLITE_ROW)
  1971. {
  1972. double ulength = sqlite3_column_double(statement, 0);
  1973. double uwidth = sqlite3_column_double(statement, 1);
  1974. double uheight = sqlite3_column_double(statement, 2);
  1975. double uweight = sqlite3_column_double(statement, 3);
  1976. double mlength = sqlite3_column_double(statement, 4);
  1977. double mwidth = sqlite3_column_double(statement, 5);
  1978. double mheight = sqlite3_column_double(statement, 6);
  1979. double mweight = sqlite3_column_double(statement, 7);
  1980. double ilength = sqlite3_column_double(statement, 8);
  1981. double iwidth = sqlite3_column_double(statement, 9);
  1982. double iheight = sqlite3_column_double(statement, 10);
  1983. double iweight = sqlite3_column_double(statement, 11);
  1984. int pcs = sqlite3_column_int(statement,12);
  1985. int mpack = sqlite3_column_int(statement, 13);
  1986. int ipack = sqlite3_column_int(statement, 14);
  1987. double ucbf = sqlite3_column_double(statement, 15);
  1988. double icbf = sqlite3_column_double(statement, 16);
  1989. double mcbf = sqlite3_column_double(statement, 17);
  1990. if(ipack==0)
  1991. {
  1992. carton= count/mpack ;
  1993. weight = mweight*carton;
  1994. cuft= carton*(mlength*mwidth*mheight);
  1995. int remain=count%mpack;
  1996. if(remain==0)
  1997. {
  1998. //do nothing;
  1999. }
  2000. else
  2001. {
  2002. carton++;
  2003. weight += uweight*remain;
  2004. cuft += (ulength*uwidth*uheight)*remain;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. carton = count/(mpack*ipack);
  2010. weight = mweight*carton;
  2011. cuft= carton*(mlength*mwidth*mheight);
  2012. int remain=count%(mpack*ipack);
  2013. if(remain==0)
  2014. {
  2015. // do nothing;
  2016. }
  2017. else
  2018. {
  2019. carton++;
  2020. int icarton =remain/ipack;
  2021. int iremain=remain%ipack;
  2022. weight += iweight*icarton;
  2023. cuft += (ilength*iwidth*iheight)*icarton;
  2024. if(iremain==0)
  2025. {
  2026. //do nothing;
  2027. }
  2028. else
  2029. {
  2030. weight += uweight*iremain;
  2031. cuft += (ulength*uwidth*uheight)*iremain;
  2032. }
  2033. }
  2034. }
  2035. #ifdef BUILD_NPD
  2036. cuft=ucbf*count;
  2037. weight= uweight*count;
  2038. #endif
  2039. }
  2040. sqlite3_finalize(statement);
  2041. }
  2042. if(compute)
  2043. {
  2044. NSArray * arr_count=nil;
  2045. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2046. for(int i=0;i<arr_bundle.count;i++)
  2047. {
  2048. dict_item[arr_bundle[i]]=@"1";
  2049. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2050. cuft+=[bundlejson[@"cuft"] doubleValue];
  2051. weight+=[bundlejson[@"weight"] doubleValue];
  2052. carton+=[bundlejson[@"carton"] intValue];
  2053. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2054. }
  2055. }
  2056. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2057. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2058. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2059. ret[@"items"]=dict_item;
  2060. return ret;
  2061. }
  2062. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2063. {
  2064. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2065. // 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 ];
  2066. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2067. DebugLog(@"offline_login sql:%@",sqlQuery);
  2068. sqlite3_stmt * statement;
  2069. int cart_count=0;
  2070. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2071. if ( dbresult== SQLITE_OK)
  2072. {
  2073. while (sqlite3_step(statement) == SQLITE_ROW)
  2074. {
  2075. int item_id = sqlite3_column_int(statement, 0);
  2076. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2077. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2078. }
  2079. sqlite3_finalize(statement);
  2080. }
  2081. return cart_count;
  2082. }
  2083. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2084. {
  2085. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2086. sqlite3 *db = [iSalesDB get_db];
  2087. UIApplication * app = [UIApplication sharedApplication];
  2088. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2089. NSString* orderCode=params[@"orderCode"];
  2090. int sort = [[params objectForKey:@"sort"] intValue];
  2091. NSString *sort_str = @"";
  2092. switch (sort) {
  2093. case 0:{
  2094. sort_str = @"order by c.modify_time desc";
  2095. }
  2096. break;
  2097. case 1:{
  2098. sort_str = @"order by c.modify_time asc";
  2099. }
  2100. break;
  2101. case 2:{
  2102. sort_str = @"order by m.name asc";
  2103. }
  2104. break;
  2105. case 3:{
  2106. sort_str = @"order by m.name desc";
  2107. }
  2108. break;
  2109. case 4:{
  2110. sort_str = @"order by m.description asc";
  2111. }
  2112. break;
  2113. default:
  2114. break;
  2115. }
  2116. 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,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  2117. // 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 ];
  2118. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2119. DebugLog(@"offline_login sql:%@",sqlQuery);
  2120. sqlite3_stmt * statement;
  2121. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2122. NSDate *date1 = [NSDate date];
  2123. int count=0;
  2124. int cart_count=0;
  2125. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2126. if ( dbresult== SQLITE_OK)
  2127. {
  2128. while (sqlite3_step(statement) == SQLITE_ROW)
  2129. {
  2130. NSDate *row_date = [NSDate date];
  2131. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2132. int product_id = sqlite3_column_int(statement, 0);
  2133. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2134. int item_id = sqlite3_column_int(statement, 7);
  2135. NSString* Price=nil;
  2136. if(str_price==nil)
  2137. {
  2138. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2139. // NSDate *price_date = [NSDate date];
  2140. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2141. // DebugLog(@"price time interval");
  2142. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2143. if(price==nil)
  2144. Price=@"No Price.";
  2145. else
  2146. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2147. }
  2148. else
  2149. {
  2150. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2151. }
  2152. double discount = sqlite3_column_double(statement, 2);
  2153. int item_count = sqlite3_column_int(statement, 3);
  2154. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2155. NSString *nsline_note=nil;
  2156. if(line_note!=nil)
  2157. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2158. char *name = (char*)sqlite3_column_text(statement, 5);
  2159. NSString *nsname=nil;
  2160. if(name!=nil)
  2161. nsname= [[NSString alloc]initWithUTF8String:name];
  2162. char *description = (char*)sqlite3_column_text(statement, 6);
  2163. NSString *nsdescription=nil;
  2164. if(description!=nil)
  2165. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2166. int stockUom = sqlite3_column_int(statement, 8);
  2167. int _id = sqlite3_column_int(statement, 9);
  2168. // NSDate *subtotal_date = [NSDate date];
  2169. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2170. // DebugLog(@"subtotal_date time interval");
  2171. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2172. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2173. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2174. int carton=[bsubtotaljson[@"carton"] intValue];
  2175. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2176. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2177. // NSDate *img_date = [NSDate date];
  2178. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2179. // DebugLog(@"img_date time interval");
  2180. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2181. itemjson[@"model"]=nsname;
  2182. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2183. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2184. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2185. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2186. itemjson[@"check"]=@"true";
  2187. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2188. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2189. itemjson[@"unit_price"]=Price;
  2190. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2191. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2192. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2193. itemjson[@"note"]=nsline_note;
  2194. // NSDate *date2 = [NSDate date];
  2195. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2196. // DebugLog(@"model_bundle time interval");
  2197. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2198. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2199. count++;
  2200. // DebugLog(@"row time interval");
  2201. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2202. }
  2203. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2204. sqlite3_finalize(statement);
  2205. }
  2206. DebugLog(@"request cart total time interval");
  2207. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2208. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2209. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2210. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2211. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2212. ret[@"count"]=[NSNumber numberWithInt:count ];
  2213. ret[@"mode"]=@"Regular Mode";
  2214. [iSalesDB close_db:db];
  2215. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2216. DebugLog(@"general notes :%@",general_note);
  2217. ret[@"general_note"]= general_note;
  2218. return [RAUtils dict2data:ret];
  2219. }
  2220. +(NSData*) offline_login :(NSMutableDictionary *) params
  2221. {
  2222. NSString* user = [params valueForKey:@"user"];
  2223. NSString* password = [params valueForKey:@"password"];
  2224. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2225. sqlite3 *db = [iSalesDB get_db];
  2226. 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"]];
  2227. DebugLog(@"offline_login sql:%@",sqlQuery);
  2228. sqlite3_stmt * statement;
  2229. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2230. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2231. {
  2232. if (sqlite3_step(statement) == SQLITE_ROW)
  2233. {
  2234. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2235. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2236. int can_show_price = sqlite3_column_int(statement, 0);
  2237. int can_see_price = sqlite3_column_int(statement, 1);
  2238. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2239. if(contact_id==nil)
  2240. contact_id="";
  2241. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2242. int user_type = sqlite3_column_int(statement, 3);
  2243. int can_cancel_order = sqlite3_column_int(statement, 4);
  2244. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2245. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2246. int can_delete_order = sqlite3_column_int(statement, 7);
  2247. int can_submit_order = sqlite3_column_int(statement, 8);
  2248. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2249. int can_create_order = sqlite3_column_int(statement, 10);
  2250. char *mode = (char*)sqlite3_column_text(statement, 11);
  2251. if(mode==nil)
  2252. mode="";
  2253. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2254. char *username = (char*)sqlite3_column_text(statement, 12);
  2255. if(username==nil)
  2256. username="";
  2257. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2258. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2259. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2260. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2261. [header setValue:nscontact_id forKey:@"contact_id"];
  2262. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2263. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2264. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2265. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2266. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2267. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2268. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2269. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2270. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2271. [header setValue:nsusername forKey:@"username"];
  2272. NSError* error=nil;
  2273. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2274. [header setValue:statusFilter forKey:@"statusFilter"];
  2275. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2276. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2277. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2278. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2279. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2280. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2281. [ret setObject:header forKey:@"header"];
  2282. [ret setValue:nsmode forKey:@"mode"];
  2283. }
  2284. sqlite3_finalize(statement);
  2285. }
  2286. [iSalesDB close_db:db];
  2287. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2288. return [RAUtils dict2data:ret];
  2289. }
  2290. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2291. {
  2292. NSString* contactId = [params valueForKey:@"contactId"];
  2293. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2294. sqlite3 *db = [iSalesDB get_db];
  2295. NSString *sqlQuery = nil;
  2296. {
  2297. 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];
  2298. }
  2299. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2300. sqlite3_stmt * statement;
  2301. [ret setValue:@"2" forKey:@"result"];
  2302. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2303. {
  2304. //int i = 0;
  2305. if (sqlite3_step(statement) == SQLITE_ROW)
  2306. {
  2307. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2308. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2309. // int editable = sqlite3_column_int(statement, 0);
  2310. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2311. NSString *nscompany_name =nil;
  2312. if(company_name==nil)
  2313. nscompany_name=@"";
  2314. else
  2315. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2316. char *country = (char*)sqlite3_column_text(statement, 2);
  2317. if(country==nil)
  2318. country="";
  2319. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2320. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2321. // if(addr==nil)
  2322. // addr="";
  2323. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2324. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2325. if(zipcode==nil)
  2326. zipcode="";
  2327. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2328. char *state = (char*)sqlite3_column_text(statement, 5);
  2329. if(state==nil)
  2330. state="";
  2331. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2332. char *city = (char*)sqlite3_column_text(statement, 6);
  2333. if(city==nil)
  2334. city="";
  2335. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2336. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2337. // NSString *nscontact_name = nil;
  2338. // if(contact_name==nil)
  2339. // nscontact_name=@"";
  2340. // else
  2341. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2342. char *phone = (char*)sqlite3_column_text(statement, 8);
  2343. NSString *nsphone = nil;
  2344. if(phone==nil)
  2345. nsphone=@"";
  2346. else
  2347. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2348. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2349. if(contact_id==nil)
  2350. contact_id="";
  2351. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2352. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2353. if(addr_1==nil)
  2354. addr_1="";
  2355. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2356. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2357. if(addr_2==nil)
  2358. addr_2="";
  2359. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2360. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2361. if(addr_3==nil)
  2362. addr_3="";
  2363. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2364. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2365. if(addr_4==nil)
  2366. addr_4="";
  2367. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2368. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2369. if(first_name==nil)
  2370. first_name="";
  2371. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2372. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2373. if(last_name==nil)
  2374. last_name="";
  2375. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2376. char *fax = (char*)sqlite3_column_text(statement, 16);
  2377. NSString *nsfax = nil;
  2378. if(fax==nil)
  2379. nsfax=@"";
  2380. else
  2381. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2382. char *email = (char*)sqlite3_column_text(statement, 17);
  2383. NSString *nsemail = nil;
  2384. if(email==nil)
  2385. nsemail=@"";
  2386. else
  2387. nsemail= [[NSString alloc]initWithUTF8String:email];
  2388. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2389. NSString *nsimg_0 = nil;
  2390. if(img_0==nil)
  2391. nsimg_0=@"";
  2392. else
  2393. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2394. [self copy_bcardImg:nsimg_0];
  2395. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2396. NSString *nsimg_1 = nil;
  2397. if(img_1==nil)
  2398. nsimg_1=@"";
  2399. else
  2400. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2401. [self copy_bcardImg:nsimg_1];
  2402. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2403. NSString *nsimg_2 = nil;
  2404. if(img_2==nil)
  2405. nsimg_2=@"";
  2406. else
  2407. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2408. [self copy_bcardImg:nsimg_2];
  2409. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2410. NSString *nsprice_type = nil;
  2411. if(price_type==nil)
  2412. nsprice_type=@"";
  2413. else
  2414. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2415. char *notes = (char*)sqlite3_column_text(statement, 22);
  2416. NSString *nsnotes = nil;
  2417. if(notes==nil)
  2418. nsnotes=@"";
  2419. else
  2420. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2421. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2422. NSString *nssalesrep = nil;
  2423. if(salesrep==nil)
  2424. nssalesrep=@"";
  2425. else
  2426. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2427. {
  2428. // decrypt
  2429. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2430. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2431. nsphone=[AESCrypt fastdecrypt:nsphone];
  2432. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2433. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2434. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2435. }
  2436. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2437. [arr_name addObject:nsfirst_name];
  2438. [arr_name addObject:nslast_name];
  2439. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2440. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2441. [arr_ext addObject:nsaddr_1];
  2442. [arr_ext addObject:nsaddr_2];
  2443. [arr_ext addObject:nsaddr_3];
  2444. [arr_ext addObject:nsaddr_4];
  2445. [arr_ext addObject:@"\r\n"];
  2446. [arr_ext addObject:nscity];
  2447. [arr_ext addObject:nsstate];
  2448. [arr_ext addObject:nszipcode];
  2449. [arr_ext addObject:nscountry];
  2450. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2451. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2452. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2453. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2454. [item setValue:nscountry forKey:@"customer_country"];
  2455. [item setValue:nsphone forKey:@"customer_phone"];
  2456. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2457. [item setValue:nscompany_name forKey:@"customer_name"];
  2458. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2459. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2460. [item setValue:nsext forKey:@"customer_contact_ext"];
  2461. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2462. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2463. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2464. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2465. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2466. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2467. [item setValue:nslast_name forKey:@"customer_last_name"];
  2468. [item setValue:nscity forKey:@"customer_city"];
  2469. [item setValue:nsstate forKey:@"customer_state"];
  2470. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2471. [item setValue:contactId forKey:@"customer_cid"];
  2472. [item setValue:nscontact_name forKey:@"customer_contact"];
  2473. [item setValue:nsfax forKey:@"customer_fax"];
  2474. [item setValue:nsemail forKey:@"customer_email"];
  2475. [ret setObject:item forKey:@"customerInfo"];
  2476. // i++;
  2477. }
  2478. UIApplication * app = [UIApplication sharedApplication];
  2479. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2480. [ret setValue:appDelegate.mode forKey:@"mode"];
  2481. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2482. sqlite3_finalize(statement);
  2483. }
  2484. [iSalesDB close_db:db];
  2485. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2486. return ret;
  2487. }
  2488. + (bool) copy_bcardImg:(NSString*) filename
  2489. {
  2490. if(filename.length==0)
  2491. return false;
  2492. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2493. bool ret=false;
  2494. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2495. NSString *cachefolder = [paths objectAtIndex:0];
  2496. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2497. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2498. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2499. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2500. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2501. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2502. //
  2503. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2504. NSFileManager* fileManager = [NSFileManager defaultManager];
  2505. BOOL bdir=NO;
  2506. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2507. {
  2508. NSError *error = nil;
  2509. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2510. {
  2511. ret=false;
  2512. }
  2513. else
  2514. {
  2515. ret=true;
  2516. }
  2517. // NSError *error = nil;
  2518. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2519. //
  2520. // if(!bsuccess)
  2521. // {
  2522. // DebugLog(@"Create offline_createimg folder failed");
  2523. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2524. // return [RAUtils dict2data:ret];
  2525. // }
  2526. // if(bsuccess)
  2527. // {
  2528. // sqlite3 *db = [self get_db];
  2529. //
  2530. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2531. // [iSalesDB close_db:db];
  2532. // }
  2533. }
  2534. return ret;
  2535. //
  2536. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2537. // if(bsuccess)
  2538. // {
  2539. // NSError *error = nil;
  2540. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2541. // {
  2542. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2543. // }
  2544. // else
  2545. // {
  2546. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2547. //
  2548. // ret[@"img_url_aname"]=filename;
  2549. // ret[@"img_url"]=savedImagePath;
  2550. // }
  2551. // }
  2552. }
  2553. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2554. {
  2555. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2556. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2557. NSString *cachefolder = [paths objectAtIndex:0];
  2558. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2559. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2560. NSFileManager* fileManager = [NSFileManager defaultManager];
  2561. BOOL bdir=YES;
  2562. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2563. {
  2564. NSError *error = nil;
  2565. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2566. if(!bsuccess)
  2567. {
  2568. DebugLog(@"Create offline_createimg folder failed");
  2569. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2570. return [RAUtils dict2data:ret];
  2571. }
  2572. // if(bsuccess)
  2573. // {
  2574. // sqlite3 *db = [self get_db];
  2575. //
  2576. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2577. // [iSalesDB close_db:db];
  2578. // }
  2579. }
  2580. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2581. //JEPG格式
  2582. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2583. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2584. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2585. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2586. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2587. if(bsuccess)
  2588. {
  2589. NSError *error = nil;
  2590. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2591. {
  2592. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2593. }
  2594. else
  2595. {
  2596. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2597. ret[@"img_url_aname"]=filename;
  2598. ret[@"img_url"]=filename;
  2599. }
  2600. }
  2601. else
  2602. {
  2603. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2604. }
  2605. return [RAUtils dict2data:ret];
  2606. }
  2607. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2608. {
  2609. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2610. category = [category substringToIndex:3];
  2611. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2612. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2613. params[@"category"]= category;
  2614. ret[@"params"]= params;
  2615. [ret setValue:@"detail" forKey:@"target"];
  2616. [ret setValue:@"popup" forKey:@"action"];
  2617. [ret setValue:@"content" forKey:@"type"];
  2618. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2619. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2620. [ret setValue:@"true" forKey:@"single_row"];
  2621. [ret setValue:@"true" forKey:@"partial_refresh"];
  2622. // sqlite3 *db = [iSalesDB get_db];
  2623. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2624. 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 ;
  2625. sqlite3_stmt * statement;
  2626. int count = 0;
  2627. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2628. // int count=0;
  2629. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2630. {
  2631. int i=0;
  2632. while (sqlite3_step(statement) == SQLITE_ROW)
  2633. {
  2634. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2635. // char *name = (char*)sqlite3_column_text(statement, 1);
  2636. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2637. char *name = (char*)sqlite3_column_text(statement, 0);
  2638. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2639. int product_id = sqlite3_column_int(statement, 1);
  2640. char *url = (char*)sqlite3_column_text(statement, 2);
  2641. if(url==nil)
  2642. url="";
  2643. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2644. [item setValue:nsurl forKey:@"picture_path"];
  2645. [item setValue:nsname forKey:@"fash_name"];
  2646. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2647. [item setValue:category forKey:@"category"];
  2648. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2649. i++;
  2650. }
  2651. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2652. sqlite3_finalize(statement);
  2653. }
  2654. NSLog(@"count:%d",count);
  2655. // [iSalesDB close_db:db];
  2656. return ret;
  2657. }
  2658. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2659. {
  2660. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2661. [ret setValue:key forKey:@"key"];
  2662. [ret setValue:value forKey:@"val"];
  2663. return ret;
  2664. }
  2665. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2666. {
  2667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2668. [ret setValue:@"0" forKey:@"img_count"];
  2669. // sqlite3 *db = [iSalesDB get_db];
  2670. 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 ;
  2671. sqlite3_stmt * statement;
  2672. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2673. {
  2674. int i=0;
  2675. if (sqlite3_step(statement) == SQLITE_ROW)
  2676. {
  2677. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2678. // char *name = (char*)sqlite3_column_text(statement, 1);
  2679. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2680. char *value = (char*)sqlite3_column_text(statement, 0);
  2681. if(value==nil)
  2682. value="";
  2683. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2684. char *key = (char*)sqlite3_column_text(statement, 1);
  2685. if(key==nil)
  2686. key="";
  2687. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2688. [item setValue:nsvalue forKey:@"val"];
  2689. [item setValue:nskey forKey:@"key"];
  2690. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2691. [ret setValue:@"1" forKey:@"count"];
  2692. i++;
  2693. }
  2694. sqlite3_finalize(statement);
  2695. }
  2696. // [iSalesDB close_db:db];
  2697. return ret;
  2698. }
  2699. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2700. {
  2701. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2702. [ret setValue:@"0" forKey:@"count"];
  2703. // sqlite3 *db = [iSalesDB get_db];
  2704. 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;
  2705. sqlite3_stmt * statement;
  2706. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2707. {
  2708. int i=0;
  2709. while (sqlite3_step(statement) == SQLITE_ROW)
  2710. {
  2711. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2712. // char *name = (char*)sqlite3_column_text(statement, 1);
  2713. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2714. char *value = (char*)sqlite3_column_text(statement, 0);
  2715. if(value==nil)
  2716. value="";
  2717. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2718. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2719. if(selector_display==nil)
  2720. selector_display="";
  2721. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2722. int product_id = sqlite3_column_int(statement, 2);
  2723. char *category = (char*)sqlite3_column_text(statement, 3);
  2724. if(category==nil)
  2725. category="";
  2726. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2727. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2728. [item setValue:nsvalue forKey:@"title"];
  2729. [item setValue:url forKey:@"pic_url"];
  2730. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2731. [params setValue:@"2" forKey:@"count"];
  2732. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2733. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2734. [param0 setValue:@"product_id" forKey:@"name"];
  2735. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2736. [param1 setValue:nscategory forKey:@"val"];
  2737. [param1 setValue:@"category" forKey:@"name"];
  2738. [params setObject:param0 forKey:@"param_0"];
  2739. [params setObject:param1 forKey:@"param_1"];
  2740. [item setObject:params forKey:@"params"];
  2741. [ret setValue:nsselector_display forKey:@"name"];
  2742. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2743. i++;
  2744. }
  2745. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2746. [ret setValue:@"switch" forKey:@"action"];
  2747. sqlite3_finalize(statement);
  2748. }
  2749. // [iSalesDB close_db:db];
  2750. return ret;
  2751. }
  2752. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2753. {
  2754. // model 在 category search 显示的图片。
  2755. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2756. // sqlite3 *db = [iSalesDB get_db];
  2757. 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];
  2758. sqlite3_stmt * statement;
  2759. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2760. {
  2761. while (sqlite3_step(statement) == SQLITE_ROW)
  2762. {
  2763. char *url = (char*)sqlite3_column_text(statement, 0);
  2764. if(url==nil)
  2765. url="";
  2766. int type = sqlite3_column_int(statement, 1);
  2767. if(type==0)
  2768. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2769. else
  2770. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2771. }
  2772. sqlite3_finalize(statement);
  2773. }
  2774. // [iSalesDB close_db:db];
  2775. return ret;
  2776. }
  2777. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2778. {
  2779. int item_id=-1;
  2780. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2781. sqlite3_stmt * statement;
  2782. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2783. {
  2784. if (sqlite3_step(statement) == SQLITE_ROW)
  2785. {
  2786. item_id = sqlite3_column_int(statement, 0);
  2787. }
  2788. sqlite3_finalize(statement);
  2789. }
  2790. return item_id;
  2791. }
  2792. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2793. {
  2794. // NSString* ret = @"";
  2795. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2796. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2797. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2798. sqlite3_stmt * statement;
  2799. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2800. {
  2801. while (sqlite3_step(statement) == SQLITE_ROW)
  2802. {
  2803. int bitem_id = sqlite3_column_int(statement, 0);
  2804. int bitem_qty = sqlite3_column_int(statement, 1);
  2805. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2806. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2807. }
  2808. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2809. sqlite3_finalize(statement);
  2810. }
  2811. // if(ret==nil)
  2812. // ret=@"";
  2813. *count=arr_count;
  2814. return arr_bundle;
  2815. }
  2816. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2817. {
  2818. // get default sold qty, return -1 if model not found;
  2819. int ret = -1;
  2820. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2821. sqlite3_stmt * statement;
  2822. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2823. {
  2824. if (sqlite3_step(statement) == SQLITE_ROW)
  2825. {
  2826. ret = sqlite3_column_int(statement, 0);
  2827. }
  2828. sqlite3_finalize(statement);
  2829. }
  2830. return ret;
  2831. }
  2832. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2833. {
  2834. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2835. [ret setValue:@"0" forKey:@"img_count"];
  2836. // sqlite3 *db = [iSalesDB get_db];
  2837. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2838. sqlite3_stmt * statement;
  2839. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2840. {
  2841. int i=0;
  2842. while (sqlite3_step(statement) == SQLITE_ROW)
  2843. {
  2844. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2845. // char *name = (char*)sqlite3_column_text(statement, 1);
  2846. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2847. char *url = (char*)sqlite3_column_text(statement, 0);
  2848. if(url==nil)
  2849. url="";
  2850. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2851. [item setValue:nsurl forKey:@"s"];
  2852. [item setValue:nsurl forKey:@"l"];
  2853. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2854. i++;
  2855. }
  2856. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2857. sqlite3_finalize(statement);
  2858. }
  2859. // [iSalesDB close_db:db];
  2860. return ret;
  2861. }
  2862. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2863. UIApplication * app = [UIApplication sharedApplication];
  2864. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2865. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2866. sqlite3 *db = [iSalesDB get_db];
  2867. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2868. NSString* product_id=params[@"product_id"];
  2869. NSString *item_count_str = params[@"item_count"];
  2870. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2871. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2872. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2873. // NSString *sql = @"";
  2874. for(int i=0;i<arr.count;i++)
  2875. {
  2876. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2877. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2878. __block int cart_count = 0;
  2879. if (!item_count_str) {
  2880. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2881. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2882. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2883. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2884. cart_count = [[model_set_components lastObject] integerValue];
  2885. }];
  2886. }
  2887. if(count==0)
  2888. {
  2889. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2890. sqlite3_stmt *stmt;
  2891. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2892. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2893. if (item_count_arr) {
  2894. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2895. } else {
  2896. sqlite3_bind_int(stmt,2,cart_count);
  2897. }
  2898. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2899. [iSalesDB execSql:@"ROLLBACK" db:db];
  2900. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2901. [iSalesDB close_db:db];
  2902. DebugLog(@"add to wishlist error");
  2903. return ret;
  2904. }
  2905. } else {
  2906. int qty = 0;
  2907. if (item_count_arr) {
  2908. qty = [item_count_arr[i] integerValue];
  2909. } else {
  2910. qty = cart_count;
  2911. }
  2912. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  2913. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2914. [iSalesDB execSql:@"ROLLBACK" db:db];
  2915. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2916. [iSalesDB close_db:db];
  2917. DebugLog(@"add to wishlist error");
  2918. return ret;
  2919. }
  2920. }
  2921. }
  2922. // [iSalesDB execSql:sql db:db];
  2923. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2924. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2925. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2926. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2927. [iSalesDB close_db:db];
  2928. appDelegate.wish_count =count;
  2929. [appDelegate update_count_mark];
  2930. ret[@"result"]= [NSNumber numberWithInt:2];
  2931. return ret;
  2932. }
  2933. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2934. // 0 category
  2935. // 1 search
  2936. // 2 itemsearch
  2937. NSData *ret = nil;
  2938. NSDictionary *items = nil;
  2939. switch (from) {
  2940. case 0:{
  2941. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2942. }
  2943. break;
  2944. case 1:{
  2945. items = [[self search:params limited:NO] objectForKey:@"items"];
  2946. }
  2947. break;
  2948. case 2:{
  2949. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2950. }
  2951. break;
  2952. default:
  2953. break;
  2954. }
  2955. if (!items) {
  2956. return ret;
  2957. }
  2958. int count = [[items objectForKey:@"count"] intValue];
  2959. NSMutableString *product_id_str = [@"" mutableCopy];
  2960. for (int i = 0; i < count; i++) {
  2961. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2962. NSDictionary *item = [items objectForKey:key];
  2963. NSString *product_id = [item objectForKey:@"product_id"];
  2964. if (i == 0) {
  2965. [product_id_str appendString:product_id];
  2966. } else {
  2967. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2968. }
  2969. }
  2970. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2971. if ([add_to isEqualToString:@"cart"]) {
  2972. NSString *order_code = [params objectForKey:@"orderCode"];
  2973. if (order_code.length) {
  2974. NSDictionary *newParams = @{
  2975. @"product_id" : product_id_str,
  2976. @"orderCode" : order_code
  2977. };
  2978. ret = [self offline_add2cart:newParams.mutableCopy];
  2979. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2980. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2981. ret = [RAUtils dict2data:retDic];
  2982. }
  2983. } else if([add_to isEqualToString:@"wishlist"]) {
  2984. NSDictionary *newParams = @{
  2985. @"product_id" : product_id_str
  2986. };
  2987. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2988. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2989. ret = [RAUtils dict2data:retDic];
  2990. } else if([add_to isEqualToString:@"portfolio"]) {
  2991. }
  2992. return ret;
  2993. }
  2994. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2995. {
  2996. return [self addAll:params from:0];
  2997. }
  2998. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2999. {
  3000. return [self addAll:params from:1];
  3001. }
  3002. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3003. {
  3004. return [self addAll:params from:2];
  3005. }
  3006. #pragma mark - Jack
  3007. #warning 做SQL操作时转义!!
  3008. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3009. // "val_227" : {
  3010. // "check" : 1,
  3011. // "value" : "US United States",
  3012. // "value_id" : "228"
  3013. // },
  3014. if (!countryCode) {
  3015. countryCode = @"US";
  3016. }
  3017. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3018. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3019. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3020. int code_id = sqlite3_column_int(stmt, 3); // id
  3021. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3022. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3023. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3024. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3025. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3026. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3027. }
  3028. long n = *count;
  3029. *count = n + 1;
  3030. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3031. [container setValue:countryDic forKey:key];
  3032. }] mutableCopy];
  3033. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3034. return ret;
  3035. }
  3036. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3037. countryCode = [self translateSingleQuote:countryCode];
  3038. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3039. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3040. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3041. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3042. if (name == NULL) {
  3043. name = "";
  3044. }
  3045. if (code == NULL) {
  3046. code = "";
  3047. }
  3048. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3049. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3050. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3051. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3052. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3053. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3054. }
  3055. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3056. [container setValue:stateDic forKey:key];
  3057. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3058. DebugLog(@"query all state error: %@",err_msg);
  3059. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3060. // [stateDic setValue:@"Other" forKey:@"value"];
  3061. // [stateDic setValue:@"" forKey:@"value_id"];
  3062. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3063. //
  3064. // if (state_code && [@"" isEqualToString:state_code]) {
  3065. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3066. // }
  3067. //
  3068. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3069. // [container setValue:stateDic forKey:key];
  3070. }] mutableCopy];
  3071. // failure 可以不用了,一样的
  3072. if (ret.allKeys.count == 0) {
  3073. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3074. [stateDic setValue:@"Other" forKey:@"value"];
  3075. [stateDic setValue:@"" forKey:@"value_id"];
  3076. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3077. if (state_code && [@"" isEqualToString:state_code]) {
  3078. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3079. }
  3080. NSString *key = [NSString stringWithFormat:@"val_0"];
  3081. [ret setValue:stateDic forKey:key];
  3082. }
  3083. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3084. return ret;
  3085. }
  3086. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3087. codeId = [self translateSingleQuote:codeId];
  3088. 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];
  3089. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3090. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3091. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3092. if (name == NULL) {
  3093. name = "";
  3094. }
  3095. if (code == NULL) {
  3096. code = "";
  3097. }
  3098. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3099. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3100. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3101. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3102. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3103. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3104. }
  3105. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3106. [container setValue:stateDic forKey:key];
  3107. }] mutableCopy];
  3108. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3109. return ret;
  3110. }
  3111. + (NSDictionary *)offline_getPrice {
  3112. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3113. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3114. char *name = (char *) sqlite3_column_text(stmt, 1);
  3115. int type = sqlite3_column_int(stmt, 2);
  3116. int orderBy = sqlite3_column_int(stmt, 3);
  3117. if (name == NULL) {
  3118. name = "";
  3119. }
  3120. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3121. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3122. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3123. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3124. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3125. if (orderBy == 0) {
  3126. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3127. }
  3128. [container setValue:priceDic forKey:key];
  3129. }] mutableCopy];
  3130. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3131. return ret;
  3132. }
  3133. + (NSDictionary *)offline_getSalesRep {
  3134. // 首先从offline_login表中取出sales_code
  3135. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3136. NSString *user = app.user;
  3137. user = [self translateSingleQuote:user];
  3138. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3139. __block NSString *user_code = @"";
  3140. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3141. char *code = (char *)sqlite3_column_text(stmt, 0);
  3142. if (code == NULL) {
  3143. code = "";
  3144. }
  3145. user_code = [NSString stringWithUTF8String:code];
  3146. }];
  3147. // 再取所有salesRep
  3148. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3149. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3150. // 1 name 2 code 3 salesrep_id
  3151. char *name = (char *)sqlite3_column_text(stmt, 1);
  3152. char *code = (char *)sqlite3_column_text(stmt, 2);
  3153. int salesrep_id = sqlite3_column_int(stmt, 3);
  3154. if (name == NULL) {
  3155. name = "";
  3156. }
  3157. if (code == NULL) {
  3158. code = "";
  3159. }
  3160. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3161. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3162. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3163. // 比较code 相等则check
  3164. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3165. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3166. }
  3167. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3168. }] mutableCopy];
  3169. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3170. return ret;
  3171. }
  3172. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3173. zipcode = [self translateSingleQuote:zipcode];
  3174. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3175. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3176. char *country = (char *)sqlite3_column_text(stmt, 0);
  3177. char *state = (char *)sqlite3_column_text(stmt, 1);
  3178. char *city = (char *)sqlite3_column_text(stmt, 2);
  3179. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3180. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3181. if (country == NULL) {
  3182. country = "";
  3183. }
  3184. if (state == NULL) {
  3185. state = "";
  3186. }
  3187. if (city == NULL) {
  3188. city = "";
  3189. }
  3190. if (country_code == NULL) {
  3191. country_code = "";
  3192. }
  3193. if (state_code == NULL) {
  3194. state_code = "";
  3195. }
  3196. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3197. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3198. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3199. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3200. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3201. }] mutableCopy];
  3202. return ret;
  3203. }
  3204. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3205. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3206. [item setValue:value forKey:valueKey];
  3207. [dic setValue:item forKey:itemKey];
  3208. }
  3209. + (NSString *)countryCodeByid:(NSString *)code_id {
  3210. NSString *ret = nil;
  3211. code_id = [self translateSingleQuote:code_id];
  3212. sqlite3 *db = [iSalesDB get_db];
  3213. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3214. sqlite3_stmt * statement;
  3215. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3216. while (sqlite3_step(statement) == SQLITE_ROW) {
  3217. char *code = (char *)sqlite3_column_text(statement, 0);
  3218. if (code == NULL) {
  3219. code = "";
  3220. }
  3221. ret = [NSString stringWithUTF8String:code];
  3222. }
  3223. sqlite3_finalize(statement);
  3224. }
  3225. [iSalesDB close_db:db];
  3226. return ret;
  3227. }
  3228. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3229. NSString *ret = nil;
  3230. code = [self translateSingleQuote:code];
  3231. sqlite3 *db = [iSalesDB get_db];
  3232. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3233. sqlite3_stmt * statement;
  3234. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3235. while (sqlite3_step(statement) == SQLITE_ROW) {
  3236. char *_id = (char *)sqlite3_column_text(statement, 0);
  3237. if (_id == NULL) {
  3238. _id = "";
  3239. }
  3240. ret = [NSString stringWithFormat:@"%s",_id];
  3241. }
  3242. sqlite3_finalize(statement);
  3243. }
  3244. [iSalesDB close_db:db];
  3245. return ret;
  3246. }
  3247. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3248. NSString *name = nil;
  3249. codeId = [self translateSingleQuote:codeId];
  3250. sqlite3 *db = [iSalesDB get_db];
  3251. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3252. sqlite3_stmt * statement;
  3253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3254. while (sqlite3_step(statement) == SQLITE_ROW) {
  3255. char *value = (char *)sqlite3_column_text(statement, 0);
  3256. if (value == NULL) {
  3257. value = "";
  3258. }
  3259. name = [NSString stringWithUTF8String:value];
  3260. }
  3261. sqlite3_finalize(statement);
  3262. }
  3263. [iSalesDB close_db:db];
  3264. return name;
  3265. }
  3266. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3267. NSString *ret = nil;
  3268. sqlite3 *db = [iSalesDB get_db];
  3269. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3270. sqlite3_stmt * statement;
  3271. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3272. while (sqlite3_step(statement) == SQLITE_ROW) {
  3273. char *name = (char *)sqlite3_column_text(statement, 0);
  3274. if (name == NULL) {
  3275. name = "";
  3276. }
  3277. ret = [NSString stringWithUTF8String:name];
  3278. }
  3279. sqlite3_finalize(statement);
  3280. }
  3281. [iSalesDB close_db:db];
  3282. return ret;
  3283. }
  3284. + (NSString *)salesRepCodeById:(NSString *)_id {
  3285. NSString *ret = nil;
  3286. _id = [self translateSingleQuote:_id];
  3287. sqlite3 *db = [iSalesDB get_db];
  3288. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3289. sqlite3_stmt * statement;
  3290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3291. while (sqlite3_step(statement) == SQLITE_ROW) {
  3292. char *rep = (char *)sqlite3_column_text(statement, 0);
  3293. if (rep == NULL) {
  3294. rep = "";
  3295. }
  3296. ret = [NSString stringWithUTF8String:rep];
  3297. }
  3298. sqlite3_finalize(statement);
  3299. }
  3300. [iSalesDB close_db:db];
  3301. return ret;
  3302. }
  3303. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3304. char *tx = (char *)sqlite3_column_text(stmt, col);
  3305. if (tx == NULL) {
  3306. tx = "";
  3307. }
  3308. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3309. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3310. if (!text) {
  3311. text = @"";
  3312. }
  3313. // 将字符全部为' '的字符串干掉
  3314. int spaceCount = 0;
  3315. for (int i = 0; i < text.length; i++) {
  3316. if ([text characterAtIndex:i] == ' ') {
  3317. spaceCount++;
  3318. }
  3319. }
  3320. if (spaceCount == text.length) {
  3321. text = @"";
  3322. }
  3323. return text;
  3324. }
  3325. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3326. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3327. NSData *data = [NSData dataWithContentsOfFile:path];
  3328. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3329. return ret;
  3330. }
  3331. + (NSString *)textFileName:(NSString *)name {
  3332. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3333. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3334. if (!text) {
  3335. text = @"";
  3336. }
  3337. return text;
  3338. }
  3339. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3340. return [[dic objectForKey:key] mutableCopy];
  3341. }
  3342. + (id)translateSingleQuote:(NSString *)string {
  3343. if ([string isKindOfClass:[NSString class]])
  3344. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3345. return string;
  3346. }
  3347. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3348. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3349. }
  3350. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3351. NSString* ret= nil;
  3352. NSString *sqlQuery = nil;
  3353. // 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
  3354. 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];
  3355. sqlite3_stmt * statement;
  3356. // int count=0;
  3357. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3358. {
  3359. if (sqlite3_step(statement) == SQLITE_ROW)
  3360. {
  3361. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3362. if(imgurl==nil)
  3363. imgurl="";
  3364. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3365. ret=nsimgurl;
  3366. }
  3367. sqlite3_finalize(statement);
  3368. }
  3369. else
  3370. {
  3371. [ret setValue:@"8" forKey:@"result"];
  3372. }
  3373. // [iSalesDB close_db:db];
  3374. // DebugLog(@"data string: %@",ret );
  3375. return ret;
  3376. }
  3377. #pragma mark contact list
  3378. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3379. {
  3380. // contactType = "Sales_Order_Customer";
  3381. // limit = 25;
  3382. // offset = 0;
  3383. // password = 123456;
  3384. // "price_name" = 16;
  3385. // user = EvanK;
  3386. sqlite3 *db = [iSalesDB get_db];
  3387. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3388. if (contactType) {
  3389. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3390. } else {
  3391. contactType = @"1 = 1";
  3392. }
  3393. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3394. DebugLog(@"offline contact list keyword: %@",keyword);
  3395. // advanced search
  3396. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3397. if (contact_name) {
  3398. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3399. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3400. } else {
  3401. contact_name = @"";
  3402. }
  3403. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3404. if (customer_phone) {
  3405. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3406. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3407. } else {
  3408. customer_phone = @"";
  3409. }
  3410. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3411. if (customer_fax) {
  3412. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3413. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3414. } else {
  3415. customer_fax = @"";
  3416. }
  3417. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3418. if (customer_zipcode) {
  3419. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3420. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3421. } else {
  3422. customer_zipcode = @"";
  3423. }
  3424. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3425. if (customer_sales_rep) {
  3426. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3427. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3428. } else {
  3429. customer_sales_rep = @"";
  3430. }
  3431. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3432. if (customer_state) {
  3433. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3434. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3435. } else {
  3436. customer_state = @"";
  3437. }
  3438. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3439. if (customer_name) {
  3440. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3441. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3442. } else {
  3443. customer_name = @"";
  3444. }
  3445. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3446. if (customer_country) {
  3447. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3448. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3449. } else {
  3450. customer_country = @"";
  3451. }
  3452. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3453. if (customer_cid) {
  3454. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3455. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3456. } else {
  3457. customer_cid = @"";
  3458. }
  3459. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3460. if (customer_city) {
  3461. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3462. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3463. } else {
  3464. customer_city = @"";
  3465. }
  3466. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3467. if (customer_address) {
  3468. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3469. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3470. } else {
  3471. customer_address = @"";
  3472. }
  3473. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3474. if (customer_email) {
  3475. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3476. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3477. } else {
  3478. customer_email = @"";
  3479. }
  3480. NSString *price_name = [params valueForKey:@"price_name"];
  3481. if (price_name) {
  3482. if ([price_name containsString:@","]) {
  3483. // 首先从 price表中查处name
  3484. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3485. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3486. for (int i = 1;i < pArray.count;i++) {
  3487. NSString *p = pArray[i];
  3488. [mutablePStr appendFormat:@" or type = %@ ",p];
  3489. }
  3490. [mutablePStr appendString:@";"];
  3491. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3492. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3493. char *name = (char *)sqlite3_column_text(stmt, 0);
  3494. if (!name)
  3495. name = "";
  3496. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3497. }];
  3498. // 再根据name 拼sql
  3499. NSMutableString *mutable_price_name = [NSMutableString string];
  3500. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3501. for (int i = 1; i < price_name_array.count; i++) {
  3502. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3503. }
  3504. [mutable_price_name appendString:@")"];
  3505. price_name = mutable_price_name;
  3506. } else {
  3507. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3508. if ([price_name isEqualToString:@""]) {
  3509. price_name = @"";
  3510. } else {
  3511. __block NSString *price;
  3512. price_name = [self translateSingleQuote:price_name];
  3513. [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) {
  3514. char *p = (char *)sqlite3_column_text(stmt, 0);
  3515. if (p == NULL) {
  3516. p = "";
  3517. }
  3518. price = [NSString stringWithUTF8String:p];
  3519. }];
  3520. if ([price isEqualToString:@""]) {
  3521. price_name = @"";
  3522. } else {
  3523. price = [self translateSingleQuote:price];
  3524. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3525. }
  3526. }
  3527. }
  3528. } else {
  3529. price_name = @"";
  3530. }
  3531. int limit = [[params valueForKey:@"limit"] intValue];
  3532. int offset = [[params valueForKey:@"offset"] intValue];
  3533. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3534. 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];
  3535. 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];
  3536. // int result= [iSalesDB AddExFunction:db];
  3537. int count =0;
  3538. NSString *sqlQuery = nil;
  3539. if(keyword.length==0)
  3540. {
  3541. // 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];
  3542. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3543. sqlQuery = sql;
  3544. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3545. }
  3546. else
  3547. {
  3548. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3549. keyword = keyword.lowercaseString;
  3550. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3551. 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];
  3552. 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]];
  3553. }
  3554. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3555. sqlite3_stmt * statement;
  3556. [ret setValue:@"2" forKey:@"result"];
  3557. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3558. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3559. {
  3560. int i = 0;
  3561. while (sqlite3_step(statement) == SQLITE_ROW)
  3562. {
  3563. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3564. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3565. int editable = sqlite3_column_int(statement, 0);
  3566. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3567. NSString *nscompany_name =nil;
  3568. if(company_name==nil)
  3569. nscompany_name=@"";
  3570. else
  3571. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3572. char *country = (char*)sqlite3_column_text(statement, 2);
  3573. if(country==nil)
  3574. country="";
  3575. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3576. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3577. // if(addr==nil)
  3578. // addr="";
  3579. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3580. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3581. if(zipcode==nil)
  3582. zipcode="";
  3583. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3584. char *state = (char*)sqlite3_column_text(statement, 5);
  3585. if(state==nil)
  3586. state="";
  3587. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3588. char *city = (char*)sqlite3_column_text(statement, 6);
  3589. if(city==nil)
  3590. city="";
  3591. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3592. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3593. // NSString *nscontact_name = nil;
  3594. // if(contact_name==nil)
  3595. // nscontact_name=@"";
  3596. // else
  3597. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3598. char *phone = (char*)sqlite3_column_text(statement, 8);
  3599. NSString *nsphone = nil;
  3600. if(phone==nil)
  3601. nsphone=@"";
  3602. else
  3603. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3604. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3605. if(contact_id==nil)
  3606. contact_id="";
  3607. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3608. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3609. if(addr_1==nil)
  3610. addr_1="";
  3611. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3612. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3613. if(addr_2==nil)
  3614. addr_2="";
  3615. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3616. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3617. if(addr_3==nil)
  3618. addr_3="";
  3619. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3620. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3621. if(addr_4==nil)
  3622. addr_4="";
  3623. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3624. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3625. if(first_name==nil)
  3626. first_name="";
  3627. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3628. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3629. if(last_name==nil)
  3630. last_name="";
  3631. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3632. char *fax = (char*)sqlite3_column_text(statement, 16);
  3633. NSString *nsfax = nil;
  3634. if(fax==nil)
  3635. nsfax=@"";
  3636. else
  3637. {
  3638. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3639. if(nsfax.length>0)
  3640. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3641. }
  3642. char *email = (char*)sqlite3_column_text(statement, 17);
  3643. NSString *nsemail = nil;
  3644. if(email==nil)
  3645. nsemail=@"";
  3646. else
  3647. {
  3648. nsemail= [[NSString alloc]initWithUTF8String:email];
  3649. if(nsemail.length>0)
  3650. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3651. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3652. }
  3653. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3654. [arr_name addObject:nsfirst_name];
  3655. [arr_name addObject:nslast_name];
  3656. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3657. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3658. {
  3659. // decrypt
  3660. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3661. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3662. nsphone=[AESCrypt fastdecrypt:nsphone];
  3663. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3664. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3665. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3666. }
  3667. [arr_addr addObject:nscompany_name];
  3668. [arr_addr addObject:nscontact_name];
  3669. [arr_addr addObject:@"<br>"];
  3670. [arr_addr addObject:nsaddr_1];
  3671. [arr_addr addObject:nsaddr_2];
  3672. [arr_addr addObject:nsaddr_3];
  3673. [arr_addr addObject:nsaddr_4];
  3674. //[arr_addr addObject:nsaddr];
  3675. [arr_addr addObject:nszipcode];
  3676. [arr_addr addObject:nscity];
  3677. [arr_addr addObject:nsstate];
  3678. [arr_addr addObject:nscountry];
  3679. [arr_addr addObject:@"<br>"];
  3680. [arr_addr addObject:nsphone];
  3681. [arr_addr addObject:nsfax];
  3682. [arr_addr addObject:nsemail];
  3683. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3684. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3685. [item setValue:name forKey:@"name"];
  3686. [item setValue:nscontact_id forKey:@"contact_id"];
  3687. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3688. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3689. i++;
  3690. }
  3691. UIApplication * app = [UIApplication sharedApplication];
  3692. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3693. [ret setValue:appDelegate.mode forKey:@"mode"];
  3694. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3695. sqlite3_finalize(statement);
  3696. }
  3697. [iSalesDB close_db:db];
  3698. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3699. return ret;
  3700. }
  3701. #pragma mark contact Advanced search
  3702. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3703. {
  3704. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3705. return [RAUtils dict2data:contactAdvanceDic];
  3706. }
  3707. #pragma mark create new contact
  3708. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3709. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3710. NSData *data = [NSData dataWithContentsOfFile:path];
  3711. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3712. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3713. NSString *countryCode = nil;
  3714. NSString *countryCode_id = nil;
  3715. NSString *stateCode = nil;
  3716. NSString *city = nil;
  3717. NSString *zipCode = nil;
  3718. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3719. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3720. NSString *code_id = params[@"country"];
  3721. countryCode_id = code_id;
  3722. countryCode = [self countryCodeByid:code_id];
  3723. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3724. NSString *zip_code = params[@"zipcode"];
  3725. // 剔除全部为空格
  3726. int spaceCount = 0;
  3727. for (int i = 0; i < zip_code.length; i++) {
  3728. if ([zip_code characterAtIndex:i] == ' ') {
  3729. spaceCount++;
  3730. }
  3731. }
  3732. if (spaceCount == zip_code.length) {
  3733. zip_code = @"";
  3734. }
  3735. zipCode = zip_code;
  3736. if (zipCode.length > 0) {
  3737. countryCode_id = params[@"country"];
  3738. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3739. countryCode = [dic valueForKey:@"country_code"];
  3740. if (!countryCode) {
  3741. countryCode = @"US";
  3742. }
  3743. stateCode = [dic valueForKey:@"state_code"];
  3744. city = [dic valueForKey:@"city"];
  3745. // zip code
  3746. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3747. [zipDic setValue:zipCode forKey:@"value"];
  3748. [section_0 setValue:zipDic forKey:@"item_10"];
  3749. }
  3750. }
  3751. } else {
  3752. // default: US United States
  3753. countryCode = @"US";
  3754. countryCode_id = @"228";
  3755. }
  3756. // country
  3757. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3758. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3759. // state
  3760. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3761. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3762. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3763. // city
  3764. if (city) {
  3765. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3766. [cityDic setValue:city forKey:@"value"];
  3767. [section_0 setValue:cityDic forKey:@"item_12"];
  3768. }
  3769. // price type
  3770. NSDictionary *priceDic = [self offline_getPrice];
  3771. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3772. // Sales Rep
  3773. NSDictionary *repDic = [self offline_getSalesRep];
  3774. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3775. [ret setValue:section_0 forKey:@"section_0"];
  3776. return [RAUtils dict2data:ret];
  3777. }
  3778. #pragma mark save
  3779. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3780. NSString *addr = nil;
  3781. NSString *contact_name = nil;
  3782. NSString *companyName = [params objectForKey:@"company"];
  3783. if (companyName) {
  3784. companyName = [AESCrypt fastencrypt:companyName];
  3785. } else {
  3786. companyName = @"";
  3787. }
  3788. DebugLog(@"company");
  3789. companyName = [self translateSingleQuote:companyName];
  3790. NSString *addr1 = [params objectForKey:@"address"];
  3791. NSString *addr2 = [params objectForKey:@"address2"];
  3792. NSString *addr3 = [params objectForKey:@"address_3"];
  3793. NSString *addr4 = [params objectForKey:@"address_4"];
  3794. if (!addr2) {
  3795. addr2 = @"";
  3796. }
  3797. if (!addr3) {
  3798. addr3 = @"";
  3799. }
  3800. if (!addr4) {
  3801. addr4 = @"";
  3802. }
  3803. if (!addr1) {
  3804. addr1 = @"";
  3805. }
  3806. DebugLog(@"addr");
  3807. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3808. addr = [AESCrypt fastencrypt:addr];
  3809. addr = [self translateSingleQuote:addr];
  3810. if (addr1 && ![addr1 isEqualToString:@""]) {
  3811. addr1 = [AESCrypt fastencrypt:addr1];
  3812. }
  3813. addr1 = [self translateSingleQuote:addr1];
  3814. addr2 = [self translateSingleQuote:addr2];
  3815. addr3 = [self translateSingleQuote:addr3];
  3816. addr4 = [self translateSingleQuote:addr4];
  3817. NSString *country = [params objectForKey:@"country"];
  3818. if (country) {
  3819. country = [self countryNameByCountryCodeId:country];
  3820. } else {
  3821. country = @"";
  3822. }
  3823. DebugLog(@"country");
  3824. country = [self translateSingleQuote:country];
  3825. NSString *state = [params objectForKey:@"state"];
  3826. if (!state) {
  3827. state = @"";
  3828. }
  3829. DebugLog(@"state");
  3830. state = [self translateSingleQuote:state];
  3831. NSString *city = [params objectForKey:@"city"];
  3832. if (!city) {
  3833. city = @"";
  3834. }
  3835. city = [self translateSingleQuote:city];
  3836. NSString *zipcode = [params objectForKey:@"zipcode"];
  3837. if (!zipcode) {
  3838. zipcode = @"";
  3839. }
  3840. DebugLog(@"zip");
  3841. zipcode = [self translateSingleQuote:zipcode];
  3842. NSString *fistName = [params objectForKey:@"firstname"];
  3843. if (!fistName) {
  3844. fistName = @"";
  3845. }
  3846. NSString *lastName = [params objectForKey:@"lastname"];
  3847. if (!lastName) {
  3848. lastName = @"";
  3849. }
  3850. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3851. DebugLog(@"contact_name");
  3852. contact_name = [self translateSingleQuote:contact_name];
  3853. fistName = [self translateSingleQuote:fistName];
  3854. lastName = [self translateSingleQuote:lastName];
  3855. NSString *phone = [params objectForKey:@"phone"];
  3856. if (phone) {
  3857. phone = [AESCrypt fastencrypt:phone];
  3858. } else {
  3859. phone = @"";
  3860. }
  3861. DebugLog(@"PHONE");
  3862. phone = [self translateSingleQuote:phone];
  3863. NSString *fax = [params objectForKey:@"fax"];
  3864. if (!fax) {
  3865. fax = @"";
  3866. }
  3867. DebugLog(@"FAX");
  3868. fax = [self translateSingleQuote:fax];
  3869. NSString *email = [params objectForKey:@"email"];
  3870. if (!email) {
  3871. email = @"";
  3872. }
  3873. DebugLog(@"EMAIL:%@",email);
  3874. email = [self translateSingleQuote:email];
  3875. NSString *notes = [params objectForKey:@"contact_notes"];
  3876. if (!notes) {
  3877. notes = @"";
  3878. }
  3879. DebugLog(@"NOTE:%@",notes);
  3880. notes = [self translateSingleQuote:notes];
  3881. NSString *price = [params objectForKey:@"price_name"];
  3882. if (price) {
  3883. price = [self priceNameByPriceId:price];
  3884. } else {
  3885. price = @"";
  3886. }
  3887. DebugLog(@"PRICE");
  3888. price = [self translateSingleQuote:price];
  3889. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3890. if (salesRep) {
  3891. salesRep = [self salesRepCodeById:salesRep];
  3892. } else {
  3893. salesRep = @"";
  3894. }
  3895. salesRep = [self translateSingleQuote:salesRep];
  3896. NSString *img = [params objectForKey:@"business_card"];
  3897. NSArray *array = [img componentsSeparatedByString:@","];
  3898. NSString *img_0 = array[0];
  3899. if (!img_0) {
  3900. img_0 = @"";
  3901. }
  3902. img_0 = [self translateSingleQuote:img_0];
  3903. NSString *img_1 = array[1];
  3904. if (!img_1) {
  3905. img_1 = @"";
  3906. }
  3907. img_1 = [self translateSingleQuote:img_1];
  3908. NSString *img_2 = array[2];
  3909. if (!img_2) {
  3910. img_2 = @"";
  3911. }
  3912. img_2 = [self translateSingleQuote:img_2];
  3913. NSString *contact_id = [NSUUID UUID].UUIDString;
  3914. // 判断更新时是否为customer
  3915. if (update) {
  3916. contact_id = [params objectForKey:@"contact_id"];
  3917. if (!contact_id) {
  3918. contact_id = @"";
  3919. }
  3920. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3921. __block int customer = 0;
  3922. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3923. customer = sqlite3_column_int(stmt, 0);
  3924. }];
  3925. isCustomer = customer ? YES : NO;
  3926. }
  3927. NSMutableDictionary *sync_dic = [params mutableCopy];
  3928. if (isCustomer) {
  3929. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3930. } else {
  3931. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3932. }
  3933. NSString *sync_data = nil;
  3934. NSString *sql = nil;
  3935. if (update){
  3936. contact_id = [params objectForKey:@"contact_id"];
  3937. if (!contact_id) {
  3938. contact_id = @"";
  3939. }
  3940. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3941. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3942. sync_data = [RAUtils dict2string:sync_dic];
  3943. sync_data = [self translateSingleQuote:sync_data];
  3944. 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];
  3945. } else {
  3946. contact_id = [self translateSingleQuote:contact_id];
  3947. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3948. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3949. sync_data = [RAUtils dict2string:sync_dic];
  3950. sync_data = [self translateSingleQuote:sync_data];
  3951. 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];
  3952. }
  3953. int result = [iSalesDB execSql:sql];
  3954. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3955. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3956. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3957. }
  3958. #pragma mark save new contact
  3959. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3960. {
  3961. return [self offline_saveContact:params update:NO isCustomer:YES];
  3962. }
  3963. #pragma mark edit contact
  3964. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3965. {
  3966. // {
  3967. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3968. // password = 123456;
  3969. // user = EvanK;
  3970. // }
  3971. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3972. NSData *data = [NSData dataWithContentsOfFile:path];
  3973. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3974. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3975. NSString *countryCode = nil;
  3976. NSString *countryCode_id = nil;
  3977. NSString *stateCode = nil;
  3978. /*------contact infor------*/
  3979. __block NSString *country = nil;
  3980. __block NSString *company_name = nil;
  3981. __block NSString *contact_id = params[@"contact_id"];
  3982. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3983. __block NSString *zipcode = nil;
  3984. __block NSString *state = nil; // state_code
  3985. __block NSString *city = nil; //
  3986. __block NSString *firt_name,*last_name;
  3987. __block NSString *phone,*fax,*email;
  3988. __block NSString *notes,*price_type,*sales_rep;
  3989. __block NSString *img_0,*img_1,*img_2;
  3990. contact_id = [self translateSingleQuote:contact_id];
  3991. 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];
  3992. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3993. country = [self textAtColumn:0 statement:stmt]; // country name
  3994. company_name = [self textAtColumn:1 statement:stmt];
  3995. addr_1 = [self textAtColumn:2 statement:stmt];
  3996. addr_2 = [self textAtColumn:3 statement:stmt];
  3997. addr_3 = [self textAtColumn:4 statement:stmt];
  3998. addr_4 = [self textAtColumn:5 statement:stmt];
  3999. zipcode = [self textAtColumn:6 statement:stmt];
  4000. state = [self textAtColumn:7 statement:stmt]; // state code
  4001. city = [self textAtColumn:8 statement:stmt];
  4002. firt_name = [self textAtColumn:9 statement:stmt];
  4003. last_name = [self textAtColumn:10 statement:stmt];
  4004. phone = [self textAtColumn:11 statement:stmt];
  4005. fax = [self textAtColumn:12 statement:stmt];
  4006. email = [self textAtColumn:13 statement:stmt];
  4007. notes = [self textAtColumn:14 statement:stmt];
  4008. price_type = [self textAtColumn:15 statement:stmt]; // name
  4009. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4010. img_0 = [self textAtColumn:17 statement:stmt];
  4011. img_1 = [self textAtColumn:18 statement:stmt];
  4012. img_2 = [self textAtColumn:19 statement:stmt];
  4013. }];
  4014. // decrypt
  4015. if (company_name) {
  4016. company_name = [AESCrypt fastdecrypt:company_name];
  4017. }
  4018. if (addr_1) {
  4019. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4020. }
  4021. if (phone) {
  4022. phone = [AESCrypt fastdecrypt:phone];
  4023. }
  4024. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4025. countryCode = [iSalesDB jk_queryText:countrySql];
  4026. stateCode = state;
  4027. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4028. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4029. NSString *code_id = params[@"country"];
  4030. countryCode_id = code_id;
  4031. countryCode = [self countryCodeByid:code_id];
  4032. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4033. NSString *zip_code = params[@"zipcode"];
  4034. // 剔除全部为空格
  4035. int spaceCount = 0;
  4036. for (int i = 0; i < zip_code.length; i++) {
  4037. if ([zip_code characterAtIndex:i] == ' ') {
  4038. spaceCount++;
  4039. }
  4040. }
  4041. if (spaceCount == zip_code.length) {
  4042. zip_code = @"";
  4043. }
  4044. if (zipcode.length > 0) {
  4045. zipcode = zip_code;
  4046. countryCode_id = params[@"country"];
  4047. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4048. countryCode = [dic valueForKey:@"country_code"];
  4049. stateCode = [dic valueForKey:@"state_code"];
  4050. city = [dic valueForKey:@"city"];
  4051. // zip code
  4052. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4053. [zipDic setValue:zipcode forKey:@"value"];
  4054. [section_0 setValue:zipDic forKey:@"item_8"];
  4055. }
  4056. }
  4057. }
  4058. // 0 Country
  4059. // 1 Company Name
  4060. // 2 Contact ID
  4061. // 3 Picture
  4062. // 4 Address 1
  4063. // 5 Address 2
  4064. // 6 Address 3
  4065. // 7 Address 4
  4066. // 8 Zip Code
  4067. // 9 State/Province
  4068. // 10 City
  4069. // 11 Contact First Name
  4070. // 12 Contact Last Name
  4071. // 13 Phone
  4072. // 14 Fax
  4073. // 15 Email
  4074. // 16 Contact Notes
  4075. // 17 Price Type
  4076. // 18 Sales Rep
  4077. // country
  4078. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4079. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4080. // company
  4081. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4082. // contact_id
  4083. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4084. // picture
  4085. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4086. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4087. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4088. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4089. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4090. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4091. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4092. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4093. // addr 1 2 3 4
  4094. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4095. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4096. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4097. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4098. // zip code
  4099. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4100. // state
  4101. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4102. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4103. // city
  4104. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4105. // first last
  4106. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4107. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4108. // phone fax email
  4109. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4110. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4111. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4112. // notes
  4113. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4114. // price
  4115. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4116. for (NSString *key in priceDic.allKeys) {
  4117. if ([key containsString:@"val_"]) {
  4118. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4119. if ([dic[@"value"] isEqualToString:price_type]) {
  4120. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4121. [priceDic setValue:dic forKey:key];
  4122. }
  4123. }
  4124. }
  4125. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4126. // Sales Rep
  4127. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4128. for (NSString *key in repDic.allKeys) {
  4129. if ([key containsString:@"val_"]) {
  4130. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4131. NSString *value = dic[@"value"];
  4132. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4133. if (code && [code isEqualToString:sales_rep]) {
  4134. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4135. [repDic setValue:dic forKey:key];
  4136. }
  4137. }
  4138. }
  4139. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4140. [ret setValue:section_0 forKey:@"section_0"];
  4141. return [RAUtils dict2data:ret];
  4142. }
  4143. #pragma mark save contact
  4144. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4145. {
  4146. return [self offline_saveContact:params update:YES isCustomer:YES];
  4147. }
  4148. #pragma mark category
  4149. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4150. if (ck) {
  4151. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4152. for (NSString *key in res.allKeys) {
  4153. if (![key isEqualToString:@"count"]) {
  4154. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4155. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4156. if ([val[@"value"] isEqualToString:ck]) {
  4157. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4158. }
  4159. [res setValue:val forKey:key];
  4160. }
  4161. }
  4162. [dic setValue:res forKey:valueKey];
  4163. }
  4164. }
  4165. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4166. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4167. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4168. NSString* category = [params valueForKey:@"category"];
  4169. if (!category || [category isEqualToString:@""]) {
  4170. category = @"%";
  4171. }
  4172. category = [self translateSingleQuote:category];
  4173. int limit = [[params valueForKey:@"limit"] intValue];
  4174. int offset = [[params valueForKey:@"offset"] intValue];
  4175. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4176. NSString *limit_str = @"";
  4177. if (limited) {
  4178. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4179. }
  4180. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4181. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4182. sqlite3 *db = [iSalesDB get_db];
  4183. // [iSalesDB AddExFunction:db];
  4184. int count;
  4185. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4186. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  4187. double price_min = 0;
  4188. double price_max = 0;
  4189. if ([params.allKeys containsObject:@"alert"]) {
  4190. // alert
  4191. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4192. NSString *alert = params[@"alert"];
  4193. if ([alert isEqualToString:@"Display All"]) {
  4194. alert = [NSString stringWithFormat:@""];
  4195. } else {
  4196. alert = [self translateSingleQuote:alert];
  4197. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4198. }
  4199. // available
  4200. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4201. NSString *available = params[@"available"];
  4202. NSString *available_condition;
  4203. if ([available isEqualToString:@"Display All"]) {
  4204. available_condition = @"";
  4205. } else if ([available isEqualToString:@"Available Now"]) {
  4206. available_condition = @"and availability > 0";
  4207. } else {
  4208. available_condition = @"and availability == 0";
  4209. }
  4210. // best seller
  4211. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4212. NSString *best_seller = @"";
  4213. NSString *order_best_seller = @"m.name asc";
  4214. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4215. best_seller = @"and best_seller > 0";
  4216. order_best_seller = @"m.best_seller desc,m.name asc";
  4217. }
  4218. // price
  4219. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4220. NSString *price = params[@"price"];
  4221. price_min = 0;
  4222. price_max = MAXFLOAT;
  4223. if (appDelegate.user) {
  4224. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4225. NSMutableString *priceName = [NSMutableString string];
  4226. for (int i = 0; i < priceTypeArray.count; i++) {
  4227. NSString *pricetype = priceTypeArray[i];
  4228. pricetype = [self translateSingleQuote:pricetype];
  4229. if (i == 0) {
  4230. [priceName appendFormat:@"'%@'",pricetype];
  4231. } else {
  4232. [priceName appendFormat:@",'%@'",pricetype];
  4233. }
  4234. }
  4235. if ([price isEqualToString:@"Display All"]) {
  4236. price = [NSString stringWithFormat:@""];
  4237. } else if([price containsString:@"+"]){
  4238. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4239. price_min = [price doubleValue];
  4240. 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];
  4241. } else {
  4242. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4243. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4244. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4245. 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];
  4246. }
  4247. } else {
  4248. price = @"";
  4249. }
  4250. // sold_by_qty : Sold in quantities of %@
  4251. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4252. NSString *qty = params[@"sold_by_qty"];
  4253. if ([qty isEqualToString:@"Display All"]) {
  4254. qty = @"";
  4255. } else {
  4256. qty = [self translateSingleQuote:qty];
  4257. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4258. }
  4259. // cate
  4260. category = [self translateSingleQuote:category];
  4261. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4262. // where bestseller > 0 order by bestseller desc
  4263. // sql query: alert availability(int) best_seller(int) price qty
  4264. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4265. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4266. }
  4267. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4268. if (!appDelegate.user) {
  4269. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4270. }
  4271. [ret setValue:filter forKey:@"filter"];
  4272. DebugLog(@"offline_category sql:%@",sqlQuery);
  4273. sqlite3_stmt * statement;
  4274. [ret setValue:@"2" forKey:@"result"];
  4275. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4276. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4277. // int count=0;
  4278. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4279. {
  4280. int i=0;
  4281. while (sqlite3_step(statement) == SQLITE_ROW)
  4282. {
  4283. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4284. char *name = (char*)sqlite3_column_text(statement, 0);
  4285. if(name==nil)
  4286. name="";
  4287. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4288. char *description = (char*)sqlite3_column_text(statement, 1);
  4289. if(description==nil)
  4290. description="";
  4291. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4292. int product_id = sqlite3_column_int(statement, 2);
  4293. int wid = sqlite3_column_int(statement, 3);
  4294. int closeout = sqlite3_column_int(statement, 4);
  4295. int cid = sqlite3_column_int(statement, 5);
  4296. int wisdelete = sqlite3_column_int(statement, 6);
  4297. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4298. if(wid !=0 && wisdelete != 1)
  4299. [item setValue:@"true" forKey:@"wish_exists"];
  4300. else
  4301. [item setValue:@"false" forKey:@"wish_exists"];
  4302. if(closeout==0)
  4303. [item setValue:@"false" forKey:@"is_closeout"];
  4304. else
  4305. [item setValue:@"true" forKey:@"is_closeout"];
  4306. if(cid==0)
  4307. [item setValue:@"false" forKey:@"cart_exists"];
  4308. else
  4309. [item setValue:@"true" forKey:@"cart_exists"];
  4310. [item addEntriesFromDictionary:imgjson];
  4311. // [item setValue:nsurl forKey:@"img"];
  4312. [item setValue:nsname forKey:@"name"];
  4313. [item setValue:nsdescription forKey:@"description"];
  4314. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4315. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4316. i++;
  4317. }
  4318. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4319. [ret setObject:items forKey:@"items"];
  4320. sqlite3_finalize(statement);
  4321. } else {
  4322. DebugLog(@"nothing...");
  4323. }
  4324. DebugLog(@"count:%d",count);
  4325. [iSalesDB close_db:db];
  4326. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4327. return ret;
  4328. }
  4329. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4330. {
  4331. return [self categoryList:params limited:YES];
  4332. }
  4333. # pragma mark item search
  4334. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4335. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4336. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4337. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4338. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4339. // category
  4340. NSDictionary *category_menu = [self offline_category_menu];
  4341. [filter setValue:category_menu forKey:@"category"];
  4342. NSString* where= nil;
  4343. NSString* orderby= @"m.name";
  4344. if (!filterSearch) {
  4345. int covertype = [[params valueForKey:@"covertype"] intValue];
  4346. switch (covertype) {
  4347. case 0:
  4348. {
  4349. where=@"m.category like'%%#005#%%'";
  4350. break;
  4351. }
  4352. case 1:
  4353. {
  4354. where=@"m.alert like '%QS%'";
  4355. break;
  4356. }
  4357. case 2:
  4358. {
  4359. where=@"m.availability>0";
  4360. break;
  4361. }
  4362. case 3:
  4363. {
  4364. where=@"m.best_seller>0";
  4365. orderby=@"m.best_seller desc,m.name asc";
  4366. break;
  4367. }
  4368. default:
  4369. where=@"1=1";
  4370. break;
  4371. }
  4372. }
  4373. int limit = [[params valueForKey:@"limit"] intValue];
  4374. int offset = [[params valueForKey:@"offset"] intValue];
  4375. NSString *limit_str = @"";
  4376. if (limited) {
  4377. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4378. }
  4379. sqlite3 *db = [iSalesDB get_db];
  4380. // [iSalesDB AddExFunction:db];
  4381. int count;
  4382. NSString *sqlQuery = nil;
  4383. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4384. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  4385. double price_min = 0;
  4386. double price_max = 0;
  4387. if (filterSearch) {
  4388. // alert
  4389. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4390. NSString *alert = params[@"alert"];
  4391. if ([alert isEqualToString:@"Display All"]) {
  4392. alert = [NSString stringWithFormat:@""];
  4393. } else {
  4394. alert = [self translateSingleQuote:alert];
  4395. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4396. }
  4397. // available
  4398. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4399. NSString *available = params[@"available"];
  4400. NSString *available_condition;
  4401. if ([available isEqualToString:@"Display All"]) {
  4402. available_condition = @"";
  4403. } else if ([available isEqualToString:@"Available Now"]) {
  4404. available_condition = @"and availability > 0";
  4405. } else {
  4406. available_condition = @"and availability == 0";
  4407. }
  4408. // best seller
  4409. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4410. NSString *best_seller = @"";
  4411. NSString *order_best_seller = @"m.name asc";
  4412. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4413. best_seller = @"and best_seller > 0";
  4414. order_best_seller = @"m.best_seller desc,m.name asc";
  4415. }
  4416. // price
  4417. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4418. NSString *price = params[@"price"];
  4419. price_min = 0;
  4420. price_max = MAXFLOAT;
  4421. if (appDelegate.user) {
  4422. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4423. NSMutableString *priceName = [NSMutableString string];
  4424. for (int i = 0; i < priceTypeArray.count; i++) {
  4425. NSString *pricetype = priceTypeArray[i];
  4426. pricetype = [self translateSingleQuote:pricetype];
  4427. if (i == 0) {
  4428. [priceName appendFormat:@"'%@'",pricetype];
  4429. } else {
  4430. [priceName appendFormat:@",'%@'",pricetype];
  4431. }
  4432. }
  4433. if ([price isEqualToString:@"Display All"]) {
  4434. price = [NSString stringWithFormat:@""];
  4435. } else if([price containsString:@"+"]){
  4436. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4437. price_min = [price doubleValue];
  4438. 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];
  4439. } else {
  4440. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4441. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4442. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4443. 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];
  4444. }
  4445. } else {
  4446. price = @"";
  4447. }
  4448. // sold_by_qty : Sold in quantities of %@
  4449. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4450. NSString *qty = params[@"sold_by_qty"];
  4451. if ([qty isEqualToString:@"Display All"]) {
  4452. qty = @"";
  4453. } else {
  4454. qty = [self translateSingleQuote:qty];
  4455. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4456. }
  4457. // category
  4458. NSString *category_id = params[@"ctgId"];
  4459. NSMutableArray *cate_id_array = nil;
  4460. NSMutableString *cateWhere = [NSMutableString string];
  4461. if ([category_id isEqualToString:@""] || !category_id) {
  4462. [cateWhere appendString:@"1 = 1"];
  4463. } else {
  4464. if ([category_id containsString:@","]) {
  4465. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4466. } else {
  4467. cate_id_array = [@[category_id] mutableCopy];
  4468. }
  4469. /*-----------*/
  4470. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4471. for (int i = 0; i < cate_id_array.count; i++) {
  4472. for (NSString *key0 in cateDic.allKeys) {
  4473. if ([key0 containsString:@"category_"]) {
  4474. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4475. for (NSString *key1 in category0.allKeys) {
  4476. if ([key1 containsString:@"category_"]) {
  4477. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4478. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4479. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4480. cate_id_array[i] = [category1 objectForKey:@"id"];
  4481. if (i == 0) {
  4482. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4483. } else {
  4484. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4485. }
  4486. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4487. [category0 setValue:category1 forKey:key1];
  4488. [cateDic setValue:category0 forKey:key0];
  4489. }
  4490. }
  4491. }
  4492. }
  4493. }
  4494. }
  4495. [filter setValue:cateDic forKey:@"category"];
  4496. }
  4497. // where bestseller > 0 order by bestseller desc
  4498. // sql query: alert availability(int) best_seller(int) price qty
  4499. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete 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 left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4500. // count
  4501. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4502. }
  4503. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4504. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4505. if (!appDelegate.user) {
  4506. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4507. }
  4508. [ret setValue:filter forKey:@"filter"];
  4509. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4510. sqlite3_stmt * statement;
  4511. [ret setValue:@"2" forKey:@"result"];
  4512. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4513. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4514. // int count=0;
  4515. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4516. {
  4517. int i=0;
  4518. while (sqlite3_step(statement) == SQLITE_ROW)
  4519. {
  4520. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4521. char *name = (char*)sqlite3_column_text(statement, 0);
  4522. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4523. char *description = (char*)sqlite3_column_text(statement, 1);
  4524. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4525. int product_id = sqlite3_column_int(statement, 2);
  4526. int wid = sqlite3_column_int(statement, 3);
  4527. int closeout = sqlite3_column_int(statement, 4);
  4528. int cid = sqlite3_column_int(statement, 5);
  4529. int wisdelete = sqlite3_column_int(statement, 6);
  4530. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4531. if(wid !=0 && wisdelete != 1)
  4532. [item setValue:@"true" forKey:@"wish_exists"];
  4533. else
  4534. [item setValue:@"false" forKey:@"wish_exists"];
  4535. if(closeout==0)
  4536. [item setValue:@"false" forKey:@"is_closeout"];
  4537. else
  4538. [item setValue:@"true" forKey:@"is_closeout"];
  4539. if(cid==0)
  4540. [item setValue:@"false" forKey:@"cart_exists"];
  4541. else
  4542. [item setValue:@"true" forKey:@"cart_exists"];
  4543. [item addEntriesFromDictionary:imgjson];
  4544. // [item setValue:nsurl forKey:@"img"];
  4545. [item setValue:nsname forKey:@"fash_name"];
  4546. [item setValue:nsdescription forKey:@"description"];
  4547. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4548. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4549. i++;
  4550. }
  4551. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4552. [ret setObject:items forKey:@"items"];
  4553. sqlite3_finalize(statement);
  4554. }
  4555. [iSalesDB close_db:db];
  4556. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4557. return ret;
  4558. }
  4559. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4560. {
  4561. return [self itemsearch:params limited:YES];
  4562. }
  4563. #pragma mark order detail
  4564. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4565. if (str1.length == 0) {
  4566. str1 = @"&nbsp";
  4567. }
  4568. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4569. return str;
  4570. }
  4571. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4572. {
  4573. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4574. [fromformatter setDateFormat:from];
  4575. NSDate *date = [fromformatter dateFromString:datetime];
  4576. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4577. [toformatter setDateFormat:to];
  4578. NSString * ret = [toformatter stringFromDate:date];
  4579. return ret;
  4580. }
  4581. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4582. // 把毫秒去掉
  4583. if ([dateTime containsString:@"."]) {
  4584. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4585. }
  4586. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4587. formatter.dateFormat = formate;
  4588. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4589. NSDate *date = [formatter dateFromString:dateTime];
  4590. formatter.dateFormat = newFormate;
  4591. NSString *result = [formatter stringFromDate:date];
  4592. return result ? result : @"";
  4593. }
  4594. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4595. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4596. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4597. }
  4598. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4599. {
  4600. DebugLog(@"offline oderdetail params: %@",params);
  4601. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4602. int orderId = [params[@"orderId"] intValue];
  4603. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4604. // decrypt card number and card security code
  4605. // 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 ];
  4606. 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,o.lift_null,o.handling_null,o.ship_via 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 as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4607. sqlite3 *db = [iSalesDB get_db];
  4608. sqlite3_stmt * statement;
  4609. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4610. NSString *nssoid = @"";
  4611. NSString* orderinfo = @"";
  4612. NSString *moreInfo = @"";
  4613. double handlingFee = 0;
  4614. double payments_and_credist = 0;
  4615. double totalPrice = 0;
  4616. double shippingFee = 0;
  4617. double lift_gate = 0;
  4618. NSString *customer_contact = @"";
  4619. NSString *customer_email = @"";
  4620. NSString *customer_fax = @"";
  4621. NSString *customer_phone = @"";
  4622. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4623. {
  4624. if (sqlite3_step(statement) == SQLITE_ROW)
  4625. {
  4626. int order_id = sqlite3_column_int(statement, 0);
  4627. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4628. ret[@"sign_url"] = sign_url;
  4629. customer_contact = [self textAtColumn:52 statement:statement];
  4630. customer_email = [self textAtColumn:53 statement:statement];
  4631. customer_phone = [self textAtColumn:54 statement:statement];
  4632. customer_fax = [self textAtColumn:55 statement:statement];
  4633. int offline_edit=sqlite3_column_int(statement, 56);
  4634. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4635. char *soid = (char*)sqlite3_column_text(statement, 1);
  4636. if(soid==nil)
  4637. soid= "";
  4638. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4639. // so#
  4640. ret[@"so#"] = nssoid;
  4641. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4642. if(poNumber==nil)
  4643. poNumber= "";
  4644. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4645. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4646. if(create_time==nil)
  4647. create_time= "";
  4648. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4649. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4650. int status = sqlite3_column_int(statement, 4);
  4651. int erpStatus = sqlite3_column_int(statement, 49);
  4652. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4653. // status
  4654. if (status > 1 && status != 3) {
  4655. status = erpStatus;
  4656. } else if (status == 3) {
  4657. status = 15;
  4658. }
  4659. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4660. ret[@"order_status"] = nsstatus;
  4661. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4662. if(company_name==nil)
  4663. company_name= "";
  4664. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4665. // company name
  4666. ret[@"company_name"] = nscompany_name;
  4667. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4668. if(customer_contact==nil)
  4669. customer_contact= "";
  4670. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4671. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4672. if(addr_1==nil)
  4673. addr_1="";
  4674. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4675. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4676. if(addr_2==nil)
  4677. addr_2="";
  4678. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4679. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4680. if(addr_3==nil)
  4681. addr_3="";
  4682. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4683. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4684. if(addr_4==nil)
  4685. addr_4="";
  4686. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4687. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4688. [arr_addr addObject:nsaddr_1];
  4689. [arr_addr addObject:nsaddr_2];
  4690. [arr_addr addObject:nsaddr_3];
  4691. [arr_addr addObject:nsaddr_4];
  4692. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4693. char *logist = (char*)sqlite3_column_text(statement, 11);
  4694. if(logist==nil)
  4695. logist= "";
  4696. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4697. if (status == -11 || status == 10 || status == 11) {
  4698. nslogist = [self textAtColumn:59 statement:statement];
  4699. };
  4700. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4701. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4702. shipping = @"Shipping To Be Quoted";
  4703. } else {
  4704. shippingFee = sqlite3_column_double(statement, 12);
  4705. }
  4706. // Shipping
  4707. ret[@"Shipping"] = shipping;
  4708. int have_lift_gate = sqlite3_column_int(statement, 17);
  4709. lift_gate = sqlite3_column_double(statement, 13);
  4710. // Liftgate Fee(No loading dock)
  4711. if (!have_lift_gate) {
  4712. lift_gate = 0;
  4713. }
  4714. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4715. if (sqlite3_column_int(statement, 57)) {
  4716. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4717. }
  4718. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4719. if(general_notes==nil)
  4720. general_notes= "";
  4721. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4722. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4723. if(internal_notes==nil)
  4724. internal_notes= "";
  4725. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4726. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4727. if(payment_type==nil)
  4728. payment_type= "";
  4729. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4730. // order info
  4731. orderinfo = [self textFileName:@"order_info.html"];
  4732. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4733. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4734. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4735. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4736. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4737. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4738. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4739. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4740. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4741. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4742. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4743. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4744. NSString *payment = nil;
  4745. if([nspayment_type isEqualToString:@"Credit Card"])
  4746. {
  4747. payment = [self textFileName:@"creditcardpayment.html"];
  4748. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4749. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4750. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4751. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4752. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4753. NSString *card_type = [self textAtColumn:42 statement:statement];
  4754. if (card_type.length > 0) { // 显示星号
  4755. card_type = @"****";
  4756. }
  4757. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4758. if (card_number.length > 0 && card_number.length > 4) {
  4759. for (int i = 0; i < card_number.length - 4; i++) {
  4760. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4761. }
  4762. } else {
  4763. card_number = @"";
  4764. }
  4765. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4766. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4767. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4768. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4769. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4770. card_expiration = @"****";
  4771. }
  4772. NSString *card_city = [self textAtColumn:46 statement:statement];
  4773. NSString *card_state = [self textAtColumn:47 statement:statement];
  4774. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4775. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4776. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4777. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4778. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4779. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4780. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4781. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4782. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4783. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4784. }
  4785. else
  4786. {
  4787. payment=[self textFileName:@"normalpayment.html"];
  4788. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4789. }
  4790. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4791. ret[@"result"]= [NSNumber numberWithInt:2];
  4792. // more info
  4793. moreInfo = [self textFileName:@"more_info.html"];
  4794. /*****ship to******/
  4795. // ShipToCompany_or_&nbsp
  4796. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4797. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4798. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4799. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4800. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4801. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4802. /*****ship from******/
  4803. // ShipFromCompany_or_&nbsp
  4804. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4805. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4806. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4807. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4808. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4809. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4810. /*****freight to******/
  4811. // FreightBillToCompany_or_&nbsp
  4812. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4813. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4814. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4815. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4816. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4817. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4818. /*****merchandise to******/
  4819. // MerchandiseBillToCompany_or_&nbsp
  4820. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4821. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4822. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4823. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4824. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4825. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4826. /*****return to******/
  4827. // ReturnToCompany_or_&nbsp
  4828. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4829. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4830. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4831. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4832. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4833. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4834. //
  4835. // DebugLog(@"more info : %@",moreInfo);
  4836. // handling fee
  4837. handlingFee = sqlite3_column_double(statement, 33);
  4838. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4839. if (sqlite3_column_int(statement, 58)) {
  4840. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4841. }
  4842. //
  4843. // customer info
  4844. customerID = [self textAtColumn:36 statement:statement];
  4845. // mode
  4846. ret[@"mode"] = appDelegate.mode;
  4847. // model_count
  4848. ret[@"model_count"] = @(0);
  4849. }
  4850. sqlite3_finalize(statement);
  4851. }
  4852. [iSalesDB close_db:db];
  4853. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4854. // "customer_email" = "Shui Hu";
  4855. // "customer_fax" = "";
  4856. // "customer_first_name" = F;
  4857. // "customer_last_name" = L;
  4858. // "customer_name" = ",da He Xiang Dong Liu A";
  4859. // "customer_phone" = "Hey Xuan Feng";
  4860. contactInfo[@"customer_phone"] = customer_phone;
  4861. contactInfo[@"customer_fax"] = customer_fax;
  4862. contactInfo[@"customer_email"] = customer_email;
  4863. NSString *first_name = @"";
  4864. NSString *last_name = @"";
  4865. if ([customer_contact isEqualToString:@""]) {
  4866. } else if ([customer_contact containsString:@" "]) {
  4867. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4868. first_name = [customer_contact substringToIndex:first_space_index];
  4869. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4870. }
  4871. contactInfo[@"customer_first_name"] = first_name;
  4872. contactInfo[@"customer_last_name"] = last_name;
  4873. ret[@"customerInfo"] = contactInfo;
  4874. // models
  4875. if (nssoid) {
  4876. __block double TotalCuft = 0;
  4877. __block double TotalWeight = 0;
  4878. __block int TotalCarton = 0;
  4879. __block double allItemPrice = 0;
  4880. 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];
  4881. sqlite3 *db1 = [iSalesDB get_db];
  4882. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4883. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4884. int product_id = sqlite3_column_int(stmt, 0);
  4885. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4886. int item_id = sqlite3_column_int(stmt, 7);
  4887. NSString* Price=nil;
  4888. if(str_price==nil)
  4889. {
  4890. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4891. if(price==nil)
  4892. Price=@"No Price.";
  4893. else
  4894. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4895. }
  4896. else
  4897. {
  4898. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4899. }
  4900. double discount = sqlite3_column_double(stmt, 2);
  4901. int item_count = sqlite3_column_int(stmt, 3);
  4902. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4903. NSString *nsline_note=nil;
  4904. if(line_note!=nil)
  4905. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4906. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4907. // NSString *nsname = nil;
  4908. // if(name!=nil)
  4909. // nsname= [[NSString alloc]initWithUTF8String:name];
  4910. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4911. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4912. // NSString *nsdescription=nil;
  4913. // if(description!=nil)
  4914. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4915. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4916. // int stockUom = sqlite3_column_int(stmt, 8);
  4917. // int _id = sqlite3_column_int(stmt, 9);
  4918. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4919. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4920. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4921. int carton=[bsubtotaljson[@"carton"] intValue];
  4922. TotalCuft += cuft;
  4923. TotalWeight += weight;
  4924. TotalCarton += carton;
  4925. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4926. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4927. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4928. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4929. itemjson[@"note"]=nsline_note;
  4930. itemjson[@"origin_price"] = Price;
  4931. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4932. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4933. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4934. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4935. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4936. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4937. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4938. if(itemjson[@"combine"] != nil)
  4939. {
  4940. // int citem=0;
  4941. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4942. for(int bc=0;bc<bcount;bc++)
  4943. {
  4944. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4945. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4946. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4947. subTotal += uprice * modulus * item_count;
  4948. }
  4949. }
  4950. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4951. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4952. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4953. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4954. allItemPrice += subTotal;
  4955. }];
  4956. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4957. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4958. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4959. // payments/Credits
  4960. // payments_and_credist = sqlite3_column_double(statement, 34);
  4961. payments_and_credist = allItemPrice;
  4962. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4963. // // total
  4964. // totalPrice = sqlite3_column_double(statement, 35);
  4965. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4966. } else {
  4967. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4968. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4969. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4970. // payments/Credits
  4971. payments_and_credist = 0;
  4972. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4973. }
  4974. // total
  4975. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4976. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4977. ret[@"order_info"]= orderinfo;
  4978. ret[@"more_order_info"] = moreInfo;
  4979. return [RAUtils dict2data:ret];
  4980. }
  4981. #pragma mark order list
  4982. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4983. NSString *nsstatus = @"";
  4984. switch (status) {
  4985. case 0:
  4986. {
  4987. nsstatus=@"Temp Order";
  4988. break;
  4989. }
  4990. case 1:
  4991. {
  4992. nsstatus=@"Saved Order";
  4993. break;
  4994. }
  4995. case 2: {
  4996. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4997. break;
  4998. }
  4999. case 3:
  5000. case 15:
  5001. {
  5002. nsstatus=@"Cancelled";
  5003. break;
  5004. }
  5005. case 10:
  5006. {
  5007. nsstatus=@"Quote Submitted";
  5008. break;
  5009. }
  5010. case 11:
  5011. {
  5012. nsstatus=@"Sales Order Submitted";
  5013. break;
  5014. }
  5015. case 12:
  5016. {
  5017. nsstatus=@"Processing";
  5018. break;
  5019. }
  5020. case 13:
  5021. {
  5022. nsstatus=@"Shipped";
  5023. break;
  5024. }
  5025. case 14:
  5026. {
  5027. nsstatus=@"Closed";
  5028. break;
  5029. }
  5030. case -11:
  5031. {
  5032. nsstatus = @"Ready For Submit";
  5033. break;
  5034. }
  5035. default:
  5036. break;
  5037. }
  5038. return nsstatus;
  5039. }
  5040. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5041. double total = 0;
  5042. __block double payments_and_credist = 0;
  5043. __block double allItemPrice = 0;
  5044. // sqlite3 *db1 = [iSalesDB get_db];
  5045. if (so_id) {
  5046. // 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];
  5047. 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];
  5048. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5049. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5050. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5051. int product_id = sqlite3_column_int(stmt, 0);
  5052. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5053. int discount = sqlite3_column_int(stmt, 2);
  5054. int item_count = sqlite3_column_int(stmt, 3);
  5055. // int item_id = sqlite3_column_int(stmt, 7);
  5056. int item_id = sqlite3_column_int(stmt, 4);
  5057. NSString* Price=nil;
  5058. if(str_price==nil)
  5059. {
  5060. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5061. if(price==nil)
  5062. Price=@"No Price.";
  5063. else
  5064. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5065. }
  5066. else
  5067. {
  5068. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5069. }
  5070. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5071. itemjson[@"The unit price"]=Price;
  5072. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5073. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5074. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5075. if(itemjson[@"combine"] != nil)
  5076. {
  5077. // int citem=0;
  5078. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5079. for(int bc=0;bc<bcount;bc++)
  5080. {
  5081. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5082. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5083. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5084. subTotal += uprice * modulus * item_count;
  5085. }
  5086. }
  5087. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5088. allItemPrice += subTotal;
  5089. }];
  5090. payments_and_credist = allItemPrice;
  5091. } else {
  5092. // payments/Credits
  5093. payments_and_credist = 0;
  5094. }
  5095. // lift_gate handlingFee shippingFee
  5096. __block double lift_gate = 0;
  5097. __block double handlingFee = 0;
  5098. __block double shippingFee = 0;
  5099. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5100. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5101. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5102. if (have_lift_gate) {
  5103. lift_gate = sqlite3_column_double(stmt, 1);
  5104. }
  5105. handlingFee = sqlite3_column_double(stmt, 2);
  5106. shippingFee = sqlite3_column_double(stmt, 3);
  5107. }];
  5108. // total
  5109. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5110. if (close) {
  5111. [iSalesDB close_db:db1];
  5112. }
  5113. return total;
  5114. }
  5115. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5116. {
  5117. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5118. int limit = [[params valueForKey:@"limit"] intValue];
  5119. int offset = [[params valueForKey:@"offset"] intValue];
  5120. NSString* keyword = [params valueForKey:@"keyWord"];
  5121. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5122. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5123. NSString* where=@"1 = 1";
  5124. if(keyword.length>0)
  5125. 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]];
  5126. if (orderStatus.length > 0) {
  5127. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5128. if (order_status_array.count == 1) {
  5129. int status_value = [[order_status_array firstObject] integerValue];
  5130. if (status_value <= 1 || status_value == 3) {
  5131. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5132. } else {
  5133. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5134. }
  5135. } else if (order_status_array.count > 1) {
  5136. for (int i = 0; i < order_status_array.count;i++) {
  5137. NSString *status = order_status_array[i];
  5138. NSString *condition = @" or";
  5139. if (i == 0) {
  5140. condition = @" and (";
  5141. }
  5142. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5143. int status_value = [status integerValue];
  5144. if (status_value <= 1 || status_value == 3) {
  5145. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5146. } else {
  5147. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5148. }
  5149. }
  5150. where = [where stringByAppendingString:@" )"];
  5151. }
  5152. }
  5153. 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.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5154. // DebugLog(@"order list sql: %@",sqlQuery);
  5155. sqlite3 *db = [iSalesDB get_db];
  5156. sqlite3_stmt * statement;
  5157. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5158. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5159. {
  5160. int count=0;
  5161. while (sqlite3_step(statement) == SQLITE_ROW)
  5162. {
  5163. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5164. int order_id = sqlite3_column_double(statement, 0);
  5165. char *soid = (char*)sqlite3_column_text(statement, 1);
  5166. if(soid==nil)
  5167. soid= "";
  5168. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5169. int status = sqlite3_column_double(statement, 2);
  5170. int erpStatus = sqlite3_column_double(statement, 9);
  5171. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5172. if(sales_rep==nil)
  5173. sales_rep= "";
  5174. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5175. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5176. if(create_by==nil)
  5177. create_by= "";
  5178. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5179. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5180. if(company_name==nil)
  5181. company_name= "";
  5182. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5183. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5184. if(create_time==nil)
  5185. create_time= "";
  5186. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5187. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5188. // double total_price = sqlite3_column_double(statement, 7);
  5189. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5190. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5191. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5192. int offline_edit = sqlite3_column_int(statement, 10);
  5193. // ": "JH",
  5194. // "": "$8307.00",
  5195. // "": "MOB1608050001",
  5196. // "": "ArpithaT",
  5197. // "": "1st Stage Property Transformations",
  5198. // "": "JANICE SUTTON",
  5199. // "": 2255,
  5200. // "": "08/02/2016 09:49:18",
  5201. // "": 1,
  5202. // "": "Saved Order"
  5203. // "": "1470384050483",
  5204. // "model_count": "6 / 28"
  5205. item[@"sales_rep"]= nssales_rep;
  5206. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5207. item[@"so#"]= nssoid;
  5208. item[@"create_by"]= nscreate_by;
  5209. item[@"customer_name"]= nscompany_name;
  5210. item[@"customer_contact"] = customer_contact;
  5211. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5212. item[@"purchase_time"]= nscreate_time;
  5213. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5214. item[@"order_status"]= nsstatus;
  5215. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5216. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5217. // item[@"model_count"]
  5218. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5219. count++;
  5220. }
  5221. ret[@"count"]= [NSNumber numberWithInt:count];
  5222. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5223. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5224. ret[@"result"]= [NSNumber numberWithInt:2];
  5225. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5226. ret[@"time_zone"] = @"PST";
  5227. sqlite3_finalize(statement);
  5228. }
  5229. 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];
  5230. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5231. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5232. [iSalesDB close_db:db];
  5233. return [RAUtils dict2data:ret];
  5234. }
  5235. #pragma mark update gnotes
  5236. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5237. {
  5238. DebugLog(@"params: %@",params);
  5239. // comments = Meyoyoyoyoyoyoy;
  5240. // orderCode = MOB1608110001;
  5241. // password = 123456;
  5242. // user = EvanK;
  5243. 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]];
  5244. int ret = [iSalesDB execSql:sql];
  5245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5246. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5247. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5248. // [dic setValue:@"160409" forKey:@"min_ver"];
  5249. return [RAUtils dict2data:dic];
  5250. }
  5251. #pragma mark move to wishlist
  5252. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5253. {
  5254. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5255. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5256. _id = [NSString stringWithFormat:@"(%@)",_id];
  5257. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5258. sqlite3 *db = [iSalesDB get_db];
  5259. __block NSString *product_id = @"";
  5260. __block NSString *item_count_str = @"";
  5261. // __block NSString *item_id = nil;
  5262. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5263. // product_id = [self textAtColumn:0 statement:stmt];
  5264. int item_count = sqlite3_column_int(stmt, 1);
  5265. // item_id = [self textAtColumn:2 statement:stmt];
  5266. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5267. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5268. if (p_id.length) {
  5269. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5270. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5271. }
  5272. }];
  5273. [iSalesDB close_db:db];
  5274. // 去除第一个,
  5275. product_id = [product_id substringFromIndex:1];
  5276. item_count_str = [item_count_str substringFromIndex:1];
  5277. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5278. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5279. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5280. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5281. sqlite3 *db1 = [iSalesDB get_db];
  5282. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5283. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5284. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5285. // 删除
  5286. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5287. int ret = [iSalesDB execSql:deleteSql db:db1];
  5288. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5289. [iSalesDB close_db:db1];
  5290. return [RAUtils dict2data:dic];
  5291. }
  5292. #pragma mark cart delete
  5293. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5294. {
  5295. // cartItemId = 548;
  5296. // orderCode = MOB1608110001;
  5297. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5298. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5299. _id = [NSString stringWithFormat:@"(%@)",_id];
  5300. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5301. int ret = [iSalesDB execSql:sql];
  5302. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5303. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5304. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5305. // [dic setValue:@"160409" forKey:@"min_ver"];
  5306. return [RAUtils dict2data:dic];
  5307. }
  5308. #pragma mark set price
  5309. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5310. {
  5311. DebugLog(@"cart set price params: %@",params);
  5312. // "cartitem_id" = 1;
  5313. // discount = "0.000000";
  5314. // "item_note" = "";
  5315. // price = "269.000000";
  5316. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5317. NSString *notes = [self valueInParams:params key:@"item_note"];
  5318. NSString *discount = [self valueInParams:params key:@"discount"];
  5319. NSString *price = [self valueInParams:params key:@"price"];
  5320. // bool badd_price_changed=false;
  5321. // sqlite3* db=[iSalesDB get_db];
  5322. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5323. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5324. // NSRange range;
  5325. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5326. //
  5327. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5328. // badd_price_changed=true;
  5329. // [iSalesDB close_db:db];
  5330. //
  5331. // if(badd_price_changed)
  5332. // {
  5333. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5334. // }
  5335. //
  5336. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5337. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5338. int ret = [iSalesDB execSql:sql];
  5339. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5340. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5341. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5342. // [dic setValue:@"160409" forKey:@"min_ver"];
  5343. return [RAUtils dict2data:dic];
  5344. }
  5345. #pragma mark set line notes
  5346. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5347. {
  5348. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5349. NSString *notes = [self valueInParams:params key:@"notes"];
  5350. notes = [self translateSingleQuote:notes];
  5351. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5352. int ret = [iSalesDB execSql:sql];
  5353. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5354. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5355. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5356. // [dic setValue:@"160409" forKey:@"min_ver"];
  5357. return [RAUtils dict2data:dic];
  5358. }
  5359. #pragma mark set qty
  5360. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5361. {
  5362. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5363. int item_count = [params[@"inputInt"] integerValue];
  5364. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5365. int ret = [iSalesDB execSql:sql];
  5366. __block int item_id = 0;
  5367. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5368. item_id = sqlite3_column_int(stmt, 0);
  5369. }];
  5370. sqlite3 *db = [iSalesDB get_db];
  5371. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5372. [iSalesDB close_db:db];
  5373. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5374. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5375. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5376. // [dic setValue:@"160409" forKey:@"min_ver"];
  5377. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5378. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5379. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5380. return [RAUtils dict2data:dic];
  5381. }
  5382. #pragma mark place order
  5383. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5384. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5385. if (aname) {
  5386. [dic setValue:aname forKey:@"aname"];
  5387. }
  5388. if (control) {
  5389. [dic setValue:control forKey:@"control"];
  5390. }
  5391. if (keyboard) {
  5392. [dic setValue:keyboard forKey:@"keyboard"];
  5393. }
  5394. if (name) {
  5395. [dic setValue:name forKey:@"name"];
  5396. }
  5397. if (value) {
  5398. [dic setValue:value forKey:@"value"];
  5399. }
  5400. return dic;
  5401. }
  5402. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5403. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5404. orderCode = [self translateSingleQuote:orderCode];
  5405. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5406. 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];
  5407. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5408. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5409. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5410. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5411. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5412. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5413. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5414. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5415. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5416. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5417. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5418. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5419. NSString *email = [self textAtColumn:11 statement:stmt];
  5420. NSString *phone = [self textAtColumn:12 statement:stmt];
  5421. NSString *fax = [self textAtColumn:13 statement:stmt];
  5422. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5423. NSString *city = [self textAtColumn:15 statement:stmt];
  5424. NSString *state = [self textAtColumn:16 statement:stmt];
  5425. NSString *country = [self textAtColumn:17 statement:stmt];
  5426. NSString *name = [self textAtColumn:18 statement:stmt];
  5427. // contact id
  5428. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5429. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5430. [contact_id_dic setValue:@"text" forKey:@"control"];
  5431. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5432. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5433. [contact_id_dic setValue:@"true" forKey:@"required"];
  5434. [contact_id_dic setValue:contact_id forKey:@"value"];
  5435. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5436. // business card
  5437. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5438. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5439. [business_card_dic setValue:@"img" forKey:@"control"];
  5440. [business_card_dic setValue:@"1" forKey:@"disable"];
  5441. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5442. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5443. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5444. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5445. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5446. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5447. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5448. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5449. // fax
  5450. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5451. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5452. // zipcode
  5453. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5454. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5455. // city
  5456. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5457. [customer_dic setValue:city_dic forKey:@"item_12"];
  5458. // state
  5459. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5460. [customer_dic setValue:state_dic forKey:@"item_13"];
  5461. // country
  5462. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5463. [customer_dic setValue:country_dic forKey:@"item_14"];
  5464. // company name
  5465. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5466. [customer_dic setValue:company_dic forKey:@"item_2"];
  5467. // addr_1
  5468. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5469. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5470. // addr_2
  5471. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5472. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5473. // addr_3
  5474. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5475. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5476. // addr_4
  5477. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5478. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5479. // Contact
  5480. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5481. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5482. // email
  5483. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5484. [customer_dic setValue:email_dic forKey:@"item_8"];
  5485. // phone
  5486. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5487. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5488. // title
  5489. [customer_dic setValue:@"Customer" forKey:@"title"];
  5490. // count
  5491. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5492. }];
  5493. // setting
  5494. NSDictionary *setting = params[@"setting"];
  5495. NSNumber *hide = setting[@"CustomerHide"];
  5496. [customer_dic setValue:hide forKey:@"hide"];
  5497. return customer_dic;
  5498. }
  5499. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5500. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5501. // setting
  5502. NSDictionary *setting = params[@"setting"];
  5503. NSNumber *hide = setting[@"ShipToHide"];
  5504. [dic setValue:hide forKey:@"hide"];
  5505. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5506. orderCode = [self translateSingleQuote:orderCode];
  5507. 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];
  5508. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5509. NSString *cid = [self textAtColumn:0 statement:stmt];
  5510. NSString *name = [self textAtColumn:1 statement:stmt];
  5511. NSString *ext = [self textAtColumn:2 statement:stmt];
  5512. NSString *contact = [self textAtColumn:3 statement:stmt];
  5513. NSString *email = [self textAtColumn:4 statement:stmt];
  5514. NSString *fax = [self textAtColumn:5 statement:stmt];
  5515. NSString *phone = [self textAtColumn:6 statement:stmt];
  5516. // count
  5517. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5518. // title
  5519. [dic setValue:@"Ship To" forKey:@"title"];
  5520. // choose
  5521. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5522. [choose_dic setValue:@"choose" forKey:@"aname"];
  5523. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5524. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5525. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5526. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5527. @"receive_contact" : @"customer_contact",
  5528. @"receive_email" : @"customer_email",
  5529. @"receive_ext" : @"customer_contact_ext",
  5530. @"receive_fax" : @"customer_fax",
  5531. @"receive_name" : @"customer_name",
  5532. @"receive_phone" : @"customer_phone"},
  5533. @"refresh" : [NSNumber numberWithInteger:1],
  5534. @"type" : @"pull"};
  5535. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5536. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5537. @"name" : @"Add new address",
  5538. @"refresh" : [NSNumber numberWithInteger:1],
  5539. @"value" : @"new_addr"
  5540. };
  5541. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5542. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5543. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5544. @"receive_contact" : @"customer_contact",
  5545. @"receive_email" : @"customer_email",
  5546. @"receive_ext" : @"customer_contact_ext",
  5547. @"receive_fax" : @"customer_fax",
  5548. @"receive_name" : @"customer_name",
  5549. @"receive_phone" : @"customer_phone"},
  5550. @"name" : @"select_ship_to",
  5551. @"refresh" : [NSNumber numberWithInteger:1],
  5552. @"value" : @"Sales_Order_Ship_To"};
  5553. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5554. [dic setValue:choose_dic forKey:@"item_0"];
  5555. // contact id
  5556. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5557. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5558. [contact_id_dic setValue:@"true" forKey:@"required"];
  5559. [dic setValue:contact_id_dic forKey:@"item_1"];
  5560. // company name
  5561. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5562. [dic setValue:company_name_dic forKey:@"item_2"];
  5563. // address
  5564. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5565. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5566. if ([required integerValue]) {
  5567. [ext_dic setValue:@"true" forKey:@"required"];
  5568. }
  5569. [dic setValue:ext_dic forKey:@"item_3"];
  5570. // contact
  5571. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5572. [dic setValue:contact_dic forKey:@"item_4"];
  5573. // phone
  5574. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5575. [dic setValue:phone_dic forKey:@"item_5"];
  5576. // fax
  5577. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5578. [dic setValue:fax_dic forKey:@"item_6"];
  5579. // email
  5580. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5581. [dic setValue:email_dic forKey:@"item_7"];
  5582. }];
  5583. return dic;
  5584. }
  5585. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5586. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5587. // setting
  5588. NSDictionary *setting = params[@"setting"];
  5589. NSNumber *hide = setting[@"ShipFromHide"];
  5590. [dic setValue:hide forKey:@"hide"];
  5591. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5592. orderCode = [self translateSingleQuote:orderCode];
  5593. 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];
  5594. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  5595. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5596. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5597. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5598. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5599. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5600. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5601. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5602. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5603. if (!cid.length) {
  5604. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5605. cid = [self textAtColumn:0 statement:statment];
  5606. name = [self textAtColumn:1 statement:statment];
  5607. ext = [self textAtColumn:2 statement:statment];
  5608. contact = [self textAtColumn:3 statement:statment];
  5609. email = [self textAtColumn:4 statement:statment];
  5610. fax = [self textAtColumn:5 statement:statment];
  5611. phone = [self textAtColumn:6 statement:statment];
  5612. }];
  5613. }
  5614. // count
  5615. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5616. // title
  5617. [dic setValue:@"Ship From" forKey:@"title"];
  5618. // hide
  5619. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5620. // choose
  5621. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5622. [choose_dic setValue:@"choose" forKey:@"aname"];
  5623. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5624. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5625. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5626. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5627. @"sender_contact" : @"customer_contact",
  5628. @"sender_email" : @"customer_email",
  5629. @"sender_ext" : @"customer_contact_ext",
  5630. @"sender_fax" : @"customer_fax",
  5631. @"sender_name" : @"customer_name",
  5632. @"sender_phone" : @"customer_phone"},
  5633. @"name" : @"select_cid",
  5634. @"refresh" : [NSNumber numberWithInteger:0],
  5635. @"value" : @"Sales_Order_Ship_From"};
  5636. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5637. [dic setValue:choose_dic forKey:@"item_0"];
  5638. // contact id
  5639. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5640. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5641. [contact_id_dic setValue:@"true" forKey:@"required"];
  5642. [dic setValue:contact_id_dic forKey:@"item_1"];
  5643. // company name
  5644. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5645. [dic setValue:company_name_dic forKey:@"item_2"];
  5646. // address
  5647. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5648. [dic setValue:ext_dic forKey:@"item_3"];
  5649. // contact
  5650. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5651. [dic setValue:contact_dic forKey:@"item_4"];
  5652. // phone
  5653. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5654. [dic setValue:phone_dic forKey:@"item_5"];
  5655. // fax
  5656. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5657. [dic setValue:fax_dic forKey:@"item_6"];
  5658. // email
  5659. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5660. [dic setValue:email_dic forKey:@"item_7"];
  5661. }];
  5662. return dic;
  5663. }
  5664. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5665. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5666. // setting
  5667. NSDictionary *setting = params[@"setting"];
  5668. NSNumber *hide = setting[@"FreightBillToHide"];
  5669. [dic setValue:hide forKey:@"hide"];
  5670. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5671. orderCode = [self translateSingleQuote:orderCode];
  5672. 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];
  5673. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5674. NSString *cid = [self textAtColumn:0 statement:stmt];
  5675. NSString *name = [self textAtColumn:1 statement:stmt];
  5676. NSString *ext = [self textAtColumn:2 statement:stmt];
  5677. NSString *contact = [self textAtColumn:3 statement:stmt];
  5678. NSString *email = [self textAtColumn:4 statement:stmt];
  5679. NSString *fax = [self textAtColumn:5 statement:stmt];
  5680. NSString *phone = [self textAtColumn:6 statement:stmt];
  5681. // count
  5682. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5683. // title
  5684. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5685. // hide
  5686. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5687. // choose
  5688. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5689. [choose_dic setValue:@"choose" forKey:@"aname"];
  5690. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5691. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5692. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5693. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5694. @"shipping_billto_contact" : @"receive_contact",
  5695. @"shipping_billto_email" : @"receive_email",
  5696. @"shipping_billto_ext" : @"receive_ext",
  5697. @"shipping_billto_fax" : @"receive_fax",
  5698. @"shipping_billto_name" : @"receive_name",
  5699. @"shipping_billto_phone" : @"receive_phone"},
  5700. @"type" : @"pull"};
  5701. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5702. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5703. @"type" : @"pull",
  5704. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5705. @"shipping_billto_contact" : @"customer_contact",
  5706. @"shipping_billto_email" : @"customer_email",
  5707. @"shipping_billto_ext" : @"customer_contact_ext",
  5708. @"shipping_billto_fax" : @"customer_fax",
  5709. @"shipping_billto_name" : @"customer_name",
  5710. @"shipping_billto_phone" : @"customer_phone"}
  5711. };
  5712. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5713. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5714. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5715. @"shipping_billto_contact" : @"sender_contact",
  5716. @"shipping_billto_email" : @"sender_email",
  5717. @"shipping_billto_ext" : @"sender_ext",
  5718. @"shipping_billto_fax" : @"sender_fax",
  5719. @"shipping_billto_name" : @"sender_name",
  5720. @"shipping_billto_phone" : @"sender_phone"},
  5721. @"type" : @"pull"
  5722. };
  5723. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5724. NSDictionary *select_freight_bill_to_dic = @{
  5725. @"aname" : @"Select freight bill to",
  5726. @"name" : @"select_cid",
  5727. @"refresh" : [NSNumber numberWithInteger:0],
  5728. @"value" : @"Sales_Order_Freight_Bill_To",
  5729. @"key_map" : @{
  5730. @"shipping_billto_cid" : @"customer_cid",
  5731. @"shipping_billto_contact" : @"customer_contact",
  5732. @"shipping_billto_email" : @"customer_email",
  5733. @"shipping_billto_ext" : @"customer_contact_ext",
  5734. @"shipping_billto_fax" : @"customer_fax",
  5735. @"shipping_billto_name" : @"customer_name",
  5736. @"shipping_billto_phone" : @"customer_phone"
  5737. }
  5738. };
  5739. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5740. [dic setValue:choose_dic forKey:@"item_0"];
  5741. // contact id
  5742. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5743. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5744. [contact_id_dic setValue:@"true" forKey:@"required"];
  5745. [dic setValue:contact_id_dic forKey:@"item_1"];
  5746. // company name
  5747. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5748. [dic setValue:company_name_dic forKey:@"item_2"];
  5749. // address
  5750. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5751. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5752. if ([ext_required integerValue]) {
  5753. [ext_dic setValue:@"true" forKey:@"required"];
  5754. }
  5755. [dic setValue:ext_dic forKey:@"item_3"];
  5756. // contact
  5757. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5758. [dic setValue:contact_dic forKey:@"item_4"];
  5759. // phone
  5760. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5761. [dic setValue:phone_dic forKey:@"item_5"];
  5762. // fax
  5763. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5764. [dic setValue:fax_dic forKey:@"item_6"];
  5765. // email
  5766. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5767. [dic setValue:email_dic forKey:@"item_7"];
  5768. }];
  5769. return dic;
  5770. }
  5771. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5772. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5773. // setting
  5774. NSDictionary *setting = params[@"setting"];
  5775. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5776. [dic setValue:hide forKey:@"hide"];
  5777. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5778. orderCode = [self translateSingleQuote:orderCode];
  5779. 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];
  5780. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5781. NSString *cid = [self textAtColumn:0 statement:stmt];
  5782. NSString *name = [self textAtColumn:1 statement:stmt];
  5783. NSString *ext = [self textAtColumn:2 statement:stmt];
  5784. NSString *contact = [self textAtColumn:3 statement:stmt];
  5785. NSString *email = [self textAtColumn:4 statement:stmt];
  5786. NSString *fax = [self textAtColumn:5 statement:stmt];
  5787. NSString *phone = [self textAtColumn:6 statement:stmt];
  5788. // count
  5789. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5790. // title
  5791. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5792. // hide
  5793. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5794. // choose
  5795. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5796. [choose_dic setValue:@"choose" forKey:@"aname"];
  5797. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5798. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5799. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5800. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5801. @"billing_contact" : @"receive_contact",
  5802. @"billing_email" : @"receive_email",
  5803. @"billing_ext" : @"receive_ext",
  5804. @"billing_fax" : @"receive_fax",
  5805. @"billing_name" : @"receive_name",
  5806. @"billing_phone" : @"receive_phone"},
  5807. @"type" : @"pull"};
  5808. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5809. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5810. @"type" : @"pull",
  5811. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5812. @"billing_contact" : @"customer_contact",
  5813. @"billing_email" : @"customer_email",
  5814. @"billing_ext" : @"customer_contact_ext",
  5815. @"billing_fax" : @"customer_fax",
  5816. @"billing_name" : @"customer_name",
  5817. @"billing_phone" : @"customer_phone"}
  5818. };
  5819. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5820. NSDictionary *select_bill_to_dic = @{
  5821. @"aname" : @"Select bill to",
  5822. @"name" : @"select_cid",
  5823. @"refresh" : [NSNumber numberWithInteger:0],
  5824. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5825. @"key_map" : @{
  5826. @"billing_cid" : @"customer_cid",
  5827. @"billing_contact" : @"customer_contact",
  5828. @"billing_email" : @"customer_email",
  5829. @"billing_ext" : @"customer_contact_ext",
  5830. @"billing_fax" : @"customer_fax",
  5831. @"billing_name" : @"customer_name",
  5832. @"billing_phone" : @"customer_phone"
  5833. }
  5834. };
  5835. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5836. [dic setValue:choose_dic forKey:@"item_0"];
  5837. // contact id
  5838. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5839. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5840. [contact_id_dic setValue:@"true" forKey:@"required"];
  5841. [dic setValue:contact_id_dic forKey:@"item_1"];
  5842. // company name
  5843. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5844. [dic setValue:company_name_dic forKey:@"item_2"];
  5845. // address
  5846. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5847. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5848. if ([ext_required integerValue]) {
  5849. [ext_dic setValue:@"true" forKey:@"required"];
  5850. }
  5851. [dic setValue:ext_dic forKey:@"item_3"];
  5852. // contact
  5853. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5854. [dic setValue:contact_dic forKey:@"item_4"];
  5855. // phone
  5856. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5857. [dic setValue:phone_dic forKey:@"item_5"];
  5858. // fax
  5859. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5860. [dic setValue:fax_dic forKey:@"item_6"];
  5861. // email
  5862. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5863. [dic setValue:email_dic forKey:@"item_7"];
  5864. }];
  5865. return dic;
  5866. }
  5867. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5868. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5869. // setting
  5870. NSDictionary *setting = params[@"setting"];
  5871. NSNumber *hide = setting[@"ReturnToHide"];
  5872. [dic setValue:hide forKey:@"hide"];
  5873. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5874. orderCode = [self translateSingleQuote:orderCode];
  5875. 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];
  5876. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5877. NSString *cid = [self textAtColumn:0 statement:stmt];
  5878. NSString *name = [self textAtColumn:1 statement:stmt];
  5879. NSString *ext = [self textAtColumn:2 statement:stmt];
  5880. NSString *contact = [self textAtColumn:3 statement:stmt];
  5881. NSString *email = [self textAtColumn:4 statement:stmt];
  5882. NSString *fax = [self textAtColumn:5 statement:stmt];
  5883. NSString *phone = [self textAtColumn:6 statement:stmt];
  5884. // count
  5885. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5886. // title
  5887. [dic setValue:@"Return To" forKey:@"title"];
  5888. // hide
  5889. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5890. // choose
  5891. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5892. [choose_dic setValue:@"choose" forKey:@"aname"];
  5893. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5894. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5895. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5896. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5897. @"returnto_contact" : @"sender_contact",
  5898. @"returnto_email" : @"sender_email",
  5899. @"returnto_ext" : @"sender_ext",
  5900. @"returnto_fax" : @"sender_fax",
  5901. @"returnto_name" : @"sender_name",
  5902. @"returnto_phone" : @"sender_phone"},
  5903. @"type" : @"pull"};
  5904. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5905. NSDictionary *select_return_to_dic = @{
  5906. @"aname" : @"Select return to",
  5907. @"name" : @"select_cid",
  5908. @"refresh" : [NSNumber numberWithInteger:0],
  5909. @"value" : @"Contact_Return_To",
  5910. @"key_map" : @{
  5911. @"returnto_cid" : @"customer_cid",
  5912. @"returnto_contact" : @"customer_contact",
  5913. @"returnto_email" : @"customer_email",
  5914. @"returnto_ext" : @"customer_contact_ext",
  5915. @"returnto_fax" : @"customer_fax",
  5916. @"returnto_name" : @"customer_name",
  5917. @"returnto_phone" : @"customer_phone"
  5918. }
  5919. };
  5920. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5921. [dic setValue:choose_dic forKey:@"item_0"];
  5922. // contact id
  5923. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5924. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5925. [contact_id_dic setValue:@"true" forKey:@"required"];
  5926. [dic setValue:contact_id_dic forKey:@"item_1"];
  5927. // company name
  5928. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5929. [dic setValue:company_name_dic forKey:@"item_2"];
  5930. // address
  5931. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5932. [dic setValue:ext_dic forKey:@"item_3"];
  5933. // contact
  5934. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5935. [dic setValue:contact_dic forKey:@"item_4"];
  5936. // phone
  5937. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5938. [dic setValue:phone_dic forKey:@"item_5"];
  5939. // fax
  5940. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5941. [dic setValue:fax_dic forKey:@"item_6"];
  5942. // email
  5943. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5944. [dic setValue:email_dic forKey:@"item_7"];
  5945. }];
  5946. return dic;
  5947. }
  5948. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5949. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5950. orderCode = [self translateSingleQuote:orderCode];
  5951. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5952. __block double TotalCuft = 0;
  5953. __block double TotalWeight = 0;
  5954. __block int TotalCarton = 0;
  5955. __block double payments = 0;
  5956. // setting
  5957. NSDictionary *setting = params[@"setting"];
  5958. NSNumber *hide = setting[@"ModelInformationHide"];
  5959. [dic setValue:hide forKey:@"hide"];
  5960. 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];
  5961. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5962. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5963. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5964. // item id
  5965. int item_id = sqlite3_column_int(stmt, 0);
  5966. // count
  5967. int item_count = sqlite3_column_int(stmt, 1);
  5968. // stockUom
  5969. int stockUom = sqlite3_column_int(stmt, 2);
  5970. // unit price
  5971. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5972. NSString* Price=nil;
  5973. if([str_price isEqualToString:@""])
  5974. {
  5975. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5976. if(price==nil)
  5977. Price=@"No Price.";
  5978. else
  5979. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5980. }
  5981. else
  5982. {
  5983. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5984. }
  5985. // discount
  5986. double discount = sqlite3_column_double(stmt, 4);
  5987. // name
  5988. NSString *name = [self textAtColumn:5 statement:stmt];
  5989. // description
  5990. NSString *description = [self textAtColumn:6 statement:stmt];
  5991. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5992. // line note
  5993. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5994. int avaulability = sqlite3_column_int(stmt, 8);
  5995. // img
  5996. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5997. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5998. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5999. if(combine != nil)
  6000. {
  6001. // int citem=0;
  6002. int bcount=[[combine valueForKey:@"count"] intValue];
  6003. for(int bc=0;bc<bcount;bc++)
  6004. {
  6005. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6006. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6007. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6008. subTotal += uprice * modulus * item_count;
  6009. }
  6010. }
  6011. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6012. [model_dic setValue:@"model" forKey:@"control"];
  6013. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6014. [model_dic setValue:description forKey:@"description"];
  6015. [model_dic setValue:line_note forKey:@"note"];
  6016. [model_dic setValue:img forKey:@"img_url"];
  6017. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6018. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6019. [model_dic setValue:Price forKey:@"unit_price"];
  6020. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6021. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6022. if (combine) {
  6023. [model_dic setValue:combine forKey:@"combine"];
  6024. }
  6025. // well,what under the row is the info for total
  6026. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6027. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6028. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6029. int carton=[bsubtotaljson[@"carton"] intValue];
  6030. TotalCuft += cuft;
  6031. TotalWeight += weight;
  6032. TotalCarton += carton;
  6033. payments += subTotal;
  6034. //---------------------------
  6035. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6036. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6037. }];
  6038. [dic setValue:@"Model Information" forKey:@"title"];
  6039. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6040. return dic;
  6041. }
  6042. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6043. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6044. [dic setValue:@"Remarks Content" forKey:@"title"];
  6045. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6046. // setting
  6047. NSDictionary *setting = params[@"setting"];
  6048. NSNumber *hide = setting[@"RemarksContentHide"];
  6049. [dic setValue:hide forKey:@"hide"];
  6050. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6051. orderCode = [self translateSingleQuote:orderCode];
  6052. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6053. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6054. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6055. int mustCall = sqlite3_column_int(stmt, 1);
  6056. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6057. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6058. NSDictionary *po_dic = @{
  6059. @"aname" : @"PO#",
  6060. @"control" : @"edit",
  6061. @"keyboard" : @"text",
  6062. @"name" : @"poNumber",
  6063. @"value" : poNumber
  6064. };
  6065. NSDictionary *must_call_dic = @{
  6066. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6067. @"control" : @"switch",
  6068. @"name" : @"must_call",
  6069. @"value" : mustCall ? @"true" : @"false"
  6070. };
  6071. NSDictionary *general_notes_dic = @{
  6072. @"aname" : @"General notes",
  6073. @"control" : @"text_view",
  6074. @"keyboard" : @"text",
  6075. @"name" : @"comments",
  6076. @"value" : generalNotes
  6077. };
  6078. NSDictionary *internal_notes_dic = @{
  6079. @"aname" : @"Internal notes",
  6080. @"control" : @"text_view",
  6081. @"keyboard" : @"text",
  6082. @"name" : @"internal_notes",
  6083. @"value" : internalNotes
  6084. };
  6085. [dic setValue:po_dic forKey:@"item_0"];
  6086. [dic setValue:must_call_dic forKey:@"item_1"];
  6087. [dic setValue:general_notes_dic forKey:@"item_2"];
  6088. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6089. }];
  6090. return dic;
  6091. }
  6092. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6093. // params
  6094. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6095. orderCode = [self translateSingleQuote:orderCode];
  6096. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6097. // setting
  6098. NSDictionary *setting = params[@"setting"];
  6099. NSNumber *hide = setting[@"OrderTotalHide"];
  6100. [dic setValue:hide forKey:@"hide"];
  6101. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6102. __block double lift_gate_value = 0;
  6103. __block double handling_fee = 0;
  6104. __block double shipping = 0;
  6105. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6106. int lift_gate = sqlite3_column_int(stmt, 0);
  6107. lift_gate_value = sqlite3_column_double(stmt, 1);
  6108. shipping = sqlite3_column_double(stmt, 2);
  6109. handling_fee = sqlite3_column_double(stmt, 3);
  6110. if (!lift_gate) {
  6111. lift_gate_value = 0;
  6112. }
  6113. }];
  6114. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6115. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6116. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6117. double payments = [[total valueForKey:@"payments"] doubleValue];
  6118. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6119. double totalPrice = payments;
  6120. [dic setValue:@"Order Total" forKey:@"title"];
  6121. NSDictionary *payments_dic = @{
  6122. @"align" : @"right",
  6123. @"aname" : @"Payments/Credits",
  6124. @"control" : @"text",
  6125. @"name" : @"paymentsAndCredits",
  6126. @"type" : @"price",
  6127. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6128. };
  6129. [dic setValue:payments_dic forKey:@"item_0"];
  6130. NSDictionary *handling_fee_dic = @{
  6131. @"align" : @"right",
  6132. @"aname" : @"Handling Fee",
  6133. @"control" : @"text",
  6134. @"name" : @"handling_fee_value",
  6135. @"required" : @"true",
  6136. @"type" : @"price",
  6137. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6138. };
  6139. NSDictionary *shipping_dic = @{
  6140. @"align" : @"right",
  6141. @"aname" : @"Shipping*",
  6142. @"control" : @"text",
  6143. @"name" : @"shipping",
  6144. @"required" : @"true",
  6145. @"type" : @"price",
  6146. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6147. };
  6148. NSDictionary *lift_gate_dic = @{
  6149. @"align" : @"right",
  6150. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6151. @"control" : @"text",
  6152. @"name" : @"lift_gate_value",
  6153. @"required" : @"true",
  6154. @"type" : @"price",
  6155. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6156. };
  6157. int item_count = 1;
  6158. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6159. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6160. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6161. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6162. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6163. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6164. } else {
  6165. }
  6166. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6167. NSDictionary *total_price_dic = @{
  6168. @"align" : @"right",
  6169. @"aname" : @"Total",
  6170. @"control" : @"text",
  6171. @"name" : @"totalPrice",
  6172. @"type" : @"price",
  6173. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6174. };
  6175. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6176. NSDictionary *total_cuft_dic = @{
  6177. @"align" : @"right",
  6178. @"aname" : @"Total Cuft",
  6179. @"control" : @"text",
  6180. @"name" : @"",
  6181. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6182. };
  6183. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6184. NSDictionary *total_weight_dic = @{
  6185. @"align" : @"right",
  6186. @"aname" : @"Total Weight",
  6187. @"control" : @"text",
  6188. @"name" : @"",
  6189. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6190. };
  6191. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6192. NSDictionary *total_carton_dic = @{
  6193. @"align" : @"right",
  6194. @"aname" : @"Total Carton",
  6195. @"control" : @"text",
  6196. @"name" : @"",
  6197. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6198. };
  6199. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6200. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6201. return dic;
  6202. }
  6203. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6204. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6205. orderCode = [self translateSingleQuote:orderCode];
  6206. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6207. // setting
  6208. NSDictionary *setting = params[@"setting"];
  6209. NSNumber *hide = setting[@"SignatureHide"];
  6210. [dic setValue:hide forKey:@"hide"];
  6211. [dic setValue:@"Signature" forKey:@"title"];
  6212. __block NSString *pic_path = @"";
  6213. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6214. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6215. pic_path = [self textAtColumn:0 statement:stmt];
  6216. }];
  6217. NSDictionary *pic_dic = @{
  6218. @"aname" : @"Signature",
  6219. @"avalue" :pic_path,
  6220. @"control" : @"signature",
  6221. @"name" : @"sign_picpath",
  6222. @"value" : pic_path
  6223. };
  6224. [dic setValue:pic_dic forKey:@"item_0"];
  6225. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6226. return dic;
  6227. }
  6228. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6229. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6230. // setting
  6231. NSDictionary *setting = params[@"setting"];
  6232. NSNumber *hide = setting[@"ShippingMethodHide"];
  6233. [dic setValue:hide forKey:@"hide"];
  6234. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6235. orderCode = [self translateSingleQuote:orderCode];
  6236. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6237. __block NSString *logist = @"";
  6238. __block NSString *lift_gate = @"";
  6239. __block int lift_gate_integer = 0;
  6240. __block NSString *logistic_note = @"";
  6241. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6242. logist = [self textAtColumn:0 statement:stmt];
  6243. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6244. logistic_note = [self textAtColumn:2 statement:stmt];
  6245. }];
  6246. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6247. [dic setValue:@"Shipping Method" forKey:@"title"];
  6248. // val_0
  6249. int PERSONAL_PICK_UP_check = 0;
  6250. int USE_MY_CARRIER_check = 0;
  6251. NSString *logistic_note_text = @"";
  6252. int will_call_check = 0;
  6253. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6254. will_call_check = 1;
  6255. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6256. PERSONAL_PICK_UP_check = 1;
  6257. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6258. USE_MY_CARRIER_check = 1;
  6259. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6260. logistic_note = [logistic_note_array firstObject];
  6261. if (logistic_note_array.count > 1) {
  6262. logistic_note_text = [logistic_note_array lastObject];
  6263. }
  6264. }
  6265. }
  6266. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6267. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6268. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6269. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6270. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6271. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6272. NSDictionary *val0_subItem_item0 = @{
  6273. @"aname" : @"Option",
  6274. @"cadedate" : @{
  6275. @"count" : [NSNumber numberWithInteger:2],
  6276. @"val_0" : @{
  6277. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6278. @"refresh" : [NSNumber numberWithInteger:0],
  6279. @"value" : @"PERSONAL PICK UP",
  6280. @"value_id" : @"PERSONAL PICK UP"
  6281. },
  6282. @"val_1" : @{
  6283. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6284. @"refresh" : [NSNumber numberWithInteger:0],
  6285. @"sub_item" : @{
  6286. @"count" : [NSNumber numberWithInteger:1],
  6287. @"item_0" : @{
  6288. @"aname" : @"BOL",
  6289. @"control" : @"edit",
  6290. @"keyboard" : @"text",
  6291. @"name" : @"logist_note_text",
  6292. @"refresh" : [NSNumber numberWithInteger:0],
  6293. @"required" : @"false",
  6294. @"value" : logistic_note_text
  6295. }
  6296. },
  6297. @"value" : @"USE MY CARRIER",
  6298. @"value_id" : @"USE MY CARRIER"
  6299. }
  6300. },
  6301. @"control" : @"enum",
  6302. @"name" : @"logistic_note",
  6303. @"required" : @"true",
  6304. @"single_select" : @"true"
  6305. };
  6306. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6307. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6308. // val_1
  6309. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6310. if([lift_gate isEqualToString:@""]) {
  6311. if (lift_gate_integer == 1) {
  6312. lift_gate = @"true";
  6313. } else {
  6314. lift_gate = @"false";
  6315. }
  6316. }
  6317. int common_carrier_check = 0;
  6318. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6319. common_carrier_check = 1;
  6320. [params setValue:lift_gate forKey:@"lift_gate"];
  6321. }
  6322. NSDictionary *val_1 = @{
  6323. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6324. @"sub_item" : @{
  6325. @"count" : [NSNumber numberWithInteger:1],
  6326. @"item_0" : @{
  6327. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6328. @"control" : @"switch",
  6329. @"name" : @"lift_gate",
  6330. @"refresh" : [NSNumber numberWithInteger:1],
  6331. @"required" : @"true",
  6332. @"value" : lift_gate
  6333. }
  6334. },
  6335. @"value" : @"COMMON CARRIER",
  6336. @"value_id" : @"COMMON CARRIER"
  6337. };
  6338. // cadedate
  6339. NSDictionary *cadedate = @{
  6340. @"count" : [NSNumber numberWithInteger:2],
  6341. @"val_0" : val_0,
  6342. @"val_1" : val_1
  6343. };
  6344. // item_0
  6345. NSMutableDictionary *item_0 = @{
  6346. @"aname" : @"Shipping",
  6347. @"cadedate" : cadedate,
  6348. @"control" : @"enum",
  6349. @"name" : @"logist",
  6350. @"refresh" : [NSNumber numberWithInteger:1],
  6351. @"single_select" : @"true",
  6352. }.mutableCopy;
  6353. NSNumber *required = setting[@"ShippingMethodRequire"];
  6354. if ([required integerValue]) {
  6355. [item_0 setValue:@"true" forKey:@"required"];
  6356. }
  6357. [dic setValue:item_0 forKey:@"item_0"];
  6358. return dic;
  6359. }
  6360. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6361. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6362. orderCode = [self translateSingleQuote:orderCode];
  6363. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6364. __block int submit_as_integer = 0;
  6365. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6366. submit_as_integer = sqlite3_column_int(stmt, 0);
  6367. }];
  6368. int check11 = 0;
  6369. int check10 = 0;
  6370. if (submit_as_integer == 11) {
  6371. check11 = 1;
  6372. }
  6373. if (submit_as_integer == 10) {
  6374. check10 = 1;
  6375. }
  6376. // section_0
  6377. NSMutableDictionary *dic = @{
  6378. @"count" : @(1),
  6379. @"item_0" : @{
  6380. @"aname" : @"Submit Order As",
  6381. @"cadedate" : @{
  6382. @"count" : @(2),
  6383. @"val_0" : @{
  6384. @"check" : [NSNumber numberWithInteger:check11],
  6385. @"value" : @"Sales Order",
  6386. @"value_id" : @(11)
  6387. },
  6388. @"val_1" : @{
  6389. @"check" : [NSNumber numberWithInteger:check10],
  6390. @"value" : @"Quote",
  6391. @"value_id" : @(10)
  6392. }
  6393. },
  6394. @"control" : @"enum",
  6395. @"name" : @"erpOrderStatus",
  6396. @"required" : @"true",
  6397. @"single_select" : @"true"
  6398. },
  6399. @"title" : @"Order Type"
  6400. }.mutableCopy;
  6401. // setting
  6402. NSDictionary *setting = params[@"setting"];
  6403. NSNumber *hide = setting[@"OrderTypeHide"];
  6404. [dic setValue:hide forKey:@"hide"];
  6405. return dic;
  6406. }
  6407. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6408. countryCode = [self translateSingleQuote:countryCode];
  6409. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6410. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6411. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6412. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6413. if (name == NULL) {
  6414. name = "";
  6415. }
  6416. if (code == NULL) {
  6417. code = "";
  6418. }
  6419. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6420. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6421. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6422. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6423. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6424. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6425. }
  6426. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6427. [container setValue:stateDic forKey:key];
  6428. }] mutableCopy];
  6429. // failure 可以不用了,一样的
  6430. if (ret.allKeys.count == 0) {
  6431. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6432. [stateDic setValue:@"Other" forKey:@"value"];
  6433. [stateDic setValue:@"" forKey:@"value_id"];
  6434. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6435. if (state_code && [@"" isEqualToString:state_code]) {
  6436. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6437. }
  6438. NSString *key = [NSString stringWithFormat:@"val_0"];
  6439. [ret setValue:stateDic forKey:key];
  6440. }
  6441. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6442. return ret;
  6443. }
  6444. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6445. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6446. orderCode = [self translateSingleQuote:orderCode];
  6447. 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];
  6448. __block NSString *payType = @"";
  6449. __block NSString *firstName = @"";
  6450. __block NSString *lastName = @"";
  6451. __block NSString *addr1 = @"";
  6452. __block NSString *addr2 = @"";
  6453. __block NSString *zipcode = @"";
  6454. __block NSString *cardType = @"";
  6455. __block NSString *cardNumber = @"";
  6456. __block NSString *securityCode = @"";
  6457. __block NSString *month = @"";
  6458. __block NSString *year = @"";
  6459. __block NSString *city = @"";
  6460. __block NSString *state = @"";
  6461. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6462. payType = [self textAtColumn:0 statement:stmt];
  6463. firstName = [self textAtColumn:1 statement:stmt];
  6464. lastName = [self textAtColumn:2 statement:stmt];
  6465. addr1 = [self textAtColumn:3 statement:stmt];
  6466. addr2 = [self textAtColumn:4 statement:stmt];
  6467. zipcode = [self textAtColumn:5 statement:stmt];
  6468. cardType = [self textAtColumn:6 statement:stmt];
  6469. cardNumber = [self textAtColumn:7 statement:stmt];
  6470. securityCode = [self textAtColumn:8 statement:stmt];
  6471. month = [self textAtColumn:9 statement:stmt];
  6472. year = [self textAtColumn:10 statement:stmt];
  6473. city = [self textAtColumn:11 statement:stmt];
  6474. state = [self textAtColumn:12 statement:stmt];
  6475. }];
  6476. NSString *required = @"true";
  6477. // setting
  6478. NSDictionary *setting = params[@"setting"];
  6479. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6480. if ([requiredNumber integerValue]) {
  6481. required = @"true";
  6482. } else {
  6483. required = @"false";
  6484. }
  6485. // "section_2"
  6486. NSMutableDictionary *dic = @{
  6487. @"count" : @(1),
  6488. @"item_0" : @{
  6489. @"aname" : @"Payment",
  6490. @"required" : required,
  6491. @"cadedate" : @{
  6492. @"count" : @(6),
  6493. @"val_0" : @{
  6494. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6495. @"value" : @"Check",
  6496. @"value_id" : @"Check"
  6497. },
  6498. @"val_1" : @{
  6499. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6500. @"value" : @"Cash",
  6501. @"value_id" : @"Cash"
  6502. },
  6503. @"val_2" : @{
  6504. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6505. @"value" : @"NET 30",
  6506. @"value_id" : @"NET 30"
  6507. },
  6508. @"val_3" : @{
  6509. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6510. @"value" : @"Wire Transfer",
  6511. @"value_id" : @"Wire Transfer"
  6512. },
  6513. @"val_4" : @{
  6514. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6515. @"sub_item" : @{
  6516. @"count" : @(3),
  6517. @"item_0" : @{
  6518. @"aname" : @"choose",
  6519. @"control" : @"multi_action",
  6520. @"count" : @(1),
  6521. @"item_0" : @{
  6522. @"aname" : @"Same as customer",
  6523. @"key_map" : @{
  6524. @"credit_card_address1" : @"customer_address1",
  6525. @"credit_card_address2" : @"customer_address2",
  6526. @"credit_card_city" : @"customer_city",
  6527. @"credit_card_first_name" : @"customer_first_name",
  6528. @"credit_card_last_name" : @"customer_last_name",
  6529. @"credit_card_state" : @"customer_state",
  6530. @"credit_card_zipcode" : @"customer_zipcode"
  6531. },
  6532. @"type" : @"pull"
  6533. }
  6534. },
  6535. @"item_1" : @{
  6536. @"aname" : @"",
  6537. @"color" : @"red",
  6538. @"control" : @"text",
  6539. @"name" : @"",
  6540. @"value" : @"USA Credit cards only"
  6541. },
  6542. @"item_2" : @{
  6543. @"aname" : @"Fill",
  6544. @"cadedate" : @{
  6545. @"count" : @(2),
  6546. @"val_0" : @{
  6547. @"check" : @(1),
  6548. @"sub_item" : @{
  6549. @"count" : @(11),
  6550. @"item_0" : @{
  6551. @"aname" : @"Type",
  6552. @"cadedate" : @{
  6553. @"count" : @(2),
  6554. @"val_0" : @{
  6555. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6556. @"value" : @"VISA",
  6557. @"value_id" : @(0)
  6558. },
  6559. @"val_1" : @{
  6560. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6561. @"value" : @"MASTER CARD",
  6562. @"value_id" : @(1)
  6563. }
  6564. },
  6565. @"control" : @"enum",
  6566. @"name" : @"credit_card_type",
  6567. @"required" : @"true",
  6568. @"single_select" : @"true"
  6569. },
  6570. @"item_1" : @{
  6571. @"aname" : @"Number",
  6572. @"control" : @"edit",
  6573. @"keyboard" : @"int",
  6574. @"length" : @"16",
  6575. @"name" : @"credit_card_number",
  6576. @"required" : @"true",
  6577. @"value" : cardNumber
  6578. },
  6579. @"item_10" : @{
  6580. @"aname" : @"State",
  6581. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6582. @"control" : @"enum",
  6583. @"enum" : @"true",
  6584. @"name" : @"credit_card_state",
  6585. @"required" : @"true",
  6586. @"single_select" : @"true"
  6587. },
  6588. @"item_2" : @{
  6589. @"aname" : @"Expiration Date",
  6590. @"control" : @"monthpicker",
  6591. @"name" : @"credit_card_expiration",
  6592. @"required" : @"true",
  6593. @"type" : @"date",
  6594. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6595. },
  6596. @"item_3" : @{
  6597. @"aname" : @"Security Code",
  6598. @"control" : @"edit",
  6599. @"keyboard" : @"int",
  6600. @"length" : @"3",
  6601. @"name" : @"credit_card_security_code",
  6602. @"required" : @"true",
  6603. @"value" : securityCode
  6604. },
  6605. @"item_4" : @{
  6606. @"aname" : @"First Name",
  6607. @"control" : @"edit",
  6608. @"keyboard" : @"text",
  6609. @"name" : @"credit_card_first_name",
  6610. @"required" : @"true",
  6611. @"value" : firstName
  6612. },
  6613. @"item_5" : @{
  6614. @"aname" : @"Last Name",
  6615. @"control" : @"edit",
  6616. @"keyboard" : @"text",
  6617. @"name" : @"credit_card_last_name",
  6618. @"required" : @"true",
  6619. @"value" : lastName
  6620. },
  6621. @"item_6" : @{
  6622. @"aname" : @"Address 1",
  6623. @"control" : @"edit",
  6624. @"keyboard" : @"text",
  6625. @"name" : @"credit_card_address1",
  6626. @"required" : @"true",
  6627. @"value" : addr1
  6628. },
  6629. @"item_7" : @{
  6630. @"aname" : @"Address 2",
  6631. @"control" : @"edit",
  6632. @"keyboard" : @"text",
  6633. @"name" : @"credit_card_address2",
  6634. @"value" : addr2
  6635. },
  6636. @"item_8" : @{
  6637. @"aname" : @"zip code",
  6638. @"control" : @"edit",
  6639. @"keyboard" : @"text",
  6640. @"name" : @"credit_card_zipcode",
  6641. @"required" : @"true",
  6642. @"value" : zipcode
  6643. },
  6644. @"item_9" : @{
  6645. @"aname" : @"City",
  6646. @"control" : @"edit",
  6647. @"keyboard" : @"text",
  6648. @"name" : @"credit_card_city",
  6649. @"required" : @"true",
  6650. @"value" : city
  6651. }
  6652. },
  6653. @"value" : @"Fill Now",
  6654. @"value_id" : @""
  6655. },
  6656. @"val_1" : @{
  6657. @"check" : @(0),
  6658. @"value" : @"Fill Later",
  6659. @"value_id" : @""
  6660. }
  6661. },
  6662. @"control" : @"enum",
  6663. @"name" : @"",
  6664. @"single_select" : @"true"
  6665. }
  6666. },
  6667. @"value" : @"Credit Card",
  6668. @"value_id" : @"Credit Card"
  6669. },
  6670. @"val_5" : @{
  6671. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6672. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6673. @"value_id" : @"FOLLOW EXISTING"
  6674. }
  6675. },
  6676. @"control" : @"enum",
  6677. @"name" : @"paymentType",
  6678. @"single_select" : @"true"
  6679. },
  6680. @"title" : @"Payment Information"
  6681. }.mutableCopy;
  6682. NSNumber *hide = setting[@"PaymentInformationHide"];
  6683. [dic setValue:hide forKey:@"hide"];
  6684. return dic;
  6685. }
  6686. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6687. {
  6688. sqlite3 *db = [iSalesDB get_db];
  6689. // UISetting
  6690. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6691. NSString *cachefolder = [paths objectAtIndex:0];
  6692. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6693. NSData* json =nil;
  6694. json=[NSData dataWithContentsOfFile:img_cache];
  6695. NSError *error=nil;
  6696. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6697. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6698. [params setObject:setting forKey:@"setting"];
  6699. int section_count = 0;
  6700. // params
  6701. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6702. orderCode = [self translateSingleQuote:orderCode];
  6703. // 0 Order Type 1 Shipping Method 2 Payment Information
  6704. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6705. // 0 Order Type
  6706. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6707. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6708. [ret setValue:order_type_dic forKey:key0];
  6709. // 1 Shipping Method
  6710. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6711. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6712. [ret setValue:shipping_method_dic forKey:key1];
  6713. // 2 Payment Information
  6714. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6715. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6716. [ret setValue:payment_info_dic forKey:key2];
  6717. // 3 Customer
  6718. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6719. NSDictionary *customer_dic = [self customerDic:params db:db];
  6720. [ret setValue:customer_dic forKey:key3];
  6721. // 4 Ship To
  6722. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6723. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6724. [ret setValue:ship_to_dic forKey:key4];
  6725. // 5 Ship From
  6726. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6727. if (![shipFromDisable integerValue]) {
  6728. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6729. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6730. [ret setValue:ship_from_dic forKey:key5];
  6731. }
  6732. // 6 Freight Bill To
  6733. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6734. if (![freightBillToDisable integerValue]) {
  6735. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6736. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6737. [ret setValue:freight_bill_to forKey:key6];
  6738. }
  6739. // 7 Merchandise Bill To
  6740. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6741. if (![merchandiseBillToDisable integerValue]) {
  6742. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6743. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6744. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6745. }
  6746. // 8 Return To
  6747. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6748. if (![returnToDisable integerValue]) {
  6749. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6750. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6751. [ret setValue:return_to_dic forKey:key8];
  6752. }
  6753. // 9 Model Information
  6754. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6755. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6756. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6757. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6758. [ret setValue:model_info_dic forKey:key9];
  6759. // 10 Remarks Content
  6760. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6761. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6762. [ret setValue:remarks_content_dic forKey:key10];
  6763. // 11 Order Total
  6764. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6765. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6766. [ret setValue:order_total_dic forKey:key11];
  6767. // 12 Signature
  6768. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6769. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6770. [ret setValue:sign_dic forKey:key12];
  6771. [ret setValue:@(section_count) forKey:@"section_count"];
  6772. [iSalesDB close_db:db];
  6773. return [RAUtils dict2data:ret];
  6774. // return nil;
  6775. }
  6776. #pragma mark addr editor
  6777. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6778. NSMutableDictionary *new_item = [item mutableCopy];
  6779. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6780. return new_item;
  6781. }
  6782. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6783. {
  6784. // "is_subaction" = true;
  6785. // orderCode = MOB1608240002;
  6786. // password = 123456;
  6787. // "subaction_tag" = 1;
  6788. // user = EvanK;
  6789. // {
  6790. // "is_subaction" = true;
  6791. // orderCode = MOB1608240002;
  6792. // password = 123456;
  6793. // "refresh_trigger" = zipcode;
  6794. // "subaction_tag" = 1;
  6795. // user = EvanK;
  6796. // }
  6797. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6798. [ret removeObjectForKey:@"up_params"];
  6799. [ret setObject:@"New Address" forKey:@"title"];
  6800. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6801. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6802. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6803. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6804. [country_dic removeObjectForKey:@"refresh"];
  6805. [country_dic removeObjectForKey:@"restore"];
  6806. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6807. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6808. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6809. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6810. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6811. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6812. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6813. [zip_code_dic removeObjectForKey:@"refresh"];
  6814. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6815. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6816. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6817. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6818. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6819. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6820. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6821. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6822. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6823. // country
  6824. NSString *countryCode = @"US";
  6825. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6826. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6827. // state
  6828. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6829. NSDictionary *tmpDic = @{
  6830. @"value" : @"Other",
  6831. @"value_id" : @"",
  6832. @"check" : [NSNumber numberWithInteger:0]
  6833. };
  6834. for (int i = 0; i < allState.allKeys.count; i++) {
  6835. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6836. NSDictionary *tmp = allState[key];
  6837. [allState setValue:tmpDic forKey:key];
  6838. tmpDic = tmp;
  6839. }
  6840. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6841. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6842. [ret setValue:new_section_0 forKey:@"section_0"];
  6843. return [RAUtils dict2data:ret];
  6844. }
  6845. #pragma mark save addr
  6846. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6847. {
  6848. // NSString *companyName = [self valueInParams:params key:@"company"];
  6849. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6850. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6851. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6852. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6853. // NSString *countryId = [self valueInParams:params key:@"country"];
  6854. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6855. // NSString *city = [self valueInParams:params key:@"city"];
  6856. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6857. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6858. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6859. // NSString *phone = [self valueInParams:params key:@"phone"];
  6860. // NSString *fax = [self valueInParams:params key:@"fax"];
  6861. // NSString *email = [self valueInParams:params key:@"email"];
  6862. return [self offline_saveContact:params update:NO isCustomer:NO];
  6863. }
  6864. #pragma mark cancel order
  6865. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6866. {
  6867. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6868. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6869. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6870. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6871. int ret = [iSalesDB execSql:sql];
  6872. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6873. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6874. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6875. // [dic setValue:@"160409" forKey:@"min_ver"];
  6876. return [RAUtils dict2data:dic];
  6877. }
  6878. #pragma mark sign order
  6879. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6880. {
  6881. //参考 offline_saveBusinesscard
  6882. NSLog(@"sign order params: %@",params);
  6883. // orderCode = MOB1608240002;
  6884. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6885. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6886. orderCode = [self translateSingleQuote:orderCode];
  6887. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6888. picPath = [self translateSingleQuote:picPath];
  6889. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6890. int ret = [iSalesDB execSql:sql];
  6891. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6892. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6893. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6894. // [dic setValue:@"160409" forKey:@"min_ver"];
  6895. return [RAUtils dict2data:dic];
  6896. }
  6897. #pragma mark save order
  6898. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6899. NSString *ret = [self valueInParams:params key:key];
  6900. if (translate) {
  6901. ret = [self translateSingleQuote:ret];
  6902. }
  6903. return ret;
  6904. }
  6905. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6906. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6907. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6908. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6909. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6910. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6911. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6912. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6913. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6914. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6915. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6916. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6917. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6918. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6919. if (![total_price isEqualToString:@""]) {
  6920. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6921. } else {
  6922. total_price = @"";
  6923. }
  6924. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6925. if ([paymentsAndCredits isEqualToString:@""]) {
  6926. paymentsAndCredits = @"";
  6927. } else {
  6928. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6929. }
  6930. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6931. if ([handling_fee_value isEqualToString:@""]) {
  6932. handling_fee_value = @"";
  6933. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6934. handling_fee_value = @"";
  6935. } else {
  6936. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6937. }
  6938. NSString *handling_fee_placeholder = handling_fee_value;
  6939. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6940. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6941. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6942. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6943. NSString *lift_gate_placeholder = @"";
  6944. if ([lift_gate_value isEqualToString:@""]) {
  6945. lift_gate_placeholder = @"";
  6946. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6947. lift_gate_placeholder = @"";
  6948. } else {
  6949. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6950. }
  6951. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6952. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6953. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6954. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6955. 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];
  6956. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6957. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6958. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6959. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6960. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6961. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6962. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6963. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6964. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6965. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6966. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6967. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6968. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6969. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6970. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6971. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6972. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6973. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6974. if (!number_nil) {
  6975. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6976. }
  6977. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6978. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6979. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6980. if (!security_code_nil) {
  6981. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6982. }
  6983. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6984. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6985. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6986. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6987. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6988. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6989. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6990. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6991. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6992. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6993. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6994. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6995. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6996. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6997. NSString *contact_id = customer_cid;
  6998. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6999. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  7000. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7001. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7002. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7003. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7004. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7005. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7006. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7007. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7008. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7009. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7010. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7011. //
  7012. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7013. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7014. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7015. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7016. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7017. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7018. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7019. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7020. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7021. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7022. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7023. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7024. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7025. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7026. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7027. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7028. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7029. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7030. 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];
  7031. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7032. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7033. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7034. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7035. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7036. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7037. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7038. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7039. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7040. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7041. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7042. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7043. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7044. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7045. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7046. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7047. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7048. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7049. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7050. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7051. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7052. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7053. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7054. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7055. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7056. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7057. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7058. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7059. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7060. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7061. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7062. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7063. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7064. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7065. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7066. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7067. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7068. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7069. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7070. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7071. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7072. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7073. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7074. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7075. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7076. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7077. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7078. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7079. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7080. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7081. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7082. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7083. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7084. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7085. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7086. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7087. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7088. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7089. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7090. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7091. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7092. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7093. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7094. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7095. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7096. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7097. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7098. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7099. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7100. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7101. NSString *order_status = @"status = 1,";
  7102. if (submit) {
  7103. order_status = @"status = -11,";
  7104. }
  7105. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7106. NSString *sync_sql = @"";
  7107. if (submit) {
  7108. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7109. [param setValue:sales_rep forKey:@"sales_rep"];
  7110. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7111. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7112. }
  7113. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ 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_sql,so_id];
  7114. DebugLog(@"save order contactSql: %@",contactSql);
  7115. DebugLog(@"save order orderSql: %@",orderSql);
  7116. // int contact_ret = [iSalesDB execSql:contactSql];
  7117. int order_ret = [iSalesDB execSql:orderSql];
  7118. int ret = order_ret;
  7119. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7120. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7121. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7122. // [dic setValue:@"160409" forKey:@"min_ver"];
  7123. [dic setObject:so_id forKey:@"so#"];
  7124. return [RAUtils dict2data:dic];
  7125. }
  7126. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7127. {
  7128. // id foo = nil;
  7129. // NSMutableArray *a = @[].mutableCopy;
  7130. // [a addObject:foo];
  7131. return [self saveorder:param submit:NO];
  7132. }
  7133. #pragma mark add to cart by name
  7134. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7135. {
  7136. NSString *orderCode = [params objectForKey:@"orderCode"];
  7137. NSString *product_name = [params objectForKey:@"product_name"];
  7138. product_name = [self translateSingleQuote:product_name];
  7139. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7140. sqlite3 *db = [iSalesDB get_db];
  7141. __block NSMutableString *product_id_string = [NSMutableString string];
  7142. for (int i = 0; i < product_name_array.count; i++) {
  7143. NSString *name = [product_name_array objectAtIndex:i];
  7144. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7145. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7146. int product_id = sqlite3_column_int(stmt, 0);
  7147. if (i == product_name_array.count - 1) {
  7148. [product_id_string appendFormat:@"%d",product_id];
  7149. } else {
  7150. [product_id_string appendFormat:@"%d,",product_id];
  7151. }
  7152. }];
  7153. }
  7154. NSDictionary *newParams = @{
  7155. @"product_id" : product_id_string,
  7156. @"orderCode" : orderCode
  7157. };
  7158. [iSalesDB close_db:db];
  7159. return [self offline_add2cart:[newParams mutableCopy]];
  7160. }
  7161. #pragma mark reset order
  7162. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7163. {
  7164. UIApplication * app = [UIApplication sharedApplication];
  7165. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7166. appDelegate.disable_trigger=true;
  7167. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7168. appDelegate.disable_trigger=false;
  7169. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7170. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7171. return [RAUtils dict2data:dic];
  7172. }
  7173. #pragma mark submit order
  7174. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7175. {
  7176. return [self saveorder:params submit:YES];
  7177. }
  7178. #pragma mark copy order
  7179. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7180. {
  7181. NSMutableDictionary *ret = @{}.mutableCopy;
  7182. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7183. sqlite3 *db = [iSalesDB get_db];
  7184. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7185. // 首先查看联系人是否active
  7186. 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];
  7187. __block int customer_is_active = 1;
  7188. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7189. customer_is_active = sqlite3_column_int(stmt, 0);
  7190. }];
  7191. if (!customer_is_active) {
  7192. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7193. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7194. [iSalesDB close_db:db];
  7195. return [RAUtils dict2data:ret];
  7196. }
  7197. // new order
  7198. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7199. NSString *new_order_code = [self get_offline_soid:db];
  7200. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7201. user = [self translateSingleQuote:user];
  7202. 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
  7203. __block int result = 1;
  7204. result = [iSalesDB execSql:insert_order_sql db:db];
  7205. if (!result) {
  7206. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7207. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7208. [iSalesDB close_db:db];
  7209. return [RAUtils dict2data:ret];
  7210. }
  7211. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7212. // __block NSString *product_id = @"";
  7213. __weak typeof(self) weakSelf = self;
  7214. 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];
  7215. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7216. int is_active = sqlite3_column_int(stmt, 1);
  7217. if (is_active) {
  7218. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7219. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7220. 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];
  7221. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7222. }
  7223. }];
  7224. // product_id = [product_id substringFromIndex:1];
  7225. //
  7226. // [self offline_add2cart:@{}.mutableCopy];
  7227. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7228. [iSalesDB close_db:db];
  7229. if (result) {
  7230. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7231. } else {
  7232. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7233. }
  7234. [ret setObject:new_order_code forKey:@"so_id"];
  7235. return [RAUtils dict2data:ret];
  7236. }
  7237. #pragma mark move wish list to cart
  7238. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7239. NSString *collectId = params[@"collectId"];
  7240. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7241. __block NSString *product_id = @"";
  7242. __block NSString *qty = @"";
  7243. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7244. int productId = sqlite3_column_int(stmt, 0);
  7245. int item_qty = sqlite3_column_int(stmt, 1);
  7246. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7247. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7248. }];
  7249. product_id = [product_id substringFromIndex:1];
  7250. qty = [qty substringFromIndex:1];
  7251. NSDictionary *newParams = @{
  7252. @"product_id" : product_id,
  7253. @"orderCode" : params[@"orderCode"],
  7254. @"qty" : qty
  7255. };
  7256. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7257. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7258. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7259. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7260. //
  7261. // int ret = [iSalesDB execSql:deleteSQL];
  7262. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7263. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7264. }
  7265. return [RAUtils dict2data:retDic];
  7266. }
  7267. #pragma mark - portfolio
  7268. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7269. int sort = [[params valueForKey:@"sort"] integerValue];
  7270. int offset = [[params valueForKey:@"offset"] integerValue];
  7271. int limit = [[params valueForKey:@"limit"] integerValue];
  7272. NSString *orderBy = @"";
  7273. switch (sort) {
  7274. case 0:{
  7275. orderBy = @"modify_time desc";
  7276. }
  7277. break;
  7278. case 1:{
  7279. orderBy = @"modify_time asc";
  7280. }
  7281. break;
  7282. case 2:{
  7283. orderBy = @"name asc";
  7284. }
  7285. break;
  7286. case 3:{
  7287. orderBy = @"name desc";
  7288. }
  7289. break;
  7290. case 4:{
  7291. orderBy = @"description asc";
  7292. }
  7293. break;
  7294. default:
  7295. break;
  7296. }
  7297. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  7298. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7299. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7300. sqlite3 *db = [iSalesDB get_db];
  7301. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7302. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7303. int product_id = sqlite3_column_int(stmt, 0);
  7304. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7305. NSString *name = [self textAtColumn:1 statement:stmt];
  7306. NSString *description = [self textAtColumn:2 statement:stmt];
  7307. double price = sqlite3_column_double(stmt, 3);
  7308. double discount = sqlite3_column_double(stmt,4);
  7309. int qty = sqlite3_column_int(stmt, 5);
  7310. int percentage = sqlite3_column_int(stmt, 6);
  7311. int item_id = sqlite3_column_int(stmt, 7);
  7312. // int fashion_id = sqlite3_column_int(stmt, 8);
  7313. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7314. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7315. double percent = sqlite3_column_double(stmt, 11);
  7316. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7317. if ([price_null isEqualToString:@"null"]) {
  7318. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7319. }
  7320. NSMutableDictionary *item = @{
  7321. @"linenotes": line_note,
  7322. @"check": @(1),
  7323. @"product_id": product_id_string,
  7324. @"available_qty": @(qty),
  7325. @"available_percent" : @(percent),
  7326. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7327. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7328. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7329. @"img": img_path,
  7330. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7331. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7332. }.mutableCopy;
  7333. if (percentage) {
  7334. [item removeObjectForKey:@"available_qty"];
  7335. } else {
  7336. [item removeObjectForKey:@"available_percent"];
  7337. }
  7338. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7339. if ([qty_null isEqualToString:@"null"]) {
  7340. [item removeObjectForKey:@"available_qty"];
  7341. }
  7342. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7343. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7344. }];
  7345. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7346. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7347. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7348. }
  7349. [iSalesDB close_db:db];
  7350. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7351. [dic setValue:@"" forKey:@"email_content"];
  7352. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7353. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7354. return [RAUtils dict2data:dic];
  7355. }
  7356. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  7357. __block int qty = 0;
  7358. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7359. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7360. qty = sqlite3_column_int(stmt, 0);
  7361. }];
  7362. return qty;
  7363. }
  7364. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7365. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7366. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7367. NSMutableDictionary * values = params[@"replaceValue"];
  7368. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  7369. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7370. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7371. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7372. NSString *pdf_path = @"";
  7373. if (direct) {
  7374. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7375. } else {
  7376. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7377. }
  7378. NSString *create_user = [self valueInParams:params key:@"user"];
  7379. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7380. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7381. // model info
  7382. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7383. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7384. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  7385. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7386. sqlite3 *db = [iSalesDB get_db];
  7387. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7388. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7389. int product_id = sqlite3_column_int(stmt, 0);
  7390. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7391. double price = sqlite3_column_double(stmt, 1);
  7392. double discount = sqlite3_column_double(stmt,2);
  7393. int qty = sqlite3_column_int(stmt, 3);
  7394. int percentage = sqlite3_column_int(stmt, 4);
  7395. int item_id = sqlite3_column_int(stmt, 5);
  7396. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7397. double percent = sqlite3_column_double(stmt, 7);
  7398. NSString *price_null = [self textAtColumn:1 statement:stmt];
  7399. /* if ([price_null isEqualToString:@"null"]) {
  7400. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7401. }
  7402. */
  7403. [product_ids_string appendFormat:@"%@,",product_id_string];
  7404. // Regular Price
  7405. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  7406. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  7407. // QTY
  7408. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7409. if ([qty_null isEqualToString:@"null"]) {
  7410. // 查available
  7411. qty = [self model_QTY:product_id_string db:db];
  7412. }
  7413. if (percentage) {
  7414. qty = qty * percent;
  7415. }
  7416. // Special Price
  7417. if ([price_null isEqualToString:@"null"]) {
  7418. // price = regular price
  7419. price = [regular_price_str doubleValue];
  7420. }
  7421. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  7422. if (![discount_null isEqualToString:@"null"]) {
  7423. price = price * (1 - discount / 100.0);
  7424. }
  7425. NSMutableDictionary *item = @{
  7426. @"linenotes": line_note,
  7427. @"product_id": product_id_string,
  7428. @"available_qty": @(qty),
  7429. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7430. @"regular_price" : regular_price_str,
  7431. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  7432. }.mutableCopy;
  7433. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7434. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7435. }];
  7436. [iSalesDB close_db:db];
  7437. if (product_ids_string.length > 0) {
  7438. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  7439. }
  7440. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7441. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7442. return [RAUtils dict2data:resultDictionary];
  7443. }
  7444. NSString *model_info = [RAUtils dict2string:dic];
  7445. // 创建PDF
  7446. // 在preview情况下保存,则不需要新建了
  7447. if (direct) {
  7448. NSMutableDictionary *tear_sheet_params = params;
  7449. // if (tear_sheet_id) {
  7450. // tear_sheet_params = values;
  7451. // }
  7452. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  7453. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7454. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7455. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7456. resultDictionary = pdfInfo.mutableCopy;
  7457. } else { // 创建PDF失败
  7458. return pdfData;
  7459. }
  7460. } else {
  7461. // pdf_path 就是本地路径
  7462. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7463. }
  7464. // 将文件移动到PDF Cache文件夹
  7465. NSString *newPath = [pdf_path lastPathComponent];
  7466. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7467. NSString *cachefolder = [paths objectAtIndex:0];
  7468. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7469. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7470. NSFileManager *fileManager = [NSFileManager defaultManager];
  7471. NSError *error = nil;
  7472. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7473. if (error) { // 移动文件失败
  7474. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7475. return [RAUtils dict2data:resultDictionary];
  7476. }
  7477. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7478. pdf_path = [newPath lastPathComponent];
  7479. // 保存信息
  7480. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  7481. NSString *off_params = [RAUtils dict2string:params];
  7482. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  7483. pdf_path = [self translateSingleQuote:pdf_path];
  7484. create_user = [self translateSingleQuote:create_user];
  7485. tear_note = [self translateSingleQuote:tear_note];
  7486. tear_name = [self translateSingleQuote:tear_name];
  7487. model_info = [self translateSingleQuote:model_info];
  7488. configureParams = [self translateSingleQuote:configureParams];
  7489. off_params = [self translateSingleQuote:off_params];
  7490. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  7491. if (tear_sheet_id) {
  7492. int _id = [tear_sheet_id integerValue];
  7493. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  7494. }
  7495. int result = [iSalesDB execSql:save_pdf_sql];
  7496. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7497. //
  7498. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7499. if (remove_Item) {
  7500. // portfolioId
  7501. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7502. }
  7503. return [RAUtils dict2data:resultDictionary];
  7504. }
  7505. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7506. return [self offline_savePDF:params direct:YES];
  7507. }
  7508. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7509. return [self offline_savePDF:params direct:NO];
  7510. }
  7511. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7512. int offset = [[params valueForKey:@"offset"] integerValue];
  7513. int limit = [[params valueForKey:@"limit"] integerValue];
  7514. NSString *keyword = [params valueForKey:@"keyWord"];
  7515. NSString *where = @"where is_delete is null or is_delete = 0";
  7516. if (keyword.length) {
  7517. keyword = [self translateSingleQuote:keyword];
  7518. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  7519. }
  7520. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  7521. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7522. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7523. NSString *cachefolder = [paths objectAtIndex:0];
  7524. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7525. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7526. NSString *name = [self textAtColumn:0 statement:stmt];
  7527. NSString *note = [self textAtColumn:1 statement:stmt];
  7528. NSString *time = [self textAtColumn:2 statement:stmt];
  7529. NSString *user = [self textAtColumn:3 statement:stmt];
  7530. NSString *path = [self textAtColumn:4 statement:stmt];
  7531. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7532. NSString *off_params = [self textAtColumn:7 statement:stmt];
  7533. path = [pdfFolder stringByAppendingPathComponent:path];
  7534. BOOL bdir=NO;
  7535. NSFileManager* fileManager = [NSFileManager defaultManager];
  7536. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  7537. {
  7538. //pdf文件不存在
  7539. path=nil;
  7540. }
  7541. time = [self changeDateTimeFormate:time];
  7542. time = [time stringByAppendingString:@" PST"];
  7543. int sheet_id = sqlite3_column_int(stmt, 5);
  7544. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  7545. item[@"tearsheetsId"]=@(sheet_id);
  7546. item[@"pdf_path"]=path;
  7547. item[@"create_time"]=time;
  7548. item[@"create_user"]=user;
  7549. item[@"tear_note"]=note;
  7550. item[@"tear_name"]=name;
  7551. item[@"isLocalFile"]=@"true";
  7552. item[@"model_info"]=model_info;
  7553. item[@"off_params"]=off_params;
  7554. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7555. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7556. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7557. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7558. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7559. }];
  7560. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7561. return [RAUtils dict2data:dic];
  7562. }
  7563. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7564. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7565. // NSString *user = [params objectForKey:@"user"];
  7566. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  7567. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  7568. int result = [iSalesDB execSql:sql];
  7569. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7570. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7571. if (result == RESULT_TRUE) {
  7572. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7573. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  7574. sqlite3 *db = [iSalesDB get_db];
  7575. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  7576. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  7577. [iSalesDB close_db:db];
  7578. }
  7579. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7580. return [RAUtils dict2data:dic];
  7581. }
  7582. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7583. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7584. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7585. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7586. // Regurlar Price
  7587. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  7588. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  7589. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7590. __block long val_count = 0;
  7591. NSString *sql = @"select name,type,order_by from price order by order_by";
  7592. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7593. NSString *name = [self textAtColumn:0 statement:stmt];
  7594. int type = sqlite3_column_int(stmt, 1);
  7595. int order_by = sqlite3_column_int(stmt, 2);
  7596. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7597. NSDictionary *price_dic = @{
  7598. @"value" : name,
  7599. @"value_id" : [NSNumber numberWithInteger:type],
  7600. @"check" : order_by == 0 ? @(1) : @(0)
  7601. };
  7602. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7603. val_count = ++(*count);
  7604. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7605. val_count = 0;
  7606. }];
  7607. [cadedate setObject:@{@"value" : @"None",
  7608. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7609. val_count++;
  7610. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  7611. [price setObject:cadedate forKey:@"cadedate"];
  7612. [section1 setObject:price forKey:@"item_2"];
  7613. [dic setObject:section1 forKey:@"section_1"];
  7614. return [RAUtils dict2data:dic];
  7615. }
  7616. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7617. NSString *product_id = [params objectForKey:@"fashionId"];
  7618. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7619. __block int result = RESULT_TRUE;
  7620. __block int qty = 0;
  7621. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7622. qty = sqlite3_column_int(stmt, 0);
  7623. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7624. result = RESULT_FALSE;
  7625. }];
  7626. NSMutableDictionary *dic = @{
  7627. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7628. @"mode" : @"Regular Mode",
  7629. @"quantity_available" : @(qty),
  7630. @"result" : @(result),
  7631. }.mutableCopy;
  7632. return [RAUtils dict2data:dic];
  7633. }
  7634. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7635. NSString *item_ids = [params objectForKey:@"item_id"];
  7636. NSString *line_notes = [params objectForKey:@"notes"];
  7637. NSString *price_str = [params objectForKey:@"price"];
  7638. NSString *discount_str = [params objectForKey:@"discount"];
  7639. NSString *percent = [params objectForKey:@"available_percent"];
  7640. NSString *qty = [params objectForKey:@"available_qty"];
  7641. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7642. int dot = 0;
  7643. if (line_notes) {
  7644. line_notes = [self translateSingleQuote:line_notes];
  7645. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7646. sql = [sql stringByAppendingString:line_notes];
  7647. dot = 1;
  7648. } else {
  7649. line_notes = @"";
  7650. }
  7651. if (price_str) {
  7652. if (dot) {
  7653. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7654. } else {
  7655. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7656. dot = 1;
  7657. }
  7658. sql = [sql stringByAppendingString:price_str];
  7659. } else {
  7660. price_str = @"";
  7661. }
  7662. if (discount_str) {
  7663. if (dot) {
  7664. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7665. } else {
  7666. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7667. dot = 1;
  7668. }
  7669. sql = [sql stringByAppendingString:discount_str];
  7670. } else {
  7671. discount_str = @"";
  7672. }
  7673. if (percent) {
  7674. if (dot) {
  7675. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7676. } else {
  7677. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7678. dot = 1;
  7679. }
  7680. sql = [sql stringByAppendingString:percent];
  7681. } else {
  7682. percent = @"";
  7683. }
  7684. if (qty) {
  7685. if (dot) {
  7686. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7687. } else {
  7688. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7689. dot = 1;
  7690. }
  7691. sql = [sql stringByAppendingString:qty];
  7692. } else {
  7693. qty = @"";
  7694. }
  7695. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  7696. sql = [sql stringByAppendingString:where];
  7697. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7698. int result = [iSalesDB execSql:sql];
  7699. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7700. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7701. return [RAUtils dict2data:dic];
  7702. }
  7703. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7704. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7705. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7706. sqlite3 *db = [iSalesDB get_db];
  7707. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  7708. int result = [iSalesDB execSql:sql db:db];
  7709. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7710. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7711. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  7712. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7713. [iSalesDB close_db:db];
  7714. return [RAUtils dict2data:dic];
  7715. }
  7716. + (void)offline_removePDFWithName:(NSString *)name {
  7717. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7718. NSString *cachefolder = [paths objectAtIndex:0];
  7719. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7720. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  7721. NSFileManager *fileManager = [NSFileManager defaultManager];
  7722. [fileManager removeItemAtPath:path error:nil];
  7723. }
  7724. + (void)offline_clear_PDFCache {
  7725. NSFileManager *fileManager = [NSFileManager defaultManager];
  7726. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7727. NSString *cachefolder = [paths objectAtIndex:0];
  7728. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7729. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  7730. for (NSString *path in pdf_files) {
  7731. [self offline_removePDFWithName:[path lastPathComponent]];
  7732. }
  7733. }
  7734. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7735. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7736. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7737. NSString *user = [params objectForKey:@"user"];
  7738. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  7739. if (![create_user isEqualToString:user]) {
  7740. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7741. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7742. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7743. return [RAUtils dict2data:dic];
  7744. }
  7745. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7746. __block int is_local = 0;
  7747. __block NSString *path = @"";
  7748. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7749. is_local = sqlite3_column_int(stmt, 0);
  7750. path = [self textAtColumn:1 statement:stmt];
  7751. }];
  7752. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7753. if (is_local == 1) {
  7754. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7755. // 删除文件
  7756. [self offline_removePDFWithName:path];
  7757. }
  7758. int result = [iSalesDB execSql:sql];
  7759. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7760. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7761. return [RAUtils dict2data:dic];
  7762. }
  7763. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  7764. {
  7765. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7766. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7767. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7768. NSString* where=@"1=1";
  7769. if (ver!=nil) {
  7770. where=@"is_dirty=1";
  7771. }
  7772. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  7773. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7774. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7775. sqlite3 *db = [iSalesDB get_db];
  7776. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7777. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7778. NSInteger _id = sqlite3_column_int(stmt, 0);
  7779. NSInteger product_id = sqlite3_column_int(stmt, 1);
  7780. NSString *name = [self textAtColumn:2 statement:stmt];
  7781. NSString *desc = [self textAtColumn:3 statement:stmt];
  7782. NSInteger item_id = sqlite3_column_int(stmt, 4);
  7783. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  7784. NSInteger qty = sqlite3_column_int(stmt, 6);
  7785. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  7786. double percent = sqlite3_column_double(stmt, 8);
  7787. double price = sqlite3_column_double(stmt, 9);
  7788. double discount = sqlite3_column_double(stmt, 10);
  7789. NSString *img = [self textAtColumn:11 statement:stmt];
  7790. NSString *line_note = [self textAtColumn:12 statement:stmt];
  7791. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  7792. NSString *create_time = [self textAtColumn:14 statement:stmt];
  7793. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  7794. NSString *price_null = [self textAtColumn:9 statement:stmt];
  7795. if ([price_null isEqualToString:@"null"]) {
  7796. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7797. }
  7798. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  7799. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  7800. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  7801. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7802. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  7803. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  7804. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  7805. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  7806. if ([qty_null isEqualToString:@"null"]) {
  7807. // [item setValue:@"null" forKey:@"available_qty"];
  7808. } else {
  7809. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  7810. }
  7811. if ([is_percent_null isEqualToString:@"null"]) {
  7812. // [item setValue:@"null" forKey:@"percentage"];
  7813. } else {
  7814. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  7815. }
  7816. if ([percent_null isEqualToString:@"null"]) {
  7817. // [item setValue:@"null" forKey:@"percent"];
  7818. } else {
  7819. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  7820. }
  7821. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  7822. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  7823. [item setValue:name forKey:@"name"];
  7824. [item setValue:desc forKey:@"description"];
  7825. [item setValue:img forKey:@"img"];
  7826. [item setValue:line_note forKey:@"line_note"];
  7827. [item setValue:create_time forKey:@"createtime"];
  7828. [item setValue:modify_time forKey:@"modifytime"];
  7829. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7830. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7831. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7832. }];
  7833. [iSalesDB close_db:db];
  7834. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7835. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7836. }
  7837. return ret;
  7838. }
  7839. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  7840. {
  7841. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7842. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7843. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7844. NSString* where=@"1=1";
  7845. if (ver!=nil) {
  7846. where=@"is_dirty=1";
  7847. }
  7848. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  7849. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7850. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7851. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7852. NSInteger _id = sqlite3_column_int(stmt, 0);
  7853. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  7854. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  7855. NSString *create_user = [self textAtColumn:3 statement:stmt];
  7856. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  7857. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  7858. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7859. NSString *createtime = [self textAtColumn:7 statement:stmt];
  7860. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  7861. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  7862. NSString *off_params = [self textAtColumn:10 statement:stmt];
  7863. NSString *uuid = [NSUUID UUID].UUIDString;
  7864. int is_delete = sqlite3_column_int(stmt, 11);
  7865. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  7866. int is_local = sqlite3_column_int(stmt, 12);
  7867. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7868. if (![tearsheet_id_null isEqualToString:@"null"]) {
  7869. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  7870. }
  7871. [item setObject:pdf_path forKey:@"pdf_path"];
  7872. [item setObject:create_user forKey:@"create_user"];
  7873. [item setObject:tear_note forKey:@"tear_note"];
  7874. [item setObject:tear_name forKey:@"tear_name"];
  7875. [item setObject:model_info forKey:@"model_info"];
  7876. [item setObject:createtime forKey:@"createtime"];
  7877. [item setObject:modifytime forKey:@"modifytime"];
  7878. [item setObject:urlParams forKey:@"urlParams"];
  7879. [item setObject:off_params forKey:@"off_params"];
  7880. [item setObject:uuid forKey:@"pdf_token"];
  7881. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  7882. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  7883. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  7884. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  7885. } else {
  7886. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7887. }
  7888. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7889. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7890. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7891. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7892. }];
  7893. return ret;
  7894. }
  7895. @end