OLDataProvider.m 493 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811
  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. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. 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];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // 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
  58. 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];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"];
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. bool show_stockout=[params[@"show_stockout"] boolValue];
  98. NSString *sql = nil;
  99. if(values==nil)
  100. {
  101. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  102. }
  103. else
  104. {
  105. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in from product where product_id in(%@) order by name",params[@"product_ids"]];
  106. }
  107. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  108. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  109. sqlite3 *db = [iSalesDB get_db];
  110. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  111. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  112. int product_id = sqlite3_column_int(stmt, 0);
  113. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  114. NSString *name = [self textAtColumn:1 statement:stmt];
  115. NSString *description = [self textAtColumn:2 statement:stmt];
  116. double price = sqlite3_column_double(stmt, 3);
  117. NSString *s_price = [self textAtColumn:3 statement:stmt];
  118. double discount = sqlite3_column_double(stmt,4);
  119. int qty = sqlite3_column_int(stmt, 5);
  120. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  121. int is_percentage = sqlite3_column_int(stmt, 6);
  122. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  123. int item_id = sqlite3_column_int(stmt, 7);
  124. // int fashion_id = sqlite3_column_int(stmt, 8);
  125. NSString *line_note = [self textAtColumn:9 statement:stmt];
  126. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  127. double percent = sqlite3_column_double(stmt, 10);
  128. // int portfolio_id = sqlite3_column_int(stmt, 11);
  129. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  130. int availability = sqlite3_column_int(stmt, 12);
  131. NSString *color = [self textAtColumn:13 statement:stmt];
  132. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  133. NSString *demension = [self textAtColumn:15 statement:stmt];
  134. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  135. NSString *material = [self textAtColumn:17 statement:stmt];
  136. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  137. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  138. double volume = sqlite3_column_double(stmt, 19);
  139. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  140. double weight = sqlite3_column_double(stmt, 20);
  141. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  142. int model_set = sqlite3_column_int(stmt, 21);
  143. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  144. double load_ability = sqlite3_column_double(stmt, 22);
  145. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  146. NSString *assembling = [self textAtColumn:24 statement:stmt];
  147. NSString *made_in = [self textAtColumn:25 statement:stmt];
  148. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  149. if(is_percentage==0&&qty==0&&!show_stockout)
  150. {
  151. return;
  152. }
  153. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  154. {
  155. return;
  156. }
  157. if(s_percent.length==0||is_percentage==0)
  158. {
  159. percent=100.0;
  160. if([s_qty isEqualToString:@"null"])
  161. qty=availability;
  162. }
  163. else
  164. {
  165. qty=availability;
  166. }
  167. qty=qty*percent/100+0.5;
  168. for(int i=0;i<[values[@"count"] intValue];i++)
  169. {
  170. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  171. if([valueitem[@"product_id"] intValue]==product_id)
  172. {
  173. line_note=valueitem[@"line_note"];
  174. qty=[valueitem[@"available_qty"] intValue];
  175. if([[valueitem allKeys] containsObject:@"available_qty"])
  176. s_qty=[NSString stringWithFormat:@"%d",qty];
  177. else
  178. s_qty=@"null";
  179. if(valueitem[@"regular_price"]!=nil)
  180. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  181. s_price=valueitem[@"special_price"];
  182. price=[valueitem[@"special_price"] floatValue];
  183. discount=0;
  184. }
  185. }
  186. NSString* set_price=@"";
  187. if([params[@"entered_price"] boolValue])
  188. {
  189. if (s_price==nil ) {
  190. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  191. }
  192. else
  193. {
  194. if(price*(1-discount/100.0)!=gprice.floatValue)
  195. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  196. }
  197. }
  198. NSString* get_price=@"";
  199. {
  200. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  201. // DebugLog(@"price time interval");
  202. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  203. if(gprice==nil)
  204. get_price=@"Price:No Price.";
  205. else
  206. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  207. }
  208. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  209. // if ([qty_null isEqualToString:@"null"]) {
  210. // qty=availability;
  211. // }
  212. //
  213. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  214. // if ([qty_percent_null isEqualToString:@"null"]) {
  215. // percentage=1;
  216. // }
  217. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  218. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  219. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  220. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  221. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  222. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  223. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  224. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  225. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  226. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  227. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  228. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  229. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  230. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  231. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  232. if([params[@"availability"] boolValue]==false)
  233. str_availability=@"";
  234. else
  235. {
  236. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  237. str_availability=@"<b>Availability:</b> In Production";
  238. }
  239. if([params[@"color"] boolValue]==false || color.length==0 )
  240. str_color=@"";
  241. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  242. str_model_set=@"";
  243. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  244. str_legcolor=@"";
  245. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  246. str_demension=@"";
  247. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  248. str_seat_height=@"";
  249. if([params[@"material"] boolValue]==false || material.length==0 )
  250. str_meterial=@"";
  251. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  252. str_box_dim=@"";
  253. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  254. str_volume=@"";
  255. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  256. str_weight=@"";
  257. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  258. str_load_ability=@"";
  259. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  260. str_fabric_content=@"";
  261. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  262. str_assembling=@"";
  263. if([params[@"made"] boolValue]==false || made_in.length==0 )
  264. str_made_in=@"";
  265. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  266. str_line_note=@"";
  267. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  268. [arr_detail addObject:str_availability];
  269. [arr_detail addObject:str_color];
  270. [arr_detail addObject:str_model_set];
  271. [arr_detail addObject:str_legcolor];
  272. [arr_detail addObject:str_demension];
  273. [arr_detail addObject:str_seat_height];
  274. [arr_detail addObject:str_meterial];
  275. [arr_detail addObject:str_box_dim];
  276. [arr_detail addObject:str_volume];
  277. [arr_detail addObject:str_weight];
  278. [arr_detail addObject:str_load_ability];
  279. [arr_detail addObject:str_fabric_content];
  280. [arr_detail addObject:str_assembling];
  281. [arr_detail addObject:str_made_in];
  282. [arr_detail addObject:str_line_note];
  283. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  284. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  285. //model image;
  286. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  287. // qr image
  288. NSString* qrpath=nil;
  289. if([params[@"show_barcode"] boolValue])
  290. {
  291. NSString* temp = NSTemporaryDirectory();
  292. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  293. qrpath=[temp stringByAppendingPathComponent:filename];
  294. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  295. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  296. }
  297. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  298. item[@"img"]=img_path;
  299. item[@"img_qr"]=qrpath;
  300. item[@"special_price"]=set_price;
  301. item[@"price"]=get_price;
  302. item[@"name"]=name;
  303. item[@"description"]=description;
  304. item[@"detail"]=detail;
  305. item[@"img"]=img_path;
  306. // @{
  307. // //@"linenotes": line_note,
  308. //// @"product_id": product_id_string,
  309. //// @"available_qty": @(qty),
  310. //// @"available_percent" : @(percent),
  311. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  312. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  313. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"": ,
  320. // @"":
  321. // }.mutableCopy;
  322. // if (percentage) {
  323. // [item removeObjectForKey:@"available_qty"];
  324. // } else {
  325. // [item removeObjectForKey:@"available_percent"];
  326. // }
  327. //
  328. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  329. // if ([qty_null isEqualToString:@"null"]) {
  330. // [item removeObjectForKey:@"available_qty"];
  331. // }
  332. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  333. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  334. }];
  335. [iSalesDB close_db:db];
  336. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  337. // [dic setValue:@"" forKey:@"email_content"];
  338. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  339. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  340. //
  341. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  342. // cell[@"count"]=[NSNumber numberWithInt:10];
  343. grid[@"cell0"]=dic;
  344. data[@"grid0"]=grid;
  345. return data;
  346. }
  347. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  348. {
  349. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  351. NSString *cache_folder=[paths objectAtIndex:0];
  352. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  353. BOOL bdir=NO;
  354. NSFileManager* fileManager = [NSFileManager defaultManager];
  355. if(PDF_DEBUG)
  356. {
  357. NSData *data = [NSData dataWithContentsOfFile:default_path];
  358. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  359. return ret;
  360. }
  361. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  362. {
  363. NSError * error=nil;
  364. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  365. {
  366. return nil;
  367. }
  368. }
  369. NSData *data = [NSData dataWithContentsOfFile:template_path];
  370. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  371. return ret;
  372. }
  373. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  374. {
  375. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  376. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  377. data[@"npd_url"]=@"www.newpacificdirect.com";
  378. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  379. [formatter setDateFormat:@"MM/dd/yyyy"];
  380. NSString* date = [formatter stringFromDate:[NSDate date]];
  381. data[@"print_date"]=date;
  382. data[@"company_name"]=@"New Pacific Direct,Inc.";
  383. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  384. data[@"TOTAL_PAGE"]=0;
  385. data[@"CURRENT_PAGE"]=0;
  386. [formatter setDateFormat:@"MM/dd/yyyy"];
  387. date = [formatter stringFromDate:[NSDate date]];
  388. data[@"date"]=date;
  389. NSString* temp = NSTemporaryDirectory();
  390. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  391. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  392. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  393. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  394. data[@"img_barcode"]=qrpath;
  395. data[@"so_id"]=order_code;
  396. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  397. // 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 so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  398. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus from offline_order where so_id='%@'",order_code];
  399. sqlite3 *db = [iSalesDB get_db];
  400. sqlite3_stmt * statement;
  401. // NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  402. // NSString *nssoid = @"";
  403. // NSString* orderinfo = @"";
  404. // NSString *moreInfo = @"";
  405. // double handlingFee = 0;
  406. // double payments_and_credist = 0;
  407. // double totalPrice = 0;
  408. // double shippingFee = 0;
  409. // double lift_gate = 0;
  410. // NSString *customer_contact = @"";
  411. // NSString *customer_email = @"";
  412. // NSString *customer_fax = @"";
  413. // NSString *customer_phone = @"";
  414. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  415. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  416. row0[@"count"]=[NSNumber numberWithInt:1];
  417. datasource[@"row0"]=row0;
  418. NSString* order_type=@"Sales Order";
  419. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  420. {
  421. if (sqlite3_step(statement) == SQLITE_ROW)
  422. {
  423. int erpOrderStatus = sqlite3_column_int(statement, 0);
  424. if(erpOrderStatus==10)
  425. {
  426. order_type=@"Quoted Order";
  427. }
  428. NSString *sql_cart=[ 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 ",order_code];;
  429. sqlite3_stmt * statement_cart;
  430. // NSDate *date1 = [NSDate date];
  431. int count=0;
  432. int cart_count=0;
  433. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  434. if ( dbresult== SQLITE_OK)
  435. {
  436. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  437. int row1count=0;
  438. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  439. {
  440. NSDate *row_date = [NSDate date];
  441. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  442. // int product_id = sqlite3_column_int(statement_cart, 0);
  443. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  444. int item_id = sqlite3_column_int(statement_cart, 7);
  445. double discount = sqlite3_column_double(statement_cart, 2);
  446. NSString* Price=nil;
  447. if(str_price==nil)
  448. {
  449. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  450. // NSDate *price_date = [NSDate date];
  451. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  452. // DebugLog(@"price time interval");
  453. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  454. if(price==nil)
  455. Price=@"No Price.";
  456. else
  457. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  458. }
  459. else
  460. {
  461. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  462. }
  463. float dunitprice=0;
  464. int item_count = sqlite3_column_int(statement_cart, 3);
  465. NSString* Amount=@"";
  466. if(![Price isEqualToString:@"No Price."])
  467. {
  468. dunitprice=[Price floatValue];
  469. dunitprice=dunitprice* (1-discount/100.0);
  470. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  471. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  472. }
  473. else
  474. {
  475. Price=@"";
  476. }
  477. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  478. // NSString *nsline_note=nil;
  479. // if(line_note!=nil)
  480. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  481. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  482. NSString *nsname=nil;
  483. if(name!=nil)
  484. nsname= [[NSString alloc]initWithUTF8String:name];
  485. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  486. NSString *nsdescription=nil;
  487. if(description!=nil)
  488. nsdescription= [[NSString alloc]initWithUTF8String:description];
  489. // int stockUom = sqlite3_column_int(statement_cart, 8);
  490. // int _id = sqlite3_column_int(statement_cart, 9);
  491. // NSDate *subtotal_date = [NSDate date];
  492. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  493. // DebugLog(@"subtotal_date time interval");
  494. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  495. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  496. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  497. // int carton=[bsubtotaljson[@"carton"] intValue];
  498. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  499. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  500. // NSDate *img_date = [NSDate date];
  501. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  502. // DebugLog(@"img_date time interval");
  503. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  504. itemjson[@"model"]=nsname;
  505. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  506. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  507. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  508. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  509. // itemjson[@"check"]=@"true";
  510. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  511. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  512. itemjson[@"unit_price"]=Price;
  513. itemjson[@"amount"]=Amount;
  514. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  515. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  516. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  517. // itemjson[@"note"]=nsline_note;
  518. // NSDate *date2 = [NSDate date];
  519. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  520. // DebugLog(@"model_bundle time interval");
  521. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  522. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  523. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  524. row1count++;
  525. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false];
  526. for(int c=0;c<[combine[@"count"] intValue];c++)
  527. {
  528. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  529. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  530. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  531. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue]];
  532. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  533. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  534. row1count++;
  535. }
  536. count++;
  537. // DebugLog(@"row time interval");
  538. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  539. }
  540. row1[@"count"]=[NSNumber numberWithInt:row1count];
  541. datasource[@"row1"]=row1;
  542. data[@"table_model"]=datasource;
  543. sqlite3_finalize(statement_cart);
  544. }
  545. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  546. // ret[@"sign_url"] = sign_url;
  547. //
  548. // customer_contact = [self textAtColumn:52 statement:statement];
  549. // customer_email = [self textAtColumn:53 statement:statement];
  550. // customer_phone = [self textAtColumn:54 statement:statement];
  551. // customer_fax = [self textAtColumn:55 statement:statement];
  552. //
  553. //
  554. // int offline_edit=sqlite3_column_int(statement, 56);
  555. //
  556. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  557. //
  558. // char *soid = (char*)sqlite3_column_text(statement, 1);
  559. // if(soid==nil)
  560. // soid= "";
  561. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  562. // // so#
  563. // ret[@"so#"] = nssoid;
  564. //
  565. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  566. // if(poNumber==nil)
  567. // poNumber= "";
  568. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  569. //
  570. //
  571. //
  572. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  573. // if(create_time==nil)
  574. // create_time= "";
  575. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  576. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  577. //
  578. // int status = sqlite3_column_int(statement, 4);
  579. // int erpStatus = sqlite3_column_int(statement, 49);
  580. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  581. //
  582. // // status
  583. // if (status > 1 && status != 3) {
  584. // status = erpStatus;
  585. // } else if (status == 3) {
  586. // status = 15;
  587. // }
  588. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  589. // ret[@"order_status"] = nsstatus;
  590. //
  591. //
  592. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  593. // if(company_name==nil)
  594. // company_name= "";
  595. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  596. // // company name
  597. // ret[@"company_name"] = nscompany_name;
  598. //
  599. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  600. // if(customer_contact==nil)
  601. // customer_contact= "";
  602. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  603. //
  604. //
  605. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  606. // if(addr_1==nil)
  607. // addr_1="";
  608. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  609. //
  610. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  611. // if(addr_2==nil)
  612. // addr_2="";
  613. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  614. //
  615. //
  616. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  617. // if(addr_3==nil)
  618. // addr_3="";
  619. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  620. //
  621. //
  622. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  623. // if(addr_4==nil)
  624. // addr_4="";
  625. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  626. //
  627. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  628. // [arr_addr addObject:nsaddr_1];
  629. // [arr_addr addObject:nsaddr_2];
  630. // [arr_addr addObject:nsaddr_3];
  631. // [arr_addr addObject:nsaddr_4];
  632. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  633. //
  634. //
  635. // char *logist = (char*)sqlite3_column_text(statement, 11);
  636. // if(logist==nil)
  637. // logist= "";
  638. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  639. // if (status == -11 || status == 10 || status == 11) {
  640. // nslogist = [self textAtColumn:59 statement:statement];
  641. // };
  642. //
  643. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  644. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  645. // shipping = @"Shipping To Be Quoted";
  646. // } else {
  647. // shippingFee = sqlite3_column_double(statement, 12);
  648. // }
  649. //
  650. // // Shipping
  651. // ret[@"Shipping"] = shipping;
  652. //
  653. // int have_lift_gate = sqlite3_column_int(statement, 17);
  654. // lift_gate = sqlite3_column_double(statement, 13);
  655. // // Liftgate Fee(No loading dock)
  656. // if (!have_lift_gate) {
  657. // lift_gate = 0;
  658. // }
  659. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  660. // if (sqlite3_column_int(statement, 57)) {
  661. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  662. // }
  663. //
  664. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  665. // if(general_notes==nil)
  666. // general_notes= "";
  667. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  668. //
  669. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  670. // if(internal_notes==nil)
  671. // internal_notes= "";
  672. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  673. //
  674. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  675. // if(payment_type==nil)
  676. // payment_type= "";
  677. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  678. //
  679. //
  680. //
  681. // // order info
  682. // orderinfo = [self textFileName:@"order_info.html"];
  683. //
  684. //
  685. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  686. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  687. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  688. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  689. //
  690. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  691. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  692. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  693. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  694. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  695. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  696. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  697. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  698. //
  699. // NSString *payment = nil;
  700. // if([nspayment_type isEqualToString:@"Credit Card"])
  701. // {
  702. // payment = [self textFileName:@"creditcardpayment.html"];
  703. //
  704. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  705. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  706. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  707. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  708. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  709. // NSString *card_type = [self textAtColumn:42 statement:statement];
  710. // if (card_type.length > 0) { // 显示星号
  711. // card_type = @"****";
  712. // }
  713. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  714. // if (card_number.length > 0 && card_number.length > 4) {
  715. // for (int i = 0; i < card_number.length - 4; i++) {
  716. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  717. // }
  718. // } else {
  719. // card_number = @"";
  720. // }
  721. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  722. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  723. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  724. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  725. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  726. // card_expiration = @"****";
  727. // }
  728. //
  729. //
  730. // NSString *card_city = [self textAtColumn:46 statement:statement];
  731. // NSString *card_state = [self textAtColumn:47 statement:statement];
  732. //
  733. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  734. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  735. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  736. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  737. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  738. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  739. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  740. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  741. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  742. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  743. //
  744. //
  745. // }
  746. // else
  747. // {
  748. // payment=[self textFileName:@"normalpayment.html"];
  749. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  750. // }
  751. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  752. //
  753. // ret[@"result"]= [NSNumber numberWithInt:2];
  754. //
  755. // // more info
  756. // moreInfo = [self textFileName:@"more_info.html"];
  757. //
  758. //
  759. // // ShipToCompany_or_&nbsp
  760. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  761. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  762. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  763. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  764. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  765. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  766. //
  767. //
  768. // // ShipFromCompany_or_&nbsp
  769. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  770. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  771. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  772. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  773. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  774. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  775. //
  776. //
  777. // // FreightBillToCompany_or_&nbsp
  778. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  779. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  780. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  781. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  782. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  783. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  784. //
  785. //
  786. // // MerchandiseBillToCompany_or_&nbsp
  787. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  788. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  789. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  790. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  791. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  792. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  793. //
  794. //
  795. // // ReturnToCompany_or_&nbsp
  796. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  797. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  798. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  799. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  800. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  801. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  802. // //
  803. //
  804. // // DebugLog(@"more info : %@",moreInfo);
  805. //
  806. // // handling fee
  807. // handlingFee = sqlite3_column_double(statement, 33);
  808. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  809. // if (sqlite3_column_int(statement, 58)) {
  810. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  811. // }
  812. // //
  813. //
  814. // // customer info
  815. // customerID = [self textAtColumn:36 statement:statement];
  816. //
  817. // // mode
  818. // ret[@"mode"] = appDelegate.mode;
  819. //
  820. // // model_count
  821. // ret[@"model_count"] = @(0);
  822. }
  823. sqlite3_finalize(statement);
  824. }
  825. [iSalesDB close_db:db];
  826. data[@"order_type"]=order_type;
  827. /*
  828. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  829. // "customer_email" = "Shui Hu";
  830. // "customer_fax" = "";
  831. // "customer_first_name" = F;
  832. // "customer_last_name" = L;
  833. // "customer_name" = ",da He Xiang Dong Liu A";
  834. // "customer_phone" = "Hey Xuan Feng";
  835. contactInfo[@"customer_phone"] = customer_phone;
  836. contactInfo[@"customer_fax"] = customer_fax;
  837. contactInfo[@"customer_email"] = customer_email;
  838. NSString *first_name = @"";
  839. NSString *last_name = @"";
  840. if ([customer_contact isEqualToString:@""]) {
  841. } else if ([customer_contact containsString:@" "]) {
  842. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  843. first_name = [customer_contact substringToIndex:first_space_index];
  844. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  845. }
  846. contactInfo[@"customer_first_name"] = first_name;
  847. contactInfo[@"customer_last_name"] = last_name;
  848. ret[@"customerInfo"] = contactInfo;
  849. // models
  850. if (nssoid) {
  851. __block double TotalCuft = 0;
  852. __block double TotalWeight = 0;
  853. __block int TotalCarton = 0;
  854. __block double allItemPrice = 0;
  855. 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];
  856. sqlite3 *db1 = [iSalesDB get_db];
  857. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  858. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  859. int product_id = sqlite3_column_int(stmt, 0);
  860. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  861. int item_id = sqlite3_column_int(stmt, 7);
  862. NSString* Price=nil;
  863. if(str_price==nil)
  864. {
  865. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  866. if(price==nil)
  867. Price=@"No Price.";
  868. else
  869. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  870. }
  871. else
  872. {
  873. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  874. }
  875. double discount = sqlite3_column_double(stmt, 2);
  876. int item_count = sqlite3_column_int(stmt, 3);
  877. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  878. NSString *nsline_note=nil;
  879. if(line_note!=nil)
  880. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  881. // char *name = (char*)sqlite3_column_text(stmt, 5);
  882. // NSString *nsname = nil;
  883. // if(name!=nil)
  884. // nsname= [[NSString alloc]initWithUTF8String:name];
  885. NSString *nsname = [self textAtColumn:5 statement:stmt];
  886. // char *description = (char*)sqlite3_column_text(stmt, 6);
  887. // NSString *nsdescription=nil;
  888. // if(description!=nil)
  889. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  890. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  891. // int stockUom = sqlite3_column_int(stmt, 8);
  892. // int _id = sqlite3_column_int(stmt, 9);
  893. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  894. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  895. double weight=[bsubtotaljson[@"weight"] doubleValue];
  896. int carton=[bsubtotaljson[@"carton"] intValue];
  897. TotalCuft += cuft;
  898. TotalWeight += weight;
  899. TotalCarton += carton;
  900. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  901. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  902. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  903. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  904. itemjson[@"note"]=nsline_note;
  905. itemjson[@"origin_price"] = Price;
  906. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  907. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  908. itemjson[@"order_item_status"] = @""; // 暂时不处理
  909. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  910. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  911. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  912. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  913. if(itemjson[@"combine"] != nil)
  914. {
  915. // int citem=0;
  916. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  917. for(int bc=0;bc<bcount;bc++)
  918. {
  919. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  920. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  921. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  922. subTotal += uprice * modulus * item_count;
  923. }
  924. }
  925. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  926. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  927. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  928. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  929. allItemPrice += subTotal;
  930. }];
  931. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  932. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  933. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  934. // payments/Credits
  935. // payments_and_credist = sqlite3_column_double(statement, 34);
  936. payments_and_credist = allItemPrice;
  937. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  938. // // total
  939. // totalPrice = sqlite3_column_double(statement, 35);
  940. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  941. } else {
  942. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  943. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  944. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  945. // payments/Credits
  946. payments_and_credist = 0;
  947. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  948. }
  949. // total
  950. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  951. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  952. ret[@"order_info"]= orderinfo;
  953. ret[@"more_order_info"] = moreInfo;
  954. return [RAUtils dict2data:ret];
  955. */
  956. return data;
  957. }
  958. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  959. {
  960. NSString* template_file=nil;
  961. switch([params[@"thumb"] intValue])
  962. {
  963. case 0:
  964. template_file= @"so.json";
  965. break;
  966. case 1:
  967. template_file= @"so_thumb.json";
  968. break;
  969. default:
  970. template_file= @"so.json";
  971. }
  972. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  973. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  974. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  975. DebugLog(@"%@",file);
  976. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  977. [dic setObject:@"Regular Mode" forKey:@"mode"];
  978. if (file) {
  979. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  980. [dic setObject:file forKey:@"pdf_path"];
  981. dic[@"isLocalFile"]=@"true";
  982. return [RAUtils dict2data:dic];
  983. }
  984. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  985. dic[@"isLocalFile"]=@"true";
  986. return [RAUtils dict2data:dic];
  987. }
  988. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  989. {
  990. NSString* template_file=nil;
  991. switch([params[@"pdf_style"] intValue])
  992. {
  993. case 0:
  994. template_file= @"portfolio_2x3.json";
  995. break;
  996. case 1:
  997. template_file= @"portfolio_3x2.json";
  998. break;
  999. default:
  1000. template_file= @"portfolio_3x2.json";
  1001. }
  1002. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1003. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1004. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1005. DebugLog(@"%@",file);
  1006. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1007. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1008. if (file) {
  1009. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1010. [dic setObject:file forKey:@"pdf_path"];
  1011. dic[@"isLocalFile"]=@"true";
  1012. return [RAUtils dict2data:dic];
  1013. }
  1014. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1015. dic[@"isLocalFile"]=@"true";
  1016. return [RAUtils dict2data:dic];
  1017. }
  1018. +(NSString*) get_offline_soid:(sqlite3*)db
  1019. {
  1020. NSString* soid=nil;
  1021. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1022. [formatter setDateFormat:@"yyMMdd"];
  1023. NSString* date = [formatter stringFromDate:[NSDate date]];
  1024. for(int i=1;i<999;i++)
  1025. {
  1026. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1027. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1028. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1029. if(count==0)
  1030. return soid;
  1031. }
  1032. return nil;
  1033. }
  1034. +(NSArray*) enumOfflineOrder
  1035. {
  1036. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1037. sqlite3 *db = [iSalesDB get_db];
  1038. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1039. sqlite3_stmt * statement;
  1040. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1041. if ( dbresult== SQLITE_OK)
  1042. {
  1043. while (sqlite3_step(statement) == SQLITE_ROW)
  1044. {
  1045. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1046. NSString* nsso_id=nil;
  1047. if(so_id!=nil)
  1048. {
  1049. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1050. }
  1051. [ret addObject:nsso_id];
  1052. }
  1053. sqlite3_finalize(statement);
  1054. }
  1055. [iSalesDB close_db:db];
  1056. return ret;
  1057. }
  1058. +(void) uploadFile:(NSString*) file
  1059. {
  1060. NSData* data = [NSData dataWithContentsOfFile: file];
  1061. UIApplication * app = [UIApplication sharedApplication];
  1062. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1063. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1064. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1065. if(appDelegate.user!=nil)
  1066. [params setValue:appDelegate.user forKey:@"user"];
  1067. // if(appDelegate.contact_id!=nil)
  1068. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1069. if(appDelegate.password!=nil)
  1070. [params setValue:appDelegate.password forKey:@"password"];
  1071. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1072. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1073. } error:nil];
  1074. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1075. NSProgress *progress = nil;
  1076. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1077. if (error) {
  1078. NSString* err_msg = [error localizedDescription];
  1079. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1080. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1081. NSLog(@"data string: %@",str);
  1082. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1083. } else {
  1084. NSLog(@"response ");
  1085. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1086. // 再将NSData转为字符串
  1087. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1088. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1089. NSLog(@"data string: %@",jsonStr);
  1090. NSDictionary* json = responseObject;
  1091. if([[json valueForKey:@"result"] intValue]==2)
  1092. {
  1093. // NSString* img_url_down = json[@"img_url_aname"];
  1094. // NSString* img_url_up = json[@"img_url"];
  1095. }
  1096. else
  1097. {
  1098. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1099. }
  1100. }
  1101. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1102. }];
  1103. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1104. //
  1105. [uploadTask resume];
  1106. }
  1107. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1108. {
  1109. NSString* serial= [[NSUUID UUID] UUIDString];
  1110. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1111. NSString *cachefolder = [paths objectAtIndex:0];
  1112. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1113. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1114. NSFileManager* fileManager = [NSFileManager defaultManager];
  1115. BOOL bdir=YES;
  1116. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1117. {
  1118. NSError *error = nil;
  1119. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1120. if(!bsuccess)
  1121. DebugLog(@"Create UPLOAD folder failed");
  1122. }
  1123. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1124. ret[@"contact"]=[self prepareContact:serial];
  1125. ret[@"wishlist"]=[self prepareWishlist:serial];
  1126. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1127. ret[@"portfolio"] = [self preparePortfolio:serial];
  1128. ret[@"view_portfolio"] = [self preparePDF:serial];
  1129. NSString* str= [RAUtils dict2string:ret];
  1130. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1131. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1132. NSError *error=nil;
  1133. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1134. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1135. ZipArchive* zip = [[ZipArchive alloc] init];
  1136. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1137. for(int i=0;i<arr_files.count;i++)
  1138. {
  1139. NSString* file=arr_files[i];
  1140. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1141. }
  1142. if( ![zip CloseZipFile2] )
  1143. {
  1144. zippath = @"";
  1145. }
  1146. ret[@"file"]=zippath;
  1147. return ret;
  1148. }
  1149. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1150. {
  1151. if(filename.length==0)
  1152. return false;
  1153. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1154. bool ret=false;
  1155. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1156. NSString *cachefolder = [paths objectAtIndex:0];
  1157. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1158. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1159. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1160. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1161. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1162. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1163. NSFileManager* fileManager = [NSFileManager defaultManager];
  1164. BOOL bdir=NO;
  1165. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1166. {
  1167. NSError *error = nil;
  1168. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1169. {
  1170. ret=false;
  1171. }
  1172. else
  1173. {
  1174. ret=true;
  1175. }
  1176. }
  1177. return ret;
  1178. }
  1179. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1180. {
  1181. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1182. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1183. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1184. NSString* where=@" 1=1";
  1185. if (ver!=nil) {
  1186. where=@"is_dirty=1";
  1187. }
  1188. sqlite3 *db = [iSalesDB get_db];
  1189. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1190. sqlite3_stmt * statement;
  1191. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1192. int count=0;
  1193. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1194. if ( dbresult== SQLITE_OK)
  1195. {
  1196. while (sqlite3_step(statement) == SQLITE_ROW)
  1197. {
  1198. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1199. int _id = sqlite3_column_int(statement, 0);
  1200. int product_id = sqlite3_column_int(statement, 1);
  1201. int item_id = sqlite3_column_int(statement, 2);
  1202. int qty = sqlite3_column_int(statement, 3);
  1203. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1204. NSString* nscreate_time=nil;
  1205. if(create_time!=nil)
  1206. {
  1207. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1208. }
  1209. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1210. NSString* nsmodify_time=nil;
  1211. if(modify_time!=nil)
  1212. {
  1213. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1214. }
  1215. int is_delete = sqlite3_column_int(statement, 6);
  1216. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1217. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1218. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1219. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1220. itemjson[@"create_time"]=nscreate_time;
  1221. itemjson[@"modify_time"]=nsmodify_time;
  1222. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1223. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1224. count++;
  1225. }
  1226. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1227. sqlite3_finalize(statement);
  1228. }
  1229. ret[@"count"]=[NSNumber numberWithInt:count ];
  1230. [iSalesDB close_db:db];
  1231. return ret;
  1232. }
  1233. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1234. {
  1235. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1236. UIApplication * app = [UIApplication sharedApplication];
  1237. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1238. 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 ];
  1239. // 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 ];
  1240. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1241. DebugLog(@"offline_login sql:%@",sqlQuery);
  1242. sqlite3_stmt * statement;
  1243. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1244. int count=0;
  1245. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1246. if ( dbresult== SQLITE_OK)
  1247. {
  1248. while (sqlite3_step(statement) == SQLITE_ROW)
  1249. {
  1250. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1251. int product_id = sqlite3_column_int(statement, 0);
  1252. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1253. int item_id = sqlite3_column_int(statement, 7);
  1254. NSString* Price=nil;
  1255. if(str_price==nil)
  1256. {
  1257. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1258. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1259. if(price==nil)
  1260. Price=@"No Price.";
  1261. else
  1262. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1263. }
  1264. else
  1265. {
  1266. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1267. }
  1268. double discount = sqlite3_column_double(statement, 2);
  1269. int item_count = sqlite3_column_int(statement, 3);
  1270. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1271. NSString *nsline_note=nil;
  1272. if(line_note!=nil)
  1273. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1274. char *name = (char*)sqlite3_column_text(statement, 5);
  1275. NSString *nsname=nil;
  1276. if(name!=nil)
  1277. nsname= [[NSString alloc]initWithUTF8String:name];
  1278. char *description = (char*)sqlite3_column_text(statement, 6);
  1279. NSString *nsdescription=nil;
  1280. if(description!=nil)
  1281. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1282. // int stockUom = sqlite3_column_int(statement, 8);
  1283. // int _id = sqlite3_column_int(statement, 9);
  1284. //
  1285. //
  1286. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1287. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1288. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1289. // int carton=[bsubtotaljson[@"carton"] intValue];
  1290. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1291. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1292. itemjson[@"model"]=nsname;
  1293. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1294. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1295. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1296. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1297. // itemjson[@"check"]=@"true";
  1298. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1299. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1300. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1301. itemjson[@"unit_price"]=Price;
  1302. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1303. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1304. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1305. itemjson[@"note"]=nsline_note;
  1306. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1307. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1308. count++;
  1309. }
  1310. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1311. sqlite3_finalize(statement);
  1312. }
  1313. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1314. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1315. return ret;
  1316. }
  1317. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1318. {
  1319. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1320. sqlite3 *db = [iSalesDB get_db];
  1321. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1322. // for(int i=0;i<arr_soid.count;i++)
  1323. sqlite3_stmt * statement;
  1324. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1325. //int count=0;
  1326. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1327. if ( dbresult== SQLITE_OK)
  1328. {
  1329. int count=0;
  1330. while (sqlite3_step(statement) == SQLITE_ROW)
  1331. {
  1332. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1333. int _id = sqlite3_column_int(statement, 0);
  1334. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1335. NSString* nssync_data=nil;
  1336. if(sync_data!=nil)
  1337. {
  1338. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1339. }
  1340. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1341. NSString* nsimg_1=nil;
  1342. if(img_1!=nil)
  1343. {
  1344. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1345. [self copy_upImg:serial file:nsimg_1];
  1346. }
  1347. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1348. NSString* nsso_no=nil;
  1349. if(so_no!=nil)
  1350. {
  1351. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1352. }
  1353. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1354. itemjson[@"order_type"]=@"submit order";
  1355. else
  1356. itemjson[@"order_type"]=@"archive order";
  1357. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1358. itemjson[@"json_data"]= nssync_data;
  1359. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1360. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1361. count++;
  1362. }
  1363. ret[@"count"]=[NSNumber numberWithInt:count ];
  1364. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1365. }
  1366. sqlite3_finalize(statement);
  1367. [iSalesDB close_db:db];
  1368. return ret;
  1369. }
  1370. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1371. {
  1372. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1373. sqlite3 *db = [iSalesDB get_db];
  1374. // UIApplication * app = [UIApplication sharedApplication];
  1375. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1376. 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";
  1377. sqlite3_stmt * statement;
  1378. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1379. int count=0;
  1380. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1381. if ( dbresult== SQLITE_OK)
  1382. {
  1383. while (sqlite3_step(statement) == SQLITE_ROW)
  1384. {
  1385. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1386. int _id = sqlite3_column_int(statement, 0);
  1387. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1388. NSString* nsimg_0=nil;
  1389. if(img_0!=nil)
  1390. {
  1391. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1392. [self copy_upImg:serial file:nsimg_0];
  1393. }
  1394. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1395. NSString* nsimg_1=nil;
  1396. if(img_1!=nil)
  1397. {
  1398. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1399. [self copy_upImg:serial file:nsimg_1];
  1400. }
  1401. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1402. NSString* nsimg_2=nil;
  1403. if(img_2!=nil)
  1404. {
  1405. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1406. [self copy_upImg:serial file:nsimg_2];
  1407. }
  1408. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1409. NSString* nssync_data=nil;
  1410. if(sync_data!=nil)
  1411. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1412. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1413. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1414. itemjson[@"json_data"]= nssync_data;
  1415. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1416. count++;
  1417. }
  1418. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1419. sqlite3_finalize(statement);
  1420. }
  1421. ret[@"count"]=[NSNumber numberWithInt:count ];
  1422. [iSalesDB close_db:db];
  1423. return ret;
  1424. }
  1425. +(bool) check_offlinedata
  1426. {
  1427. UIApplication * app = [UIApplication sharedApplication];
  1428. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1429. sqlite3 *db = [iSalesDB get_db];
  1430. NSString * where=@"1=1";
  1431. // if(appDelegate.user!=nil)
  1432. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1433. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1434. [iSalesDB close_db:db];
  1435. if(count==0)
  1436. {
  1437. return false;
  1438. }
  1439. return true;
  1440. //
  1441. //[iSalesDB close_db:db];
  1442. }
  1443. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1444. {
  1445. UIApplication * app = [UIApplication sharedApplication];
  1446. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1447. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1448. sqlite3 *db = [iSalesDB get_db];
  1449. NSString* collectId=params[@"collectId"];
  1450. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1451. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1452. [iSalesDB execSql:sqlQuery db:db];
  1453. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1454. [iSalesDB close_db:db];
  1455. appDelegate.wish_count =count;
  1456. [appDelegate update_count_mark];
  1457. ret[@"result"]= [NSNumber numberWithInt:2];
  1458. return ret;
  1459. }
  1460. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1461. //{
  1462. //
  1463. // UIApplication * app = [UIApplication sharedApplication];
  1464. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1465. //
  1466. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1467. // sqlite3 *db = [iSalesDB get_db];
  1468. // NSString* product_id=params[@"product_id"];
  1469. //
  1470. //
  1471. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1472. //
  1473. //
  1474. // for(int i=0;i<arr.count;i++)
  1475. // {
  1476. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1477. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1478. // if(count==0)
  1479. // {
  1480. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1481. // [iSalesDB execSql:sqlQuery db:db];
  1482. // }
  1483. // }
  1484. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1485. // [iSalesDB close_db:db];
  1486. //
  1487. // appDelegate.wish_count =count;
  1488. //
  1489. // [appDelegate update_count_mark];
  1490. // ret[@"result"]= [NSNumber numberWithInt:2];
  1491. // return ret;
  1492. // //
  1493. // //return ret;
  1494. //}
  1495. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1496. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1497. DebugLog(@"time interval: %lf",interval);
  1498. }
  1499. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1500. {
  1501. UIApplication * app = [UIApplication sharedApplication];
  1502. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1503. int sort = [[params objectForKey:@"sort"] intValue];
  1504. NSString *sort_str = @"";
  1505. switch (sort) {
  1506. case 0:{
  1507. sort_str = @"order by w.modify_time desc";
  1508. }
  1509. break;
  1510. case 1:{
  1511. sort_str = @"order by w.modify_time asc";
  1512. }
  1513. break;
  1514. case 2:{
  1515. sort_str = @"order by m.name asc";
  1516. }
  1517. break;
  1518. case 3:{
  1519. sort_str = @"order by m.name desc";
  1520. }
  1521. break;
  1522. case 4:{
  1523. sort_str = @"order by m.description asc";
  1524. }
  1525. break;
  1526. default:
  1527. break;
  1528. }
  1529. NSString* user = appDelegate.user;
  1530. sqlite3 *db = [iSalesDB get_db];
  1531. // order by w.create_time
  1532. 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];
  1533. // 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];
  1534. sqlite3_stmt * statement;
  1535. NSDate *date1 = [NSDate date];
  1536. NSDate *date2 = nil;
  1537. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1538. int count=0;
  1539. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1540. {
  1541. while (sqlite3_step(statement) == SQLITE_ROW)
  1542. {
  1543. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1544. int product_id = sqlite3_column_double(statement, 0);
  1545. char *description = (char*)sqlite3_column_text(statement, 1);
  1546. if(description==nil)
  1547. description= "";
  1548. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1549. int item_id = sqlite3_column_double(statement, 2);
  1550. NSDate *date_image = [NSDate date];
  1551. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1552. printf("image : ");
  1553. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1554. // char *url = (char*)sqlite3_column_text(statement, 3);
  1555. // if(url==nil)
  1556. // url="";
  1557. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1558. int qty = sqlite3_column_int(statement, 3);
  1559. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1560. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1561. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1562. item[@"description"]= nsdescription;
  1563. item[@"img"]= nsurl;
  1564. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1565. count++;
  1566. }
  1567. printf("total time:");
  1568. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1569. ret[@"count"]= [NSNumber numberWithInt:count];
  1570. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1571. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1572. ret[@"result"]= [NSNumber numberWithInt:2];
  1573. appDelegate.wish_count =count;
  1574. [appDelegate update_count_mark];
  1575. sqlite3_finalize(statement);
  1576. }
  1577. [iSalesDB close_db:db];
  1578. return ret;
  1579. }
  1580. +(NSDictionary*) offline_notimpl
  1581. {
  1582. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1583. ret[@"result"]=@"8";
  1584. ret[@"err_msg"]=@"offline mode does not support this function.";
  1585. return ret;
  1586. }
  1587. +(NSDictionary*) offline_home
  1588. {
  1589. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1590. NSString *cachefolder = [paths objectAtIndex:0];
  1591. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1592. NSData* json =nil;
  1593. json=[NSData dataWithContentsOfFile:img_cache];
  1594. NSError *error=nil;
  1595. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1596. return menu;
  1597. }
  1598. +(NSDictionary*) offline_category_menu
  1599. {
  1600. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1601. NSString *cachefolder = [paths objectAtIndex:0];
  1602. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1603. NSData* json =nil;
  1604. json=[NSData dataWithContentsOfFile:img_cache];
  1605. if(json==nil)
  1606. return nil;
  1607. NSError *error=nil;
  1608. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1609. return menu;
  1610. }
  1611. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1612. {
  1613. NSString* offline_command=params[@"offline_Command"];
  1614. NSDictionary* ret=nil;
  1615. if([offline_command isEqualToString:@"model_NIYMAL"])
  1616. {
  1617. NSString* category = params[@"category"];
  1618. ret = [self refresh_model_NIYMAL:category];
  1619. }
  1620. return ret;
  1621. }
  1622. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1623. {
  1624. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1625. [ret setValue:@"2" forKey:@"result"];
  1626. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1627. sqlite3* db= [iSalesDB get_db];
  1628. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1629. [iSalesDB close_db:db];
  1630. [ret setObject:detail1_section forKey:@"detail_1"];
  1631. return ret;
  1632. }
  1633. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1634. {
  1635. UIApplication * app = [UIApplication sharedApplication];
  1636. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1637. // NSArray* arr1 = [self get_user_all_price_type];
  1638. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1639. // NSSet *set1 = [NSSet setWithArray:arr1];
  1640. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1641. // if(appDelegate.contact_id==nil)
  1642. // set2=[set1 mutableCopy];
  1643. // else
  1644. // [set2 intersectsSet:set1];
  1645. // NSArray *retarr = [set2 allObjects];
  1646. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1647. // sqlite3 *db = [iSalesDB get_db];
  1648. NSString* sqlQuery = nil;
  1649. if(appDelegate.contact_id==nil)
  1650. 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];
  1651. else
  1652. 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];
  1653. sqlite3_stmt * statement;
  1654. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1655. int count=0;
  1656. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1657. {
  1658. while (sqlite3_step(statement) == SQLITE_ROW)
  1659. {
  1660. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1661. char *name = (char*)sqlite3_column_text(statement, 0);
  1662. if(name==nil)
  1663. name="";
  1664. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1665. // double price = sqlite3_column_double(statement, 1);
  1666. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1667. // if(isnull==nil)
  1668. // item[nsname]= @"No Price";
  1669. // else
  1670. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1671. char *price = (char*)sqlite3_column_text(statement, 1);
  1672. if(price!=nil)
  1673. {
  1674. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1675. nsprice=[AESCrypt fastdecrypt:nsprice];
  1676. if(nsprice.length>0)
  1677. {
  1678. double dp= [nsprice doubleValue];
  1679. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1680. }
  1681. }
  1682. else
  1683. {
  1684. item[nsname]= @"No Price";
  1685. }
  1686. // int type= sqlite3_column_int(statement, 2);
  1687. // item[@"type"]=[NSNumber numberWithInt:type ];
  1688. // item[nsname]= nsprice;
  1689. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1690. count++;
  1691. }
  1692. ret[@"count"]= [NSNumber numberWithInt:count];
  1693. sqlite3_finalize(statement);
  1694. }
  1695. // [iSalesDB close_db:db];
  1696. return ret;
  1697. }
  1698. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1699. //{
  1700. // NSArray* arr1 = [self get_user_all_price_type:db];
  1701. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1702. //
  1703. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1704. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1705. // // [set2 intersectsSet:set1];
  1706. // //
  1707. // //
  1708. // // NSArray *retarr = [set2 allObjects];
  1709. //
  1710. // NSString* whereprice=nil;
  1711. // if(contact_id==nil)
  1712. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1713. // else
  1714. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1715. //
  1716. //
  1717. // // sqlite3 *db = [iSalesDB get_db];
  1718. //
  1719. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1720. // sqlite3_stmt * statement;
  1721. //
  1722. //
  1723. // NSNumber* ret = nil;
  1724. // double dprice=DBL_MAX;
  1725. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1726. // {
  1727. //
  1728. //
  1729. // while (sqlite3_step(statement) == SQLITE_ROW)
  1730. // {
  1731. //
  1732. // // double val = sqlite3_column_double(statement, 0);
  1733. // char *price = (char*)sqlite3_column_text(statement, 0);
  1734. // if(price!=nil)
  1735. // {
  1736. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1737. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1738. // if(nsprice.length>0)
  1739. // {
  1740. // double dp= [nsprice doubleValue];
  1741. // if(dp<dprice)
  1742. // dprice=dp;
  1743. // }
  1744. // }
  1745. // }
  1746. //
  1747. //
  1748. //
  1749. //
  1750. // sqlite3_finalize(statement);
  1751. //
  1752. //
  1753. //
  1754. //
  1755. // }
  1756. //
  1757. // // [iSalesDB close_db:db];
  1758. //
  1759. // if(dprice==DBL_MAX)
  1760. // ret= nil;
  1761. // else
  1762. // ret= [NSNumber numberWithDouble:dprice];
  1763. // return ret;
  1764. //}
  1765. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1766. {
  1767. NSArray* arr1 = [self get_user_all_price_type:db];
  1768. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1769. NSString* whereprice=nil;
  1770. if(contact_id==nil)
  1771. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1772. else
  1773. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1774. // sqlite3 *db = [iSalesDB get_db];
  1775. NSString *productIdCondition = @"1 = 1";
  1776. if (product_id) {
  1777. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1778. }
  1779. NSString *itemIdCondition = @"";
  1780. if (item_id) {
  1781. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1782. }
  1783. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1784. sqlite3_stmt * statement;
  1785. NSNumber* ret = nil;
  1786. double dprice=DBL_MAX;
  1787. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1788. {
  1789. while (sqlite3_step(statement) == SQLITE_ROW)
  1790. {
  1791. // double val = sqlite3_column_double(statement, 0);
  1792. char *price = (char*)sqlite3_column_text(statement, 0);
  1793. if(price!=nil)
  1794. {
  1795. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1796. nsprice=[AESCrypt fastdecrypt:nsprice];
  1797. if(nsprice.length>0)
  1798. {
  1799. double dp= [nsprice doubleValue];
  1800. if(dp<dprice)
  1801. dprice=dp;
  1802. }
  1803. }
  1804. }
  1805. sqlite3_finalize(statement);
  1806. }
  1807. // [iSalesDB close_db:db];
  1808. if(dprice==DBL_MAX)
  1809. ret= nil;
  1810. else
  1811. ret= [NSNumber numberWithDouble:dprice];
  1812. return ret;
  1813. }
  1814. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1815. {
  1816. NSArray* ret=nil;
  1817. // sqlite3 *db = [iSalesDB get_db];
  1818. // no customer assigned , use login user contact_id
  1819. UIApplication * app = [UIApplication sharedApplication];
  1820. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1821. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1822. sqlite3_stmt * statement;
  1823. // int count=0;
  1824. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1825. {
  1826. if (sqlite3_step(statement) == SQLITE_ROW)
  1827. {
  1828. char *val = (char*)sqlite3_column_text(statement, 0);
  1829. if(val==nil)
  1830. val="";
  1831. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1832. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1833. }
  1834. sqlite3_finalize(statement);
  1835. }
  1836. // [iSalesDB close_db:db];
  1837. return ret;
  1838. }
  1839. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1840. {
  1841. // sqlite3 *db = [iSalesDB get_db];
  1842. if(contact_id==nil)
  1843. {
  1844. // no customer assigned , use login user contact_id
  1845. UIApplication * app = [UIApplication sharedApplication];
  1846. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1847. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1848. sqlite3_stmt * statement;
  1849. // int count=0;
  1850. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1851. {
  1852. if (sqlite3_step(statement) == SQLITE_ROW)
  1853. {
  1854. char *val = (char*)sqlite3_column_text(statement, 0);
  1855. if(val==nil)
  1856. val="";
  1857. contact_id = [[NSString alloc]initWithUTF8String:val];
  1858. }
  1859. sqlite3_finalize(statement);
  1860. }
  1861. if(contact_id.length<=0)
  1862. {
  1863. // [iSalesDB close_db:db];
  1864. return nil;
  1865. }
  1866. }
  1867. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1868. sqlite3_stmt * statement;
  1869. NSArray* ret=nil;
  1870. // int count=0;
  1871. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1872. {
  1873. if (sqlite3_step(statement) == SQLITE_ROW)
  1874. {
  1875. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1876. if(price_type==nil)
  1877. price_type="";
  1878. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1879. if(nsprice_type.length>0)
  1880. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1881. else
  1882. ret=nil;
  1883. }
  1884. sqlite3_finalize(statement);
  1885. }
  1886. // [iSalesDB close_db:db];
  1887. return ret;
  1888. }
  1889. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1890. {
  1891. NSString* ret= nil;
  1892. // sqlite3 *db = [iSalesDB get_db];
  1893. NSString *sqlQuery = nil;
  1894. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1895. // 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;
  1896. // 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
  1897. if(product_id==nil && model_name)
  1898. 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;
  1899. else if (product_id)
  1900. 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
  1901. sqlite3_stmt * statement;
  1902. // int count=0;
  1903. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1904. {
  1905. if (sqlite3_step(statement) == SQLITE_ROW)
  1906. {
  1907. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1908. if(imgurl==nil)
  1909. imgurl="";
  1910. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1911. ret=nsimgurl;
  1912. }
  1913. sqlite3_finalize(statement);
  1914. }
  1915. else
  1916. {
  1917. [ret setValue:@"8" forKey:@"result"];
  1918. }
  1919. // [iSalesDB close_db:db];
  1920. DebugLog(@"data string: %@",ret );
  1921. return ret;
  1922. }
  1923. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1924. {
  1925. NSString* ret= nil;
  1926. sqlite3 *db = [iSalesDB get_db];
  1927. NSString *sqlQuery = nil;
  1928. if(product_id==nil)
  1929. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1930. else
  1931. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1932. sqlite3_stmt * statement;
  1933. // int count=0;
  1934. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1935. {
  1936. if (sqlite3_step(statement) == SQLITE_ROW)
  1937. {
  1938. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1939. if(default_category==nil)
  1940. default_category="";
  1941. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1942. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1943. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1944. ret=nsdefault_category;
  1945. }
  1946. sqlite3_finalize(statement);
  1947. }
  1948. else
  1949. {
  1950. [ret setValue:@"8" forKey:@"result"];
  1951. }
  1952. [iSalesDB close_db:db];
  1953. DebugLog(@"data string: %@",ret );
  1954. return ret;
  1955. }
  1956. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1957. {
  1958. NSString* model_name = [params valueForKey:@"product_name"];
  1959. NSString* product_id = [params valueForKey:@"product_id"];
  1960. NSString* category = [params valueForKey:@"category"];
  1961. if(category==nil)
  1962. category = [self model_default_category:product_id model_name:model_name];
  1963. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1964. sqlite3 *db = [iSalesDB get_db];
  1965. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1966. NSString *sqlQuery = nil;
  1967. if(product_id==nil)
  1968. 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='%@';
  1969. else
  1970. 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=%@;
  1971. sqlite3_stmt * statement;
  1972. [ret setValue:@"2" forKey:@"result"];
  1973. [ret setValue:@"3" forKey:@"detail_section_count"];
  1974. // int count=0;
  1975. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1976. {
  1977. if (sqlite3_step(statement) == SQLITE_ROW)
  1978. {
  1979. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1980. char *name = (char*)sqlite3_column_text(statement, 0);
  1981. if(name==nil)
  1982. name="";
  1983. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1984. char *description = (char*)sqlite3_column_text(statement, 1);
  1985. if(description==nil)
  1986. description="";
  1987. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1988. int product_id = sqlite3_column_int(statement, 2);
  1989. char *color = (char*)sqlite3_column_text(statement, 3);
  1990. if(color==nil)
  1991. color="";
  1992. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1993. //
  1994. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1995. // if(legcolor==nil)
  1996. // legcolor="";
  1997. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1998. //
  1999. //
  2000. int availability = sqlite3_column_int(statement, 5);
  2001. //
  2002. int incoming_stock = sqlite3_column_int(statement, 6);
  2003. char *demension = (char*)sqlite3_column_text(statement, 7);
  2004. if(demension==nil)
  2005. demension="";
  2006. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2007. // ,,,,,,,,,
  2008. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2009. if(seat_height==nil)
  2010. seat_height="";
  2011. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2012. char *material = (char*)sqlite3_column_text(statement, 9);
  2013. if(material==nil)
  2014. material="";
  2015. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2016. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2017. if(box_dim==nil)
  2018. box_dim="";
  2019. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2020. char *volume = (char*)sqlite3_column_text(statement, 11);
  2021. if(volume==nil)
  2022. volume="";
  2023. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2024. double weight = sqlite3_column_double(statement, 12);
  2025. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2026. if(model_set==nil)
  2027. model_set="";
  2028. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2029. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2030. if(load_ability==nil)
  2031. load_ability="";
  2032. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2033. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2034. if(default_category==nil)
  2035. default_category="";
  2036. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2037. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2038. if(fabric_content==nil)
  2039. fabric_content="";
  2040. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2041. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2042. if(assembling==nil)
  2043. assembling="";
  2044. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2045. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2046. if(made_in==nil)
  2047. made_in="";
  2048. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2049. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2050. if(special_remarks==nil)
  2051. special_remarks="";
  2052. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2053. int stockUcom = sqlite3_column_double(statement, 20);
  2054. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2055. if(product_group==nil)
  2056. product_group="";
  2057. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2058. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2059. // if(fashion_selector==nil)
  2060. // fashion_selector="";
  2061. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2062. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2063. if(selector_field==nil)
  2064. selector_field="";
  2065. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2066. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2067. if(property_field==nil)
  2068. property_field="";
  2069. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2070. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2071. if(packaging==nil)
  2072. packaging="";
  2073. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2074. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2075. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2076. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2077. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2078. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2079. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2080. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2081. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2082. NSString* Availability=nil;
  2083. if(availability>0)
  2084. Availability=[NSString stringWithFormat:@"%d",availability];
  2085. else
  2086. Availability = @"In Production";
  2087. [img_section setValue:Availability forKey:@"Availability"];
  2088. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2089. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2090. char *eta = (char*)sqlite3_column_text(statement, 25);
  2091. if(eta==nil)
  2092. eta="";
  2093. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2094. [img_section setValue:nseta forKey:@"ETA"];
  2095. int item_id = sqlite3_column_int(statement, 26);
  2096. NSString* Price=nil;
  2097. if(appDelegate.bLogin==false)
  2098. Price=@"Must Sign in.";
  2099. else
  2100. {
  2101. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2102. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2103. if(price==nil)
  2104. Price=@"No Price.";
  2105. else
  2106. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2107. }
  2108. [img_section setValue:Price forKey:@"price"];
  2109. [img_section setValue:nsname forKey:@"model_name"];
  2110. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2111. [ret setObject:img_section forKey:@"img_section"];
  2112. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2113. int detail0_item_count=0;
  2114. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2115. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2116. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2117. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2118. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2119. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2120. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2121. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2122. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2123. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2124. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2125. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2126. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2127. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2128. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2129. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2130. {
  2131. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2132. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2133. }
  2134. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2135. [detail0_section setValue:@"kv" forKey:@"type"];
  2136. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2137. [ret setObject:detail0_section forKey:@"detail_0"];
  2138. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2139. // [detail1_section setValue:@"detail" forKey:@"target"];
  2140. // [detail1_section setValue:@"popup" forKey:@"action"];
  2141. // [detail1_section setValue:@"content" forKey:@"type"];
  2142. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2143. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2144. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2145. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2146. [ret setObject:detail1_section forKey:@"detail_1"];
  2147. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2148. [detail2_section setValue:@"detail" forKey:@"target"];
  2149. [detail2_section setValue:@"popup" forKey:@"action"];
  2150. [detail2_section setValue:@"content" forKey:@"type"];
  2151. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2152. [detail2_section setValue:@"true" forKey:@"single_row"];
  2153. [detail2_section setValue:@"local" forKey:@"data"];
  2154. [ret setObject:detail2_section forKey:@"detail_2"];
  2155. }
  2156. sqlite3_finalize(statement);
  2157. }
  2158. else
  2159. {
  2160. [ret setValue:@"8" forKey:@"result"];
  2161. }
  2162. NSLog(@"count:%d",count);
  2163. [iSalesDB close_db:db];
  2164. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2165. return ret;
  2166. }
  2167. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2168. NSString* orderCode = [params valueForKey:@"orderCode"];
  2169. NSString* keyword = [params valueForKey:@"keyword"];
  2170. keyword=keyword.lowercaseString;
  2171. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2172. int limit = [[params valueForKey:@"limit"] intValue];
  2173. int offset = [[params valueForKey:@"offset"] intValue];
  2174. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2175. NSString *limit_str = @"";
  2176. if (limited) {
  2177. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2178. }
  2179. sqlite3 *db = [iSalesDB get_db];
  2180. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2181. UIApplication * app = [UIApplication sharedApplication];
  2182. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2183. NSString *sqlQuery = nil;
  2184. if(exactMatch )
  2185. 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 ;
  2186. else
  2187. 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
  2188. DebugLog(@"offline_search sql:%@",sqlQuery);
  2189. sqlite3_stmt * statement;
  2190. [ret setValue:@"2" forKey:@"result"];
  2191. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2192. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2193. // int count=0;
  2194. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2195. {
  2196. int i=0;
  2197. while (sqlite3_step(statement) == SQLITE_ROW)
  2198. {
  2199. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2200. // char *name = (char*)sqlite3_column_text(statement, 1);
  2201. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2202. char *name = (char*)sqlite3_column_text(statement, 0);
  2203. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2204. char *description = (char*)sqlite3_column_text(statement, 1);
  2205. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2206. int product_id = sqlite3_column_int(statement, 2);
  2207. // char *url = (char*)sqlite3_column_text(statement, 3);
  2208. // if(url==nil)
  2209. // url="";
  2210. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2211. int wid = sqlite3_column_int(statement, 3);
  2212. int closeout = sqlite3_column_int(statement, 4);
  2213. int cid = sqlite3_column_int(statement, 5);
  2214. int wisdelete = sqlite3_column_int(statement, 6);
  2215. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2216. if(wid !=0 && wisdelete != 1)
  2217. [item setValue:@"true" forKey:@"wish_exists"];
  2218. else
  2219. [item setValue:@"false" forKey:@"wish_exists"];
  2220. if(closeout==0)
  2221. [item setValue:@"false" forKey:@"is_closeout"];
  2222. else
  2223. [item setValue:@"true" forKey:@"is_closeout"];
  2224. if(cid==0)
  2225. [item setValue:@"false" forKey:@"cart_exists"];
  2226. else
  2227. [item setValue:@"true" forKey:@"cart_exists"];
  2228. [item addEntriesFromDictionary:imgjson];
  2229. // [item setValue:nsurl forKey:@"img"];
  2230. [item setValue:nsname forKey:@"fash_name"];
  2231. [item setValue:nsdescription forKey:@"description"];
  2232. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2233. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2234. i++;
  2235. }
  2236. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2237. [ret setObject:items forKey:@"items"];
  2238. sqlite3_finalize(statement);
  2239. }
  2240. NSLog(@"count:%d",count);
  2241. [iSalesDB close_db:db];
  2242. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2243. return ret;
  2244. }
  2245. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2246. {
  2247. return [self search:params limited:YES];
  2248. }
  2249. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2250. {
  2251. UIApplication * app = [UIApplication sharedApplication];
  2252. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2253. appDelegate.disable_trigger=true;
  2254. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2255. appDelegate.disable_trigger=false;
  2256. //
  2257. // NSString* user = [params valueForKey:@"user"];
  2258. //
  2259. // NSString* password = [params valueForKey:@"password"];
  2260. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2261. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2262. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2263. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2264. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2265. [appDelegate update_count_mark];
  2266. appDelegate.can_show_price =false;
  2267. appDelegate.can_see_price =false;
  2268. appDelegate.can_create_portfolio =false;
  2269. appDelegate.can_create_order =false;
  2270. appDelegate.can_cancel_order =false;
  2271. appDelegate.can_set_cart_price =false;
  2272. appDelegate.can_delete_order =false;
  2273. appDelegate.can_submit_order =false;
  2274. appDelegate.can_set_tearsheet_price =false;
  2275. appDelegate.can_update_contact_info = false;
  2276. appDelegate.save_order_logout = false;
  2277. appDelegate.submit_order_logout = false;
  2278. appDelegate.alert_sold_in_quantities = false;
  2279. appDelegate.ipad_perm =nil ;
  2280. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2281. appDelegate.OrderFilter= nil;
  2282. [appDelegate SetSo:nil];
  2283. [appDelegate set_main_button_panel];
  2284. // sqlite3 *db = [iSalesDB get_db];
  2285. //
  2286. //
  2287. //
  2288. //
  2289. //
  2290. // 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"]];
  2291. //
  2292. //
  2293. //
  2294. //
  2295. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2296. // sqlite3_stmt * statement;
  2297. //
  2298. //
  2299. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2300. //
  2301. //
  2302. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2303. // {
  2304. //
  2305. //
  2306. // if (sqlite3_step(statement) == SQLITE_ROW)
  2307. // {
  2308. //
  2309. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2310. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2311. //
  2312. //
  2313. //
  2314. // int can_show_price = sqlite3_column_int(statement, 0);
  2315. // int can_see_price = sqlite3_column_int(statement, 1);
  2316. //
  2317. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2318. // if(contact_id==nil)
  2319. // contact_id="";
  2320. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2321. //
  2322. // int user_type = sqlite3_column_int(statement, 3);
  2323. //
  2324. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2325. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2326. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2327. // int can_delete_order = sqlite3_column_int(statement, 7);
  2328. // int can_submit_order = sqlite3_column_int(statement, 8);
  2329. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2330. // int can_create_order = sqlite3_column_int(statement, 10);
  2331. //
  2332. //
  2333. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2334. // if(mode==nil)
  2335. // mode="";
  2336. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2337. //
  2338. //
  2339. // char *username = (char*)sqlite3_column_text(statement, 12);
  2340. // if(username==nil)
  2341. // username="";
  2342. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2343. //
  2344. //
  2345. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2346. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2347. // [header setValue:nscontact_id forKey:@"contact_id"];
  2348. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2349. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2350. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2351. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2352. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2353. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2354. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2355. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2356. //
  2357. // [header setValue:nsusername forKey:@"username"];
  2358. //
  2359. //
  2360. // [ret setObject:header forKey:@"header"];
  2361. // [ret setValue:nsmode forKey:@"mode"];
  2362. //
  2363. //
  2364. // }
  2365. //
  2366. //
  2367. //
  2368. // sqlite3_finalize(statement);
  2369. // }
  2370. //
  2371. //
  2372. //
  2373. // [iSalesDB close_db:db];
  2374. //
  2375. //
  2376. //
  2377. //
  2378. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2379. return ret;
  2380. }
  2381. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2382. {
  2383. UIApplication * app = [UIApplication sharedApplication];
  2384. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2385. appDelegate.disable_trigger=true;
  2386. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2387. appDelegate.disable_trigger=false;
  2388. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2389. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2390. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2391. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2392. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2393. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2394. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2395. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2396. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2397. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2398. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2399. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2400. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2401. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2402. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2403. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2404. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2405. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2406. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2407. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2408. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2409. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2410. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2411. [arr_name addObject:customer_first_name];
  2412. [arr_name addObject:customer_last_name];
  2413. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2414. // default ship from
  2415. 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';";
  2416. __block NSString *cid = @"";
  2417. __block NSString *name = @"";
  2418. __block NSString *ext = @"";
  2419. __block NSString *contact = @"";
  2420. __block NSString *email = @"";
  2421. __block NSString *fax = @"";
  2422. __block NSString *phone = @"";
  2423. sqlite3 *db = [iSalesDB get_db];
  2424. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2425. cid = [self textAtColumn:0 statement:statment];
  2426. name = [self textAtColumn:1 statement:statment];
  2427. ext = [self textAtColumn:2 statement:statment];
  2428. contact = [self textAtColumn:3 statement:statment];
  2429. email = [self textAtColumn:4 statement:statment];
  2430. fax = [self textAtColumn:5 statement:statment];
  2431. phone = [self textAtColumn:6 statement:statment];
  2432. }];
  2433. NSString* so_id = [self get_offline_soid:db];
  2434. if(so_id==nil)
  2435. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2436. 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];
  2437. int result =[iSalesDB execSql:sql_neworder db:db];
  2438. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2439. //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'
  2440. //soId
  2441. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2442. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2443. sqlite3_stmt * statement;
  2444. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2445. {
  2446. if (sqlite3_step(statement) == SQLITE_ROW)
  2447. {
  2448. // char *name = (char*)sqlite3_column_text(statement, 1);
  2449. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2450. //ret = sqlite3_column_int(statement, 0);
  2451. char *soId = (char*)sqlite3_column_text(statement, 0);
  2452. if(soId==nil)
  2453. soId="";
  2454. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2455. [ret setValue:nssoId forKey:@"soId"];
  2456. [ret setValue:nssoId forKey:@"orderCode"];
  2457. }
  2458. sqlite3_finalize(statement);
  2459. }
  2460. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2461. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2462. [iSalesDB close_db:db];
  2463. return [RAUtils dict2data:ret];
  2464. }
  2465. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2466. {
  2467. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2468. NSString* orderCode = [params valueForKey:@"orderCode"];
  2469. UIApplication * app = [UIApplication sharedApplication];
  2470. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2471. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2472. {
  2473. appDelegate.disable_trigger=true;
  2474. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2475. appDelegate.disable_trigger=false;
  2476. }
  2477. sqlite3 *db = [iSalesDB get_db];
  2478. int cart_count=[self query_ordercartcount:orderCode db:db];
  2479. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2480. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2481. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2482. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2483. [iSalesDB close_db:db];
  2484. return [RAUtils dict2data:ret];
  2485. }
  2486. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2487. {
  2488. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2489. sqlite3 *db = [iSalesDB get_db];
  2490. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2491. int count =0;
  2492. if(params[@"count"]!=nil)
  2493. {
  2494. count = [params[@"count"] intValue];
  2495. }
  2496. NSString* product_id=params[@"product_id"];
  2497. NSString* orderCode=params[@"orderCode"];
  2498. NSString *qty = params[@"qty"];
  2499. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2500. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2501. for(int i=0;i<arr_id.count;i++)
  2502. {
  2503. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2504. int item_qty= count;
  2505. if (qty) {
  2506. item_qty = [qty_arr[i] integerValue];
  2507. }
  2508. if(item_qty==0)
  2509. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2510. NSString* sql=nil;
  2511. sqlite3_stmt *stmt;
  2512. if(_id<0)
  2513. {
  2514. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2515. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2516. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2517. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2518. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2519. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2520. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2521. }
  2522. else
  2523. {
  2524. if (qty) {
  2525. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2526. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2527. sqlite3_bind_int(stmt, 1, _id);
  2528. } else {
  2529. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2530. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2531. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2532. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2533. }
  2534. }
  2535. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2536. [iSalesDB execSql:@"ROLLBACK" db:db];
  2537. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2538. [iSalesDB close_db:db];
  2539. DebugLog(@"add to cart error");
  2540. return [RAUtils dict2data:ret];
  2541. }
  2542. }
  2543. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2544. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2545. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2546. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2547. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2548. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2549. [iSalesDB close_db:db];
  2550. return [RAUtils dict2data:ret];
  2551. }
  2552. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  2553. {
  2554. UIApplication * app = [UIApplication sharedApplication];
  2555. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2556. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2557. 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];
  2558. sqlite3_stmt * statement;
  2559. int count=0;
  2560. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2561. {
  2562. while (sqlite3_step(statement) == SQLITE_ROW)
  2563. {
  2564. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2565. int bitem_id = sqlite3_column_int(statement, 0);
  2566. int bitem_qty = sqlite3_column_int(statement, 1);
  2567. char *name = (char*)sqlite3_column_text(statement, 2);
  2568. if(name==nil)
  2569. name="";
  2570. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2571. char *description = (char*)sqlite3_column_text(statement, 3);
  2572. if(description==nil)
  2573. description="";
  2574. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2575. double unit_price = sqlite3_column_double(statement, 4);
  2576. int use_unitprice = sqlite3_column_int(statement, 5);
  2577. if(use_unitprice!=1)
  2578. {
  2579. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  2580. }
  2581. itemjson[@"model"]=nsname;
  2582. itemjson[@"description"]=nsdescription;
  2583. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2584. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2585. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2586. if(compute)
  2587. {
  2588. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  2589. }
  2590. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2591. count++;
  2592. }
  2593. sqlite3_finalize(statement);
  2594. }
  2595. ret[@"count"]=@(count);
  2596. if(count==0)
  2597. return nil;
  2598. else
  2599. return ret;
  2600. }
  2601. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2602. {
  2603. //compute: add part to subtotal;
  2604. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2605. dict_item[@(item_id)]=@"1";
  2606. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2607. double cuft=0;
  2608. double weight=0;
  2609. int carton=0;
  2610. 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];
  2611. sqlite3_stmt * statement;
  2612. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2613. {
  2614. if (sqlite3_step(statement) == SQLITE_ROW)
  2615. {
  2616. double ulength = sqlite3_column_double(statement, 0);
  2617. double uwidth = sqlite3_column_double(statement, 1);
  2618. double uheight = sqlite3_column_double(statement, 2);
  2619. double uweight = sqlite3_column_double(statement, 3);
  2620. double mlength = sqlite3_column_double(statement, 4);
  2621. double mwidth = sqlite3_column_double(statement, 5);
  2622. double mheight = sqlite3_column_double(statement, 6);
  2623. double mweight = sqlite3_column_double(statement, 7);
  2624. double ilength = sqlite3_column_double(statement, 8);
  2625. double iwidth = sqlite3_column_double(statement, 9);
  2626. double iheight = sqlite3_column_double(statement, 10);
  2627. double iweight = sqlite3_column_double(statement, 11);
  2628. int pcs = sqlite3_column_int(statement,12);
  2629. int mpack = sqlite3_column_int(statement, 13);
  2630. int ipack = sqlite3_column_int(statement, 14);
  2631. double ucbf = sqlite3_column_double(statement, 15);
  2632. double icbf = sqlite3_column_double(statement, 16);
  2633. double mcbf = sqlite3_column_double(statement, 17);
  2634. if(ipack==0)
  2635. {
  2636. carton= count/mpack ;
  2637. weight = mweight*carton;
  2638. cuft= carton*(mlength*mwidth*mheight);
  2639. int remain=count%mpack;
  2640. if(remain==0)
  2641. {
  2642. //do nothing;
  2643. }
  2644. else
  2645. {
  2646. carton++;
  2647. weight += uweight*remain;
  2648. cuft += (ulength*uwidth*uheight)*remain;
  2649. }
  2650. }
  2651. else
  2652. {
  2653. carton = count/(mpack*ipack);
  2654. weight = mweight*carton;
  2655. cuft= carton*(mlength*mwidth*mheight);
  2656. int remain=count%(mpack*ipack);
  2657. if(remain==0)
  2658. {
  2659. // do nothing;
  2660. }
  2661. else
  2662. {
  2663. carton++;
  2664. int icarton =remain/ipack;
  2665. int iremain=remain%ipack;
  2666. weight += iweight*icarton;
  2667. cuft += (ilength*iwidth*iheight)*icarton;
  2668. if(iremain==0)
  2669. {
  2670. //do nothing;
  2671. }
  2672. else
  2673. {
  2674. weight += uweight*iremain;
  2675. cuft += (ulength*uwidth*uheight)*iremain;
  2676. }
  2677. }
  2678. }
  2679. #ifdef BUILD_NPD
  2680. cuft=ucbf*count;
  2681. weight= uweight*count;
  2682. #endif
  2683. }
  2684. sqlite3_finalize(statement);
  2685. }
  2686. if(compute)
  2687. {
  2688. NSArray * arr_count=nil;
  2689. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2690. for(int i=0;i<arr_bundle.count;i++)
  2691. {
  2692. dict_item[arr_bundle[i]]=@"1";
  2693. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2694. cuft+=[bundlejson[@"cuft"] doubleValue];
  2695. weight+=[bundlejson[@"weight"] doubleValue];
  2696. carton+=[bundlejson[@"carton"] intValue];
  2697. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2698. }
  2699. }
  2700. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2701. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2702. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2703. ret[@"items"]=dict_item;
  2704. return ret;
  2705. }
  2706. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2707. {
  2708. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2709. // 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 ];
  2710. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2711. DebugLog(@"offline_login sql:%@",sqlQuery);
  2712. sqlite3_stmt * statement;
  2713. int cart_count=0;
  2714. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2715. if ( dbresult== SQLITE_OK)
  2716. {
  2717. while (sqlite3_step(statement) == SQLITE_ROW)
  2718. {
  2719. int item_id = sqlite3_column_int(statement, 0);
  2720. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2721. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2722. }
  2723. sqlite3_finalize(statement);
  2724. }
  2725. return cart_count;
  2726. }
  2727. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2728. {
  2729. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2730. sqlite3 *db = [iSalesDB get_db];
  2731. UIApplication * app = [UIApplication sharedApplication];
  2732. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2733. NSString* orderCode=params[@"orderCode"];
  2734. int sort = [[params objectForKey:@"sort"] intValue];
  2735. NSString *sort_str = @"";
  2736. switch (sort) {
  2737. case 0:{
  2738. sort_str = @"order by c.modify_time desc";
  2739. }
  2740. break;
  2741. case 1:{
  2742. sort_str = @"order by c.modify_time asc";
  2743. }
  2744. break;
  2745. case 2:{
  2746. sort_str = @"order by m.name asc";
  2747. }
  2748. break;
  2749. case 3:{
  2750. sort_str = @"order by m.name desc";
  2751. }
  2752. break;
  2753. case 4:{
  2754. sort_str = @"order by m.description asc";
  2755. }
  2756. break;
  2757. default:
  2758. break;
  2759. }
  2760. 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 ];
  2761. // 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 ];
  2762. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2763. DebugLog(@"offline_login sql:%@",sqlQuery);
  2764. sqlite3_stmt * statement;
  2765. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2766. NSDate *date1 = [NSDate date];
  2767. int count=0;
  2768. int cart_count=0;
  2769. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2770. if ( dbresult== SQLITE_OK)
  2771. {
  2772. while (sqlite3_step(statement) == SQLITE_ROW)
  2773. {
  2774. NSDate *row_date = [NSDate date];
  2775. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2776. int product_id = sqlite3_column_int(statement, 0);
  2777. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2778. int item_id = sqlite3_column_int(statement, 7);
  2779. NSString* Price=nil;
  2780. if(str_price==nil)
  2781. {
  2782. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2783. // NSDate *price_date = [NSDate date];
  2784. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2785. // DebugLog(@"price time interval");
  2786. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2787. if(price==nil)
  2788. Price=@"No Price.";
  2789. else
  2790. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2791. }
  2792. else
  2793. {
  2794. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2795. }
  2796. double discount = sqlite3_column_double(statement, 2);
  2797. int item_count = sqlite3_column_int(statement, 3);
  2798. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2799. NSString *nsline_note=nil;
  2800. if(line_note!=nil)
  2801. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2802. char *name = (char*)sqlite3_column_text(statement, 5);
  2803. NSString *nsname=nil;
  2804. if(name!=nil)
  2805. nsname= [[NSString alloc]initWithUTF8String:name];
  2806. char *description = (char*)sqlite3_column_text(statement, 6);
  2807. NSString *nsdescription=nil;
  2808. if(description!=nil)
  2809. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2810. int stockUom = sqlite3_column_int(statement, 8);
  2811. int _id = sqlite3_column_int(statement, 9);
  2812. // NSDate *subtotal_date = [NSDate date];
  2813. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2814. // DebugLog(@"subtotal_date time interval");
  2815. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2816. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2817. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2818. int carton=[bsubtotaljson[@"carton"] intValue];
  2819. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2820. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2821. // NSDate *img_date = [NSDate date];
  2822. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2823. // DebugLog(@"img_date time interval");
  2824. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2825. itemjson[@"model"]=nsname;
  2826. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2827. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2828. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2829. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2830. itemjson[@"check"]=@"true";
  2831. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2832. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2833. itemjson[@"unit_price"]=Price;
  2834. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2835. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2836. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2837. itemjson[@"note"]=nsline_note;
  2838. // NSDate *date2 = [NSDate date];
  2839. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2840. // DebugLog(@"model_bundle time interval");
  2841. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2842. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2843. count++;
  2844. // DebugLog(@"row time interval");
  2845. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2846. }
  2847. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2848. sqlite3_finalize(statement);
  2849. }
  2850. DebugLog(@"request cart total time interval");
  2851. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2852. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2853. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2854. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2855. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2856. ret[@"count"]=[NSNumber numberWithInt:count ];
  2857. ret[@"mode"]=@"Regular Mode";
  2858. [iSalesDB close_db:db];
  2859. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2860. DebugLog(@"general notes :%@",general_note);
  2861. ret[@"general_note"]= general_note;
  2862. return [RAUtils dict2data:ret];
  2863. }
  2864. +(NSData*) offline_login :(NSMutableDictionary *) params
  2865. {
  2866. NSString* user = [params valueForKey:@"user"];
  2867. NSString* password = [params valueForKey:@"password"];
  2868. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2869. sqlite3 *db = [iSalesDB get_db];
  2870. 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"]];
  2871. DebugLog(@"offline_login sql:%@",sqlQuery);
  2872. sqlite3_stmt * statement;
  2873. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2874. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2875. {
  2876. if (sqlite3_step(statement) == SQLITE_ROW)
  2877. {
  2878. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2879. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2880. int can_show_price = sqlite3_column_int(statement, 0);
  2881. int can_see_price = sqlite3_column_int(statement, 1);
  2882. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2883. if(contact_id==nil)
  2884. contact_id="";
  2885. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2886. int user_type = sqlite3_column_int(statement, 3);
  2887. int can_cancel_order = sqlite3_column_int(statement, 4);
  2888. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2889. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2890. int can_delete_order = sqlite3_column_int(statement, 7);
  2891. int can_submit_order = sqlite3_column_int(statement, 8);
  2892. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2893. int can_create_order = sqlite3_column_int(statement, 10);
  2894. char *mode = (char*)sqlite3_column_text(statement, 11);
  2895. if(mode==nil)
  2896. mode="";
  2897. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2898. char *username = (char*)sqlite3_column_text(statement, 12);
  2899. if(username==nil)
  2900. username="";
  2901. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2902. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2903. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2904. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2905. [header setValue:nscontact_id forKey:@"contact_id"];
  2906. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2907. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2908. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2909. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2910. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2911. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2912. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2913. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2914. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2915. [header setValue:nsusername forKey:@"username"];
  2916. NSError* error=nil;
  2917. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2918. [header setValue:statusFilter forKey:@"statusFilter"];
  2919. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2920. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2921. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2922. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2923. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2924. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2925. [ret setObject:header forKey:@"header"];
  2926. [ret setValue:nsmode forKey:@"mode"];
  2927. }
  2928. sqlite3_finalize(statement);
  2929. }
  2930. [iSalesDB close_db:db];
  2931. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2932. return [RAUtils dict2data:ret];
  2933. }
  2934. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2935. {
  2936. NSString* contactId = [params valueForKey:@"contactId"];
  2937. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2938. sqlite3 *db = [iSalesDB get_db];
  2939. NSString *sqlQuery = nil;
  2940. {
  2941. 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,type from offline_contact where contact_id='%@'",contactId];
  2942. }
  2943. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2944. sqlite3_stmt * statement;
  2945. [ret setValue:@"2" forKey:@"result"];
  2946. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2947. {
  2948. //int i = 0;
  2949. if (sqlite3_step(statement) == SQLITE_ROW)
  2950. {
  2951. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2952. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2953. // int editable = sqlite3_column_int(statement, 0);
  2954. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2955. NSString *nscompany_name =nil;
  2956. if(company_name==nil)
  2957. nscompany_name=@"";
  2958. else
  2959. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2960. char *country = (char*)sqlite3_column_text(statement, 2);
  2961. if(country==nil)
  2962. country="";
  2963. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2964. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2965. // if(addr==nil)
  2966. // addr="";
  2967. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2968. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2969. if(zipcode==nil)
  2970. zipcode="";
  2971. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2972. char *state = (char*)sqlite3_column_text(statement, 5);
  2973. if(state==nil)
  2974. state="";
  2975. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2976. char *city = (char*)sqlite3_column_text(statement, 6);
  2977. if(city==nil)
  2978. city="";
  2979. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2980. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2981. // NSString *nscontact_name = nil;
  2982. // if(contact_name==nil)
  2983. // nscontact_name=@"";
  2984. // else
  2985. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2986. char *phone = (char*)sqlite3_column_text(statement, 8);
  2987. NSString *nsphone = nil;
  2988. if(phone==nil)
  2989. nsphone=@"";
  2990. else
  2991. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2992. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2993. if(contact_id==nil)
  2994. contact_id="";
  2995. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2996. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2997. if(addr_1==nil)
  2998. addr_1="";
  2999. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3000. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3001. if(addr_2==nil)
  3002. addr_2="";
  3003. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3004. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3005. if(addr_3==nil)
  3006. addr_3="";
  3007. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3008. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3009. if(addr_4==nil)
  3010. addr_4="";
  3011. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3012. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3013. if(first_name==nil)
  3014. first_name="";
  3015. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3016. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3017. if(last_name==nil)
  3018. last_name="";
  3019. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3020. char *fax = (char*)sqlite3_column_text(statement, 16);
  3021. NSString *nsfax = nil;
  3022. if(fax==nil)
  3023. nsfax=@"";
  3024. else
  3025. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3026. char *email = (char*)sqlite3_column_text(statement, 17);
  3027. NSString *nsemail = nil;
  3028. if(email==nil)
  3029. nsemail=@"";
  3030. else
  3031. nsemail= [[NSString alloc]initWithUTF8String:email];
  3032. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3033. NSString *nsimg_0 = nil;
  3034. if(img_0==nil)
  3035. nsimg_0=@"";
  3036. else
  3037. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3038. [self copy_bcardImg:nsimg_0];
  3039. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3040. NSString *nsimg_1 = nil;
  3041. if(img_1==nil)
  3042. nsimg_1=@"";
  3043. else
  3044. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3045. [self copy_bcardImg:nsimg_1];
  3046. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3047. NSString *nsimg_2 = nil;
  3048. if(img_2==nil)
  3049. nsimg_2=@"";
  3050. else
  3051. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3052. [self copy_bcardImg:nsimg_2];
  3053. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3054. NSString *nsprice_type = nil;
  3055. if(price_type==nil)
  3056. nsprice_type=@"";
  3057. else
  3058. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3059. char *notes = (char*)sqlite3_column_text(statement, 22);
  3060. NSString *nsnotes = nil;
  3061. if(notes==nil)
  3062. nsnotes=@"";
  3063. else
  3064. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3065. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3066. NSString *nssalesrep = nil;
  3067. if(salesrep==nil)
  3068. nssalesrep=@"";
  3069. else
  3070. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3071. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3072. {
  3073. // decrypt
  3074. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3075. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3076. nsphone=[AESCrypt fastdecrypt:nsphone];
  3077. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3078. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3079. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3080. }
  3081. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3082. [arr_name addObject:nsfirst_name];
  3083. [arr_name addObject:nslast_name];
  3084. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3085. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3086. [arr_ext addObject:nsaddr_1];
  3087. [arr_ext addObject:nsaddr_2];
  3088. [arr_ext addObject:nsaddr_3];
  3089. [arr_ext addObject:nsaddr_4];
  3090. [arr_ext addObject:@"\r\n"];
  3091. [arr_ext addObject:nscity];
  3092. [arr_ext addObject:nsstate];
  3093. [arr_ext addObject:nszipcode];
  3094. [arr_ext addObject:nscountry];
  3095. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3096. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3097. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3098. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3099. [item setValue:nscountry forKey:@"customer_country"];
  3100. [item setValue:nsphone forKey:@"customer_phone"];
  3101. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3102. [item setValue:nscompany_name forKey:@"customer_name"];
  3103. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3104. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3105. [item setValue:nsext forKey:@"customer_contact_ext"];
  3106. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3107. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3108. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3109. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3110. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3111. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3112. [item setValue:nslast_name forKey:@"customer_last_name"];
  3113. [item setValue:nscity forKey:@"customer_city"];
  3114. [item setValue:nsstate forKey:@"customer_state"];
  3115. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3116. [item setValue:contactId forKey:@"customer_cid"];
  3117. [item setValue:nscontact_name forKey:@"customer_contact"];
  3118. [item setValue:nsfax forKey:@"customer_fax"];
  3119. [item setValue:nsemail forKey:@"customer_email"];
  3120. [item setValue:contact_type forKey:@"customer_contact_type"];
  3121. [ret setObject:item forKey:@"customerInfo"];
  3122. // i++;
  3123. }
  3124. UIApplication * app = [UIApplication sharedApplication];
  3125. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3126. [ret setValue:appDelegate.mode forKey:@"mode"];
  3127. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3128. sqlite3_finalize(statement);
  3129. }
  3130. [iSalesDB close_db:db];
  3131. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3132. return ret;
  3133. }
  3134. + (bool) copy_bcardImg:(NSString*) filename
  3135. {
  3136. if(filename.length==0)
  3137. return false;
  3138. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3139. bool ret=false;
  3140. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3141. NSString *cachefolder = [paths objectAtIndex:0];
  3142. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3143. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3144. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3145. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3146. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3147. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3148. //
  3149. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3150. NSFileManager* fileManager = [NSFileManager defaultManager];
  3151. BOOL bdir=NO;
  3152. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3153. {
  3154. NSError *error = nil;
  3155. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3156. {
  3157. ret=false;
  3158. }
  3159. else
  3160. {
  3161. ret=true;
  3162. }
  3163. // NSError *error = nil;
  3164. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3165. //
  3166. // if(!bsuccess)
  3167. // {
  3168. // DebugLog(@"Create offline_createimg folder failed");
  3169. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3170. // return [RAUtils dict2data:ret];
  3171. // }
  3172. // if(bsuccess)
  3173. // {
  3174. // sqlite3 *db = [self get_db];
  3175. //
  3176. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3177. // [iSalesDB close_db:db];
  3178. // }
  3179. }
  3180. return ret;
  3181. //
  3182. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3183. // if(bsuccess)
  3184. // {
  3185. // NSError *error = nil;
  3186. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3187. // {
  3188. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3189. // }
  3190. // else
  3191. // {
  3192. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3193. //
  3194. // ret[@"img_url_aname"]=filename;
  3195. // ret[@"img_url"]=savedImagePath;
  3196. // }
  3197. // }
  3198. }
  3199. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3200. {
  3201. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3202. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3203. NSString *cachefolder = [paths objectAtIndex:0];
  3204. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3205. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3206. NSFileManager* fileManager = [NSFileManager defaultManager];
  3207. BOOL bdir=YES;
  3208. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3209. {
  3210. NSError *error = nil;
  3211. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3212. if(!bsuccess)
  3213. {
  3214. DebugLog(@"Create offline_createimg folder failed");
  3215. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3216. return [RAUtils dict2data:ret];
  3217. }
  3218. // if(bsuccess)
  3219. // {
  3220. // sqlite3 *db = [self get_db];
  3221. //
  3222. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3223. // [iSalesDB close_db:db];
  3224. // }
  3225. }
  3226. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3227. //JEPG格式
  3228. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3229. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3230. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3231. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3232. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3233. if(bsuccess)
  3234. {
  3235. NSError *error = nil;
  3236. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3237. {
  3238. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3239. }
  3240. else
  3241. {
  3242. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3243. ret[@"img_url_aname"]=filename;
  3244. ret[@"img_url"]=filename;
  3245. }
  3246. }
  3247. else
  3248. {
  3249. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3250. }
  3251. return [RAUtils dict2data:ret];
  3252. }
  3253. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3254. {
  3255. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3256. category = [category substringToIndex:3];
  3257. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3258. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3259. params[@"category"]= category;
  3260. ret[@"params"]= params;
  3261. [ret setValue:@"detail" forKey:@"target"];
  3262. [ret setValue:@"popup" forKey:@"action"];
  3263. [ret setValue:@"content" forKey:@"type"];
  3264. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3265. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3266. [ret setValue:@"true" forKey:@"single_row"];
  3267. [ret setValue:@"true" forKey:@"partial_refresh"];
  3268. // sqlite3 *db = [iSalesDB get_db];
  3269. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3270. 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 ;
  3271. sqlite3_stmt * statement;
  3272. int count = 0;
  3273. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3274. // int count=0;
  3275. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3276. {
  3277. int i=0;
  3278. while (sqlite3_step(statement) == SQLITE_ROW)
  3279. {
  3280. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3281. // char *name = (char*)sqlite3_column_text(statement, 1);
  3282. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3283. char *name = (char*)sqlite3_column_text(statement, 0);
  3284. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3285. int product_id = sqlite3_column_int(statement, 1);
  3286. char *url = (char*)sqlite3_column_text(statement, 2);
  3287. if(url==nil)
  3288. url="";
  3289. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3290. [item setValue:nsurl forKey:@"picture_path"];
  3291. [item setValue:nsname forKey:@"fash_name"];
  3292. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3293. [item setValue:category forKey:@"category"];
  3294. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3295. i++;
  3296. }
  3297. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3298. sqlite3_finalize(statement);
  3299. }
  3300. NSLog(@"count:%d",count);
  3301. // [iSalesDB close_db:db];
  3302. return ret;
  3303. }
  3304. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3305. {
  3306. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3307. [ret setValue:key forKey:@"key"];
  3308. [ret setValue:value forKey:@"val"];
  3309. return ret;
  3310. }
  3311. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3312. {
  3313. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3314. [ret setValue:@"0" forKey:@"img_count"];
  3315. // sqlite3 *db = [iSalesDB get_db];
  3316. 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 ;
  3317. sqlite3_stmt * statement;
  3318. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3319. {
  3320. int i=0;
  3321. if (sqlite3_step(statement) == SQLITE_ROW)
  3322. {
  3323. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3324. // char *name = (char*)sqlite3_column_text(statement, 1);
  3325. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3326. char *value = (char*)sqlite3_column_text(statement, 0);
  3327. if(value==nil)
  3328. value="";
  3329. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3330. char *key = (char*)sqlite3_column_text(statement, 1);
  3331. if(key==nil)
  3332. key="";
  3333. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3334. [item setValue:nsvalue forKey:@"val"];
  3335. [item setValue:nskey forKey:@"key"];
  3336. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3337. [ret setValue:@"1" forKey:@"count"];
  3338. i++;
  3339. }
  3340. sqlite3_finalize(statement);
  3341. }
  3342. // [iSalesDB close_db:db];
  3343. return ret;
  3344. }
  3345. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3346. {
  3347. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3348. [ret setValue:@"0" forKey:@"count"];
  3349. // sqlite3 *db = [iSalesDB get_db];
  3350. 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;
  3351. sqlite3_stmt * statement;
  3352. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3353. {
  3354. int i=0;
  3355. while (sqlite3_step(statement) == SQLITE_ROW)
  3356. {
  3357. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3358. // char *name = (char*)sqlite3_column_text(statement, 1);
  3359. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3360. char *value = (char*)sqlite3_column_text(statement, 0);
  3361. if(value==nil)
  3362. value="";
  3363. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3364. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3365. if(selector_display==nil)
  3366. selector_display="";
  3367. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3368. int product_id = sqlite3_column_int(statement, 2);
  3369. char *category = (char*)sqlite3_column_text(statement, 3);
  3370. if(category==nil)
  3371. category="";
  3372. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3373. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3374. [item setValue:nsvalue forKey:@"title"];
  3375. [item setValue:url forKey:@"pic_url"];
  3376. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3377. [params setValue:@"2" forKey:@"count"];
  3378. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3379. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3380. [param0 setValue:@"product_id" forKey:@"name"];
  3381. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3382. [param1 setValue:nscategory forKey:@"val"];
  3383. [param1 setValue:@"category" forKey:@"name"];
  3384. [params setObject:param0 forKey:@"param_0"];
  3385. [params setObject:param1 forKey:@"param_1"];
  3386. [item setObject:params forKey:@"params"];
  3387. [ret setValue:nsselector_display forKey:@"name"];
  3388. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3389. i++;
  3390. }
  3391. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3392. [ret setValue:@"switch" forKey:@"action"];
  3393. sqlite3_finalize(statement);
  3394. }
  3395. // [iSalesDB close_db:db];
  3396. return ret;
  3397. }
  3398. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3399. {
  3400. // model 在 category search 显示的图片。
  3401. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3402. // sqlite3 *db = [iSalesDB get_db];
  3403. 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];
  3404. sqlite3_stmt * statement;
  3405. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3406. {
  3407. while (sqlite3_step(statement) == SQLITE_ROW)
  3408. {
  3409. char *url = (char*)sqlite3_column_text(statement, 0);
  3410. if(url==nil)
  3411. url="";
  3412. int type = sqlite3_column_int(statement, 1);
  3413. if(type==0)
  3414. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3415. else
  3416. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3417. }
  3418. sqlite3_finalize(statement);
  3419. }
  3420. // [iSalesDB close_db:db];
  3421. return ret;
  3422. }
  3423. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3424. {
  3425. int item_id=-1;
  3426. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3427. sqlite3_stmt * statement;
  3428. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3429. {
  3430. if (sqlite3_step(statement) == SQLITE_ROW)
  3431. {
  3432. item_id = sqlite3_column_int(statement, 0);
  3433. }
  3434. sqlite3_finalize(statement);
  3435. }
  3436. return item_id;
  3437. }
  3438. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3439. {
  3440. // NSString* ret = @"";
  3441. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3442. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3443. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3444. sqlite3_stmt * statement;
  3445. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3446. {
  3447. while (sqlite3_step(statement) == SQLITE_ROW)
  3448. {
  3449. int bitem_id = sqlite3_column_int(statement, 0);
  3450. int bitem_qty = sqlite3_column_int(statement, 1);
  3451. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3452. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3453. }
  3454. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3455. sqlite3_finalize(statement);
  3456. }
  3457. // if(ret==nil)
  3458. // ret=@"";
  3459. *count=arr_count;
  3460. return arr_bundle;
  3461. }
  3462. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3463. {
  3464. // get default sold qty, return -1 if model not found;
  3465. int ret = -1;
  3466. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3467. sqlite3_stmt * statement;
  3468. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3469. {
  3470. if (sqlite3_step(statement) == SQLITE_ROW)
  3471. {
  3472. ret = sqlite3_column_int(statement, 0);
  3473. }
  3474. sqlite3_finalize(statement);
  3475. }
  3476. return ret;
  3477. }
  3478. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3479. {
  3480. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3481. [ret setValue:@"0" forKey:@"img_count"];
  3482. // sqlite3 *db = [iSalesDB get_db];
  3483. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3484. sqlite3_stmt * statement;
  3485. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3486. {
  3487. int i=0;
  3488. while (sqlite3_step(statement) == SQLITE_ROW)
  3489. {
  3490. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3491. // char *name = (char*)sqlite3_column_text(statement, 1);
  3492. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3493. char *url = (char*)sqlite3_column_text(statement, 0);
  3494. if(url==nil)
  3495. url="";
  3496. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3497. [item setValue:nsurl forKey:@"s"];
  3498. [item setValue:nsurl forKey:@"l"];
  3499. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3500. i++;
  3501. }
  3502. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3503. sqlite3_finalize(statement);
  3504. }
  3505. // [iSalesDB close_db:db];
  3506. return ret;
  3507. }
  3508. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3509. UIApplication * app = [UIApplication sharedApplication];
  3510. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3511. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3512. sqlite3 *db = [iSalesDB get_db];
  3513. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3514. NSString* product_id=params[@"product_id"];
  3515. NSString *item_count_str = params[@"item_count"];
  3516. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3517. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3518. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3519. // NSString *sql = @"";
  3520. for(int i=0;i<arr.count;i++)
  3521. {
  3522. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3523. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3524. __block int cart_count = 0;
  3525. if (!item_count_str) {
  3526. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3527. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3528. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3529. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3530. cart_count = [[model_set_components lastObject] integerValue];
  3531. }];
  3532. }
  3533. if(count==0)
  3534. {
  3535. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3536. sqlite3_stmt *stmt;
  3537. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3538. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3539. if (item_count_arr) {
  3540. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3541. } else {
  3542. sqlite3_bind_int(stmt,2,cart_count);
  3543. }
  3544. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3545. [iSalesDB execSql:@"ROLLBACK" db:db];
  3546. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3547. [iSalesDB close_db:db];
  3548. DebugLog(@"add to wishlist error");
  3549. return ret;
  3550. }
  3551. } else {
  3552. int qty = 0;
  3553. if (item_count_arr) {
  3554. qty = [item_count_arr[i] integerValue];
  3555. } else {
  3556. qty = cart_count;
  3557. }
  3558. 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]];
  3559. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3560. [iSalesDB execSql:@"ROLLBACK" db:db];
  3561. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3562. [iSalesDB close_db:db];
  3563. DebugLog(@"add to wishlist error");
  3564. return ret;
  3565. }
  3566. }
  3567. }
  3568. // [iSalesDB execSql:sql db:db];
  3569. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3570. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3571. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3572. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3573. [iSalesDB close_db:db];
  3574. appDelegate.wish_count =count;
  3575. [appDelegate update_count_mark];
  3576. ret[@"result"]= [NSNumber numberWithInt:2];
  3577. return ret;
  3578. }
  3579. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3580. // 0 category
  3581. // 1 search
  3582. // 2 itemsearch
  3583. NSData *ret = nil;
  3584. NSDictionary *items = nil;
  3585. switch (from) {
  3586. case 0:{
  3587. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3588. }
  3589. break;
  3590. case 1:{
  3591. items = [[self search:params limited:NO] objectForKey:@"items"];
  3592. }
  3593. break;
  3594. case 2:{
  3595. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3596. }
  3597. break;
  3598. default:
  3599. break;
  3600. }
  3601. if (!items) {
  3602. return ret;
  3603. }
  3604. int count = [[items objectForKey:@"count"] intValue];
  3605. NSMutableString *product_id_str = [@"" mutableCopy];
  3606. for (int i = 0; i < count; i++) {
  3607. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3608. NSDictionary *item = [items objectForKey:key];
  3609. NSString *product_id = [item objectForKey:@"product_id"];
  3610. if (i == 0) {
  3611. [product_id_str appendString:product_id];
  3612. } else {
  3613. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3614. }
  3615. }
  3616. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3617. if ([add_to isEqualToString:@"cart"]) {
  3618. NSString *order_code = [params objectForKey:@"orderCode"];
  3619. if (order_code.length) {
  3620. NSDictionary *newParams = @{
  3621. @"product_id" : product_id_str,
  3622. @"orderCode" : order_code
  3623. };
  3624. ret = [self offline_add2cart:newParams.mutableCopy];
  3625. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3626. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3627. ret = [RAUtils dict2data:retDic];
  3628. }
  3629. } else if([add_to isEqualToString:@"wishlist"]) {
  3630. NSDictionary *newParams = @{
  3631. @"product_id" : product_id_str
  3632. };
  3633. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3634. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3635. ret = [RAUtils dict2data:retDic];
  3636. } else if([add_to isEqualToString:@"portfolio"]) {
  3637. }
  3638. return ret;
  3639. }
  3640. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3641. {
  3642. return [self addAll:params from:0];
  3643. }
  3644. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3645. {
  3646. return [self addAll:params from:1];
  3647. }
  3648. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3649. {
  3650. return [self addAll:params from:2];
  3651. }
  3652. #pragma mark - Jack
  3653. #warning 做SQL操作时转义!!
  3654. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3655. // "val_227" : {
  3656. // "check" : 1,
  3657. // "value" : "US United States",
  3658. // "value_id" : "228"
  3659. // },
  3660. if (!countryCode) {
  3661. countryCode = @"US";
  3662. }
  3663. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3664. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3665. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3666. int code_id = sqlite3_column_int(stmt, 3); // id
  3667. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3668. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3669. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3670. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3671. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3672. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3673. }
  3674. long n = *count;
  3675. *count = n + 1;
  3676. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3677. [container setValue:countryDic forKey:key];
  3678. }] mutableCopy];
  3679. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3680. return ret;
  3681. }
  3682. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3683. countryCode = [self translateSingleQuote:countryCode];
  3684. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3685. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3686. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3687. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3688. if (name == NULL) {
  3689. name = "";
  3690. }
  3691. if (code == NULL) {
  3692. code = "";
  3693. }
  3694. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3695. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3696. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3697. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3698. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3699. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3700. }
  3701. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3702. [container setValue:stateDic forKey:key];
  3703. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3704. DebugLog(@"query all state error: %@",err_msg);
  3705. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3706. // [stateDic setValue:@"Other" forKey:@"value"];
  3707. // [stateDic setValue:@"" forKey:@"value_id"];
  3708. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3709. //
  3710. // if (state_code && [@"" isEqualToString:state_code]) {
  3711. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3712. // }
  3713. //
  3714. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3715. // [container setValue:stateDic forKey:key];
  3716. }] mutableCopy];
  3717. // failure 可以不用了,一样的
  3718. if (ret.allKeys.count == 0) {
  3719. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3720. [stateDic setValue:@"Other" forKey:@"value"];
  3721. [stateDic setValue:@"" forKey:@"value_id"];
  3722. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3723. if (state_code && [@"" isEqualToString:state_code]) {
  3724. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3725. }
  3726. NSString *key = [NSString stringWithFormat:@"val_0"];
  3727. [ret setValue:stateDic forKey:key];
  3728. }
  3729. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3730. return ret;
  3731. }
  3732. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3733. codeId = [self translateSingleQuote:codeId];
  3734. 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];
  3735. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3736. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3737. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3738. if (name == NULL) {
  3739. name = "";
  3740. }
  3741. if (code == NULL) {
  3742. code = "";
  3743. }
  3744. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3745. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3746. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3747. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3748. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3749. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3750. }
  3751. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3752. [container setValue:stateDic forKey:key];
  3753. }] mutableCopy];
  3754. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3755. return ret;
  3756. }
  3757. + (NSDictionary *)offline_getPrice {
  3758. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3759. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3760. char *name = (char *) sqlite3_column_text(stmt, 1);
  3761. int type = sqlite3_column_int(stmt, 2);
  3762. int orderBy = sqlite3_column_int(stmt, 3);
  3763. if (name == NULL) {
  3764. name = "";
  3765. }
  3766. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3767. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3768. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3769. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3770. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3771. if (orderBy == 0) {
  3772. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3773. }
  3774. [container setValue:priceDic forKey:key];
  3775. }] mutableCopy];
  3776. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3777. return ret;
  3778. }
  3779. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  3780. NSString *sql = @"select _id,type_name from contact_type";
  3781. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  3782. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  3783. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3784. int _id = sqlite3_column_int(stmt, 0);
  3785. NSString *name = [self textAtColumn:1 statement:stmt];
  3786. NSDictionary *typeDic = @{
  3787. @"value_id" : name,
  3788. @"value" : name,
  3789. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  3790. };
  3791. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  3792. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  3793. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  3794. }];
  3795. return ret;
  3796. }
  3797. + (NSDictionary *)offline_getSalesRep {
  3798. // 首先从offline_login表中取出sales_code
  3799. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3800. NSString *user = app.user;
  3801. user = [self translateSingleQuote:user];
  3802. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3803. __block NSString *user_code = @"";
  3804. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3805. char *code = (char *)sqlite3_column_text(stmt, 0);
  3806. if (code == NULL) {
  3807. code = "";
  3808. }
  3809. user_code = [NSString stringWithUTF8String:code];
  3810. }];
  3811. // 再取所有salesRep
  3812. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3813. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3814. // 1 name 2 code 3 salesrep_id
  3815. char *name = (char *)sqlite3_column_text(stmt, 1);
  3816. char *code = (char *)sqlite3_column_text(stmt, 2);
  3817. int salesrep_id = sqlite3_column_int(stmt, 3);
  3818. if (name == NULL) {
  3819. name = "";
  3820. }
  3821. if (code == NULL) {
  3822. code = "";
  3823. }
  3824. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3825. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3826. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3827. // 比较code 相等则check
  3828. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3829. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3830. }
  3831. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3832. }] mutableCopy];
  3833. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3834. return ret;
  3835. }
  3836. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3837. zipcode = [self translateSingleQuote:zipcode];
  3838. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3839. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3840. char *country = (char *)sqlite3_column_text(stmt, 0);
  3841. char *state = (char *)sqlite3_column_text(stmt, 1);
  3842. char *city = (char *)sqlite3_column_text(stmt, 2);
  3843. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3844. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3845. if (country == NULL) {
  3846. country = "";
  3847. }
  3848. if (state == NULL) {
  3849. state = "";
  3850. }
  3851. if (city == NULL) {
  3852. city = "";
  3853. }
  3854. if (country_code == NULL) {
  3855. country_code = "";
  3856. }
  3857. if (state_code == NULL) {
  3858. state_code = "";
  3859. }
  3860. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3861. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3862. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3863. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3864. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3865. }] mutableCopy];
  3866. return ret;
  3867. }
  3868. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3869. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3870. [item setValue:value forKey:valueKey];
  3871. [dic setValue:item forKey:itemKey];
  3872. }
  3873. + (NSString *)countryCodeByid:(NSString *)code_id {
  3874. NSString *ret = nil;
  3875. code_id = [self translateSingleQuote:code_id];
  3876. sqlite3 *db = [iSalesDB get_db];
  3877. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3878. sqlite3_stmt * statement;
  3879. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3880. while (sqlite3_step(statement) == SQLITE_ROW) {
  3881. char *code = (char *)sqlite3_column_text(statement, 0);
  3882. if (code == NULL) {
  3883. code = "";
  3884. }
  3885. ret = [NSString stringWithUTF8String:code];
  3886. }
  3887. sqlite3_finalize(statement);
  3888. }
  3889. [iSalesDB close_db:db];
  3890. return ret;
  3891. }
  3892. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3893. NSString *ret = nil;
  3894. code = [self translateSingleQuote:code];
  3895. sqlite3 *db = [iSalesDB get_db];
  3896. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3897. sqlite3_stmt * statement;
  3898. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3899. while (sqlite3_step(statement) == SQLITE_ROW) {
  3900. char *_id = (char *)sqlite3_column_text(statement, 0);
  3901. if (_id == NULL) {
  3902. _id = "";
  3903. }
  3904. ret = [NSString stringWithFormat:@"%s",_id];
  3905. }
  3906. sqlite3_finalize(statement);
  3907. }
  3908. [iSalesDB close_db:db];
  3909. return ret;
  3910. }
  3911. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3912. NSString *name = nil;
  3913. codeId = [self translateSingleQuote:codeId];
  3914. sqlite3 *db = [iSalesDB get_db];
  3915. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3916. sqlite3_stmt * statement;
  3917. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3918. while (sqlite3_step(statement) == SQLITE_ROW) {
  3919. char *value = (char *)sqlite3_column_text(statement, 0);
  3920. if (value == NULL) {
  3921. value = "";
  3922. }
  3923. name = [NSString stringWithUTF8String:value];
  3924. }
  3925. sqlite3_finalize(statement);
  3926. }
  3927. [iSalesDB close_db:db];
  3928. return name;
  3929. }
  3930. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3931. NSString *ret = nil;
  3932. sqlite3 *db = [iSalesDB get_db];
  3933. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3934. sqlite3_stmt * statement;
  3935. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3936. while (sqlite3_step(statement) == SQLITE_ROW) {
  3937. char *name = (char *)sqlite3_column_text(statement, 0);
  3938. if (name == NULL) {
  3939. name = "";
  3940. }
  3941. ret = [NSString stringWithUTF8String:name];
  3942. }
  3943. sqlite3_finalize(statement);
  3944. }
  3945. [iSalesDB close_db:db];
  3946. return ret;
  3947. }
  3948. + (NSString *)salesRepCodeById:(NSString *)_id {
  3949. NSString *ret = nil;
  3950. _id = [self translateSingleQuote:_id];
  3951. sqlite3 *db = [iSalesDB get_db];
  3952. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3953. sqlite3_stmt * statement;
  3954. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3955. while (sqlite3_step(statement) == SQLITE_ROW) {
  3956. char *rep = (char *)sqlite3_column_text(statement, 0);
  3957. if (rep == NULL) {
  3958. rep = "";
  3959. }
  3960. ret = [NSString stringWithUTF8String:rep];
  3961. }
  3962. sqlite3_finalize(statement);
  3963. }
  3964. [iSalesDB close_db:db];
  3965. return ret;
  3966. }
  3967. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3968. char *tx = (char *)sqlite3_column_text(stmt, col);
  3969. if (tx == NULL) {
  3970. tx = "";
  3971. }
  3972. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3973. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3974. if (!text) {
  3975. text = @"";
  3976. }
  3977. // 将字符全部为' '的字符串干掉
  3978. int spaceCount = 0;
  3979. for (int i = 0; i < text.length; i++) {
  3980. if ([text characterAtIndex:i] == ' ') {
  3981. spaceCount++;
  3982. }
  3983. }
  3984. if (spaceCount == text.length) {
  3985. text = @"";
  3986. }
  3987. return text;
  3988. }
  3989. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3990. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3991. NSData *data = [NSData dataWithContentsOfFile:path];
  3992. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3993. return ret;
  3994. }
  3995. + (NSString *)textFileName:(NSString *)name {
  3996. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3997. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3998. if (!text) {
  3999. text = @"";
  4000. }
  4001. return text;
  4002. }
  4003. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4004. return [[dic objectForKey:key] mutableCopy];
  4005. }
  4006. + (id)translateSingleQuote:(NSString *)string {
  4007. if ([string isKindOfClass:[NSString class]])
  4008. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4009. return string;
  4010. }
  4011. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4012. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4013. }
  4014. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4015. NSString* ret= nil;
  4016. NSString *sqlQuery = nil;
  4017. // 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
  4018. 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];
  4019. sqlite3_stmt * statement;
  4020. // int count=0;
  4021. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4022. {
  4023. if (sqlite3_step(statement) == SQLITE_ROW)
  4024. {
  4025. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4026. if(imgurl==nil)
  4027. imgurl="";
  4028. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4029. ret=nsimgurl;
  4030. }
  4031. sqlite3_finalize(statement);
  4032. }
  4033. else
  4034. {
  4035. [ret setValue:@"8" forKey:@"result"];
  4036. }
  4037. // [iSalesDB close_db:db];
  4038. // DebugLog(@"data string: %@",ret );
  4039. return ret;
  4040. }
  4041. #pragma mark contact list
  4042. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4043. {
  4044. // contactType = "Sales_Order_Customer";
  4045. // limit = 25;
  4046. // offset = 0;
  4047. // password = 123456;
  4048. // "price_name" = 16;
  4049. // user = EvanK;
  4050. sqlite3 *db = [iSalesDB get_db];
  4051. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4052. if (contactType) {
  4053. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4054. } else {
  4055. contactType = @"1 = 1";
  4056. }
  4057. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4058. DebugLog(@"offline contact list keyword: %@",keyword);
  4059. // advanced search
  4060. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4061. if (contact_name) {
  4062. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4063. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4064. } else {
  4065. contact_name = @"";
  4066. }
  4067. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4068. if (customer_phone) {
  4069. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4070. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4071. } else {
  4072. customer_phone = @"";
  4073. }
  4074. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4075. if (customer_fax) {
  4076. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4077. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4078. } else {
  4079. customer_fax = @"";
  4080. }
  4081. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4082. if (customer_zipcode) {
  4083. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4084. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4085. } else {
  4086. customer_zipcode = @"";
  4087. }
  4088. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4089. if (customer_sales_rep) {
  4090. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4091. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4092. } else {
  4093. customer_sales_rep = @"";
  4094. }
  4095. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4096. if (customer_state) {
  4097. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4098. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4099. } else {
  4100. customer_state = @"";
  4101. }
  4102. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4103. if (customer_name) {
  4104. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4105. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4106. } else {
  4107. customer_name = @"";
  4108. }
  4109. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4110. if (customer_country) {
  4111. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4112. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4113. } else {
  4114. customer_country = @"";
  4115. }
  4116. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4117. if (customer_cid) {
  4118. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4119. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4120. } else {
  4121. customer_cid = @"";
  4122. }
  4123. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4124. if (customer_city) {
  4125. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4126. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4127. } else {
  4128. customer_city = @"";
  4129. }
  4130. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4131. if (customer_address) {
  4132. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4133. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4134. } else {
  4135. customer_address = @"";
  4136. }
  4137. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4138. if (customer_email) {
  4139. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4140. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4141. } else {
  4142. customer_email = @"";
  4143. }
  4144. NSString *price_name = [params valueForKey:@"price_name"];
  4145. if (price_name) {
  4146. if ([price_name containsString:@","]) {
  4147. // 首先从 price表中查处name
  4148. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4149. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4150. for (int i = 1;i < pArray.count;i++) {
  4151. NSString *p = pArray[i];
  4152. [mutablePStr appendFormat:@" or type = %@ ",p];
  4153. }
  4154. [mutablePStr appendString:@";"];
  4155. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4156. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4157. char *name = (char *)sqlite3_column_text(stmt, 0);
  4158. if (!name)
  4159. name = "";
  4160. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4161. }];
  4162. // 再根据name 拼sql
  4163. NSMutableString *mutable_price_name = [NSMutableString string];
  4164. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4165. for (int i = 1; i < price_name_array.count; i++) {
  4166. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4167. }
  4168. [mutable_price_name appendString:@")"];
  4169. price_name = mutable_price_name;
  4170. } else {
  4171. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4172. if ([price_name isEqualToString:@""]) {
  4173. price_name = @"";
  4174. } else {
  4175. __block NSString *price;
  4176. price_name = [self translateSingleQuote:price_name];
  4177. [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) {
  4178. char *p = (char *)sqlite3_column_text(stmt, 0);
  4179. if (p == NULL) {
  4180. p = "";
  4181. }
  4182. price = [NSString stringWithUTF8String:p];
  4183. }];
  4184. if ([price isEqualToString:@""]) {
  4185. price_name = @"";
  4186. } else {
  4187. price = [self translateSingleQuote:price];
  4188. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4189. }
  4190. }
  4191. }
  4192. } else {
  4193. price_name = @"";
  4194. }
  4195. int limit = [[params valueForKey:@"limit"] intValue];
  4196. int offset = [[params valueForKey:@"offset"] intValue];
  4197. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4198. 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];
  4199. 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];
  4200. // int result= [iSalesDB AddExFunction:db];
  4201. int count =0;
  4202. NSString *sqlQuery = nil;
  4203. if(keyword.length==0)
  4204. {
  4205. // 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];
  4206. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4207. sqlQuery = sql;
  4208. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4209. }
  4210. else
  4211. {
  4212. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4213. keyword = keyword.lowercaseString;
  4214. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4215. 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];
  4216. 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]];
  4217. }
  4218. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4219. sqlite3_stmt * statement;
  4220. [ret setValue:@"2" forKey:@"result"];
  4221. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4222. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4223. {
  4224. int i = 0;
  4225. while (sqlite3_step(statement) == SQLITE_ROW)
  4226. {
  4227. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4228. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4229. int editable = sqlite3_column_int(statement, 0);
  4230. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4231. NSString *nscompany_name =nil;
  4232. if(company_name==nil)
  4233. nscompany_name=@"";
  4234. else
  4235. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4236. char *country = (char*)sqlite3_column_text(statement, 2);
  4237. if(country==nil)
  4238. country="";
  4239. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4240. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4241. // if(addr==nil)
  4242. // addr="";
  4243. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4244. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4245. if(zipcode==nil)
  4246. zipcode="";
  4247. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4248. char *state = (char*)sqlite3_column_text(statement, 5);
  4249. if(state==nil)
  4250. state="";
  4251. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4252. char *city = (char*)sqlite3_column_text(statement, 6);
  4253. if(city==nil)
  4254. city="";
  4255. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4256. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4257. // NSString *nscontact_name = nil;
  4258. // if(contact_name==nil)
  4259. // nscontact_name=@"";
  4260. // else
  4261. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4262. char *phone = (char*)sqlite3_column_text(statement, 8);
  4263. NSString *nsphone = nil;
  4264. if(phone==nil)
  4265. nsphone=@"";
  4266. else
  4267. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4268. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4269. if(contact_id==nil)
  4270. contact_id="";
  4271. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4272. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4273. if(addr_1==nil)
  4274. addr_1="";
  4275. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4276. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4277. if(addr_2==nil)
  4278. addr_2="";
  4279. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4280. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4281. if(addr_3==nil)
  4282. addr_3="";
  4283. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4284. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4285. if(addr_4==nil)
  4286. addr_4="";
  4287. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4288. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4289. if(first_name==nil)
  4290. first_name="";
  4291. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4292. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4293. if(last_name==nil)
  4294. last_name="";
  4295. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4296. char *fax = (char*)sqlite3_column_text(statement, 16);
  4297. NSString *nsfax = nil;
  4298. if(fax==nil)
  4299. nsfax=@"";
  4300. else
  4301. {
  4302. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4303. if(nsfax.length>0)
  4304. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4305. }
  4306. char *email = (char*)sqlite3_column_text(statement, 17);
  4307. NSString *nsemail = nil;
  4308. if(email==nil)
  4309. nsemail=@"";
  4310. else
  4311. {
  4312. nsemail= [[NSString alloc]initWithUTF8String:email];
  4313. if(nsemail.length>0)
  4314. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4315. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4316. }
  4317. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4318. [arr_name addObject:nsfirst_name];
  4319. [arr_name addObject:nslast_name];
  4320. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4321. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4322. {
  4323. // decrypt
  4324. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4325. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4326. nsphone=[AESCrypt fastdecrypt:nsphone];
  4327. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4328. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4329. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4330. }
  4331. [arr_addr addObject:nscompany_name];
  4332. [arr_addr addObject:nscontact_name];
  4333. [arr_addr addObject:@"<br>"];
  4334. [arr_addr addObject:nsaddr_1];
  4335. [arr_addr addObject:nsaddr_2];
  4336. [arr_addr addObject:nsaddr_3];
  4337. [arr_addr addObject:nsaddr_4];
  4338. //[arr_addr addObject:nsaddr];
  4339. [arr_addr addObject:nszipcode];
  4340. [arr_addr addObject:nscity];
  4341. [arr_addr addObject:nsstate];
  4342. [arr_addr addObject:nscountry];
  4343. [arr_addr addObject:@"<br>"];
  4344. [arr_addr addObject:nsphone];
  4345. [arr_addr addObject:nsfax];
  4346. [arr_addr addObject:nsemail];
  4347. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4348. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4349. [item setValue:name forKey:@"name"];
  4350. [item setValue:nscontact_id forKey:@"contact_id"];
  4351. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4352. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4353. i++;
  4354. }
  4355. UIApplication * app = [UIApplication sharedApplication];
  4356. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4357. [ret setValue:appDelegate.mode forKey:@"mode"];
  4358. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4359. sqlite3_finalize(statement);
  4360. }
  4361. [iSalesDB close_db:db];
  4362. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4363. return ret;
  4364. }
  4365. #pragma mark contact Advanced search
  4366. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4367. {
  4368. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4369. return [RAUtils dict2data:contactAdvanceDic];
  4370. }
  4371. #pragma mark create new contact
  4372. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4373. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4374. NSData *data = [NSData dataWithContentsOfFile:path];
  4375. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4376. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4377. NSString *countryCode = nil;
  4378. NSString *countryCode_id = nil;
  4379. NSString *stateCode = nil;
  4380. NSString *city = nil;
  4381. NSString *zipCode = nil;
  4382. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4383. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4384. NSString *code_id = params[@"country"];
  4385. countryCode_id = code_id;
  4386. countryCode = [self countryCodeByid:code_id];
  4387. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4388. NSString *zip_code = params[@"zipcode"];
  4389. // 剔除全部为空格
  4390. int spaceCount = 0;
  4391. for (int i = 0; i < zip_code.length; i++) {
  4392. if ([zip_code characterAtIndex:i] == ' ') {
  4393. spaceCount++;
  4394. }
  4395. }
  4396. if (spaceCount == zip_code.length) {
  4397. zip_code = @"";
  4398. }
  4399. zipCode = zip_code;
  4400. if (zipCode.length > 0) {
  4401. countryCode_id = params[@"country"];
  4402. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4403. countryCode = [dic valueForKey:@"country_code"];
  4404. if (!countryCode) {
  4405. countryCode = @"US";
  4406. }
  4407. stateCode = [dic valueForKey:@"state_code"];
  4408. city = [dic valueForKey:@"city"];
  4409. // zip code
  4410. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4411. [zipDic setValue:zipCode forKey:@"value"];
  4412. [section_0 setValue:zipDic forKey:@"item_11"];
  4413. }
  4414. }
  4415. } else {
  4416. // default: US United States
  4417. countryCode = @"US";
  4418. countryCode_id = @"228";
  4419. }
  4420. // country
  4421. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4422. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4423. // state
  4424. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4425. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4426. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4427. // city
  4428. if (city) {
  4429. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4430. [cityDic setValue:city forKey:@"value"];
  4431. [section_0 setValue:cityDic forKey:@"item_13"];
  4432. }
  4433. // price type
  4434. NSDictionary *priceDic = [self offline_getPrice];
  4435. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4436. // contact type
  4437. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4438. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4439. // Sales Rep
  4440. NSDictionary *repDic = [self offline_getSalesRep];
  4441. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4442. [ret setValue:section_0 forKey:@"section_0"];
  4443. return [RAUtils dict2data:ret];
  4444. }
  4445. #pragma mark save
  4446. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4447. NSString *addr = nil;
  4448. NSString *contact_name = nil;
  4449. NSString *companyName = [params objectForKey:@"company"];
  4450. if (companyName) {
  4451. companyName = [AESCrypt fastencrypt:companyName];
  4452. } else {
  4453. companyName = @"";
  4454. }
  4455. DebugLog(@"company");
  4456. companyName = [self translateSingleQuote:companyName];
  4457. NSString *addr1 = [params objectForKey:@"address"];
  4458. NSString *addr2 = [params objectForKey:@"address2"];
  4459. NSString *addr3 = [params objectForKey:@"address_3"];
  4460. NSString *addr4 = [params objectForKey:@"address_4"];
  4461. if (!addr2) {
  4462. addr2 = @"";
  4463. }
  4464. if (!addr3) {
  4465. addr3 = @"";
  4466. }
  4467. if (!addr4) {
  4468. addr4 = @"";
  4469. }
  4470. if (!addr1) {
  4471. addr1 = @"";
  4472. }
  4473. DebugLog(@"addr");
  4474. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4475. addr = [AESCrypt fastencrypt:addr];
  4476. addr = [self translateSingleQuote:addr];
  4477. if (addr1 && ![addr1 isEqualToString:@""]) {
  4478. addr1 = [AESCrypt fastencrypt:addr1];
  4479. }
  4480. addr1 = [self translateSingleQuote:addr1];
  4481. addr2 = [self translateSingleQuote:addr2];
  4482. addr3 = [self translateSingleQuote:addr3];
  4483. addr4 = [self translateSingleQuote:addr4];
  4484. NSString *country = [params objectForKey:@"country"];
  4485. if (country) {
  4486. country = [self countryNameByCountryCodeId:country];
  4487. } else {
  4488. country = @"";
  4489. }
  4490. DebugLog(@"country");
  4491. country = [self translateSingleQuote:country];
  4492. NSString *state = [params objectForKey:@"state"];
  4493. if (!state) {
  4494. state = @"";
  4495. }
  4496. DebugLog(@"state");
  4497. state = [self translateSingleQuote:state];
  4498. NSString *city = [params objectForKey:@"city"];
  4499. if (!city) {
  4500. city = @"";
  4501. }
  4502. city = [self translateSingleQuote:city];
  4503. NSString *zipcode = [params objectForKey:@"zipcode"];
  4504. if (!zipcode) {
  4505. zipcode = @"";
  4506. }
  4507. DebugLog(@"zip");
  4508. zipcode = [self translateSingleQuote:zipcode];
  4509. NSString *fistName = [params objectForKey:@"firstname"];
  4510. if (!fistName) {
  4511. fistName = @"";
  4512. }
  4513. NSString *lastName = [params objectForKey:@"lastname"];
  4514. if (!lastName) {
  4515. lastName = @"";
  4516. }
  4517. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4518. DebugLog(@"contact_name");
  4519. contact_name = [self translateSingleQuote:contact_name];
  4520. fistName = [self translateSingleQuote:fistName];
  4521. lastName = [self translateSingleQuote:lastName];
  4522. NSString *phone = [params objectForKey:@"phone"];
  4523. if (phone) {
  4524. phone = [AESCrypt fastencrypt:phone];
  4525. } else {
  4526. phone = @"";
  4527. }
  4528. DebugLog(@"PHONE");
  4529. phone = [self translateSingleQuote:phone];
  4530. NSString *fax = [params objectForKey:@"fax"];
  4531. if (!fax) {
  4532. fax = @"";
  4533. }
  4534. DebugLog(@"FAX");
  4535. fax = [self translateSingleQuote:fax];
  4536. NSString *email = [params objectForKey:@"email"];
  4537. if (!email) {
  4538. email = @"";
  4539. }
  4540. DebugLog(@"EMAIL:%@",email);
  4541. email = [self translateSingleQuote:email];
  4542. NSString *notes = [params objectForKey:@"contact_notes"];
  4543. if (!notes) {
  4544. notes = @"";
  4545. }
  4546. DebugLog(@"NOTE:%@",notes);
  4547. notes = [self translateSingleQuote:notes];
  4548. NSString *price = [params objectForKey:@"price_name"];
  4549. if (price) {
  4550. price = [self priceNameByPriceId:price];
  4551. } else {
  4552. price = @"";
  4553. }
  4554. DebugLog(@"PRICE");
  4555. price = [self translateSingleQuote:price];
  4556. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4557. if (salesRep) {
  4558. salesRep = [self salesRepCodeById:salesRep];
  4559. } else {
  4560. salesRep = @"";
  4561. }
  4562. salesRep = [self translateSingleQuote:salesRep];
  4563. NSString *img = [params objectForKey:@"business_card"];
  4564. NSArray *array = [img componentsSeparatedByString:@","];
  4565. NSString *img_0 = array[0];
  4566. if (!img_0) {
  4567. img_0 = @"";
  4568. }
  4569. img_0 = [self translateSingleQuote:img_0];
  4570. NSString *img_1 = array[1];
  4571. if (!img_1) {
  4572. img_1 = @"";
  4573. }
  4574. img_1 = [self translateSingleQuote:img_1];
  4575. NSString *img_2 = array[2];
  4576. if (!img_2) {
  4577. img_2 = @"";
  4578. }
  4579. img_2 = [self translateSingleQuote:img_2];
  4580. NSString *contact_id = [NSUUID UUID].UUIDString;
  4581. NSString *contact_type = [params objectForKey:@"type_name"];
  4582. if (!contact_type) {
  4583. contact_type = @"";
  4584. }
  4585. contact_type = [self translateSingleQuote:contact_type];
  4586. // 判断更新时是否为customer
  4587. if (update) {
  4588. contact_id = [params objectForKey:@"contact_id"];
  4589. if (!contact_id) {
  4590. contact_id = @"";
  4591. }
  4592. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4593. __block int customer = 0;
  4594. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4595. customer = sqlite3_column_int(stmt, 0);
  4596. }];
  4597. isCustomer = customer ? YES : NO;
  4598. }
  4599. NSMutableDictionary *sync_dic = [params mutableCopy];
  4600. if (isCustomer) {
  4601. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4602. } else {
  4603. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4604. }
  4605. NSString *sync_data = nil;
  4606. NSString *sql = nil;
  4607. if (update){
  4608. contact_id = [params objectForKey:@"contact_id"];
  4609. if (!contact_id) {
  4610. contact_id = @"";
  4611. }
  4612. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4613. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4614. sync_data = [RAUtils dict2string:sync_dic];
  4615. sync_data = [self translateSingleQuote:sync_data];
  4616. 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 = '%@',type = '%@' 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_type,contact_id];
  4617. } else {
  4618. contact_id = [self translateSingleQuote:contact_id];
  4619. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4620. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4621. sync_data = [RAUtils dict2string:sync_dic];
  4622. sync_data = [self translateSingleQuote:sync_data];
  4623. 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,type) 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,contact_type];
  4624. }
  4625. int result = [iSalesDB execSql:sql];
  4626. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4627. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4628. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4629. }
  4630. #pragma mark save new contact
  4631. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4632. {
  4633. return [self offline_saveContact:params update:NO isCustomer:YES];
  4634. }
  4635. #pragma mark edit contact
  4636. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4637. {
  4638. // {
  4639. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4640. // password = 123456;
  4641. // user = EvanK;
  4642. // }
  4643. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4644. NSData *data = [NSData dataWithContentsOfFile:path];
  4645. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4646. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4647. NSString *countryCode = nil;
  4648. NSString *countryCode_id = nil;
  4649. NSString *stateCode = nil;
  4650. /*------contact infor------*/
  4651. __block NSString *country = nil;
  4652. __block NSString *company_name = nil;
  4653. __block NSString *contact_id = params[@"contact_id"];
  4654. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4655. __block NSString *zipcode = nil;
  4656. __block NSString *state = nil; // state_code
  4657. __block NSString *city = nil; //
  4658. __block NSString *firt_name,*last_name;
  4659. __block NSString *phone,*fax,*email;
  4660. __block NSString *notes,*price_type,*sales_rep;
  4661. __block NSString *img_0,*img_1,*img_2;
  4662. __block NSString *contact_type;
  4663. contact_id = [self translateSingleQuote:contact_id];
  4664. 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,type from offline_contact where contact_id = '%@';",contact_id];
  4665. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4666. country = [self textAtColumn:0 statement:stmt]; // country name
  4667. company_name = [self textAtColumn:1 statement:stmt];
  4668. addr_1 = [self textAtColumn:2 statement:stmt];
  4669. addr_2 = [self textAtColumn:3 statement:stmt];
  4670. addr_3 = [self textAtColumn:4 statement:stmt];
  4671. addr_4 = [self textAtColumn:5 statement:stmt];
  4672. zipcode = [self textAtColumn:6 statement:stmt];
  4673. state = [self textAtColumn:7 statement:stmt]; // state code
  4674. city = [self textAtColumn:8 statement:stmt];
  4675. firt_name = [self textAtColumn:9 statement:stmt];
  4676. last_name = [self textAtColumn:10 statement:stmt];
  4677. phone = [self textAtColumn:11 statement:stmt];
  4678. fax = [self textAtColumn:12 statement:stmt];
  4679. email = [self textAtColumn:13 statement:stmt];
  4680. notes = [self textAtColumn:14 statement:stmt];
  4681. price_type = [self textAtColumn:15 statement:stmt]; // name
  4682. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4683. img_0 = [self textAtColumn:17 statement:stmt];
  4684. img_1 = [self textAtColumn:18 statement:stmt];
  4685. img_2 = [self textAtColumn:19 statement:stmt];
  4686. contact_type = [self textAtColumn:20 statement:stmt];
  4687. }];
  4688. // decrypt
  4689. if (company_name) {
  4690. company_name = [AESCrypt fastdecrypt:company_name];
  4691. }
  4692. if (addr_1) {
  4693. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4694. }
  4695. if (phone) {
  4696. phone = [AESCrypt fastdecrypt:phone];
  4697. }
  4698. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4699. countryCode = [iSalesDB jk_queryText:countrySql];
  4700. stateCode = state;
  4701. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4702. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4703. NSString *code_id = params[@"country"];
  4704. countryCode_id = code_id;
  4705. countryCode = [self countryCodeByid:code_id];
  4706. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4707. NSString *zip_code = params[@"zipcode"];
  4708. // 剔除全部为空格
  4709. int spaceCount = 0;
  4710. for (int i = 0; i < zip_code.length; i++) {
  4711. if ([zip_code characterAtIndex:i] == ' ') {
  4712. spaceCount++;
  4713. }
  4714. }
  4715. if (spaceCount == zip_code.length) {
  4716. zip_code = @"";
  4717. }
  4718. if (zipcode.length > 0) {
  4719. zipcode = zip_code;
  4720. countryCode_id = params[@"country"];
  4721. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4722. countryCode = [dic valueForKey:@"country_code"];
  4723. stateCode = [dic valueForKey:@"state_code"];
  4724. city = [dic valueForKey:@"city"];
  4725. // zip code
  4726. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4727. [zipDic setValue:zipcode forKey:@"value"];
  4728. [section_0 setValue:zipDic forKey:@"item_8"];
  4729. }
  4730. }
  4731. }
  4732. // 0 Country
  4733. // 1 Company Name
  4734. // 2 Contact ID
  4735. // 3 Picture
  4736. // 4 Address 1
  4737. // 5 Address 2
  4738. // 6 Address 3
  4739. // 7 Address 4
  4740. // 8 Zip Code
  4741. // 9 State/Province
  4742. // 10 City
  4743. // 11 Contact First Name
  4744. // 12 Contact Last Name
  4745. // 13 Phone
  4746. // 14 Fax
  4747. // 15 Email
  4748. // 16 Contact Notes
  4749. // 17 Price Type
  4750. // 18 Contact Type
  4751. // 19 Sales Rep
  4752. // country
  4753. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4754. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4755. // company
  4756. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4757. // contact_id
  4758. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4759. // picture
  4760. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4761. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4762. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4763. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4764. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4765. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4766. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4767. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4768. // addr 1 2 3 4
  4769. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4770. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4771. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4772. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4773. // zip code
  4774. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4775. // state
  4776. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4777. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4778. // city
  4779. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4780. // first last
  4781. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4782. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4783. // phone fax email
  4784. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4785. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4786. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4787. // notes
  4788. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4789. // price
  4790. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4791. for (NSString *key in priceDic.allKeys) {
  4792. if ([key containsString:@"val_"]) {
  4793. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4794. if ([dic[@"value"] isEqualToString:price_type]) {
  4795. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4796. [priceDic setValue:dic forKey:key];
  4797. }
  4798. }
  4799. }
  4800. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4801. // Contact Rep
  4802. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  4803. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4804. // Sales Rep
  4805. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4806. for (NSString *key in repDic.allKeys) {
  4807. if ([key containsString:@"val_"]) {
  4808. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4809. NSString *value = dic[@"value"];
  4810. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4811. if (code && [code isEqualToString:sales_rep]) {
  4812. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4813. [repDic setValue:dic forKey:key];
  4814. }
  4815. }
  4816. }
  4817. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  4818. [ret setValue:section_0 forKey:@"section_0"];
  4819. return [RAUtils dict2data:ret];
  4820. }
  4821. #pragma mark save contact
  4822. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4823. {
  4824. return [self offline_saveContact:params update:YES isCustomer:YES];
  4825. }
  4826. #pragma mark category
  4827. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4828. if (ck) {
  4829. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4830. for (NSString *key in res.allKeys) {
  4831. if (![key isEqualToString:@"count"]) {
  4832. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4833. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4834. if ([val[@"value"] isEqualToString:ck]) {
  4835. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4836. }
  4837. [res setValue:val forKey:key];
  4838. }
  4839. }
  4840. [dic setValue:res forKey:valueKey];
  4841. }
  4842. }
  4843. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4844. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4845. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4846. NSString* category = [params valueForKey:@"category"];
  4847. if (!category || [category isEqualToString:@""]) {
  4848. category = @"%";
  4849. }
  4850. category = [self translateSingleQuote:category];
  4851. int limit = [[params valueForKey:@"limit"] intValue];
  4852. int offset = [[params valueForKey:@"offset"] intValue];
  4853. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4854. NSString *limit_str = @"";
  4855. if (limited) {
  4856. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4857. }
  4858. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4859. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4860. sqlite3 *db = [iSalesDB get_db];
  4861. // [iSalesDB AddExFunction:db];
  4862. int count;
  4863. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4864. 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];
  4865. double price_min = 0;
  4866. double price_max = 0;
  4867. if ([params.allKeys containsObject:@"alert"]) {
  4868. // alert
  4869. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4870. NSString *alert = params[@"alert"];
  4871. if ([alert isEqualToString:@"Display All"]) {
  4872. alert = [NSString stringWithFormat:@""];
  4873. } else {
  4874. alert = [self translateSingleQuote:alert];
  4875. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4876. }
  4877. // available
  4878. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4879. NSString *available = params[@"available"];
  4880. NSString *available_condition;
  4881. if ([available isEqualToString:@"Display All"]) {
  4882. available_condition = @"";
  4883. } else if ([available isEqualToString:@"Available Now"]) {
  4884. available_condition = @"and availability > 0";
  4885. } else {
  4886. available_condition = @"and availability == 0";
  4887. }
  4888. // best seller
  4889. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4890. NSString *best_seller = @"";
  4891. NSString *order_best_seller = @"m.name asc";
  4892. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4893. best_seller = @"and best_seller > 0";
  4894. order_best_seller = @"m.best_seller desc,m.name asc";
  4895. }
  4896. // price
  4897. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4898. NSString *price = params[@"price"];
  4899. price_min = 0;
  4900. price_max = MAXFLOAT;
  4901. if (appDelegate.user) {
  4902. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4903. NSMutableString *priceName = [NSMutableString string];
  4904. for (int i = 0; i < priceTypeArray.count; i++) {
  4905. NSString *pricetype = priceTypeArray[i];
  4906. pricetype = [self translateSingleQuote:pricetype];
  4907. if (i == 0) {
  4908. [priceName appendFormat:@"'%@'",pricetype];
  4909. } else {
  4910. [priceName appendFormat:@",'%@'",pricetype];
  4911. }
  4912. }
  4913. if ([price isEqualToString:@"Display All"]) {
  4914. price = [NSString stringWithFormat:@""];
  4915. } else if([price containsString:@"+"]){
  4916. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4917. price_min = [price doubleValue];
  4918. 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];
  4919. } else {
  4920. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4921. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4922. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4923. 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];
  4924. }
  4925. } else {
  4926. price = @"";
  4927. }
  4928. // sold_by_qty : Sold in quantities of %@
  4929. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4930. NSString *qty = params[@"sold_by_qty"];
  4931. if ([qty isEqualToString:@"Display All"]) {
  4932. qty = @"";
  4933. } else {
  4934. qty = [self translateSingleQuote:qty];
  4935. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4936. }
  4937. // cate
  4938. category = [self translateSingleQuote:category];
  4939. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4940. // where bestseller > 0 order by bestseller desc
  4941. // sql query: alert availability(int) best_seller(int) price qty
  4942. 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];
  4943. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4944. }
  4945. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4946. if (!appDelegate.user) {
  4947. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4948. }
  4949. [ret setValue:filter forKey:@"filter"];
  4950. DebugLog(@"offline_category sql:%@",sqlQuery);
  4951. sqlite3_stmt * statement;
  4952. [ret setValue:@"2" forKey:@"result"];
  4953. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4954. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4955. // int count=0;
  4956. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4957. {
  4958. int i=0;
  4959. while (sqlite3_step(statement) == SQLITE_ROW)
  4960. {
  4961. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4962. char *name = (char*)sqlite3_column_text(statement, 0);
  4963. if(name==nil)
  4964. name="";
  4965. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4966. char *description = (char*)sqlite3_column_text(statement, 1);
  4967. if(description==nil)
  4968. description="";
  4969. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4970. int product_id = sqlite3_column_int(statement, 2);
  4971. int wid = sqlite3_column_int(statement, 3);
  4972. int closeout = sqlite3_column_int(statement, 4);
  4973. int cid = sqlite3_column_int(statement, 5);
  4974. int wisdelete = sqlite3_column_int(statement, 6);
  4975. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4976. if(wid !=0 && wisdelete != 1)
  4977. [item setValue:@"true" forKey:@"wish_exists"];
  4978. else
  4979. [item setValue:@"false" forKey:@"wish_exists"];
  4980. if(closeout==0)
  4981. [item setValue:@"false" forKey:@"is_closeout"];
  4982. else
  4983. [item setValue:@"true" forKey:@"is_closeout"];
  4984. if(cid==0)
  4985. [item setValue:@"false" forKey:@"cart_exists"];
  4986. else
  4987. [item setValue:@"true" forKey:@"cart_exists"];
  4988. [item addEntriesFromDictionary:imgjson];
  4989. // [item setValue:nsurl forKey:@"img"];
  4990. [item setValue:nsname forKey:@"name"];
  4991. [item setValue:nsdescription forKey:@"description"];
  4992. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4993. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4994. i++;
  4995. }
  4996. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4997. [ret setObject:items forKey:@"items"];
  4998. sqlite3_finalize(statement);
  4999. } else {
  5000. DebugLog(@"nothing...");
  5001. }
  5002. DebugLog(@"count:%d",count);
  5003. [iSalesDB close_db:db];
  5004. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5005. return ret;
  5006. }
  5007. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5008. {
  5009. return [self categoryList:params limited:YES];
  5010. }
  5011. # pragma mark item search
  5012. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5013. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5014. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5015. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5016. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5017. // category
  5018. NSDictionary *category_menu = [self offline_category_menu];
  5019. [filter setValue:category_menu forKey:@"category"];
  5020. NSString* where= nil;
  5021. NSString* orderby= @"m.name";
  5022. if (!filterSearch) {
  5023. int covertype = [[params valueForKey:@"covertype"] intValue];
  5024. switch (covertype) {
  5025. case 0:
  5026. {
  5027. where=@"m.category like'%%#005#%%'";
  5028. break;
  5029. }
  5030. case 1:
  5031. {
  5032. where=@"m.alert like '%QS%'";
  5033. break;
  5034. }
  5035. case 2:
  5036. {
  5037. where=@"m.availability>0";
  5038. break;
  5039. }
  5040. case 3:
  5041. {
  5042. where=@"m.best_seller>0";
  5043. orderby=@"m.best_seller desc,m.name asc";
  5044. break;
  5045. }
  5046. default:
  5047. where=@"1=1";
  5048. break;
  5049. }
  5050. }
  5051. int limit = [[params valueForKey:@"limit"] intValue];
  5052. int offset = [[params valueForKey:@"offset"] intValue];
  5053. NSString *limit_str = @"";
  5054. if (limited) {
  5055. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5056. }
  5057. sqlite3 *db = [iSalesDB get_db];
  5058. // [iSalesDB AddExFunction:db];
  5059. int count;
  5060. NSString *sqlQuery = nil;
  5061. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5062. 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];
  5063. double price_min = 0;
  5064. double price_max = 0;
  5065. if (filterSearch) {
  5066. // alert
  5067. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5068. NSString *alert = params[@"alert"];
  5069. if ([alert isEqualToString:@"Display All"]) {
  5070. alert = [NSString stringWithFormat:@""];
  5071. } else {
  5072. alert = [self translateSingleQuote:alert];
  5073. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5074. }
  5075. // available
  5076. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5077. NSString *available = params[@"available"];
  5078. NSString *available_condition;
  5079. if ([available isEqualToString:@"Display All"]) {
  5080. available_condition = @"";
  5081. } else if ([available isEqualToString:@"Available Now"]) {
  5082. available_condition = @"and availability > 0";
  5083. } else {
  5084. available_condition = @"and availability == 0";
  5085. }
  5086. // best seller
  5087. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5088. NSString *best_seller = @"";
  5089. NSString *order_best_seller = @"m.name asc";
  5090. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5091. best_seller = @"and best_seller > 0";
  5092. order_best_seller = @"m.best_seller desc,m.name asc";
  5093. }
  5094. // price
  5095. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5096. NSString *price = params[@"price"];
  5097. price_min = 0;
  5098. price_max = MAXFLOAT;
  5099. if (appDelegate.user) {
  5100. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5101. NSMutableString *priceName = [NSMutableString string];
  5102. for (int i = 0; i < priceTypeArray.count; i++) {
  5103. NSString *pricetype = priceTypeArray[i];
  5104. pricetype = [self translateSingleQuote:pricetype];
  5105. if (i == 0) {
  5106. [priceName appendFormat:@"'%@'",pricetype];
  5107. } else {
  5108. [priceName appendFormat:@",'%@'",pricetype];
  5109. }
  5110. }
  5111. if ([price isEqualToString:@"Display All"]) {
  5112. price = [NSString stringWithFormat:@""];
  5113. } else if([price containsString:@"+"]){
  5114. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5115. price_min = [price doubleValue];
  5116. 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];
  5117. } else {
  5118. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5119. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5120. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5121. 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];
  5122. }
  5123. } else {
  5124. price = @"";
  5125. }
  5126. // sold_by_qty : Sold in quantities of %@
  5127. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5128. NSString *qty = params[@"sold_by_qty"];
  5129. if ([qty isEqualToString:@"Display All"]) {
  5130. qty = @"";
  5131. } else {
  5132. qty = [self translateSingleQuote:qty];
  5133. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5134. }
  5135. // category
  5136. NSString *category_id = params[@"ctgId"];
  5137. NSMutableArray *cate_id_array = nil;
  5138. NSMutableString *cateWhere = [NSMutableString string];
  5139. if ([category_id isEqualToString:@""] || !category_id) {
  5140. [cateWhere appendString:@"1 = 1"];
  5141. } else {
  5142. if ([category_id containsString:@","]) {
  5143. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5144. } else {
  5145. cate_id_array = [@[category_id] mutableCopy];
  5146. }
  5147. /*-----------*/
  5148. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5149. for (int i = 0; i < cate_id_array.count; i++) {
  5150. for (NSString *key0 in cateDic.allKeys) {
  5151. if ([key0 containsString:@"category_"]) {
  5152. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5153. for (NSString *key1 in category0.allKeys) {
  5154. if ([key1 containsString:@"category_"]) {
  5155. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5156. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5157. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5158. cate_id_array[i] = [category1 objectForKey:@"id"];
  5159. if (i == 0) {
  5160. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5161. } else {
  5162. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5163. }
  5164. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5165. [category0 setValue:category1 forKey:key1];
  5166. [cateDic setValue:category0 forKey:key0];
  5167. }
  5168. }
  5169. }
  5170. }
  5171. }
  5172. }
  5173. [filter setValue:cateDic forKey:@"category"];
  5174. }
  5175. // where bestseller > 0 order by bestseller desc
  5176. // sql query: alert availability(int) best_seller(int) price qty
  5177. 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];
  5178. // count
  5179. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5180. }
  5181. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5182. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5183. if (!appDelegate.user) {
  5184. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5185. }
  5186. [ret setValue:filter forKey:@"filter"];
  5187. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5188. sqlite3_stmt * statement;
  5189. [ret setValue:@"2" forKey:@"result"];
  5190. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5191. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5192. // int count=0;
  5193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5194. {
  5195. int i=0;
  5196. while (sqlite3_step(statement) == SQLITE_ROW)
  5197. {
  5198. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5199. char *name = (char*)sqlite3_column_text(statement, 0);
  5200. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5201. char *description = (char*)sqlite3_column_text(statement, 1);
  5202. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5203. int product_id = sqlite3_column_int(statement, 2);
  5204. int wid = sqlite3_column_int(statement, 3);
  5205. int closeout = sqlite3_column_int(statement, 4);
  5206. int cid = sqlite3_column_int(statement, 5);
  5207. int wisdelete = sqlite3_column_int(statement, 6);
  5208. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5209. if(wid !=0 && wisdelete != 1)
  5210. [item setValue:@"true" forKey:@"wish_exists"];
  5211. else
  5212. [item setValue:@"false" forKey:@"wish_exists"];
  5213. if(closeout==0)
  5214. [item setValue:@"false" forKey:@"is_closeout"];
  5215. else
  5216. [item setValue:@"true" forKey:@"is_closeout"];
  5217. if(cid==0)
  5218. [item setValue:@"false" forKey:@"cart_exists"];
  5219. else
  5220. [item setValue:@"true" forKey:@"cart_exists"];
  5221. [item addEntriesFromDictionary:imgjson];
  5222. // [item setValue:nsurl forKey:@"img"];
  5223. [item setValue:nsname forKey:@"fash_name"];
  5224. [item setValue:nsdescription forKey:@"description"];
  5225. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5226. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5227. i++;
  5228. }
  5229. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5230. [ret setObject:items forKey:@"items"];
  5231. sqlite3_finalize(statement);
  5232. }
  5233. [iSalesDB close_db:db];
  5234. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5235. return ret;
  5236. }
  5237. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5238. {
  5239. return [self itemsearch:params limited:YES];
  5240. }
  5241. #pragma mark order detail
  5242. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5243. if (str1.length == 0) {
  5244. str1 = @"&nbsp";
  5245. }
  5246. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5247. return str;
  5248. }
  5249. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5250. {
  5251. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5252. [fromformatter setDateFormat:from];
  5253. NSDate *date = [fromformatter dateFromString:datetime];
  5254. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5255. [toformatter setDateFormat:to];
  5256. NSString * ret = [toformatter stringFromDate:date];
  5257. return ret;
  5258. }
  5259. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5260. // 把毫秒去掉
  5261. if ([dateTime containsString:@"."]) {
  5262. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5263. }
  5264. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5265. formatter.dateFormat = formate;
  5266. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5267. NSDate *date = [formatter dateFromString:dateTime];
  5268. formatter.dateFormat = newFormate;
  5269. NSString *result = [formatter stringFromDate:date];
  5270. return result ? result : @"";
  5271. }
  5272. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5273. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5274. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5275. }
  5276. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5277. {
  5278. DebugLog(@"offline oderdetail params: %@",params);
  5279. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5280. int orderId = [params[@"orderId"] intValue];
  5281. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5282. // decrypt card number and card security code
  5283. // 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 ];
  5284. 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];
  5285. sqlite3 *db = [iSalesDB get_db];
  5286. sqlite3_stmt * statement;
  5287. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5288. NSString *nssoid = @"";
  5289. NSString* orderinfo = @"";
  5290. NSString *moreInfo = @"";
  5291. double handlingFee = 0;
  5292. double payments_and_credist = 0;
  5293. double totalPrice = 0;
  5294. double shippingFee = 0;
  5295. double lift_gate = 0;
  5296. NSString *customer_contact = @"";
  5297. NSString *customer_email = @"";
  5298. NSString *customer_fax = @"";
  5299. NSString *customer_phone = @"";
  5300. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5301. {
  5302. if (sqlite3_step(statement) == SQLITE_ROW)
  5303. {
  5304. int order_id = sqlite3_column_int(statement, 0);
  5305. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5306. ret[@"sign_url"] = sign_url;
  5307. customer_contact = [self textAtColumn:52 statement:statement];
  5308. customer_email = [self textAtColumn:53 statement:statement];
  5309. customer_phone = [self textAtColumn:54 statement:statement];
  5310. customer_fax = [self textAtColumn:55 statement:statement];
  5311. int offline_edit=sqlite3_column_int(statement, 56);
  5312. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5313. char *soid = (char*)sqlite3_column_text(statement, 1);
  5314. if(soid==nil)
  5315. soid= "";
  5316. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5317. // so#
  5318. ret[@"so#"] = nssoid;
  5319. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5320. if(poNumber==nil)
  5321. poNumber= "";
  5322. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5323. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5324. if(create_time==nil)
  5325. create_time= "";
  5326. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5327. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5328. int status = sqlite3_column_int(statement, 4);
  5329. int erpStatus = sqlite3_column_int(statement, 49);
  5330. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5331. // status
  5332. if (status > 1 && status != 3) {
  5333. status = erpStatus;
  5334. } else if (status == 3) {
  5335. status = 15;
  5336. }
  5337. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5338. ret[@"order_status"] = nsstatus;
  5339. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5340. if(company_name==nil)
  5341. company_name= "";
  5342. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5343. // company name
  5344. ret[@"company_name"] = nscompany_name;
  5345. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5346. if(customer_contact==nil)
  5347. customer_contact= "";
  5348. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5349. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5350. if(addr_1==nil)
  5351. addr_1="";
  5352. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5353. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5354. if(addr_2==nil)
  5355. addr_2="";
  5356. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5357. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5358. if(addr_3==nil)
  5359. addr_3="";
  5360. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5361. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5362. if(addr_4==nil)
  5363. addr_4="";
  5364. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5365. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5366. [arr_addr addObject:nsaddr_1];
  5367. [arr_addr addObject:nsaddr_2];
  5368. [arr_addr addObject:nsaddr_3];
  5369. [arr_addr addObject:nsaddr_4];
  5370. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5371. char *logist = (char*)sqlite3_column_text(statement, 11);
  5372. if(logist==nil)
  5373. logist= "";
  5374. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5375. if (status == -11 || status == 10 || status == 11) {
  5376. nslogist = [self textAtColumn:59 statement:statement];
  5377. };
  5378. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5379. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5380. shipping = @"Shipping To Be Quoted";
  5381. } else {
  5382. shippingFee = sqlite3_column_double(statement, 12);
  5383. }
  5384. // Shipping
  5385. ret[@"Shipping"] = shipping;
  5386. int have_lift_gate = sqlite3_column_int(statement, 17);
  5387. lift_gate = sqlite3_column_double(statement, 13);
  5388. // Liftgate Fee(No loading dock)
  5389. if (!have_lift_gate) {
  5390. lift_gate = 0;
  5391. }
  5392. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5393. if (sqlite3_column_int(statement, 57)) {
  5394. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5395. }
  5396. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5397. if(general_notes==nil)
  5398. general_notes= "";
  5399. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5400. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5401. if(internal_notes==nil)
  5402. internal_notes= "";
  5403. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5404. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5405. if(payment_type==nil)
  5406. payment_type= "";
  5407. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5408. // order info
  5409. orderinfo = [self textFileName:@"order_info.html"];
  5410. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5411. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5412. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5413. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5414. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5415. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5416. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5417. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5418. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5419. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5420. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5421. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5422. NSString *payment = nil;
  5423. if([nspayment_type isEqualToString:@"Credit Card"])
  5424. {
  5425. payment = [self textFileName:@"creditcardpayment.html"];
  5426. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5427. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5428. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5429. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5430. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5431. NSString *card_type = [self textAtColumn:42 statement:statement];
  5432. if (card_type.length > 0) { // 显示星号
  5433. card_type = @"****";
  5434. }
  5435. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5436. if (card_number.length > 0 && card_number.length > 4) {
  5437. for (int i = 0; i < card_number.length - 4; i++) {
  5438. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5439. }
  5440. } else {
  5441. card_number = @"";
  5442. }
  5443. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5444. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5445. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5446. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5447. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5448. card_expiration = @"****";
  5449. }
  5450. NSString *card_city = [self textAtColumn:46 statement:statement];
  5451. NSString *card_state = [self textAtColumn:47 statement:statement];
  5452. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5453. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5454. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5455. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5456. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5457. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5458. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5459. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5460. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5461. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5462. }
  5463. else
  5464. {
  5465. payment=[self textFileName:@"normalpayment.html"];
  5466. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5467. }
  5468. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5469. ret[@"result"]= [NSNumber numberWithInt:2];
  5470. // more info
  5471. moreInfo = [self textFileName:@"more_info.html"];
  5472. /*****ship to******/
  5473. // ShipToCompany_or_&nbsp
  5474. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5475. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5476. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5477. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5478. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5479. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5480. /*****ship from******/
  5481. // ShipFromCompany_or_&nbsp
  5482. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5483. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5484. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5485. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5486. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5487. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5488. /*****freight to******/
  5489. // FreightBillToCompany_or_&nbsp
  5490. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5491. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5492. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5493. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5494. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5495. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5496. /*****merchandise to******/
  5497. // MerchandiseBillToCompany_or_&nbsp
  5498. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5499. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5500. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5501. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5502. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5503. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5504. /*****return to******/
  5505. // ReturnToCompany_or_&nbsp
  5506. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5507. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5508. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5509. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5510. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5511. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5512. //
  5513. // DebugLog(@"more info : %@",moreInfo);
  5514. // handling fee
  5515. handlingFee = sqlite3_column_double(statement, 33);
  5516. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5517. if (sqlite3_column_int(statement, 58)) {
  5518. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5519. }
  5520. //
  5521. // customer info
  5522. customerID = [self textAtColumn:36 statement:statement];
  5523. // mode
  5524. ret[@"mode"] = appDelegate.mode;
  5525. // model_count
  5526. ret[@"model_count"] = @(0);
  5527. }
  5528. sqlite3_finalize(statement);
  5529. }
  5530. [iSalesDB close_db:db];
  5531. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5532. // "customer_email" = "Shui Hu";
  5533. // "customer_fax" = "";
  5534. // "customer_first_name" = F;
  5535. // "customer_last_name" = L;
  5536. // "customer_name" = ",da He Xiang Dong Liu A";
  5537. // "customer_phone" = "Hey Xuan Feng";
  5538. contactInfo[@"customer_phone"] = customer_phone;
  5539. contactInfo[@"customer_fax"] = customer_fax;
  5540. contactInfo[@"customer_email"] = customer_email;
  5541. NSString *first_name = @"";
  5542. NSString *last_name = @"";
  5543. if ([customer_contact isEqualToString:@""]) {
  5544. } else if ([customer_contact containsString:@" "]) {
  5545. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5546. first_name = [customer_contact substringToIndex:first_space_index];
  5547. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5548. }
  5549. contactInfo[@"customer_first_name"] = first_name;
  5550. contactInfo[@"customer_last_name"] = last_name;
  5551. ret[@"customerInfo"] = contactInfo;
  5552. // models
  5553. if (nssoid) {
  5554. __block double TotalCuft = 0;
  5555. __block double TotalWeight = 0;
  5556. __block int TotalCarton = 0;
  5557. __block double allItemPrice = 0;
  5558. 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];
  5559. sqlite3 *db1 = [iSalesDB get_db];
  5560. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5561. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5562. int product_id = sqlite3_column_int(stmt, 0);
  5563. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5564. int item_id = sqlite3_column_int(stmt, 7);
  5565. NSString* Price=nil;
  5566. if(str_price==nil)
  5567. {
  5568. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5569. if(price==nil)
  5570. Price=@"No Price.";
  5571. else
  5572. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5573. }
  5574. else
  5575. {
  5576. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5577. }
  5578. double discount = sqlite3_column_double(stmt, 2);
  5579. int item_count = sqlite3_column_int(stmt, 3);
  5580. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5581. NSString *nsline_note=nil;
  5582. if(line_note!=nil)
  5583. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5584. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5585. // NSString *nsname = nil;
  5586. // if(name!=nil)
  5587. // nsname= [[NSString alloc]initWithUTF8String:name];
  5588. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5589. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5590. // NSString *nsdescription=nil;
  5591. // if(description!=nil)
  5592. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5593. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5594. // int stockUom = sqlite3_column_int(stmt, 8);
  5595. // int _id = sqlite3_column_int(stmt, 9);
  5596. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5597. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5598. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5599. int carton=[bsubtotaljson[@"carton"] intValue];
  5600. TotalCuft += cuft;
  5601. TotalWeight += weight;
  5602. TotalCarton += carton;
  5603. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5604. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5605. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5606. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5607. itemjson[@"note"]=nsline_note;
  5608. itemjson[@"origin_price"] = Price;
  5609. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5610. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5611. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5612. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5613. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5614. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5615. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5616. if(itemjson[@"combine"] != nil)
  5617. {
  5618. // int citem=0;
  5619. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5620. for(int bc=0;bc<bcount;bc++)
  5621. {
  5622. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5623. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5624. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5625. subTotal += uprice * modulus * item_count;
  5626. }
  5627. }
  5628. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5629. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5630. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5631. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5632. allItemPrice += subTotal;
  5633. }];
  5634. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5635. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5636. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5637. // payments/Credits
  5638. // payments_and_credist = sqlite3_column_double(statement, 34);
  5639. payments_and_credist = allItemPrice;
  5640. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5641. // // total
  5642. // totalPrice = sqlite3_column_double(statement, 35);
  5643. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5644. } else {
  5645. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5646. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5647. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  5648. // payments/Credits
  5649. payments_and_credist = 0;
  5650. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5651. }
  5652. // total
  5653. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5654. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5655. ret[@"order_info"]= orderinfo;
  5656. ret[@"more_order_info"] = moreInfo;
  5657. return [RAUtils dict2data:ret];
  5658. }
  5659. #pragma mark order list
  5660. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  5661. NSString *nsstatus = @"";
  5662. switch (status) {
  5663. case 0:
  5664. {
  5665. nsstatus=@"Temp Order";
  5666. break;
  5667. }
  5668. case 1:
  5669. {
  5670. nsstatus=@"Saved Order";
  5671. break;
  5672. }
  5673. case 2: {
  5674. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  5675. break;
  5676. }
  5677. case 3:
  5678. case 15:
  5679. {
  5680. nsstatus=@"Cancelled";
  5681. break;
  5682. }
  5683. case 10:
  5684. {
  5685. nsstatus=@"Quote Submitted";
  5686. break;
  5687. }
  5688. case 11:
  5689. {
  5690. nsstatus=@"Sales Order Submitted";
  5691. break;
  5692. }
  5693. case 12:
  5694. {
  5695. nsstatus=@"Processing";
  5696. break;
  5697. }
  5698. case 13:
  5699. {
  5700. nsstatus=@"Shipped";
  5701. break;
  5702. }
  5703. case 14:
  5704. {
  5705. nsstatus=@"Closed";
  5706. break;
  5707. }
  5708. case -11:
  5709. {
  5710. nsstatus = @"Ready For Submit";
  5711. break;
  5712. }
  5713. default:
  5714. break;
  5715. }
  5716. return nsstatus;
  5717. }
  5718. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5719. double total = 0;
  5720. __block double payments_and_credist = 0;
  5721. __block double allItemPrice = 0;
  5722. // sqlite3 *db1 = [iSalesDB get_db];
  5723. if (so_id) {
  5724. // 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];
  5725. 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];
  5726. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5727. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5728. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5729. int product_id = sqlite3_column_int(stmt, 0);
  5730. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5731. int discount = sqlite3_column_int(stmt, 2);
  5732. int item_count = sqlite3_column_int(stmt, 3);
  5733. // int item_id = sqlite3_column_int(stmt, 7);
  5734. int item_id = sqlite3_column_int(stmt, 4);
  5735. NSString* Price=nil;
  5736. if(str_price==nil)
  5737. {
  5738. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5739. if(price==nil)
  5740. Price=@"No Price.";
  5741. else
  5742. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5743. }
  5744. else
  5745. {
  5746. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5747. }
  5748. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5749. itemjson[@"The unit price"]=Price;
  5750. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5751. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5752. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5753. if(itemjson[@"combine"] != nil)
  5754. {
  5755. // int citem=0;
  5756. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5757. for(int bc=0;bc<bcount;bc++)
  5758. {
  5759. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5760. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5761. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5762. subTotal += uprice * modulus * item_count;
  5763. }
  5764. }
  5765. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5766. allItemPrice += subTotal;
  5767. }];
  5768. payments_and_credist = allItemPrice;
  5769. } else {
  5770. // payments/Credits
  5771. payments_and_credist = 0;
  5772. }
  5773. // lift_gate handlingFee shippingFee
  5774. __block double lift_gate = 0;
  5775. __block double handlingFee = 0;
  5776. __block double shippingFee = 0;
  5777. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5778. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5779. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5780. if (have_lift_gate) {
  5781. lift_gate = sqlite3_column_double(stmt, 1);
  5782. }
  5783. handlingFee = sqlite3_column_double(stmt, 2);
  5784. shippingFee = sqlite3_column_double(stmt, 3);
  5785. }];
  5786. // total
  5787. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5788. if (close) {
  5789. [iSalesDB close_db:db1];
  5790. }
  5791. return total;
  5792. }
  5793. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5794. {
  5795. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5796. int limit = [[params valueForKey:@"limit"] intValue];
  5797. int offset = [[params valueForKey:@"offset"] intValue];
  5798. NSString* keyword = [params valueForKey:@"keyWord"];
  5799. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5800. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5801. NSString* where=@"1 = 1";
  5802. if(keyword.length>0)
  5803. 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]];
  5804. if (orderStatus.length > 0) {
  5805. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5806. if (order_status_array.count == 1) {
  5807. int status_value = [[order_status_array firstObject] integerValue];
  5808. if (status_value <= 1 || status_value == 3) {
  5809. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5810. } else {
  5811. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5812. }
  5813. } else if (order_status_array.count > 1) {
  5814. for (int i = 0; i < order_status_array.count;i++) {
  5815. NSString *status = order_status_array[i];
  5816. NSString *condition = @" or";
  5817. if (i == 0) {
  5818. condition = @" and (";
  5819. }
  5820. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5821. int status_value = [status integerValue];
  5822. if (status_value <= 1 || status_value == 3) {
  5823. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5824. } else {
  5825. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5826. }
  5827. }
  5828. where = [where stringByAppendingString:@" )"];
  5829. }
  5830. }
  5831. 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];
  5832. // DebugLog(@"order list sql: %@",sqlQuery);
  5833. sqlite3 *db = [iSalesDB get_db];
  5834. sqlite3_stmt * statement;
  5835. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5836. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5837. {
  5838. int count=0;
  5839. while (sqlite3_step(statement) == SQLITE_ROW)
  5840. {
  5841. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5842. int order_id = sqlite3_column_double(statement, 0);
  5843. char *soid = (char*)sqlite3_column_text(statement, 1);
  5844. if(soid==nil)
  5845. soid= "";
  5846. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5847. int status = sqlite3_column_double(statement, 2);
  5848. int erpStatus = sqlite3_column_double(statement, 9);
  5849. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5850. if(sales_rep==nil)
  5851. sales_rep= "";
  5852. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5853. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5854. if(create_by==nil)
  5855. create_by= "";
  5856. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5857. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5858. if(company_name==nil)
  5859. company_name= "";
  5860. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5861. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5862. if(create_time==nil)
  5863. create_time= "";
  5864. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5865. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5866. // double total_price = sqlite3_column_double(statement, 7);
  5867. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5868. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5869. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5870. int offline_edit = sqlite3_column_int(statement, 10);
  5871. // ": "JH",
  5872. // "": "$8307.00",
  5873. // "": "MOB1608050001",
  5874. // "": "ArpithaT",
  5875. // "": "1st Stage Property Transformations",
  5876. // "": "JANICE SUTTON",
  5877. // "": 2255,
  5878. // "": "08/02/2016 09:49:18",
  5879. // "": 1,
  5880. // "": "Saved Order"
  5881. // "": "1470384050483",
  5882. // "model_count": "6 / 28"
  5883. item[@"sales_rep"]= nssales_rep;
  5884. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5885. item[@"so#"]= nssoid;
  5886. item[@"create_by"]= nscreate_by;
  5887. item[@"customer_name"]= nscompany_name;
  5888. item[@"customer_contact"] = customer_contact;
  5889. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5890. item[@"purchase_time"]= nscreate_time;
  5891. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5892. item[@"order_status"]= nsstatus;
  5893. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5894. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5895. // item[@"model_count"]
  5896. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5897. count++;
  5898. }
  5899. ret[@"count"]= [NSNumber numberWithInt:count];
  5900. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5901. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5902. ret[@"result"]= [NSNumber numberWithInt:2];
  5903. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5904. ret[@"time_zone"] = @"PST";
  5905. sqlite3_finalize(statement);
  5906. }
  5907. 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];
  5908. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5909. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5910. [iSalesDB close_db:db];
  5911. return [RAUtils dict2data:ret];
  5912. }
  5913. #pragma mark update gnotes
  5914. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5915. {
  5916. DebugLog(@"params: %@",params);
  5917. // comments = Meyoyoyoyoyoyoy;
  5918. // orderCode = MOB1608110001;
  5919. // password = 123456;
  5920. // user = EvanK;
  5921. 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]];
  5922. int ret = [iSalesDB execSql:sql];
  5923. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5924. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5925. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5926. // [dic setValue:@"160409" forKey:@"min_ver"];
  5927. return [RAUtils dict2data:dic];
  5928. }
  5929. #pragma mark move to wishlist
  5930. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5931. {
  5932. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5933. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5934. _id = [NSString stringWithFormat:@"(%@)",_id];
  5935. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5936. sqlite3 *db = [iSalesDB get_db];
  5937. __block NSString *product_id = @"";
  5938. __block NSString *item_count_str = @"";
  5939. // __block NSString *item_id = nil;
  5940. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5941. // product_id = [self textAtColumn:0 statement:stmt];
  5942. int item_count = sqlite3_column_int(stmt, 1);
  5943. // item_id = [self textAtColumn:2 statement:stmt];
  5944. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5945. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5946. if (p_id.length) {
  5947. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5948. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5949. }
  5950. }];
  5951. [iSalesDB close_db:db];
  5952. // 去除第一个,
  5953. product_id = [product_id substringFromIndex:1];
  5954. item_count_str = [item_count_str substringFromIndex:1];
  5955. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5956. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5957. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5958. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5959. sqlite3 *db1 = [iSalesDB get_db];
  5960. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5961. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5962. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5963. // 删除
  5964. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5965. int ret = [iSalesDB execSql:deleteSql db:db1];
  5966. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5967. [iSalesDB close_db:db1];
  5968. return [RAUtils dict2data:dic];
  5969. }
  5970. #pragma mark cart delete
  5971. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5972. {
  5973. // cartItemId = 548;
  5974. // orderCode = MOB1608110001;
  5975. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5976. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5977. _id = [NSString stringWithFormat:@"(%@)",_id];
  5978. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5979. int ret = [iSalesDB execSql:sql];
  5980. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5981. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5982. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5983. // [dic setValue:@"160409" forKey:@"min_ver"];
  5984. return [RAUtils dict2data:dic];
  5985. }
  5986. #pragma mark set price
  5987. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5988. {
  5989. DebugLog(@"cart set price params: %@",params);
  5990. // "cartitem_id" = 1;
  5991. // discount = "0.000000";
  5992. // "item_note" = "";
  5993. // price = "269.000000";
  5994. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5995. NSString *notes = [self valueInParams:params key:@"item_note"];
  5996. NSString *discount = [self valueInParams:params key:@"discount"];
  5997. NSString *price = [self valueInParams:params key:@"price"];
  5998. // bool badd_price_changed=false;
  5999. // sqlite3* db=[iSalesDB get_db];
  6000. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6001. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6002. // NSRange range;
  6003. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6004. //
  6005. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6006. // badd_price_changed=true;
  6007. // [iSalesDB close_db:db];
  6008. //
  6009. // if(badd_price_changed)
  6010. // {
  6011. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6012. // }
  6013. //
  6014. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6015. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6016. int ret = [iSalesDB execSql:sql];
  6017. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6018. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6019. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6020. // [dic setValue:@"160409" forKey:@"min_ver"];
  6021. return [RAUtils dict2data:dic];
  6022. }
  6023. #pragma mark set line notes
  6024. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6025. {
  6026. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6027. NSString *notes = [self valueInParams:params key:@"notes"];
  6028. notes = [self translateSingleQuote:notes];
  6029. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6030. int ret = [iSalesDB execSql:sql];
  6031. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6032. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6033. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6034. // [dic setValue:@"160409" forKey:@"min_ver"];
  6035. return [RAUtils dict2data:dic];
  6036. }
  6037. #pragma mark set qty
  6038. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6039. {
  6040. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6041. int item_count = [params[@"inputInt"] integerValue];
  6042. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6043. int ret = [iSalesDB execSql:sql];
  6044. __block int item_id = 0;
  6045. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6046. item_id = sqlite3_column_int(stmt, 0);
  6047. }];
  6048. sqlite3 *db = [iSalesDB get_db];
  6049. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6050. [iSalesDB close_db:db];
  6051. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6052. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6053. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6054. // [dic setValue:@"160409" forKey:@"min_ver"];
  6055. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6056. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6057. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6058. return [RAUtils dict2data:dic];
  6059. }
  6060. #pragma mark place order
  6061. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6062. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6063. if (aname) {
  6064. [dic setValue:aname forKey:@"aname"];
  6065. }
  6066. if (control) {
  6067. [dic setValue:control forKey:@"control"];
  6068. }
  6069. if (keyboard) {
  6070. [dic setValue:keyboard forKey:@"keyboard"];
  6071. }
  6072. if (name) {
  6073. [dic setValue:name forKey:@"name"];
  6074. }
  6075. if (value) {
  6076. [dic setValue:value forKey:@"value"];
  6077. }
  6078. return dic;
  6079. }
  6080. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6081. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6082. orderCode = [self translateSingleQuote:orderCode];
  6083. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6084. 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];
  6085. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6086. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6087. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6088. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6089. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6090. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6091. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6092. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6093. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6094. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6095. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6096. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6097. NSString *email = [self textAtColumn:11 statement:stmt];
  6098. NSString *phone = [self textAtColumn:12 statement:stmt];
  6099. NSString *fax = [self textAtColumn:13 statement:stmt];
  6100. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6101. NSString *city = [self textAtColumn:15 statement:stmt];
  6102. NSString *state = [self textAtColumn:16 statement:stmt];
  6103. NSString *country = [self textAtColumn:17 statement:stmt];
  6104. NSString *name = [self textAtColumn:18 statement:stmt];
  6105. // contact id
  6106. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6107. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6108. [contact_id_dic setValue:@"text" forKey:@"control"];
  6109. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6110. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6111. [contact_id_dic setValue:@"true" forKey:@"required"];
  6112. [contact_id_dic setValue:contact_id forKey:@"value"];
  6113. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6114. // business card
  6115. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6116. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6117. [business_card_dic setValue:@"img" forKey:@"control"];
  6118. [business_card_dic setValue:@"1" forKey:@"disable"];
  6119. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6120. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6121. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6122. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6123. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6124. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6125. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6126. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6127. // fax
  6128. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6129. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6130. // zipcode
  6131. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6132. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6133. // city
  6134. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6135. [customer_dic setValue:city_dic forKey:@"item_12"];
  6136. // state
  6137. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6138. [customer_dic setValue:state_dic forKey:@"item_13"];
  6139. // country
  6140. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6141. [customer_dic setValue:country_dic forKey:@"item_14"];
  6142. // company name
  6143. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6144. [customer_dic setValue:company_dic forKey:@"item_2"];
  6145. // addr_1
  6146. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6147. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6148. // addr_2
  6149. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6150. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6151. // addr_3
  6152. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6153. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6154. // addr_4
  6155. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6156. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6157. // Contact
  6158. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6159. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6160. // email
  6161. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6162. [customer_dic setValue:email_dic forKey:@"item_8"];
  6163. // phone
  6164. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6165. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6166. // title
  6167. [customer_dic setValue:@"Customer" forKey:@"title"];
  6168. // count
  6169. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6170. }];
  6171. // setting
  6172. NSDictionary *setting = params[@"setting"];
  6173. NSNumber *hide = setting[@"CustomerHide"];
  6174. [customer_dic setValue:hide forKey:@"hide"];
  6175. return customer_dic;
  6176. }
  6177. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6178. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6179. // setting
  6180. NSDictionary *setting = params[@"setting"];
  6181. NSNumber *hide = setting[@"ShipToHide"];
  6182. [dic setValue:hide forKey:@"hide"];
  6183. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6184. orderCode = [self translateSingleQuote:orderCode];
  6185. 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];
  6186. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6187. NSString *cid = [self textAtColumn:0 statement:stmt];
  6188. NSString *name = [self textAtColumn:1 statement:stmt];
  6189. NSString *ext = [self textAtColumn:2 statement:stmt];
  6190. NSString *contact = [self textAtColumn:3 statement:stmt];
  6191. NSString *email = [self textAtColumn:4 statement:stmt];
  6192. NSString *fax = [self textAtColumn:5 statement:stmt];
  6193. NSString *phone = [self textAtColumn:6 statement:stmt];
  6194. // count
  6195. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6196. // title
  6197. [dic setValue:@"Ship To" forKey:@"title"];
  6198. // choose
  6199. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6200. [choose_dic setValue:@"choose" forKey:@"aname"];
  6201. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6202. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6203. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6204. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6205. @"receive_contact" : @"customer_contact",
  6206. @"receive_email" : @"customer_email",
  6207. @"receive_ext" : @"customer_contact_ext",
  6208. @"receive_fax" : @"customer_fax",
  6209. @"receive_name" : @"customer_name",
  6210. @"receive_phone" : @"customer_phone"},
  6211. @"refresh" : [NSNumber numberWithInteger:1],
  6212. @"type" : @"pull"};
  6213. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6214. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6215. @"name" : @"Add new address",
  6216. @"refresh" : [NSNumber numberWithInteger:1],
  6217. @"value" : @"new_addr"
  6218. };
  6219. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6220. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6221. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6222. @"receive_contact" : @"customer_contact",
  6223. @"receive_email" : @"customer_email",
  6224. @"receive_ext" : @"customer_contact_ext",
  6225. @"receive_fax" : @"customer_fax",
  6226. @"receive_name" : @"customer_name",
  6227. @"receive_phone" : @"customer_phone"},
  6228. @"name" : @"select_ship_to",
  6229. @"refresh" : [NSNumber numberWithInteger:1],
  6230. @"value" : @"Sales_Order_Ship_To"};
  6231. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6232. [dic setValue:choose_dic forKey:@"item_0"];
  6233. // contact id
  6234. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6235. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6236. [contact_id_dic setValue:@"true" forKey:@"required"];
  6237. [dic setValue:contact_id_dic forKey:@"item_1"];
  6238. // company name
  6239. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6240. [dic setValue:company_name_dic forKey:@"item_2"];
  6241. // address
  6242. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6243. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6244. if ([required integerValue]) {
  6245. [ext_dic setValue:@"true" forKey:@"required"];
  6246. }
  6247. [dic setValue:ext_dic forKey:@"item_3"];
  6248. // contact
  6249. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6250. [dic setValue:contact_dic forKey:@"item_4"];
  6251. // phone
  6252. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6253. [dic setValue:phone_dic forKey:@"item_5"];
  6254. // fax
  6255. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6256. [dic setValue:fax_dic forKey:@"item_6"];
  6257. // email
  6258. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6259. [dic setValue:email_dic forKey:@"item_7"];
  6260. }];
  6261. return dic;
  6262. }
  6263. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6264. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6265. // setting
  6266. NSDictionary *setting = params[@"setting"];
  6267. NSNumber *hide = setting[@"ShipFromHide"];
  6268. [dic setValue:hide forKey:@"hide"];
  6269. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6270. orderCode = [self translateSingleQuote:orderCode];
  6271. 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];
  6272. 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';";
  6273. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6274. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6275. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6276. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6277. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6278. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6279. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6280. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6281. if (!cid.length) {
  6282. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6283. cid = [self textAtColumn:0 statement:statment];
  6284. name = [self textAtColumn:1 statement:statment];
  6285. ext = [self textAtColumn:2 statement:statment];
  6286. contact = [self textAtColumn:3 statement:statment];
  6287. email = [self textAtColumn:4 statement:statment];
  6288. fax = [self textAtColumn:5 statement:statment];
  6289. phone = [self textAtColumn:6 statement:statment];
  6290. }];
  6291. }
  6292. // count
  6293. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6294. // title
  6295. [dic setValue:@"Ship From" forKey:@"title"];
  6296. // hide
  6297. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6298. // choose
  6299. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6300. [choose_dic setValue:@"choose" forKey:@"aname"];
  6301. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6302. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6303. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6304. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6305. @"sender_contact" : @"customer_contact",
  6306. @"sender_email" : @"customer_email",
  6307. @"sender_ext" : @"customer_contact_ext",
  6308. @"sender_fax" : @"customer_fax",
  6309. @"sender_name" : @"customer_name",
  6310. @"sender_phone" : @"customer_phone"},
  6311. @"name" : @"select_cid",
  6312. @"refresh" : [NSNumber numberWithInteger:0],
  6313. @"value" : @"Sales_Order_Ship_From"};
  6314. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6315. [dic setValue:choose_dic forKey:@"item_0"];
  6316. // contact id
  6317. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6318. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6319. [contact_id_dic setValue:@"true" forKey:@"required"];
  6320. [dic setValue:contact_id_dic forKey:@"item_1"];
  6321. // company name
  6322. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6323. [dic setValue:company_name_dic forKey:@"item_2"];
  6324. // address
  6325. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6326. [dic setValue:ext_dic forKey:@"item_3"];
  6327. // contact
  6328. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6329. [dic setValue:contact_dic forKey:@"item_4"];
  6330. // phone
  6331. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6332. [dic setValue:phone_dic forKey:@"item_5"];
  6333. // fax
  6334. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6335. [dic setValue:fax_dic forKey:@"item_6"];
  6336. // email
  6337. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6338. [dic setValue:email_dic forKey:@"item_7"];
  6339. }];
  6340. return dic;
  6341. }
  6342. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6343. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6344. // setting
  6345. NSDictionary *setting = params[@"setting"];
  6346. NSNumber *hide = setting[@"FreightBillToHide"];
  6347. [dic setValue:hide forKey:@"hide"];
  6348. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6349. orderCode = [self translateSingleQuote:orderCode];
  6350. 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];
  6351. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6352. NSString *cid = [self textAtColumn:0 statement:stmt];
  6353. NSString *name = [self textAtColumn:1 statement:stmt];
  6354. NSString *ext = [self textAtColumn:2 statement:stmt];
  6355. NSString *contact = [self textAtColumn:3 statement:stmt];
  6356. NSString *email = [self textAtColumn:4 statement:stmt];
  6357. NSString *fax = [self textAtColumn:5 statement:stmt];
  6358. NSString *phone = [self textAtColumn:6 statement:stmt];
  6359. // count
  6360. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6361. // title
  6362. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6363. // hide
  6364. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6365. // choose
  6366. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6367. [choose_dic setValue:@"choose" forKey:@"aname"];
  6368. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6369. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6370. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6371. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6372. @"shipping_billto_contact" : @"receive_contact",
  6373. @"shipping_billto_email" : @"receive_email",
  6374. @"shipping_billto_ext" : @"receive_ext",
  6375. @"shipping_billto_fax" : @"receive_fax",
  6376. @"shipping_billto_name" : @"receive_name",
  6377. @"shipping_billto_phone" : @"receive_phone"},
  6378. @"type" : @"pull"};
  6379. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6380. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6381. @"type" : @"pull",
  6382. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6383. @"shipping_billto_contact" : @"customer_contact",
  6384. @"shipping_billto_email" : @"customer_email",
  6385. @"shipping_billto_ext" : @"customer_contact_ext",
  6386. @"shipping_billto_fax" : @"customer_fax",
  6387. @"shipping_billto_name" : @"customer_name",
  6388. @"shipping_billto_phone" : @"customer_phone"}
  6389. };
  6390. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6391. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6392. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6393. @"shipping_billto_contact" : @"sender_contact",
  6394. @"shipping_billto_email" : @"sender_email",
  6395. @"shipping_billto_ext" : @"sender_ext",
  6396. @"shipping_billto_fax" : @"sender_fax",
  6397. @"shipping_billto_name" : @"sender_name",
  6398. @"shipping_billto_phone" : @"sender_phone"},
  6399. @"type" : @"pull"
  6400. };
  6401. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6402. NSDictionary *select_freight_bill_to_dic = @{
  6403. @"aname" : @"Select freight bill to",
  6404. @"name" : @"select_cid",
  6405. @"refresh" : [NSNumber numberWithInteger:0],
  6406. @"value" : @"Sales_Order_Freight_Bill_To",
  6407. @"key_map" : @{
  6408. @"shipping_billto_cid" : @"customer_cid",
  6409. @"shipping_billto_contact" : @"customer_contact",
  6410. @"shipping_billto_email" : @"customer_email",
  6411. @"shipping_billto_ext" : @"customer_contact_ext",
  6412. @"shipping_billto_fax" : @"customer_fax",
  6413. @"shipping_billto_name" : @"customer_name",
  6414. @"shipping_billto_phone" : @"customer_phone"
  6415. }
  6416. };
  6417. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6418. [dic setValue:choose_dic forKey:@"item_0"];
  6419. // contact id
  6420. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6421. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6422. [contact_id_dic setValue:@"true" forKey:@"required"];
  6423. [dic setValue:contact_id_dic forKey:@"item_1"];
  6424. // company name
  6425. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6426. [dic setValue:company_name_dic forKey:@"item_2"];
  6427. // address
  6428. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6429. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6430. if ([ext_required integerValue]) {
  6431. [ext_dic setValue:@"true" forKey:@"required"];
  6432. }
  6433. [dic setValue:ext_dic forKey:@"item_3"];
  6434. // contact
  6435. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6436. [dic setValue:contact_dic forKey:@"item_4"];
  6437. // phone
  6438. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6439. [dic setValue:phone_dic forKey:@"item_5"];
  6440. // fax
  6441. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6442. [dic setValue:fax_dic forKey:@"item_6"];
  6443. // email
  6444. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6445. [dic setValue:email_dic forKey:@"item_7"];
  6446. }];
  6447. return dic;
  6448. }
  6449. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6450. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6451. // setting
  6452. NSDictionary *setting = params[@"setting"];
  6453. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6454. [dic setValue:hide forKey:@"hide"];
  6455. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6456. orderCode = [self translateSingleQuote:orderCode];
  6457. 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];
  6458. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6459. NSString *cid = [self textAtColumn:0 statement:stmt];
  6460. NSString *name = [self textAtColumn:1 statement:stmt];
  6461. NSString *ext = [self textAtColumn:2 statement:stmt];
  6462. NSString *contact = [self textAtColumn:3 statement:stmt];
  6463. NSString *email = [self textAtColumn:4 statement:stmt];
  6464. NSString *fax = [self textAtColumn:5 statement:stmt];
  6465. NSString *phone = [self textAtColumn:6 statement:stmt];
  6466. // count
  6467. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6468. // title
  6469. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6470. // hide
  6471. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6472. // choose
  6473. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6474. [choose_dic setValue:@"choose" forKey:@"aname"];
  6475. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6476. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6477. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6478. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6479. @"billing_contact" : @"receive_contact",
  6480. @"billing_email" : @"receive_email",
  6481. @"billing_ext" : @"receive_ext",
  6482. @"billing_fax" : @"receive_fax",
  6483. @"billing_name" : @"receive_name",
  6484. @"billing_phone" : @"receive_phone"},
  6485. @"type" : @"pull"};
  6486. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6487. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6488. @"type" : @"pull",
  6489. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6490. @"billing_contact" : @"customer_contact",
  6491. @"billing_email" : @"customer_email",
  6492. @"billing_ext" : @"customer_contact_ext",
  6493. @"billing_fax" : @"customer_fax",
  6494. @"billing_name" : @"customer_name",
  6495. @"billing_phone" : @"customer_phone"}
  6496. };
  6497. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6498. NSDictionary *select_bill_to_dic = @{
  6499. @"aname" : @"Select bill to",
  6500. @"name" : @"select_cid",
  6501. @"refresh" : [NSNumber numberWithInteger:0],
  6502. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6503. @"key_map" : @{
  6504. @"billing_cid" : @"customer_cid",
  6505. @"billing_contact" : @"customer_contact",
  6506. @"billing_email" : @"customer_email",
  6507. @"billing_ext" : @"customer_contact_ext",
  6508. @"billing_fax" : @"customer_fax",
  6509. @"billing_name" : @"customer_name",
  6510. @"billing_phone" : @"customer_phone"
  6511. }
  6512. };
  6513. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6514. [dic setValue:choose_dic forKey:@"item_0"];
  6515. // contact id
  6516. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6517. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6518. [contact_id_dic setValue:@"true" forKey:@"required"];
  6519. [dic setValue:contact_id_dic forKey:@"item_1"];
  6520. // company name
  6521. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6522. [dic setValue:company_name_dic forKey:@"item_2"];
  6523. // address
  6524. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6525. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6526. if ([ext_required integerValue]) {
  6527. [ext_dic setValue:@"true" forKey:@"required"];
  6528. }
  6529. [dic setValue:ext_dic forKey:@"item_3"];
  6530. // contact
  6531. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6532. [dic setValue:contact_dic forKey:@"item_4"];
  6533. // phone
  6534. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6535. [dic setValue:phone_dic forKey:@"item_5"];
  6536. // fax
  6537. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6538. [dic setValue:fax_dic forKey:@"item_6"];
  6539. // email
  6540. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6541. [dic setValue:email_dic forKey:@"item_7"];
  6542. }];
  6543. return dic;
  6544. }
  6545. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6546. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6547. // setting
  6548. NSDictionary *setting = params[@"setting"];
  6549. NSNumber *hide = setting[@"ReturnToHide"];
  6550. [dic setValue:hide forKey:@"hide"];
  6551. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6552. orderCode = [self translateSingleQuote:orderCode];
  6553. 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];
  6554. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6555. NSString *cid = [self textAtColumn:0 statement:stmt];
  6556. NSString *name = [self textAtColumn:1 statement:stmt];
  6557. NSString *ext = [self textAtColumn:2 statement:stmt];
  6558. NSString *contact = [self textAtColumn:3 statement:stmt];
  6559. NSString *email = [self textAtColumn:4 statement:stmt];
  6560. NSString *fax = [self textAtColumn:5 statement:stmt];
  6561. NSString *phone = [self textAtColumn:6 statement:stmt];
  6562. // count
  6563. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6564. // title
  6565. [dic setValue:@"Return To" forKey:@"title"];
  6566. // hide
  6567. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6568. // choose
  6569. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6570. [choose_dic setValue:@"choose" forKey:@"aname"];
  6571. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6572. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6573. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6574. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6575. @"returnto_contact" : @"sender_contact",
  6576. @"returnto_email" : @"sender_email",
  6577. @"returnto_ext" : @"sender_ext",
  6578. @"returnto_fax" : @"sender_fax",
  6579. @"returnto_name" : @"sender_name",
  6580. @"returnto_phone" : @"sender_phone"},
  6581. @"type" : @"pull"};
  6582. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6583. NSDictionary *select_return_to_dic = @{
  6584. @"aname" : @"Select return to",
  6585. @"name" : @"select_cid",
  6586. @"refresh" : [NSNumber numberWithInteger:0],
  6587. @"value" : @"Contact_Return_To",
  6588. @"key_map" : @{
  6589. @"returnto_cid" : @"customer_cid",
  6590. @"returnto_contact" : @"customer_contact",
  6591. @"returnto_email" : @"customer_email",
  6592. @"returnto_ext" : @"customer_contact_ext",
  6593. @"returnto_fax" : @"customer_fax",
  6594. @"returnto_name" : @"customer_name",
  6595. @"returnto_phone" : @"customer_phone"
  6596. }
  6597. };
  6598. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6599. [dic setValue:choose_dic forKey:@"item_0"];
  6600. // contact id
  6601. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6602. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6603. [contact_id_dic setValue:@"true" forKey:@"required"];
  6604. [dic setValue:contact_id_dic forKey:@"item_1"];
  6605. // company name
  6606. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6607. [dic setValue:company_name_dic forKey:@"item_2"];
  6608. // address
  6609. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6610. [dic setValue:ext_dic forKey:@"item_3"];
  6611. // contact
  6612. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6613. [dic setValue:contact_dic forKey:@"item_4"];
  6614. // phone
  6615. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6616. [dic setValue:phone_dic forKey:@"item_5"];
  6617. // fax
  6618. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6619. [dic setValue:fax_dic forKey:@"item_6"];
  6620. // email
  6621. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6622. [dic setValue:email_dic forKey:@"item_7"];
  6623. }];
  6624. return dic;
  6625. }
  6626. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6627. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6628. orderCode = [self translateSingleQuote:orderCode];
  6629. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6630. __block double TotalCuft = 0;
  6631. __block double TotalWeight = 0;
  6632. __block int TotalCarton = 0;
  6633. __block double payments = 0;
  6634. // setting
  6635. NSDictionary *setting = params[@"setting"];
  6636. NSNumber *hide = setting[@"ModelInformationHide"];
  6637. [dic setValue:hide forKey:@"hide"];
  6638. 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];
  6639. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6640. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6641. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6642. // item id
  6643. int item_id = sqlite3_column_int(stmt, 0);
  6644. // count
  6645. int item_count = sqlite3_column_int(stmt, 1);
  6646. // stockUom
  6647. int stockUom = sqlite3_column_int(stmt, 2);
  6648. // unit price
  6649. NSString *str_price = [self textAtColumn:3 statement:stmt];
  6650. NSString* Price=nil;
  6651. if([str_price isEqualToString:@""])
  6652. {
  6653. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  6654. if(price==nil)
  6655. Price=@"No Price.";
  6656. else
  6657. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6658. }
  6659. else
  6660. {
  6661. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  6662. }
  6663. // discount
  6664. double discount = sqlite3_column_double(stmt, 4);
  6665. // name
  6666. NSString *name = [self textAtColumn:5 statement:stmt];
  6667. // description
  6668. NSString *description = [self textAtColumn:6 statement:stmt];
  6669. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  6670. // line note
  6671. NSString *line_note = [self textAtColumn:7 statement:stmt];
  6672. int avaulability = sqlite3_column_int(stmt, 8);
  6673. // img
  6674. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  6675. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  6676. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6677. if(combine != nil)
  6678. {
  6679. // int citem=0;
  6680. int bcount=[[combine valueForKey:@"count"] intValue];
  6681. for(int bc=0;bc<bcount;bc++)
  6682. {
  6683. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6684. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6685. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6686. subTotal += uprice * modulus * item_count;
  6687. }
  6688. }
  6689. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6690. [model_dic setValue:@"model" forKey:@"control"];
  6691. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6692. [model_dic setValue:description forKey:@"description"];
  6693. [model_dic setValue:line_note forKey:@"note"];
  6694. [model_dic setValue:img forKey:@"img_url"];
  6695. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6696. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6697. [model_dic setValue:Price forKey:@"unit_price"];
  6698. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6699. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6700. if (combine) {
  6701. [model_dic setValue:combine forKey:@"combine"];
  6702. }
  6703. // well,what under the row is the info for total
  6704. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6705. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6706. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6707. int carton=[bsubtotaljson[@"carton"] intValue];
  6708. TotalCuft += cuft;
  6709. TotalWeight += weight;
  6710. TotalCarton += carton;
  6711. payments += subTotal;
  6712. //---------------------------
  6713. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6714. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6715. }];
  6716. [dic setValue:@"Model Information" forKey:@"title"];
  6717. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6718. return dic;
  6719. }
  6720. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6721. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6722. [dic setValue:@"Remarks Content" forKey:@"title"];
  6723. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6724. // setting
  6725. NSDictionary *setting = params[@"setting"];
  6726. NSNumber *hide = setting[@"RemarksContentHide"];
  6727. [dic setValue:hide forKey:@"hide"];
  6728. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6729. orderCode = [self translateSingleQuote:orderCode];
  6730. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6731. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6732. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6733. int mustCall = sqlite3_column_int(stmt, 1);
  6734. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6735. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6736. NSDictionary *po_dic = @{
  6737. @"aname" : @"PO#",
  6738. @"control" : @"edit",
  6739. @"keyboard" : @"text",
  6740. @"name" : @"poNumber",
  6741. @"value" : poNumber
  6742. };
  6743. NSDictionary *must_call_dic = @{
  6744. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6745. @"control" : @"switch",
  6746. @"name" : @"must_call",
  6747. @"value" : mustCall ? @"true" : @"false"
  6748. };
  6749. NSDictionary *general_notes_dic = @{
  6750. @"aname" : @"General notes",
  6751. @"control" : @"text_view",
  6752. @"keyboard" : @"text",
  6753. @"name" : @"comments",
  6754. @"value" : generalNotes
  6755. };
  6756. NSDictionary *internal_notes_dic = @{
  6757. @"aname" : @"Internal notes",
  6758. @"control" : @"text_view",
  6759. @"keyboard" : @"text",
  6760. @"name" : @"internal_notes",
  6761. @"value" : internalNotes
  6762. };
  6763. [dic setValue:po_dic forKey:@"item_0"];
  6764. [dic setValue:must_call_dic forKey:@"item_1"];
  6765. [dic setValue:general_notes_dic forKey:@"item_2"];
  6766. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6767. }];
  6768. return dic;
  6769. }
  6770. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6771. // params
  6772. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6773. orderCode = [self translateSingleQuote:orderCode];
  6774. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6775. // setting
  6776. NSDictionary *setting = params[@"setting"];
  6777. NSNumber *hide = setting[@"OrderTotalHide"];
  6778. [dic setValue:hide forKey:@"hide"];
  6779. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6780. __block double lift_gate_value = 0;
  6781. __block double handling_fee = 0;
  6782. __block double shipping = 0;
  6783. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6784. int lift_gate = sqlite3_column_int(stmt, 0);
  6785. lift_gate_value = sqlite3_column_double(stmt, 1);
  6786. shipping = sqlite3_column_double(stmt, 2);
  6787. handling_fee = sqlite3_column_double(stmt, 3);
  6788. if (!lift_gate) {
  6789. lift_gate_value = 0;
  6790. }
  6791. }];
  6792. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6793. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6794. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6795. double payments = [[total valueForKey:@"payments"] doubleValue];
  6796. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6797. double totalPrice = payments;
  6798. [dic setValue:@"Order Total" forKey:@"title"];
  6799. NSDictionary *payments_dic = @{
  6800. @"align" : @"right",
  6801. @"aname" : @"Payments/Credits",
  6802. @"control" : @"text",
  6803. @"name" : @"paymentsAndCredits",
  6804. @"type" : @"price",
  6805. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6806. };
  6807. [dic setValue:payments_dic forKey:@"item_0"];
  6808. NSDictionary *handling_fee_dic = @{
  6809. @"align" : @"right",
  6810. @"aname" : @"Handling Fee",
  6811. @"control" : @"text",
  6812. @"name" : @"handling_fee_value",
  6813. @"required" : @"true",
  6814. @"type" : @"price",
  6815. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6816. };
  6817. NSDictionary *shipping_dic = @{
  6818. @"align" : @"right",
  6819. @"aname" : @"Shipping*",
  6820. @"control" : @"text",
  6821. @"name" : @"shipping",
  6822. @"required" : @"true",
  6823. @"type" : @"price",
  6824. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6825. };
  6826. NSDictionary *lift_gate_dic = @{
  6827. @"align" : @"right",
  6828. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6829. @"control" : @"text",
  6830. @"name" : @"lift_gate_value",
  6831. @"required" : @"true",
  6832. @"type" : @"price",
  6833. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6834. };
  6835. int item_count = 1;
  6836. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6837. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6838. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6839. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6840. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6841. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6842. } else {
  6843. }
  6844. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6845. NSDictionary *total_price_dic = @{
  6846. @"align" : @"right",
  6847. @"aname" : @"Total",
  6848. @"control" : @"text",
  6849. @"name" : @"totalPrice",
  6850. @"type" : @"price",
  6851. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6852. };
  6853. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6854. NSDictionary *total_cuft_dic = @{
  6855. @"align" : @"right",
  6856. @"aname" : @"Total Cuft",
  6857. @"control" : @"text",
  6858. @"name" : @"",
  6859. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6860. };
  6861. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6862. NSDictionary *total_weight_dic = @{
  6863. @"align" : @"right",
  6864. @"aname" : @"Total Weight",
  6865. @"control" : @"text",
  6866. @"name" : @"",
  6867. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6868. };
  6869. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6870. NSDictionary *total_carton_dic = @{
  6871. @"align" : @"right",
  6872. @"aname" : @"Total Carton",
  6873. @"control" : @"text",
  6874. @"name" : @"",
  6875. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6876. };
  6877. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6878. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6879. return dic;
  6880. }
  6881. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6882. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6883. orderCode = [self translateSingleQuote:orderCode];
  6884. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6885. // setting
  6886. NSDictionary *setting = params[@"setting"];
  6887. NSNumber *hide = setting[@"SignatureHide"];
  6888. [dic setValue:hide forKey:@"hide"];
  6889. [dic setValue:@"Signature" forKey:@"title"];
  6890. __block NSString *pic_path = @"";
  6891. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6892. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6893. pic_path = [self textAtColumn:0 statement:stmt];
  6894. }];
  6895. NSDictionary *pic_dic = @{
  6896. @"aname" : @"Signature",
  6897. @"avalue" :pic_path,
  6898. @"control" : @"signature",
  6899. @"name" : @"sign_picpath",
  6900. @"value" : pic_path
  6901. };
  6902. [dic setValue:pic_dic forKey:@"item_0"];
  6903. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6904. return dic;
  6905. }
  6906. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6907. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6908. // setting
  6909. NSDictionary *setting = params[@"setting"];
  6910. NSNumber *hide = setting[@"ShippingMethodHide"];
  6911. [dic setValue:hide forKey:@"hide"];
  6912. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6913. orderCode = [self translateSingleQuote:orderCode];
  6914. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6915. __block NSString *logist = @"";
  6916. __block NSString *lift_gate = @"";
  6917. __block int lift_gate_integer = 0;
  6918. __block NSString *logistic_note = @"";
  6919. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6920. logist = [self textAtColumn:0 statement:stmt];
  6921. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6922. logistic_note = [self textAtColumn:2 statement:stmt];
  6923. }];
  6924. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6925. [dic setValue:@"Shipping Method" forKey:@"title"];
  6926. // val_0
  6927. int PERSONAL_PICK_UP_check = 0;
  6928. int USE_MY_CARRIER_check = 0;
  6929. NSString *logistic_note_text = @"";
  6930. int will_call_check = 0;
  6931. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6932. will_call_check = 1;
  6933. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6934. PERSONAL_PICK_UP_check = 1;
  6935. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6936. USE_MY_CARRIER_check = 1;
  6937. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6938. logistic_note = [logistic_note_array firstObject];
  6939. if (logistic_note_array.count > 1) {
  6940. logistic_note_text = [logistic_note_array lastObject];
  6941. }
  6942. }
  6943. }
  6944. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6945. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6946. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6947. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6948. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6949. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6950. NSDictionary *val0_subItem_item0 = @{
  6951. @"aname" : @"Option",
  6952. @"cadedate" : @{
  6953. @"count" : [NSNumber numberWithInteger:2],
  6954. @"val_0" : @{
  6955. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6956. @"refresh" : [NSNumber numberWithInteger:0],
  6957. @"value" : @"PERSONAL PICK UP",
  6958. @"value_id" : @"PERSONAL PICK UP"
  6959. },
  6960. @"val_1" : @{
  6961. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6962. @"refresh" : [NSNumber numberWithInteger:0],
  6963. @"sub_item" : @{
  6964. @"count" : [NSNumber numberWithInteger:1],
  6965. @"item_0" : @{
  6966. @"aname" : @"BOL",
  6967. @"control" : @"edit",
  6968. @"keyboard" : @"text",
  6969. @"name" : @"logist_note_text",
  6970. @"refresh" : [NSNumber numberWithInteger:0],
  6971. @"required" : @"false",
  6972. @"value" : logistic_note_text
  6973. }
  6974. },
  6975. @"value" : @"USE MY CARRIER",
  6976. @"value_id" : @"USE MY CARRIER"
  6977. }
  6978. },
  6979. @"control" : @"enum",
  6980. @"name" : @"logistic_note",
  6981. @"required" : @"true",
  6982. @"single_select" : @"true"
  6983. };
  6984. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6985. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6986. // val_1
  6987. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6988. if([lift_gate isEqualToString:@""]) {
  6989. if (lift_gate_integer == 1) {
  6990. lift_gate = @"true";
  6991. } else {
  6992. lift_gate = @"false";
  6993. }
  6994. }
  6995. int common_carrier_check = 0;
  6996. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6997. common_carrier_check = 1;
  6998. [params setValue:lift_gate forKey:@"lift_gate"];
  6999. }
  7000. NSDictionary *val_1 = @{
  7001. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7002. @"sub_item" : @{
  7003. @"count" : [NSNumber numberWithInteger:1],
  7004. @"item_0" : @{
  7005. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7006. @"control" : @"switch",
  7007. @"name" : @"lift_gate",
  7008. @"refresh" : [NSNumber numberWithInteger:1],
  7009. @"required" : @"true",
  7010. @"value" : lift_gate
  7011. }
  7012. },
  7013. @"value" : @"COMMON CARRIER",
  7014. @"value_id" : @"COMMON CARRIER"
  7015. };
  7016. // cadedate
  7017. NSDictionary *cadedate = @{
  7018. @"count" : [NSNumber numberWithInteger:2],
  7019. @"val_0" : val_0,
  7020. @"val_1" : val_1
  7021. };
  7022. // item_0
  7023. NSMutableDictionary *item_0 = @{
  7024. @"aname" : @"Shipping",
  7025. @"cadedate" : cadedate,
  7026. @"control" : @"enum",
  7027. @"name" : @"logist",
  7028. @"refresh" : [NSNumber numberWithInteger:1],
  7029. @"single_select" : @"true",
  7030. }.mutableCopy;
  7031. NSNumber *required = setting[@"ShippingMethodRequire"];
  7032. if ([required integerValue]) {
  7033. [item_0 setValue:@"true" forKey:@"required"];
  7034. }
  7035. [dic setValue:item_0 forKey:@"item_0"];
  7036. return dic;
  7037. }
  7038. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7039. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7040. orderCode = [self translateSingleQuote:orderCode];
  7041. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7042. __block int submit_as_integer = 0;
  7043. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7044. submit_as_integer = sqlite3_column_int(stmt, 0);
  7045. }];
  7046. int check11 = 0;
  7047. int check10 = 0;
  7048. if (submit_as_integer == 11) {
  7049. check11 = 1;
  7050. }
  7051. if (submit_as_integer == 10) {
  7052. check10 = 1;
  7053. }
  7054. // section_0
  7055. NSMutableDictionary *dic = @{
  7056. @"count" : @(1),
  7057. @"item_0" : @{
  7058. @"aname" : @"Submit Order As",
  7059. @"cadedate" : @{
  7060. @"count" : @(2),
  7061. @"val_0" : @{
  7062. @"check" : [NSNumber numberWithInteger:check11],
  7063. @"value" : @"Sales Order",
  7064. @"value_id" : @(11)
  7065. },
  7066. @"val_1" : @{
  7067. @"check" : [NSNumber numberWithInteger:check10],
  7068. @"value" : @"Quote",
  7069. @"value_id" : @(10)
  7070. }
  7071. },
  7072. @"control" : @"enum",
  7073. @"name" : @"erpOrderStatus",
  7074. @"required" : @"true",
  7075. @"single_select" : @"true"
  7076. },
  7077. @"title" : @"Order Type"
  7078. }.mutableCopy;
  7079. // setting
  7080. NSDictionary *setting = params[@"setting"];
  7081. NSNumber *hide = setting[@"OrderTypeHide"];
  7082. [dic setValue:hide forKey:@"hide"];
  7083. return dic;
  7084. }
  7085. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7086. countryCode = [self translateSingleQuote:countryCode];
  7087. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7088. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7089. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7090. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7091. if (name == NULL) {
  7092. name = "";
  7093. }
  7094. if (code == NULL) {
  7095. code = "";
  7096. }
  7097. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7098. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7099. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7100. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7101. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7102. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7103. }
  7104. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7105. [container setValue:stateDic forKey:key];
  7106. }] mutableCopy];
  7107. // failure 可以不用了,一样的
  7108. if (ret.allKeys.count == 0) {
  7109. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7110. [stateDic setValue:@"Other" forKey:@"value"];
  7111. [stateDic setValue:@"" forKey:@"value_id"];
  7112. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7113. if (state_code && [@"" isEqualToString:state_code]) {
  7114. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7115. }
  7116. NSString *key = [NSString stringWithFormat:@"val_0"];
  7117. [ret setValue:stateDic forKey:key];
  7118. }
  7119. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7120. return ret;
  7121. }
  7122. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7123. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7124. orderCode = [self translateSingleQuote:orderCode];
  7125. 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];
  7126. __block NSString *payType = @"";
  7127. __block NSString *firstName = @"";
  7128. __block NSString *lastName = @"";
  7129. __block NSString *addr1 = @"";
  7130. __block NSString *addr2 = @"";
  7131. __block NSString *zipcode = @"";
  7132. __block NSString *cardType = @"";
  7133. __block NSString *cardNumber = @"";
  7134. __block NSString *securityCode = @"";
  7135. __block NSString *month = @"";
  7136. __block NSString *year = @"";
  7137. __block NSString *city = @"";
  7138. __block NSString *state = @"";
  7139. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7140. payType = [self textAtColumn:0 statement:stmt];
  7141. firstName = [self textAtColumn:1 statement:stmt];
  7142. lastName = [self textAtColumn:2 statement:stmt];
  7143. addr1 = [self textAtColumn:3 statement:stmt];
  7144. addr2 = [self textAtColumn:4 statement:stmt];
  7145. zipcode = [self textAtColumn:5 statement:stmt];
  7146. cardType = [self textAtColumn:6 statement:stmt];
  7147. cardNumber = [self textAtColumn:7 statement:stmt];
  7148. securityCode = [self textAtColumn:8 statement:stmt];
  7149. month = [self textAtColumn:9 statement:stmt];
  7150. year = [self textAtColumn:10 statement:stmt];
  7151. city = [self textAtColumn:11 statement:stmt];
  7152. state = [self textAtColumn:12 statement:stmt];
  7153. }];
  7154. NSString *required = @"true";
  7155. // setting
  7156. NSDictionary *setting = params[@"setting"];
  7157. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7158. if ([requiredNumber integerValue]) {
  7159. required = @"true";
  7160. } else {
  7161. required = @"false";
  7162. }
  7163. // "section_2"
  7164. NSMutableDictionary *dic = @{
  7165. @"count" : @(1),
  7166. @"item_0" : @{
  7167. @"aname" : @"Payment",
  7168. @"required" : required,
  7169. @"cadedate" : @{
  7170. @"count" : @(6),
  7171. @"val_0" : @{
  7172. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7173. @"value" : @"Check",
  7174. @"value_id" : @"Check"
  7175. },
  7176. @"val_1" : @{
  7177. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7178. @"value" : @"Cash",
  7179. @"value_id" : @"Cash"
  7180. },
  7181. @"val_2" : @{
  7182. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7183. @"value" : @"NET 30",
  7184. @"value_id" : @"NET 30"
  7185. },
  7186. @"val_3" : @{
  7187. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7188. @"value" : @"Wire Transfer",
  7189. @"value_id" : @"Wire Transfer"
  7190. },
  7191. @"val_4" : @{
  7192. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7193. @"sub_item" : @{
  7194. @"count" : @(3),
  7195. @"item_0" : @{
  7196. @"aname" : @"choose",
  7197. @"control" : @"multi_action",
  7198. @"count" : @(1),
  7199. @"item_0" : @{
  7200. @"aname" : @"Same as customer",
  7201. @"key_map" : @{
  7202. @"credit_card_address1" : @"customer_address1",
  7203. @"credit_card_address2" : @"customer_address2",
  7204. @"credit_card_city" : @"customer_city",
  7205. @"credit_card_first_name" : @"customer_first_name",
  7206. @"credit_card_last_name" : @"customer_last_name",
  7207. @"credit_card_state" : @"customer_state",
  7208. @"credit_card_zipcode" : @"customer_zipcode"
  7209. },
  7210. @"type" : @"pull"
  7211. }
  7212. },
  7213. @"item_1" : @{
  7214. @"aname" : @"",
  7215. @"color" : @"red",
  7216. @"control" : @"text",
  7217. @"name" : @"",
  7218. @"value" : @"USA Credit cards only"
  7219. },
  7220. @"item_2" : @{
  7221. @"aname" : @"Fill",
  7222. @"cadedate" : @{
  7223. @"count" : @(2),
  7224. @"val_0" : @{
  7225. @"check" : @(1),
  7226. @"sub_item" : @{
  7227. @"count" : @(11),
  7228. @"item_0" : @{
  7229. @"aname" : @"Type",
  7230. @"cadedate" : @{
  7231. @"count" : @(2),
  7232. @"val_0" : @{
  7233. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7234. @"value" : @"VISA",
  7235. @"value_id" : @(0)
  7236. },
  7237. @"val_1" : @{
  7238. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7239. @"value" : @"MASTER CARD",
  7240. @"value_id" : @(1)
  7241. }
  7242. },
  7243. @"control" : @"enum",
  7244. @"name" : @"credit_card_type",
  7245. @"required" : @"true",
  7246. @"single_select" : @"true"
  7247. },
  7248. @"item_1" : @{
  7249. @"aname" : @"Number",
  7250. @"control" : @"edit",
  7251. @"keyboard" : @"int",
  7252. @"length" : @"16",
  7253. @"name" : @"credit_card_number",
  7254. @"required" : @"true",
  7255. @"value" : cardNumber
  7256. },
  7257. @"item_10" : @{
  7258. @"aname" : @"State",
  7259. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7260. @"control" : @"enum",
  7261. @"enum" : @"true",
  7262. @"name" : @"credit_card_state",
  7263. @"required" : @"true",
  7264. @"single_select" : @"true"
  7265. },
  7266. @"item_2" : @{
  7267. @"aname" : @"Expiration Date",
  7268. @"control" : @"monthpicker",
  7269. @"name" : @"credit_card_expiration",
  7270. @"required" : @"true",
  7271. @"type" : @"date",
  7272. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7273. },
  7274. @"item_3" : @{
  7275. @"aname" : @"Security Code",
  7276. @"control" : @"edit",
  7277. @"keyboard" : @"int",
  7278. @"length" : @"3",
  7279. @"name" : @"credit_card_security_code",
  7280. @"required" : @"true",
  7281. @"value" : securityCode
  7282. },
  7283. @"item_4" : @{
  7284. @"aname" : @"First Name",
  7285. @"control" : @"edit",
  7286. @"keyboard" : @"text",
  7287. @"name" : @"credit_card_first_name",
  7288. @"required" : @"true",
  7289. @"value" : firstName
  7290. },
  7291. @"item_5" : @{
  7292. @"aname" : @"Last Name",
  7293. @"control" : @"edit",
  7294. @"keyboard" : @"text",
  7295. @"name" : @"credit_card_last_name",
  7296. @"required" : @"true",
  7297. @"value" : lastName
  7298. },
  7299. @"item_6" : @{
  7300. @"aname" : @"Address 1",
  7301. @"control" : @"edit",
  7302. @"keyboard" : @"text",
  7303. @"name" : @"credit_card_address1",
  7304. @"required" : @"true",
  7305. @"value" : addr1
  7306. },
  7307. @"item_7" : @{
  7308. @"aname" : @"Address 2",
  7309. @"control" : @"edit",
  7310. @"keyboard" : @"text",
  7311. @"name" : @"credit_card_address2",
  7312. @"value" : addr2
  7313. },
  7314. @"item_8" : @{
  7315. @"aname" : @"zip code",
  7316. @"control" : @"edit",
  7317. @"keyboard" : @"text",
  7318. @"name" : @"credit_card_zipcode",
  7319. @"required" : @"true",
  7320. @"value" : zipcode
  7321. },
  7322. @"item_9" : @{
  7323. @"aname" : @"City",
  7324. @"control" : @"edit",
  7325. @"keyboard" : @"text",
  7326. @"name" : @"credit_card_city",
  7327. @"required" : @"true",
  7328. @"value" : city
  7329. }
  7330. },
  7331. @"value" : @"Fill Now",
  7332. @"value_id" : @""
  7333. },
  7334. @"val_1" : @{
  7335. @"check" : @(0),
  7336. @"value" : @"Fill Later",
  7337. @"value_id" : @""
  7338. }
  7339. },
  7340. @"control" : @"enum",
  7341. @"name" : @"",
  7342. @"single_select" : @"true"
  7343. }
  7344. },
  7345. @"value" : @"Credit Card",
  7346. @"value_id" : @"Credit Card"
  7347. },
  7348. @"val_5" : @{
  7349. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7350. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7351. @"value_id" : @"FOLLOW EXISTING"
  7352. }
  7353. },
  7354. @"control" : @"enum",
  7355. @"name" : @"paymentType",
  7356. @"single_select" : @"true"
  7357. },
  7358. @"title" : @"Payment Information"
  7359. }.mutableCopy;
  7360. NSNumber *hide = setting[@"PaymentInformationHide"];
  7361. [dic setValue:hide forKey:@"hide"];
  7362. return dic;
  7363. }
  7364. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7365. {
  7366. sqlite3 *db = [iSalesDB get_db];
  7367. // UISetting
  7368. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7369. NSString *cachefolder = [paths objectAtIndex:0];
  7370. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7371. NSData* json =nil;
  7372. json=[NSData dataWithContentsOfFile:img_cache];
  7373. NSError *error=nil;
  7374. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7375. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7376. [params setObject:setting forKey:@"setting"];
  7377. int section_count = 0;
  7378. // params
  7379. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7380. orderCode = [self translateSingleQuote:orderCode];
  7381. // 0 Order Type 1 Shipping Method 2 Payment Information
  7382. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7383. // 0 Order Type
  7384. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7385. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7386. [ret setValue:order_type_dic forKey:key0];
  7387. // 1 Shipping Method
  7388. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7389. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7390. [ret setValue:shipping_method_dic forKey:key1];
  7391. // 2 Payment Information
  7392. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7393. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7394. [ret setValue:payment_info_dic forKey:key2];
  7395. // 3 Customer
  7396. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7397. NSDictionary *customer_dic = [self customerDic:params db:db];
  7398. [ret setValue:customer_dic forKey:key3];
  7399. // 4 Ship To
  7400. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7401. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7402. [ret setValue:ship_to_dic forKey:key4];
  7403. // 5 Ship From
  7404. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7405. if (![shipFromDisable integerValue]) {
  7406. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7407. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7408. [ret setValue:ship_from_dic forKey:key5];
  7409. }
  7410. // 6 Freight Bill To
  7411. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7412. if (![freightBillToDisable integerValue]) {
  7413. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7414. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7415. [ret setValue:freight_bill_to forKey:key6];
  7416. }
  7417. // 7 Merchandise Bill To
  7418. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7419. if (![merchandiseBillToDisable integerValue]) {
  7420. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7421. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7422. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7423. }
  7424. // 8 Return To
  7425. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7426. if (![returnToDisable integerValue]) {
  7427. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7428. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7429. [ret setValue:return_to_dic forKey:key8];
  7430. }
  7431. // 9 Model Information
  7432. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7433. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7434. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7435. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7436. [ret setValue:model_info_dic forKey:key9];
  7437. // 10 Remarks Content
  7438. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7439. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7440. [ret setValue:remarks_content_dic forKey:key10];
  7441. // 11 Order Total
  7442. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7443. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7444. [ret setValue:order_total_dic forKey:key11];
  7445. // 12 Signature
  7446. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7447. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7448. [ret setValue:sign_dic forKey:key12];
  7449. [ret setValue:@(section_count) forKey:@"section_count"];
  7450. [iSalesDB close_db:db];
  7451. return [RAUtils dict2data:ret];
  7452. // return nil;
  7453. }
  7454. #pragma mark addr editor
  7455. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7456. NSMutableDictionary *new_item = [item mutableCopy];
  7457. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7458. return new_item;
  7459. }
  7460. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7461. {
  7462. // "is_subaction" = true;
  7463. // orderCode = MOB1608240002;
  7464. // password = 123456;
  7465. // "subaction_tag" = 1;
  7466. // user = EvanK;
  7467. // {
  7468. // "is_subaction" = true;
  7469. // orderCode = MOB1608240002;
  7470. // password = 123456;
  7471. // "refresh_trigger" = zipcode;
  7472. // "subaction_tag" = 1;
  7473. // user = EvanK;
  7474. // }
  7475. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7476. [ret removeObjectForKey:@"up_params"];
  7477. [ret setObject:@"New Address" forKey:@"title"];
  7478. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7479. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7480. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7481. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7482. [country_dic removeObjectForKey:@"refresh"];
  7483. [country_dic removeObjectForKey:@"restore"];
  7484. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7485. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7486. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7487. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7488. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7489. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7490. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7491. [zip_code_dic removeObjectForKey:@"refresh"];
  7492. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7493. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7494. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7495. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7496. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7497. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7498. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7499. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7500. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7501. // country
  7502. NSString *countryCode = @"US";
  7503. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7504. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7505. // state
  7506. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  7507. NSDictionary *tmpDic = @{
  7508. @"value" : @"Other",
  7509. @"value_id" : @"",
  7510. @"check" : [NSNumber numberWithInteger:0]
  7511. };
  7512. for (int i = 0; i < allState.allKeys.count; i++) {
  7513. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7514. NSDictionary *tmp = allState[key];
  7515. [allState setValue:tmpDic forKey:key];
  7516. tmpDic = tmp;
  7517. }
  7518. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7519. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7520. [ret setValue:new_section_0 forKey:@"section_0"];
  7521. return [RAUtils dict2data:ret];
  7522. }
  7523. #pragma mark save addr
  7524. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7525. {
  7526. // NSString *companyName = [self valueInParams:params key:@"company"];
  7527. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7528. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7529. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7530. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7531. // NSString *countryId = [self valueInParams:params key:@"country"];
  7532. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7533. // NSString *city = [self valueInParams:params key:@"city"];
  7534. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7535. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7536. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7537. // NSString *phone = [self valueInParams:params key:@"phone"];
  7538. // NSString *fax = [self valueInParams:params key:@"fax"];
  7539. // NSString *email = [self valueInParams:params key:@"email"];
  7540. return [self offline_saveContact:params update:NO isCustomer:NO];
  7541. }
  7542. #pragma mark cancel order
  7543. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7544. {
  7545. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7546. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7547. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7548. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7549. int ret = [iSalesDB execSql:sql];
  7550. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7551. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7552. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7553. // [dic setValue:@"160409" forKey:@"min_ver"];
  7554. return [RAUtils dict2data:dic];
  7555. }
  7556. #pragma mark sign order
  7557. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7558. {
  7559. //参考 offline_saveBusinesscard
  7560. NSLog(@"sign order params: %@",params);
  7561. // orderCode = MOB1608240002;
  7562. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7563. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7564. orderCode = [self translateSingleQuote:orderCode];
  7565. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7566. picPath = [self translateSingleQuote:picPath];
  7567. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7568. int ret = [iSalesDB execSql:sql];
  7569. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7570. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7571. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7572. // [dic setValue:@"160409" forKey:@"min_ver"];
  7573. return [RAUtils dict2data:dic];
  7574. }
  7575. #pragma mark save order
  7576. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7577. NSString *ret = [self valueInParams:params key:key];
  7578. if (translate) {
  7579. ret = [self translateSingleQuote:ret];
  7580. }
  7581. return ret;
  7582. }
  7583. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7584. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7585. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7586. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7587. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7588. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7589. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  7590. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  7591. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  7592. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  7593. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  7594. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  7595. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  7596. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  7597. if (![total_price isEqualToString:@""]) {
  7598. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  7599. } else {
  7600. total_price = @"";
  7601. }
  7602. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  7603. if ([paymentsAndCredits isEqualToString:@""]) {
  7604. paymentsAndCredits = @"";
  7605. } else {
  7606. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  7607. }
  7608. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  7609. if ([handling_fee_value isEqualToString:@""]) {
  7610. handling_fee_value = @"";
  7611. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  7612. handling_fee_value = @"";
  7613. } else {
  7614. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  7615. }
  7616. NSString *handling_fee_placeholder = handling_fee_value;
  7617. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  7618. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  7619. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  7620. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  7621. NSString *lift_gate_placeholder = @"";
  7622. if ([lift_gate_value isEqualToString:@""]) {
  7623. lift_gate_placeholder = @"";
  7624. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  7625. lift_gate_placeholder = @"";
  7626. } else {
  7627. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  7628. }
  7629. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  7630. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  7631. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  7632. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  7633. 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];
  7634. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  7635. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  7636. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  7637. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  7638. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  7639. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  7640. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  7641. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  7642. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  7643. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  7644. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  7645. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  7646. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  7647. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  7648. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  7649. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  7650. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  7651. BOOL number_nil = [credit_card_number isEqualToString:@""];
  7652. if (!number_nil) {
  7653. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  7654. }
  7655. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  7656. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  7657. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  7658. if (!security_code_nil) {
  7659. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  7660. }
  7661. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  7662. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  7663. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  7664. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  7665. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  7666. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  7667. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  7668. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  7669. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  7670. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  7671. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  7672. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  7673. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  7674. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  7675. NSString *contact_id = customer_cid;
  7676. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  7677. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  7678. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7679. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7680. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7681. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7682. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7683. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7684. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7685. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7686. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7687. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7688. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7689. //
  7690. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7691. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7692. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7693. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7694. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7695. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7696. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7697. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7698. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7699. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7700. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7701. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7702. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7703. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7704. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7705. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7706. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7707. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7708. 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];
  7709. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7710. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7711. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7712. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7713. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7714. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7715. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7716. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7717. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7718. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7719. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7720. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7721. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7722. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7723. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7724. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7725. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7726. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7727. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7728. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7729. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7730. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7731. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7732. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7733. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7734. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7735. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7736. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7737. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7738. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7739. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7740. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7741. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7742. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7743. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7744. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7745. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7746. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7747. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7748. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7749. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7750. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7751. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7752. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7753. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7754. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7755. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7756. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7757. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7758. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7759. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7760. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7761. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7762. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7763. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7764. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7765. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7766. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7767. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7768. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7769. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7770. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7771. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7772. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7773. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7774. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7775. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7776. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7777. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7778. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7779. NSString *order_status = @"status = 1,";
  7780. if (submit) {
  7781. order_status = @"status = -11,";
  7782. }
  7783. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7784. NSString *sync_sql = @"";
  7785. if (submit) {
  7786. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7787. [param setValue:sales_rep forKey:@"sales_rep"];
  7788. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7789. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7790. }
  7791. 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];
  7792. DebugLog(@"save order contactSql: %@",contactSql);
  7793. DebugLog(@"save order orderSql: %@",orderSql);
  7794. // int contact_ret = [iSalesDB execSql:contactSql];
  7795. int order_ret = [iSalesDB execSql:orderSql];
  7796. int ret = order_ret;
  7797. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7798. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7799. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7800. // [dic setValue:@"160409" forKey:@"min_ver"];
  7801. [dic setObject:so_id forKey:@"so#"];
  7802. return [RAUtils dict2data:dic];
  7803. }
  7804. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7805. {
  7806. // id foo = nil;
  7807. // NSMutableArray *a = @[].mutableCopy;
  7808. // [a addObject:foo];
  7809. return [self saveorder:param submit:NO];
  7810. }
  7811. #pragma mark add to cart by name
  7812. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7813. {
  7814. NSString *orderCode = [params objectForKey:@"orderCode"];
  7815. NSString *product_name = [params objectForKey:@"product_name"];
  7816. product_name = [self translateSingleQuote:product_name];
  7817. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7818. sqlite3 *db = [iSalesDB get_db];
  7819. __block NSMutableString *product_id_string = [NSMutableString string];
  7820. for (int i = 0; i < product_name_array.count; i++) {
  7821. NSString *name = [product_name_array objectAtIndex:i];
  7822. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7823. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7824. int product_id = sqlite3_column_int(stmt, 0);
  7825. if (i == product_name_array.count - 1) {
  7826. [product_id_string appendFormat:@"%d",product_id];
  7827. } else {
  7828. [product_id_string appendFormat:@"%d,",product_id];
  7829. }
  7830. }];
  7831. }
  7832. NSDictionary *newParams = @{
  7833. @"product_id" : product_id_string,
  7834. @"orderCode" : orderCode
  7835. };
  7836. [iSalesDB close_db:db];
  7837. return [self offline_add2cart:[newParams mutableCopy]];
  7838. }
  7839. #pragma mark reset order
  7840. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7841. {
  7842. UIApplication * app = [UIApplication sharedApplication];
  7843. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7844. appDelegate.disable_trigger=true;
  7845. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7846. appDelegate.disable_trigger=false;
  7847. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7848. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7849. return [RAUtils dict2data:dic];
  7850. }
  7851. #pragma mark submit order
  7852. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7853. {
  7854. return [self saveorder:params submit:YES];
  7855. }
  7856. #pragma mark copy order
  7857. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7858. {
  7859. NSMutableDictionary *ret = @{}.mutableCopy;
  7860. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7861. sqlite3 *db = [iSalesDB get_db];
  7862. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7863. // 首先查看联系人是否active
  7864. 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];
  7865. __block int customer_is_active = 1;
  7866. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7867. customer_is_active = sqlite3_column_int(stmt, 0);
  7868. }];
  7869. if (!customer_is_active) {
  7870. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7871. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7872. [iSalesDB close_db:db];
  7873. return [RAUtils dict2data:ret];
  7874. }
  7875. // new order
  7876. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7877. NSString *new_order_code = [self get_offline_soid:db];
  7878. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7879. user = [self translateSingleQuote:user];
  7880. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,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,'%@',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
  7881. __block int result = 1;
  7882. result = [iSalesDB execSql:insert_order_sql db:db];
  7883. if (!result) {
  7884. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7885. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7886. [iSalesDB close_db:db];
  7887. return [RAUtils dict2data:ret];
  7888. }
  7889. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7890. // __block NSString *product_id = @"";
  7891. __weak typeof(self) weakSelf = self;
  7892. 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];
  7893. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7894. int is_active = sqlite3_column_int(stmt, 1);
  7895. if (is_active) {
  7896. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7897. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7898. 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];
  7899. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7900. }
  7901. }];
  7902. // product_id = [product_id substringFromIndex:1];
  7903. //
  7904. // [self offline_add2cart:@{}.mutableCopy];
  7905. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7906. [iSalesDB close_db:db];
  7907. if (result) {
  7908. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7909. } else {
  7910. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7911. }
  7912. [ret setObject:new_order_code forKey:@"so_id"];
  7913. return [RAUtils dict2data:ret];
  7914. }
  7915. #pragma mark move wish list to cart
  7916. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7917. NSString *collectId = params[@"collectId"];
  7918. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7919. __block NSString *product_id = @"";
  7920. __block NSString *qty = @"";
  7921. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7922. int productId = sqlite3_column_int(stmt, 0);
  7923. int item_qty = sqlite3_column_int(stmt, 1);
  7924. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7925. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7926. }];
  7927. product_id = [product_id substringFromIndex:1];
  7928. qty = [qty substringFromIndex:1];
  7929. NSDictionary *newParams = @{
  7930. @"product_id" : product_id,
  7931. @"orderCode" : params[@"orderCode"],
  7932. @"qty" : qty
  7933. };
  7934. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7935. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7936. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7937. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7938. //
  7939. // int ret = [iSalesDB execSql:deleteSQL];
  7940. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7941. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7942. }
  7943. return [RAUtils dict2data:retDic];
  7944. }
  7945. #pragma mark - portfolio
  7946. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7947. int sort = [[params valueForKey:@"sort"] integerValue];
  7948. int offset = [[params valueForKey:@"offset"] integerValue];
  7949. int limit = [[params valueForKey:@"limit"] integerValue];
  7950. NSString *orderBy = @"";
  7951. switch (sort) {
  7952. case 0:{
  7953. orderBy = @"modify_time desc";
  7954. }
  7955. break;
  7956. case 1:{
  7957. orderBy = @"modify_time asc";
  7958. }
  7959. break;
  7960. case 2:{
  7961. orderBy = @"name asc";
  7962. }
  7963. break;
  7964. case 3:{
  7965. orderBy = @"name desc";
  7966. }
  7967. break;
  7968. case 4:{
  7969. orderBy = @"description asc";
  7970. }
  7971. break;
  7972. default:
  7973. break;
  7974. }
  7975. 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];
  7976. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7977. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7978. sqlite3 *db = [iSalesDB get_db];
  7979. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7980. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7981. int product_id = sqlite3_column_int(stmt, 0);
  7982. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7983. NSString *name = [self textAtColumn:1 statement:stmt];
  7984. NSString *description = [self textAtColumn:2 statement:stmt];
  7985. double price = sqlite3_column_double(stmt, 3);
  7986. double discount = sqlite3_column_double(stmt,4);
  7987. int qty = sqlite3_column_int(stmt, 5);
  7988. int percentage = sqlite3_column_int(stmt, 6);
  7989. int item_id = sqlite3_column_int(stmt, 7);
  7990. // int fashion_id = sqlite3_column_int(stmt, 8);
  7991. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7992. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7993. double percent = sqlite3_column_double(stmt, 11);
  7994. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7995. if ([price_null isEqualToString:@"null"]) {
  7996. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7997. }
  7998. NSMutableDictionary *item = @{
  7999. @"linenotes": line_note,
  8000. @"check": @(1),
  8001. @"product_id": product_id_string,
  8002. @"available_qty": @(qty),
  8003. @"available_percent" : @(percent),
  8004. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8005. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8006. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8007. @"img": img_path,
  8008. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8009. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8010. }.mutableCopy;
  8011. if (percentage) {
  8012. [item removeObjectForKey:@"available_qty"];
  8013. } else {
  8014. [item removeObjectForKey:@"available_percent"];
  8015. }
  8016. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8017. if ([qty_null isEqualToString:@"null"]) {
  8018. [item removeObjectForKey:@"available_qty"];
  8019. }
  8020. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8021. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8022. }];
  8023. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8024. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8025. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8026. }
  8027. [iSalesDB close_db:db];
  8028. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8029. [dic setValue:@"" forKey:@"email_content"];
  8030. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8031. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8032. return [RAUtils dict2data:dic];
  8033. }
  8034. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8035. __block int qty = 0;
  8036. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8037. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8038. qty = sqlite3_column_int(stmt, 0);
  8039. }];
  8040. return qty;
  8041. }
  8042. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8043. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8044. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8045. NSMutableDictionary * values = params[@"replaceValue"];
  8046. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8047. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8048. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8049. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8050. NSString *pdf_path = @"";
  8051. if (direct) {
  8052. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8053. } else {
  8054. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8055. }
  8056. NSString *create_user = [self valueInParams:params key:@"user"];
  8057. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8058. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8059. // model info
  8060. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8061. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8062. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8063. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8064. sqlite3 *db = [iSalesDB get_db];
  8065. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8066. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8067. int product_id = sqlite3_column_int(stmt, 0);
  8068. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8069. double price = sqlite3_column_double(stmt, 1);
  8070. double discount = sqlite3_column_double(stmt,2);
  8071. int qty = sqlite3_column_int(stmt, 3);
  8072. int percentage = sqlite3_column_int(stmt, 4);
  8073. int item_id = sqlite3_column_int(stmt, 5);
  8074. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8075. double percent = sqlite3_column_double(stmt, 7);
  8076. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8077. /* if ([price_null isEqualToString:@"null"]) {
  8078. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8079. }
  8080. */
  8081. [product_ids_string appendFormat:@"%@,",product_id_string];
  8082. // Regular Price
  8083. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8084. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8085. // QTY
  8086. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8087. if ([qty_null isEqualToString:@"null"]) {
  8088. // 查available
  8089. qty = [self model_QTY:product_id_string db:db];
  8090. }
  8091. if (percentage) {
  8092. qty = qty * percent / 100;
  8093. }
  8094. // Special Price
  8095. if ([price_null isEqualToString:@"null"]) {
  8096. // price = regular price
  8097. price = [regular_price_str doubleValue];
  8098. }
  8099. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8100. if (![discount_null isEqualToString:@"null"]) {
  8101. price = price * (1 - discount / 100.0);
  8102. }
  8103. NSMutableDictionary *item = @{
  8104. @"line_note": line_note,
  8105. @"product_id": product_id_string,
  8106. @"available_qty": @(qty),
  8107. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8108. @"regular_price" : regular_price_str,
  8109. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8110. }.mutableCopy;
  8111. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8112. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8113. }];
  8114. [iSalesDB close_db:db];
  8115. if (product_ids_string.length > 0) {
  8116. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8117. }
  8118. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8119. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8120. return [RAUtils dict2data:resultDictionary];
  8121. }
  8122. NSString *model_info = [RAUtils dict2string:dic];
  8123. // 创建PDF
  8124. // 在preview情况下保存,则不需要新建了
  8125. if (direct) {
  8126. NSMutableDictionary *tear_sheet_params = params;
  8127. // if (tear_sheet_id) {
  8128. // tear_sheet_params = values;
  8129. // }
  8130. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8131. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8132. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8133. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8134. resultDictionary = pdfInfo.mutableCopy;
  8135. } else { // 创建PDF失败
  8136. return pdfData;
  8137. }
  8138. } else {
  8139. // pdf_path 就是本地路径
  8140. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8141. }
  8142. // 将文件移动到PDF Cache文件夹
  8143. NSString *newPath = [pdf_path lastPathComponent];
  8144. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8145. NSString *cachefolder = [paths objectAtIndex:0];
  8146. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8147. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8148. NSFileManager *fileManager = [NSFileManager defaultManager];
  8149. NSError *error = nil;
  8150. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8151. if (error) { // 移动文件失败
  8152. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8153. return [RAUtils dict2data:resultDictionary];
  8154. }
  8155. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8156. pdf_path = [newPath lastPathComponent];
  8157. // 保存信息
  8158. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8159. NSString *off_params = [RAUtils dict2string:params];
  8160. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8161. pdf_path = [self translateSingleQuote:pdf_path];
  8162. create_user = [self translateSingleQuote:create_user];
  8163. tear_note = [self translateSingleQuote:tear_note];
  8164. tear_name = [self translateSingleQuote:tear_name];
  8165. model_info = [self translateSingleQuote:model_info];
  8166. configureParams = [self translateSingleQuote:configureParams];
  8167. off_params = [self translateSingleQuote:off_params];
  8168. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8169. if (tear_sheet_id) {
  8170. int _id = [tear_sheet_id integerValue];
  8171. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8172. }
  8173. int result = [iSalesDB execSql:save_pdf_sql];
  8174. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8175. //
  8176. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8177. if (remove_Item) {
  8178. // portfolioId
  8179. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8180. }
  8181. return [RAUtils dict2data:resultDictionary];
  8182. }
  8183. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8184. return [self offline_savePDF:params direct:YES];
  8185. }
  8186. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8187. return [self offline_savePDF:params direct:NO];
  8188. }
  8189. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8190. int offset = [[params valueForKey:@"offset"] integerValue];
  8191. int limit = [[params valueForKey:@"limit"] integerValue];
  8192. NSString *keyword = [params valueForKey:@"keyWord"];
  8193. NSString *where = @"where is_delete is null or is_delete = 0";
  8194. if (keyword.length) {
  8195. keyword = [self translateSingleQuote:keyword];
  8196. 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];
  8197. }
  8198. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8199. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8200. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8201. NSString *cachefolder = [paths objectAtIndex:0];
  8202. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8203. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8204. NSString *name = [self textAtColumn:0 statement:stmt];
  8205. NSString *note = [self textAtColumn:1 statement:stmt];
  8206. NSString *time = [self textAtColumn:2 statement:stmt];
  8207. NSString *user = [self textAtColumn:3 statement:stmt];
  8208. NSString *path = [self textAtColumn:4 statement:stmt];
  8209. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8210. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8211. path = [pdfFolder stringByAppendingPathComponent:path];
  8212. BOOL bdir=NO;
  8213. NSFileManager* fileManager = [NSFileManager defaultManager];
  8214. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8215. {
  8216. //pdf文件不存在
  8217. path=nil;
  8218. }
  8219. time = [self changeDateTimeFormate:time];
  8220. time = [time stringByAppendingString:@" PST"];
  8221. int sheet_id = sqlite3_column_int(stmt, 5);
  8222. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8223. item[@"tearsheetsId"]=@(sheet_id);
  8224. item[@"pdf_path"]=path;
  8225. item[@"create_time"]=time;
  8226. item[@"create_user"]=user;
  8227. item[@"tear_note"]=note;
  8228. item[@"tear_name"]=name;
  8229. item[@"isLocalFile"]=@"true";
  8230. item[@"model_info"]=model_info;
  8231. item[@"off_params"]=off_params;
  8232. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8233. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8234. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8235. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8236. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8237. }];
  8238. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8239. return [RAUtils dict2data:dic];
  8240. }
  8241. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8242. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8243. // NSString *user = [params objectForKey:@"user"];
  8244. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  8245. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  8246. int result = [iSalesDB execSql:sql];
  8247. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8248. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8249. if (result == RESULT_TRUE) {
  8250. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8251. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8252. sqlite3 *db = [iSalesDB get_db];
  8253. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8254. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8255. [iSalesDB close_db:db];
  8256. }
  8257. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8258. return [RAUtils dict2data:dic];
  8259. }
  8260. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8261. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8262. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8263. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8264. // Regurlar Price
  8265. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8266. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8267. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8268. __block long val_count = 0;
  8269. NSString *sql = @"select name,type,order_by from price order by order_by";
  8270. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8271. NSString *name = [self textAtColumn:0 statement:stmt];
  8272. int type = sqlite3_column_int(stmt, 1);
  8273. int order_by = sqlite3_column_int(stmt, 2);
  8274. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8275. NSDictionary *price_dic = @{
  8276. @"value" : name,
  8277. @"value_id" : [NSNumber numberWithInteger:type],
  8278. @"check" : order_by == 0 ? @(1) : @(0)
  8279. };
  8280. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8281. val_count = ++(*count);
  8282. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8283. val_count = 0;
  8284. }];
  8285. [cadedate setObject:@{@"value" : @"None",
  8286. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8287. val_count++;
  8288. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8289. [price setObject:cadedate forKey:@"cadedate"];
  8290. [section1 setObject:price forKey:@"item_2"];
  8291. [dic setObject:section1 forKey:@"section_1"];
  8292. return [RAUtils dict2data:dic];
  8293. }
  8294. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8295. NSString *product_id = [params objectForKey:@"fashionId"];
  8296. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8297. __block int result = RESULT_TRUE;
  8298. __block int qty = 0;
  8299. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8300. qty = sqlite3_column_int(stmt, 0);
  8301. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8302. result = RESULT_FALSE;
  8303. }];
  8304. NSMutableDictionary *dic = @{
  8305. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8306. @"mode" : @"Regular Mode",
  8307. @"quantity_available" : @(qty),
  8308. @"result" : @(result),
  8309. }.mutableCopy;
  8310. return [RAUtils dict2data:dic];
  8311. }
  8312. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8313. NSString *item_ids = [params objectForKey:@"item_id"];
  8314. NSString *line_notes = [params objectForKey:@"notes"];
  8315. NSString *price_str = [params objectForKey:@"price"];
  8316. NSString *discount_str = [params objectForKey:@"discount"];
  8317. NSString *percent = [params objectForKey:@"available_percent"];
  8318. NSString *qty = [params objectForKey:@"available_qty"];
  8319. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8320. int dot = 0;
  8321. if (line_notes) {
  8322. line_notes = [self translateSingleQuote:line_notes];
  8323. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8324. sql = [sql stringByAppendingString:line_notes];
  8325. dot = 1;
  8326. } else {
  8327. line_notes = @"";
  8328. }
  8329. if (price_str) {
  8330. if (dot) {
  8331. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8332. } else {
  8333. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8334. dot = 1;
  8335. }
  8336. sql = [sql stringByAppendingString:price_str];
  8337. } else {
  8338. price_str = @"";
  8339. }
  8340. if (discount_str) {
  8341. if (dot) {
  8342. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8343. } else {
  8344. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8345. dot = 1;
  8346. }
  8347. sql = [sql stringByAppendingString:discount_str];
  8348. } else {
  8349. discount_str = @"";
  8350. }
  8351. if (percent) {
  8352. if (dot) {
  8353. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8354. } else {
  8355. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8356. dot = 1;
  8357. }
  8358. sql = [sql stringByAppendingString:percent];
  8359. } else {
  8360. percent = @"";
  8361. }
  8362. if (qty) {
  8363. if (dot) {
  8364. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8365. } else {
  8366. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8367. dot = 1;
  8368. }
  8369. sql = [sql stringByAppendingString:qty];
  8370. } else {
  8371. qty = @"";
  8372. }
  8373. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8374. sql = [sql stringByAppendingString:where];
  8375. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8376. int result = [iSalesDB execSql:sql];
  8377. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8378. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8379. return [RAUtils dict2data:dic];
  8380. }
  8381. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8382. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8383. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8384. sqlite3 *db = [iSalesDB get_db];
  8385. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  8386. int result = [iSalesDB execSql:sql db:db];
  8387. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8388. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8389. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8390. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8391. [iSalesDB close_db:db];
  8392. return [RAUtils dict2data:dic];
  8393. }
  8394. + (void)offline_removePDFWithName:(NSString *)name {
  8395. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8396. NSString *cachefolder = [paths objectAtIndex:0];
  8397. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8398. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8399. NSFileManager *fileManager = [NSFileManager defaultManager];
  8400. [fileManager removeItemAtPath:path error:nil];
  8401. }
  8402. + (void)offline_clear_PDFCache {
  8403. NSFileManager *fileManager = [NSFileManager defaultManager];
  8404. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8405. NSString *cachefolder = [paths objectAtIndex:0];
  8406. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8407. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8408. for (NSString *path in pdf_files) {
  8409. [self offline_removePDFWithName:[path lastPathComponent]];
  8410. }
  8411. }
  8412. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8413. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8414. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8415. NSString *user = [params objectForKey:@"user"];
  8416. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8417. if (![create_user isEqualToString:user]) {
  8418. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8419. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8420. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8421. return [RAUtils dict2data:dic];
  8422. }
  8423. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8424. __block int is_local = 0;
  8425. __block NSString *path = @"";
  8426. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8427. is_local = sqlite3_column_int(stmt, 0);
  8428. path = [self textAtColumn:1 statement:stmt];
  8429. }];
  8430. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8431. if (is_local == 1) {
  8432. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8433. // 删除文件
  8434. [self offline_removePDFWithName:path];
  8435. }
  8436. int result = [iSalesDB execSql:sql];
  8437. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8438. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8439. return [RAUtils dict2data:dic];
  8440. }
  8441. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8442. {
  8443. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8444. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8445. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8446. NSString* where=@"1=1";
  8447. if (ver!=nil) {
  8448. where=@"is_dirty=1";
  8449. }
  8450. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  8451. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8452. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8453. sqlite3 *db = [iSalesDB get_db];
  8454. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8455. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8456. NSInteger _id = sqlite3_column_int(stmt, 0);
  8457. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8458. NSString *name = [self textAtColumn:2 statement:stmt];
  8459. NSString *desc = [self textAtColumn:3 statement:stmt];
  8460. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8461. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8462. NSInteger qty = sqlite3_column_int(stmt, 6);
  8463. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8464. double percent = sqlite3_column_double(stmt, 8);
  8465. double price = sqlite3_column_double(stmt, 9);
  8466. double discount = sqlite3_column_double(stmt, 10);
  8467. NSString *img = [self textAtColumn:11 statement:stmt];
  8468. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8469. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8470. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8471. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8472. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8473. if ([price_null isEqualToString:@"null"]) {
  8474. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8475. }
  8476. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8477. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8478. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8479. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8480. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8481. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8482. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8483. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8484. if ([qty_null isEqualToString:@"null"]) {
  8485. // [item setValue:@"null" forKey:@"available_qty"];
  8486. } else {
  8487. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8488. }
  8489. if ([is_percent_null isEqualToString:@"null"]) {
  8490. // [item setValue:@"null" forKey:@"percentage"];
  8491. } else {
  8492. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8493. }
  8494. if ([percent_null isEqualToString:@"null"]) {
  8495. // [item setValue:@"null" forKey:@"percent"];
  8496. } else {
  8497. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8498. }
  8499. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8500. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8501. [item setValue:name forKey:@"name"];
  8502. [item setValue:desc forKey:@"description"];
  8503. [item setValue:img forKey:@"img"];
  8504. [item setValue:line_note forKey:@"line_note"];
  8505. [item setValue:create_time forKey:@"createtime"];
  8506. [item setValue:modify_time forKey:@"modifytime"];
  8507. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8508. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8509. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8510. }];
  8511. [iSalesDB close_db:db];
  8512. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8513. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8514. }
  8515. return ret;
  8516. }
  8517. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8518. {
  8519. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8520. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8521. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8522. NSString* where=@"1=1";
  8523. if (ver!=nil) {
  8524. where=@"is_dirty=1";
  8525. }
  8526. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  8527. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8528. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8529. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8530. NSInteger _id = sqlite3_column_int(stmt, 0);
  8531. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  8532. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  8533. NSString *create_user = [self textAtColumn:3 statement:stmt];
  8534. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  8535. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  8536. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8537. NSString *createtime = [self textAtColumn:7 statement:stmt];
  8538. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  8539. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  8540. NSString *off_params = [self textAtColumn:10 statement:stmt];
  8541. NSString *uuid = [NSUUID UUID].UUIDString;
  8542. int is_delete = sqlite3_column_int(stmt, 11);
  8543. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  8544. int is_local = sqlite3_column_int(stmt, 12);
  8545. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8546. if (![tearsheet_id_null isEqualToString:@"null"]) {
  8547. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  8548. }
  8549. [item setObject:pdf_path forKey:@"pdf_path"];
  8550. [item setObject:create_user forKey:@"create_user"];
  8551. [item setObject:tear_note forKey:@"tear_note"];
  8552. [item setObject:tear_name forKey:@"tear_name"];
  8553. [item setObject:model_info forKey:@"model_info"];
  8554. [item setObject:createtime forKey:@"createtime"];
  8555. [item setObject:modifytime forKey:@"modifytime"];
  8556. [item setObject:urlParams forKey:@"urlParams"];
  8557. [item setObject:off_params forKey:@"off_params"];
  8558. [item setObject:uuid forKey:@"pdf_token"];
  8559. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  8560. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  8561. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  8562. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  8563. } else {
  8564. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8565. }
  8566. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8567. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8568. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8569. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8570. }];
  8571. return ret;
  8572. }
  8573. @end