OLDataProvider.m 441 KB

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