OLDataProvider.m 519 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402
  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. float bprice=0;
  150. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  151. for(int b=0;b< [bundle[@"count"] intValue];b++)
  152. {
  153. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  154. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  155. }
  156. if(gprice!=nil)
  157. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  158. if(s_percent.length==0||is_percentage==0)
  159. {
  160. percent=100.0;
  161. if([s_qty isEqualToString:@"null"])
  162. qty=availability;
  163. }
  164. else
  165. {
  166. qty=availability;
  167. }
  168. qty=qty*percent/100+0.5;
  169. for(int i=0;i<[values[@"count"] intValue];i++)
  170. {
  171. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  172. if([valueitem[@"product_id"] intValue]==product_id)
  173. {
  174. line_note=valueitem[@"line_note"];
  175. qty=[valueitem[@"available_qty"] intValue];
  176. if([[valueitem allKeys] containsObject:@"available_qty"])
  177. s_qty=[NSString stringWithFormat:@"%d",qty];
  178. else
  179. s_qty=@"null";
  180. if(valueitem[@"regular_price"]!=nil)
  181. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  182. s_price=valueitem[@"special_price"];
  183. price=[valueitem[@"special_price"] floatValue];
  184. discount=0;
  185. }
  186. }
  187. if(is_percentage==0&&qty==0&&!show_stockout)
  188. {
  189. return;
  190. }
  191. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  192. {
  193. return;
  194. }
  195. NSString* set_price=@"";
  196. if([params[@"entered_price"] boolValue])
  197. {
  198. if (s_price==nil ) {
  199. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  200. }
  201. else
  202. {
  203. if(price*(1-discount/100.0)!=gprice.floatValue)
  204. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  205. }
  206. }
  207. NSString* get_price=@"";
  208. {
  209. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  210. // DebugLog(@"price time interval");
  211. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  212. if(gprice==nil)
  213. get_price=@"Price:No Price.";
  214. else
  215. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  216. }
  217. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  218. // if ([qty_null isEqualToString:@"null"]) {
  219. // qty=availability;
  220. // }
  221. //
  222. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  223. // if ([qty_percent_null isEqualToString:@"null"]) {
  224. // percentage=1;
  225. // }
  226. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  227. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  228. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  229. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  230. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  231. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  232. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  233. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  234. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  235. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  236. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  237. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  238. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  239. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  240. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  241. if([params[@"availability"] boolValue]==false)
  242. str_availability=@"";
  243. else
  244. {
  245. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  246. str_availability=@"<b>Availability:</b> In Production";
  247. }
  248. if([params[@"color"] boolValue]==false || color.length==0 )
  249. str_color=@"";
  250. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  251. str_model_set=@"";
  252. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  253. str_legcolor=@"";
  254. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  255. str_demension=@"";
  256. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  257. str_seat_height=@"";
  258. if([params[@"material"] boolValue]==false || material.length==0 )
  259. str_meterial=@"";
  260. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  261. str_box_dim=@"";
  262. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  263. str_volume=@"";
  264. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  265. str_weight=@"";
  266. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  267. str_load_ability=@"";
  268. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  269. str_fabric_content=@"";
  270. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  271. str_assembling=@"";
  272. if([params[@"made"] boolValue]==false || made_in.length==0 )
  273. str_made_in=@"";
  274. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  275. str_line_note=@"";
  276. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  277. [arr_detail addObject:str_availability];
  278. [arr_detail addObject:str_color];
  279. [arr_detail addObject:str_model_set];
  280. [arr_detail addObject:str_legcolor];
  281. [arr_detail addObject:str_demension];
  282. [arr_detail addObject:str_seat_height];
  283. [arr_detail addObject:str_meterial];
  284. [arr_detail addObject:str_box_dim];
  285. [arr_detail addObject:str_volume];
  286. [arr_detail addObject:str_weight];
  287. [arr_detail addObject:str_load_ability];
  288. [arr_detail addObject:str_fabric_content];
  289. [arr_detail addObject:str_assembling];
  290. [arr_detail addObject:str_made_in];
  291. [arr_detail addObject:str_line_note];
  292. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  293. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  294. //model image;
  295. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  296. // qr image
  297. NSString* qrpath=nil;
  298. if([params[@"show_barcode"] boolValue])
  299. {
  300. NSString* temp = NSTemporaryDirectory();
  301. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  302. qrpath=[temp stringByAppendingPathComponent:filename];
  303. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  304. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  305. }
  306. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  307. item[@"img"]=img_path;
  308. item[@"img_qr"]=qrpath;
  309. item[@"special_price"]=set_price;
  310. item[@"price"]=get_price;
  311. item[@"name"]=name;
  312. item[@"description"]=description;
  313. item[@"detail"]=detail;
  314. item[@"img"]=img_path;
  315. // @{
  316. // //@"linenotes": line_note,
  317. //// @"product_id": product_id_string,
  318. //// @"available_qty": @(qty),
  319. //// @"available_percent" : @(percent),
  320. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  321. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  322. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  323. // @"": ,
  324. // @"": ,
  325. // @"": ,
  326. // @"": ,
  327. // @"": ,
  328. // @"": ,
  329. // @"":
  330. // }.mutableCopy;
  331. // if (percentage) {
  332. // [item removeObjectForKey:@"available_qty"];
  333. // } else {
  334. // [item removeObjectForKey:@"available_percent"];
  335. // }
  336. //
  337. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  338. // if ([qty_null isEqualToString:@"null"]) {
  339. // [item removeObjectForKey:@"available_qty"];
  340. // }
  341. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  342. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  343. }];
  344. [iSalesDB close_db:db];
  345. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  346. // [dic setValue:@"" forKey:@"email_content"];
  347. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  348. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  349. //
  350. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  351. // cell[@"count"]=[NSNumber numberWithInt:10];
  352. grid[@"cell0"]=dic;
  353. data[@"grid0"]=grid;
  354. return data;
  355. }
  356. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  357. {
  358. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  359. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  360. NSString *cache_folder=[paths objectAtIndex:0];
  361. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  362. BOOL bdir=NO;
  363. NSFileManager* fileManager = [NSFileManager defaultManager];
  364. if(PDF_DEBUG)
  365. {
  366. NSData *data = [NSData dataWithContentsOfFile:default_path];
  367. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  368. return ret;
  369. }
  370. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  371. {
  372. NSError * error=nil;
  373. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  374. {
  375. return nil;
  376. }
  377. }
  378. NSData *data = [NSData dataWithContentsOfFile:template_path];
  379. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  380. return ret;
  381. }
  382. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  383. {
  384. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  385. 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,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  386. DebugLog(@"offline_login sql:%@",sqlQuery);
  387. sqlite3_stmt * statement;
  388. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  389. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  390. {
  391. if (sqlite3_step(statement) == SQLITE_ROW)
  392. {
  393. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  394. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  395. int can_show_price = sqlite3_column_int(statement, 0);
  396. int can_see_price = sqlite3_column_int(statement, 1);
  397. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  398. if(contact_id==nil)
  399. contact_id="";
  400. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  401. int user_type = sqlite3_column_int(statement, 3);
  402. int can_cancel_order = sqlite3_column_int(statement, 4);
  403. int can_set_cart_price = sqlite3_column_int(statement, 5);
  404. int can_create_portfolio = sqlite3_column_int(statement, 6);
  405. int can_delete_order = sqlite3_column_int(statement, 7);
  406. int can_submit_order = sqlite3_column_int(statement, 8);
  407. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  408. int can_create_order = sqlite3_column_int(statement, 10);
  409. char *mode = (char*)sqlite3_column_text(statement, 11);
  410. if(mode==nil)
  411. mode="";
  412. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  413. char *username = (char*)sqlite3_column_text(statement, 12);
  414. if(username==nil)
  415. username="";
  416. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  417. int can_update_contact_info = sqlite3_column_int(statement, 13);
  418. char *first_name = (char*)sqlite3_column_text(statement, 14);
  419. if(first_name==nil)
  420. first_name="";
  421. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  422. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  423. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  424. // [header setValue:nscontact_id forKey:@"contact_id"];
  425. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  426. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  427. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  428. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  429. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  430. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  431. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  432. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  433. //
  434. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  435. //
  436. // [header setValue:nsusername forKey:@"username"];
  437. //
  438. // NSError* error=nil;
  439. //
  440. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  441. // [header setValue:statusFilter forKey:@"statusFilter"];
  442. //
  443. //
  444. //
  445. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  446. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  447. //
  448. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  449. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  450. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  451. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  452. //
  453. // [ret setObject:header forKey:@"header"];
  454. [ret setValue:nsfirst_name forKey:@"first_name"];
  455. }
  456. sqlite3_finalize(statement);
  457. }
  458. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  459. return ret;
  460. }
  461. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  462. {
  463. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  464. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  465. data[@"npd_url"]=@"www.newpacificdirect.com";
  466. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  467. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  468. NSString* date = [formatter stringFromDate:[NSDate date]];
  469. data[@"print_date"]=date;
  470. data[@"company_name"]=@"New Pacific Direct,Inc.";
  471. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  472. data[@"TOTAL_PAGE"]=0;
  473. data[@"CURRENT_PAGE"]=0;
  474. // [formatter setDateFormat:@"MM/dd/yyyy"];
  475. // date = [formatter stringFromDate:[NSDate date]];
  476. // data[@"creat_date"]=date;
  477. NSString* temp = NSTemporaryDirectory();
  478. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  479. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  480. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  481. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  482. data[@"img_barcode"]=qrpath;
  483. data[@"so_id"]=order_code;
  484. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  485. // 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];
  486. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  487. sqlite3 *db = [iSalesDB get_db];
  488. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  489. sqlite3_stmt * statement;
  490. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  491. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  492. row0[@"count"]=[NSNumber numberWithInt:1];
  493. datasource[@"row0"]=row0;
  494. NSString* print_status=@"";
  495. NSString* order_type=@"Offline Quote";
  496. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  497. {
  498. if (sqlite3_step(statement) == SQLITE_ROW)
  499. {
  500. int erpOrderStatus = sqlite3_column_int(statement, 0);
  501. int status = sqlite3_column_int(statement, 26);
  502. if(status==2)
  503. {
  504. switch (erpOrderStatus) {
  505. case 10:
  506. // order_type=@"Quoted Order";
  507. print_status=@"Quote";
  508. break;
  509. case 11:
  510. print_status=@"Booked";
  511. break;
  512. case 12:
  513. print_status=@"Picked";
  514. break;
  515. case 13:
  516. print_status=@"Shipped";
  517. break;
  518. case 14:
  519. print_status=@"Closed";
  520. break;
  521. case 15:
  522. print_status=@"Canceled";
  523. break;
  524. case 16:
  525. print_status=@"All";
  526. break;
  527. case 18:
  528. print_status=@"Processing";
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. else{
  535. switch (status) {
  536. case 0:
  537. {
  538. print_status=@"Quote";
  539. break;
  540. }
  541. case 1:
  542. {
  543. print_status=@"Saved";
  544. break;
  545. }
  546. case 3:
  547. {
  548. //canceled
  549. print_status=@"Canceled";
  550. break;
  551. }
  552. default:
  553. break;
  554. }
  555. }
  556. NSString* sender_name = [self textAtColumn:1 statement:statement];
  557. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  558. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  559. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  560. NSString* sender_email = [self textAtColumn:5 statement:statement];
  561. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  562. NSString* billing_name = [self textAtColumn:7 statement:statement];
  563. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  564. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  565. if(billing_phone.length>0)
  566. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  567. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  568. if(billing_fax.length>0)
  569. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  570. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  571. [billto_arr0 addObject:billing_phone];
  572. [billto_arr0 addObject:billing_fax];
  573. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  574. NSString* billing_email = [self textAtColumn:11 statement:statement];
  575. if(billing_email.length>0)
  576. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  577. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  578. if(billing_contact.length>0)
  579. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  580. NSString* receive_name = [self textAtColumn:12 statement:statement];
  581. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  582. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  583. if(receive_phone.length>0)
  584. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  585. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  586. if(receive_fax.length>0)
  587. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  588. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  589. [receive_arr0 addObject:receive_phone];
  590. [receive_arr0 addObject:receive_fax];
  591. NSString* receive_email = [self textAtColumn:16 statement:statement];
  592. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  593. if(receive_email.length>0)
  594. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  595. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  596. if(receive_contact.length>0)
  597. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  598. NSString* poNumber = [self textAtColumn:17 statement:statement];
  599. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  600. NSString* carrier = [self textAtColumn:19 statement:statement];
  601. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  602. NSString* terms = [self textAtColumn:21 statement:statement];
  603. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  604. NSString* port_destination = [self textAtColumn:23 statement:statement];
  605. NSString* etd = [self textAtColumn:24 statement:statement];
  606. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  607. if(sign_picpath.length==0)
  608. sign_picpath=nil;
  609. NSString* create_date = [self textAtColumn:27 statement:statement];
  610. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  611. NSString* general_notes=[self textAtColumn:33 statement:statement];
  612. NSString *customerID = [self textAtColumn:34 statement:statement];
  613. if (customerID.length == 0) {
  614. customerID = nil;
  615. }
  616. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  617. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  618. if(general_notes.length>0)
  619. {
  620. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  621. row2[@"item_0"]=@{@"g_notes":general_notes };
  622. row2[@"count"]=[NSNumber numberWithInt:1];
  623. datasource[@"row2"]=row2;
  624. }
  625. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  626. [billto_arr addObject:billing_ext];
  627. [billto_arr addObject:billing_pf];
  628. [billto_arr addObject:billing_contact];
  629. [billto_arr addObject:billing_email];
  630. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  631. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  632. [receive_arr addObject:receive_ext];
  633. [receive_arr addObject:receive_pf];
  634. [receive_arr addObject:receive_contact];
  635. [receive_arr addObject:receive_email];
  636. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  637. float shipping = sqlite3_column_double(statement, 28);
  638. float lift_gate_value = sqlite3_column_double(statement, 29);
  639. float handling_fee_value = sqlite3_column_double(statement, 30);
  640. data[@"company"]=sender_name;
  641. data[@"shipfrom_addr"]=sender_ext;
  642. data[@"shipfrom_phone"]=sender_phone;
  643. data[@"shipfrom_fax"]=sender_fax;
  644. data[@"shipfrom_email"]=sender_email;
  645. data[@"warehouse_name"]=warehouse_name;
  646. data[@"bill_to_name"]=billing_name;
  647. data[@"bill_to_ext"]=billto;
  648. data[@"ship_to_name"]=receive_name;
  649. data[@"ship_to_ext"]=receive;
  650. data[@"po_no"]=poNumber;
  651. data[@"rep"]=sales_rep;
  652. data[@"e_schdule"]=etd;
  653. data[@"sales_terms"]=sales_terms;
  654. data[@"port_destination"]=port_destination;
  655. data[@"terms"]=terms;
  656. data[@"carrier"]=carrier;
  657. data[@"vendor_no"]=vendor_no;
  658. data[@"customer_sign"]=sign_picpath;
  659. data[@"print_status"]=print_status;
  660. data[@"create_date"]=create_date;
  661. 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];;
  662. sqlite3_stmt * statement_cart;
  663. // NSDate *date1 = [NSDate date];
  664. int count=0;
  665. int cart_count=0;
  666. float credits=0;
  667. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  668. if ( dbresult== SQLITE_OK)
  669. {
  670. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  671. int row1count=0;
  672. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  673. {
  674. NSDate *row_date = [NSDate date];
  675. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  676. // int product_id = sqlite3_column_int(statement_cart, 0);
  677. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  678. int item_id = sqlite3_column_int(statement_cart, 7);
  679. double discount = sqlite3_column_double(statement_cart, 2);
  680. NSString* Price=nil;
  681. if(str_price==nil)
  682. {
  683. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  684. // NSDate *price_date = [NSDate date];
  685. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  686. // DebugLog(@"price time interval");
  687. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  688. if(price==nil)
  689. Price=@"No Price.";
  690. else
  691. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  692. }
  693. else
  694. {
  695. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  696. }
  697. float dunitprice=0;
  698. int item_count = sqlite3_column_int(statement_cart, 3);
  699. NSString* Amount=@"";
  700. if(![Price isEqualToString:@"No Price."])
  701. {
  702. dunitprice=[Price floatValue];
  703. dunitprice=dunitprice* (1-discount/100.0);
  704. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  705. credits+=dunitprice*item_count;
  706. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  707. }
  708. else
  709. {
  710. Price=@"";
  711. }
  712. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  713. // NSString *nsline_note=nil;
  714. // if(line_note!=nil)
  715. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  716. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  717. NSString *nsname=nil;
  718. if(name!=nil)
  719. nsname= [[NSString alloc]initWithUTF8String:name];
  720. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  721. NSString *nsdescription=nil;
  722. if(description!=nil)
  723. nsdescription= [[NSString alloc]initWithUTF8String:description];
  724. // int stockUom = sqlite3_column_int(statement_cart, 8);
  725. // int _id = sqlite3_column_int(statement_cart, 9);
  726. // NSDate *subtotal_date = [NSDate date];
  727. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  728. // DebugLog(@"subtotal_date time interval");
  729. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  730. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  731. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  732. // int carton=[bsubtotaljson[@"carton"] intValue];
  733. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  734. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  735. // NSDate *img_date = [NSDate date];
  736. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  737. // DebugLog(@"img_date time interval");
  738. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  739. itemjson[@"model"]=nsname;
  740. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  741. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  742. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  743. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  744. // itemjson[@"check"]=@"true";
  745. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  746. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  747. itemjson[@"unit_price"]=Price;
  748. itemjson[@"amount"]=Amount;
  749. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  750. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  751. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  752. // itemjson[@"note"]=nsline_note;
  753. // NSDate *date2 = [NSDate date];
  754. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  755. // DebugLog(@"model_bundle time interval");
  756. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  757. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  758. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  759. row1count++;
  760. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  761. for(int c=0;c<[combine[@"count"] intValue];c++)
  762. {
  763. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  764. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  765. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  766. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  767. credits+=combine_amount;
  768. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  769. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  770. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  771. row1count++;
  772. }
  773. count++;
  774. // DebugLog(@"row time interval");
  775. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  776. }
  777. row1[@"count"]=[NSNumber numberWithInt:row1count];
  778. datasource[@"row1"]=row1;
  779. data[@"table_model"]=datasource;
  780. sqlite3_finalize(statement_cart);
  781. }
  782. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  783. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  784. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  785. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  786. // if(credits>0)
  787. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  788. // else
  789. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  790. if(shipping>0)
  791. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  792. else
  793. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  794. if(lift_gate_value>0)
  795. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  796. else
  797. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  798. if(handling_fee_value>0)
  799. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  800. else
  801. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  802. float total = credits+shipping+lift_gate_value+handling_fee_value;
  803. if(total>0)
  804. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  805. else
  806. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  807. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  808. fee[@"row0"]=fee_row0;
  809. data[@"table_fee"]=fee;
  810. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  811. // ret[@"sign_url"] = sign_url;
  812. //
  813. // customer_contact = [self textAtColumn:52 statement:statement];
  814. // customer_email = [self textAtColumn:53 statement:statement];
  815. // customer_phone = [self textAtColumn:54 statement:statement];
  816. // customer_fax = [self textAtColumn:55 statement:statement];
  817. //
  818. //
  819. // int offline_edit=sqlite3_column_int(statement, 56);
  820. //
  821. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  822. //
  823. // char *soid = (char*)sqlite3_column_text(statement, 1);
  824. // if(soid==nil)
  825. // soid= "";
  826. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  827. // // so#
  828. // ret[@"so#"] = nssoid;
  829. //
  830. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  831. // if(poNumber==nil)
  832. // poNumber= "";
  833. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  834. //
  835. //
  836. //
  837. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  838. // if(create_time==nil)
  839. // create_time= "";
  840. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  841. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  842. //
  843. // int status = sqlite3_column_int(statement, 4);
  844. // int erpStatus = sqlite3_column_int(statement, 49);
  845. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  846. //
  847. // // status
  848. // if (status > 1 && status != 3) {
  849. // status = erpStatus;
  850. // } else if (status == 3) {
  851. // status = 15;
  852. // }
  853. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  854. // ret[@"order_status"] = nsstatus;
  855. //
  856. //
  857. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  858. // if(company_name==nil)
  859. // company_name= "";
  860. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  861. // // company name
  862. // ret[@"company_name"] = nscompany_name;
  863. //
  864. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  865. // if(customer_contact==nil)
  866. // customer_contact= "";
  867. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  868. //
  869. //
  870. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  871. // if(addr_1==nil)
  872. // addr_1="";
  873. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  874. //
  875. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  876. // if(addr_2==nil)
  877. // addr_2="";
  878. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  879. //
  880. //
  881. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  882. // if(addr_3==nil)
  883. // addr_3="";
  884. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  885. //
  886. //
  887. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  888. // if(addr_4==nil)
  889. // addr_4="";
  890. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  891. //
  892. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  893. // [arr_addr addObject:nsaddr_1];
  894. // [arr_addr addObject:nsaddr_2];
  895. // [arr_addr addObject:nsaddr_3];
  896. // [arr_addr addObject:nsaddr_4];
  897. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  898. //
  899. //
  900. // char *logist = (char*)sqlite3_column_text(statement, 11);
  901. // if(logist==nil)
  902. // logist= "";
  903. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  904. // if (status == -11 || status == 10 || status == 11) {
  905. // nslogist = [self textAtColumn:59 statement:statement];
  906. // };
  907. //
  908. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  909. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  910. // shipping = @"Shipping To Be Quoted";
  911. // } else {
  912. // shippingFee = sqlite3_column_double(statement, 12);
  913. // }
  914. //
  915. // // Shipping
  916. // ret[@"Shipping"] = shipping;
  917. //
  918. // int have_lift_gate = sqlite3_column_int(statement, 17);
  919. // lift_gate = sqlite3_column_double(statement, 13);
  920. // // Liftgate Fee(No loading dock)
  921. // if (!have_lift_gate) {
  922. // lift_gate = 0;
  923. // }
  924. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  925. // if (sqlite3_column_int(statement, 57)) {
  926. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  927. // }
  928. //
  929. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  930. // if(general_notes==nil)
  931. // general_notes= "";
  932. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  933. //
  934. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  935. // if(internal_notes==nil)
  936. // internal_notes= "";
  937. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  938. //
  939. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  940. // if(payment_type==nil)
  941. // payment_type= "";
  942. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  943. //
  944. //
  945. //
  946. // // order info
  947. // orderinfo = [self textFileName:@"order_info.html"];
  948. //
  949. //
  950. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  951. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  952. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  953. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  954. //
  955. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  956. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  957. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  958. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  959. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  960. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  963. //
  964. // NSString *payment = nil;
  965. // if([nspayment_type isEqualToString:@"Credit Card"])
  966. // {
  967. // payment = [self textFileName:@"creditcardpayment.html"];
  968. //
  969. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  970. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  971. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  972. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  973. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  974. // NSString *card_type = [self textAtColumn:42 statement:statement];
  975. // if (card_type.length > 0) { // 显示星号
  976. // card_type = @"****";
  977. // }
  978. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  979. // if (card_number.length > 0 && card_number.length > 4) {
  980. // for (int i = 0; i < card_number.length - 4; i++) {
  981. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  982. // }
  983. // } else {
  984. // card_number = @"";
  985. // }
  986. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  987. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  988. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  989. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  990. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  991. // card_expiration = @"****";
  992. // }
  993. //
  994. //
  995. // NSString *card_city = [self textAtColumn:46 statement:statement];
  996. // NSString *card_state = [self textAtColumn:47 statement:statement];
  997. //
  998. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  999. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1000. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1001. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1002. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1003. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1004. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1005. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1006. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1007. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1008. //
  1009. //
  1010. // }
  1011. // else
  1012. // {
  1013. // payment=[self textFileName:@"normalpayment.html"];
  1014. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1015. // }
  1016. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1017. //
  1018. // ret[@"result"]= [NSNumber numberWithInt:2];
  1019. //
  1020. // // more info
  1021. // moreInfo = [self textFileName:@"more_info.html"];
  1022. //
  1023. //
  1024. // // ShipToCompany_or_&nbsp
  1025. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1026. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1027. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1028. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1029. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1030. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1031. //
  1032. //
  1033. // // ShipFromCompany_or_&nbsp
  1034. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1036. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1038. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1040. //
  1041. //
  1042. // // FreightBillToCompany_or_&nbsp
  1043. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1045. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1047. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1049. //
  1050. //
  1051. // // MerchandiseBillToCompany_or_&nbsp
  1052. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1054. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1056. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1058. //
  1059. //
  1060. // // ReturnToCompany_or_&nbsp
  1061. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1063. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1065. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1067. // //
  1068. //
  1069. // // DebugLog(@"more info : %@",moreInfo);
  1070. //
  1071. // // handling fee
  1072. // handlingFee = sqlite3_column_double(statement, 33);
  1073. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1074. // if (sqlite3_column_int(statement, 58)) {
  1075. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1076. // }
  1077. // //
  1078. //
  1079. // // customer info
  1080. // customerID = [self textAtColumn:36 statement:statement];
  1081. //
  1082. // // mode
  1083. // ret[@"mode"] = appDelegate.mode;
  1084. //
  1085. // // model_count
  1086. // ret[@"model_count"] = @(0);
  1087. }
  1088. sqlite3_finalize(statement);
  1089. }
  1090. [iSalesDB close_db:db];
  1091. data[@"order_type"]=order_type;
  1092. /*
  1093. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1094. // "customer_email" = "Shui Hu";
  1095. // "customer_fax" = "";
  1096. // "customer_first_name" = F;
  1097. // "customer_last_name" = L;
  1098. // "customer_name" = ",da He Xiang Dong Liu A";
  1099. // "customer_phone" = "Hey Xuan Feng";
  1100. contactInfo[@"customer_phone"] = customer_phone;
  1101. contactInfo[@"customer_fax"] = customer_fax;
  1102. contactInfo[@"customer_email"] = customer_email;
  1103. NSString *first_name = @"";
  1104. NSString *last_name = @"";
  1105. if ([customer_contact isEqualToString:@""]) {
  1106. } else if ([customer_contact containsString:@" "]) {
  1107. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1108. first_name = [customer_contact substringToIndex:first_space_index];
  1109. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1110. }
  1111. contactInfo[@"customer_first_name"] = first_name;
  1112. contactInfo[@"customer_last_name"] = last_name;
  1113. ret[@"customerInfo"] = contactInfo;
  1114. // models
  1115. if (nssoid) {
  1116. __block double TotalCuft = 0;
  1117. __block double TotalWeight = 0;
  1118. __block int TotalCarton = 0;
  1119. __block double allItemPrice = 0;
  1120. 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];
  1121. sqlite3 *db1 = [iSalesDB get_db];
  1122. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1123. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1124. int product_id = sqlite3_column_int(stmt, 0);
  1125. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1126. int item_id = sqlite3_column_int(stmt, 7);
  1127. NSString* Price=nil;
  1128. if(str_price==nil)
  1129. {
  1130. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1131. if(price==nil)
  1132. Price=@"No Price.";
  1133. else
  1134. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1135. }
  1136. else
  1137. {
  1138. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1139. }
  1140. double discount = sqlite3_column_double(stmt, 2);
  1141. int item_count = sqlite3_column_int(stmt, 3);
  1142. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1143. NSString *nsline_note=nil;
  1144. if(line_note!=nil)
  1145. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1146. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1147. // NSString *nsname = nil;
  1148. // if(name!=nil)
  1149. // nsname= [[NSString alloc]initWithUTF8String:name];
  1150. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1151. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1152. // NSString *nsdescription=nil;
  1153. // if(description!=nil)
  1154. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1155. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1156. // int stockUom = sqlite3_column_int(stmt, 8);
  1157. // int _id = sqlite3_column_int(stmt, 9);
  1158. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1159. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1160. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1161. int carton=[bsubtotaljson[@"carton"] intValue];
  1162. TotalCuft += cuft;
  1163. TotalWeight += weight;
  1164. TotalCarton += carton;
  1165. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1166. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1167. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1168. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1169. itemjson[@"note"]=nsline_note;
  1170. itemjson[@"origin_price"] = Price;
  1171. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1172. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1173. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1174. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1175. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1176. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1177. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1178. if(itemjson[@"combine"] != nil)
  1179. {
  1180. // int citem=0;
  1181. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1182. for(int bc=0;bc<bcount;bc++)
  1183. {
  1184. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1185. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1186. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1187. subTotal += uprice * modulus * item_count;
  1188. }
  1189. }
  1190. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1191. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1192. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1193. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1194. allItemPrice += subTotal;
  1195. }];
  1196. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1197. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1198. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1199. // payments/Credits
  1200. // payments_and_credist = sqlite3_column_double(statement, 34);
  1201. payments_and_credist = allItemPrice;
  1202. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1203. // // total
  1204. // totalPrice = sqlite3_column_double(statement, 35);
  1205. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1206. } else {
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1208. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1209. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1210. // payments/Credits
  1211. payments_and_credist = 0;
  1212. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1213. }
  1214. // total
  1215. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1216. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1217. ret[@"order_info"]= orderinfo;
  1218. ret[@"more_order_info"] = moreInfo;
  1219. return [RAUtils dict2data:ret];
  1220. */
  1221. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1222. return data;
  1223. }
  1224. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1225. {
  1226. NSString* template_file=nil;
  1227. switch([params[@"thumb"] intValue])
  1228. {
  1229. case 0:
  1230. template_file= @"so.json";
  1231. break;
  1232. case 1:
  1233. template_file= @"so_thumb.json";
  1234. break;
  1235. default:
  1236. template_file= @"so.json";
  1237. }
  1238. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1239. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1240. if(data[@"table_model"][@"row2"]==nil)
  1241. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1242. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1243. DebugLog(@"%@",file);
  1244. // return nil;
  1245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1246. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1247. if (file) {
  1248. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1249. [dic setObject:file forKey:@"pdf_path"];
  1250. dic[@"isLocalFile"]=@"true";
  1251. return [RAUtils dict2data:dic];
  1252. }
  1253. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1254. dic[@"isLocalFile"]=@"true";
  1255. return [RAUtils dict2data:dic];
  1256. }
  1257. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1258. {
  1259. NSString* template_file=nil;
  1260. switch([params[@"pdf_style"] intValue])
  1261. {
  1262. case 0:
  1263. template_file= @"portfolio_2x3.json";
  1264. break;
  1265. case 1:
  1266. template_file= @"portfolio_3x2.json";
  1267. break;
  1268. default:
  1269. template_file= @"portfolio_3x2.json";
  1270. }
  1271. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1272. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1273. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1274. DebugLog(@"%@",file);
  1275. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1276. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1277. if (file) {
  1278. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1279. [dic setObject:file forKey:@"pdf_path"];
  1280. dic[@"isLocalFile"]=@"true";
  1281. return [RAUtils dict2data:dic];
  1282. }
  1283. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1284. dic[@"isLocalFile"]=@"true";
  1285. return [RAUtils dict2data:dic];
  1286. }
  1287. +(NSString*) get_offline_soid:(sqlite3*)db
  1288. {
  1289. NSString* soid=nil;
  1290. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1291. [formatter setDateFormat:@"yyMMdd"];
  1292. NSString* date = [formatter stringFromDate:[NSDate date]];
  1293. for(int i=1;i<999;i++)
  1294. {
  1295. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1296. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1297. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1298. if(count==0)
  1299. return soid;
  1300. }
  1301. return nil;
  1302. }
  1303. +(NSArray*) enumOfflineOrder
  1304. {
  1305. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1306. sqlite3 *db = [iSalesDB get_db];
  1307. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1308. sqlite3_stmt * statement;
  1309. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1310. if ( dbresult== SQLITE_OK)
  1311. {
  1312. while (sqlite3_step(statement) == SQLITE_ROW)
  1313. {
  1314. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1315. NSString* nsso_id=nil;
  1316. if(so_id!=nil)
  1317. {
  1318. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1319. }
  1320. [ret addObject:nsso_id];
  1321. }
  1322. sqlite3_finalize(statement);
  1323. }
  1324. [iSalesDB close_db:db];
  1325. return ret;
  1326. }
  1327. //+(void) uploadFile:(NSString*) file
  1328. //{
  1329. // NSData* data = [NSData dataWithContentsOfFile: file];
  1330. // UIApplication * app = [UIApplication sharedApplication];
  1331. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1332. //
  1333. //
  1334. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1335. //
  1336. //
  1337. //
  1338. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1339. //
  1340. // if(appDelegate.user!=nil)
  1341. // [params setValue:appDelegate.user forKey:@"user"];
  1342. // // if(appDelegate.contact_id!=nil)
  1343. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1344. // if(appDelegate.password!=nil)
  1345. // [params setValue:appDelegate.password forKey:@"password"];
  1346. //
  1347. //
  1348. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1349. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1350. // } error:nil];
  1351. //
  1352. //
  1353. //
  1354. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1355. //
  1356. // NSProgress *progress = nil;
  1357. //
  1358. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1359. //
  1360. //
  1361. //
  1362. // if (error) {
  1363. //
  1364. // NSString* err_msg = [error localizedDescription];
  1365. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1366. //
  1367. //
  1368. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1369. //
  1370. // DebugLog(@"data string: %@",str);
  1371. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1372. //
  1373. // } else {
  1374. // DebugLog(@"response ");
  1375. //
  1376. //
  1377. //
  1378. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1379. //
  1380. // // 再将NSData转为字符串
  1381. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1382. //
  1383. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1384. //
  1385. // DebugLog(@"data string: %@",jsonStr);
  1386. //
  1387. // NSDictionary* json = responseObject;
  1388. //
  1389. //
  1390. // if([[json valueForKey:@"result"] intValue]==2)
  1391. // {
  1392. //// NSString* img_url_down = json[@"img_url_aname"];
  1393. //// NSString* img_url_up = json[@"img_url"];
  1394. //
  1395. //
  1396. // }
  1397. // else
  1398. // {
  1399. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1400. // }
  1401. //
  1402. //
  1403. // }
  1404. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1405. // }];
  1406. //
  1407. //
  1408. //
  1409. //
  1410. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1411. // //
  1412. //
  1413. //
  1414. //
  1415. //
  1416. //
  1417. //
  1418. // [uploadTask resume];
  1419. //
  1420. //}
  1421. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1422. {
  1423. NSString* serial= [[NSUUID UUID] UUIDString];
  1424. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1425. NSString *cachefolder = [paths objectAtIndex:0];
  1426. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1427. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1428. NSFileManager* fileManager = [NSFileManager defaultManager];
  1429. BOOL bdir=YES;
  1430. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1431. {
  1432. NSError *error = nil;
  1433. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1434. if(!bsuccess)
  1435. DebugLog(@"Create UPLOAD folder failed");
  1436. }
  1437. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1438. ret[@"contact"]=[self prepareContact:serial];
  1439. ret[@"wishlist"]=[self prepareWishlist:serial];
  1440. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1441. ret[@"portfolio"] = [self preparePortfolio:serial];
  1442. ret[@"view_portfolio"] = [self preparePDF:serial];
  1443. NSString* str= [RAUtils dict2string:ret];
  1444. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1445. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1446. NSError *error=nil;
  1447. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1448. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1449. ZipArchive* zip = [[ZipArchive alloc] init];
  1450. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1451. for(int i=0;i<arr_files.count;i++)
  1452. {
  1453. NSString* file=arr_files[i];
  1454. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1455. }
  1456. if( ![zip CloseZipFile2] )
  1457. {
  1458. zippath = @"";
  1459. }
  1460. ret[@"file"]=zippath;
  1461. return ret;
  1462. }
  1463. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1464. {
  1465. if(filename.length==0)
  1466. return false;
  1467. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1468. bool ret=false;
  1469. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1470. NSString *cachefolder = [paths objectAtIndex:0];
  1471. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1472. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1473. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1474. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1475. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1476. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1477. NSFileManager* fileManager = [NSFileManager defaultManager];
  1478. BOOL bdir=NO;
  1479. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1480. {
  1481. NSError *error = nil;
  1482. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1483. {
  1484. ret=false;
  1485. }
  1486. else
  1487. {
  1488. ret=true;
  1489. }
  1490. }
  1491. return ret;
  1492. }
  1493. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1494. {
  1495. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1496. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1497. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1498. NSString* where=@" 1=1";
  1499. if (ver!=nil) {
  1500. where=@"is_dirty=1";
  1501. }
  1502. sqlite3 *db = [iSalesDB get_db];
  1503. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1504. sqlite3_stmt * statement;
  1505. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1506. int count=0;
  1507. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1508. if ( dbresult== SQLITE_OK)
  1509. {
  1510. while (sqlite3_step(statement) == SQLITE_ROW)
  1511. {
  1512. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1513. int _id = sqlite3_column_int(statement, 0);
  1514. int product_id = sqlite3_column_int(statement, 1);
  1515. int item_id = sqlite3_column_int(statement, 2);
  1516. int qty = sqlite3_column_int(statement, 3);
  1517. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1518. NSString* nscreate_time=nil;
  1519. if(create_time!=nil)
  1520. {
  1521. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1522. }
  1523. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1524. NSString* nsmodify_time=nil;
  1525. if(modify_time!=nil)
  1526. {
  1527. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1528. }
  1529. int is_delete = sqlite3_column_int(statement, 6);
  1530. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1531. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1532. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1533. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1534. itemjson[@"create_time"]=nscreate_time;
  1535. itemjson[@"modify_time"]=nsmodify_time;
  1536. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1537. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1538. count++;
  1539. }
  1540. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1541. sqlite3_finalize(statement);
  1542. }
  1543. ret[@"count"]=[NSNumber numberWithInt:count ];
  1544. [iSalesDB close_db:db];
  1545. return ret;
  1546. }
  1547. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1548. {
  1549. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1550. UIApplication * app = [UIApplication sharedApplication];
  1551. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1552. 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,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1553. // 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 ];
  1554. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1555. DebugLog(@"offline_login sql:%@",sqlQuery);
  1556. sqlite3_stmt * statement;
  1557. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1558. int count=0;
  1559. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1560. if ( dbresult== SQLITE_OK)
  1561. {
  1562. while (sqlite3_step(statement) == SQLITE_ROW)
  1563. {
  1564. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1565. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1566. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1567. int product_id = sqlite3_column_int(statement, 0);
  1568. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1569. int item_id = sqlite3_column_int(statement, 7);
  1570. NSString* Price=nil;
  1571. if(str_price==nil)
  1572. {
  1573. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1574. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1575. if(price==nil)
  1576. Price=@"No Price.";
  1577. else
  1578. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1579. }
  1580. else
  1581. {
  1582. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1583. }
  1584. double discount = sqlite3_column_double(statement, 2);
  1585. int item_count = sqlite3_column_int(statement, 3);
  1586. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1587. NSString *nsline_note=nil;
  1588. if(line_note!=nil)
  1589. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1590. char *name = (char*)sqlite3_column_text(statement, 5);
  1591. NSString *nsname=nil;
  1592. if(name!=nil)
  1593. nsname= [[NSString alloc]initWithUTF8String:name];
  1594. char *description = (char*)sqlite3_column_text(statement, 6);
  1595. NSString *nsdescription=nil;
  1596. if(description!=nil)
  1597. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1598. // int stockUom = sqlite3_column_int(statement, 8);
  1599. // int _id = sqlite3_column_int(statement, 9);
  1600. //
  1601. //
  1602. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1603. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1604. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1605. // int carton=[bsubtotaljson[@"carton"] intValue];
  1606. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1607. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1608. itemjson[@"model"]=nsname;
  1609. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1610. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1611. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1612. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1613. // itemjson[@"check"]=@"true";
  1614. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1615. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1616. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1617. itemjson[@"unit_price"]=Price;
  1618. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1619. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1620. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1621. itemjson[@"note"]=nsline_note;
  1622. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1623. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1624. count++;
  1625. }
  1626. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1627. sqlite3_finalize(statement);
  1628. }
  1629. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1630. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1631. return ret;
  1632. }
  1633. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1634. {
  1635. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1636. sqlite3 *db = [iSalesDB get_db];
  1637. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1638. // for(int i=0;i<arr_soid.count;i++)
  1639. sqlite3_stmt * statement;
  1640. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1641. //int count=0;
  1642. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1643. if ( dbresult== SQLITE_OK)
  1644. {
  1645. int count=0;
  1646. while (sqlite3_step(statement) == SQLITE_ROW)
  1647. {
  1648. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1649. int _id = sqlite3_column_int(statement, 0);
  1650. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1651. NSString* nssync_data=nil;
  1652. if(sync_data!=nil)
  1653. {
  1654. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1655. }
  1656. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1657. NSString* nsimg_1=nil;
  1658. if(img_1!=nil)
  1659. {
  1660. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1661. [self copy_upImg:serial file:nsimg_1];
  1662. }
  1663. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1664. NSString* nsso_no=nil;
  1665. if(so_no!=nil)
  1666. {
  1667. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1668. }
  1669. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1670. itemjson[@"order_type"]=@"submit order";
  1671. else
  1672. itemjson[@"order_type"]=@"archive order";
  1673. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1674. itemjson[@"json_data"]= nssync_data;
  1675. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1676. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1677. count++;
  1678. }
  1679. ret[@"count"]=[NSNumber numberWithInt:count ];
  1680. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1681. }
  1682. sqlite3_finalize(statement);
  1683. [iSalesDB close_db:db];
  1684. return ret;
  1685. }
  1686. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1687. {
  1688. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1689. sqlite3 *db = [iSalesDB get_db];
  1690. // UIApplication * app = [UIApplication sharedApplication];
  1691. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1692. 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";
  1693. sqlite3_stmt * statement;
  1694. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1695. int count=0;
  1696. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1697. if ( dbresult== SQLITE_OK)
  1698. {
  1699. while (sqlite3_step(statement) == SQLITE_ROW)
  1700. {
  1701. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1702. int _id = sqlite3_column_int(statement, 0);
  1703. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1704. NSString* nsimg_0=nil;
  1705. if(img_0!=nil)
  1706. {
  1707. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1708. [self copy_upImg:serial file:nsimg_0];
  1709. }
  1710. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1711. NSString* nsimg_1=nil;
  1712. if(img_1!=nil)
  1713. {
  1714. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1715. [self copy_upImg:serial file:nsimg_1];
  1716. }
  1717. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1718. NSString* nsimg_2=nil;
  1719. if(img_2!=nil)
  1720. {
  1721. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1722. [self copy_upImg:serial file:nsimg_2];
  1723. }
  1724. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1725. NSString* nssync_data=nil;
  1726. if(sync_data!=nil)
  1727. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1728. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1729. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1730. itemjson[@"json_data"]= nssync_data;
  1731. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1732. count++;
  1733. }
  1734. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1735. sqlite3_finalize(statement);
  1736. }
  1737. ret[@"count"]=[NSNumber numberWithInt:count ];
  1738. [iSalesDB close_db:db];
  1739. return ret;
  1740. }
  1741. +(bool) check_offlinedata
  1742. {
  1743. UIApplication * app = [UIApplication sharedApplication];
  1744. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1745. sqlite3 *db = [iSalesDB get_db];
  1746. NSString * where=@"1=1";
  1747. // if(appDelegate.user!=nil)
  1748. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1749. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1750. [iSalesDB close_db:db];
  1751. if(count==0)
  1752. {
  1753. return false;
  1754. }
  1755. return true;
  1756. //
  1757. //[iSalesDB close_db:db];
  1758. }
  1759. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1760. {
  1761. UIApplication * app = [UIApplication sharedApplication];
  1762. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1763. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1764. sqlite3 *db = [iSalesDB get_db];
  1765. NSString* collectId=params[@"collectId"];
  1766. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1767. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1768. [iSalesDB execSql:sqlQuery db:db];
  1769. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1770. [iSalesDB close_db:db];
  1771. appDelegate.wish_count =count;
  1772. [appDelegate update_count_mark];
  1773. ret[@"result"]= [NSNumber numberWithInt:2];
  1774. return ret;
  1775. }
  1776. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1777. //{
  1778. //
  1779. // UIApplication * app = [UIApplication sharedApplication];
  1780. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1781. //
  1782. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1783. // sqlite3 *db = [iSalesDB get_db];
  1784. // NSString* product_id=params[@"product_id"];
  1785. //
  1786. //
  1787. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1788. //
  1789. //
  1790. // for(int i=0;i<arr.count;i++)
  1791. // {
  1792. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1793. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1794. // if(count==0)
  1795. // {
  1796. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1797. // [iSalesDB execSql:sqlQuery db:db];
  1798. // }
  1799. // }
  1800. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1801. // [iSalesDB close_db:db];
  1802. //
  1803. // appDelegate.wish_count =count;
  1804. //
  1805. // [appDelegate update_count_mark];
  1806. // ret[@"result"]= [NSNumber numberWithInt:2];
  1807. // return ret;
  1808. // //
  1809. // //return ret;
  1810. //}
  1811. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1812. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1813. DebugLog(@"time interval: %lf",interval);
  1814. }
  1815. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1816. {
  1817. UIApplication * app = [UIApplication sharedApplication];
  1818. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1819. int sort = [[params objectForKey:@"sort"] intValue];
  1820. NSString *sort_str = @"";
  1821. switch (sort) {
  1822. case 0:{
  1823. sort_str = @"order by w.modify_time desc";
  1824. }
  1825. break;
  1826. case 1:{
  1827. sort_str = @"order by w.modify_time asc";
  1828. }
  1829. break;
  1830. case 2:{
  1831. sort_str = @"order by m.name asc";
  1832. }
  1833. break;
  1834. case 3:{
  1835. sort_str = @"order by m.name desc";
  1836. }
  1837. break;
  1838. case 4:{
  1839. sort_str = @"order by m.description asc";
  1840. }
  1841. break;
  1842. default:
  1843. break;
  1844. }
  1845. NSString* user = appDelegate.user;
  1846. sqlite3 *db = [iSalesDB get_db];
  1847. // order by w.create_time
  1848. 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];
  1849. // 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];
  1850. sqlite3_stmt * statement;
  1851. NSDate *date1 = [NSDate date];
  1852. NSDate *date2 = nil;
  1853. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1854. int count=0;
  1855. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1856. {
  1857. while (sqlite3_step(statement) == SQLITE_ROW)
  1858. {
  1859. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1860. int product_id = sqlite3_column_double(statement, 0);
  1861. char *description = (char*)sqlite3_column_text(statement, 1);
  1862. if(description==nil)
  1863. description= "";
  1864. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1865. int item_id = sqlite3_column_double(statement, 2);
  1866. NSDate *date_image = [NSDate date];
  1867. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1868. printf("image : ");
  1869. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1870. // char *url = (char*)sqlite3_column_text(statement, 3);
  1871. // if(url==nil)
  1872. // url="";
  1873. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1874. int qty = sqlite3_column_int(statement, 3);
  1875. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1876. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1877. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1878. item[@"description"]= nsdescription;
  1879. item[@"img"]= nsurl;
  1880. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1881. count++;
  1882. }
  1883. printf("total time:");
  1884. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1885. ret[@"count"]= [NSNumber numberWithInt:count];
  1886. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1887. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1888. ret[@"result"]= [NSNumber numberWithInt:2];
  1889. appDelegate.wish_count =count;
  1890. [appDelegate update_count_mark];
  1891. sqlite3_finalize(statement);
  1892. }
  1893. [iSalesDB close_db:db];
  1894. return ret;
  1895. }
  1896. +(NSDictionary*) offline_notimpl
  1897. {
  1898. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1899. ret[@"result"]=@"8";
  1900. ret[@"err_msg"]=@"offline mode does not support this function.";
  1901. return ret;
  1902. }
  1903. +(NSDictionary*) offline_home
  1904. {
  1905. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1906. NSString *cachefolder = [paths objectAtIndex:0];
  1907. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1908. NSData* json =nil;
  1909. json=[NSData dataWithContentsOfFile:img_cache];
  1910. if(json==nil)
  1911. return nil;
  1912. NSError *error=nil;
  1913. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1914. return menu;
  1915. }
  1916. +(NSDictionary*) offline_category_menu
  1917. {
  1918. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1919. NSString *cachefolder = [paths objectAtIndex:0];
  1920. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1921. NSData* json =nil;
  1922. json=[NSData dataWithContentsOfFile:img_cache];
  1923. if(json==nil)
  1924. return nil;
  1925. NSError *error=nil;
  1926. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1927. return menu;
  1928. }
  1929. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1930. {
  1931. NSString* offline_command=params[@"offline_Command"];
  1932. NSDictionary* ret=nil;
  1933. if([offline_command isEqualToString:@"model_NIYMAL"])
  1934. {
  1935. NSString* category = params[@"category"];
  1936. ret = [self refresh_model_NIYMAL:category];
  1937. }
  1938. return ret;
  1939. }
  1940. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1941. {
  1942. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1943. [ret setValue:@"2" forKey:@"result"];
  1944. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1945. sqlite3* db= [iSalesDB get_db];
  1946. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1947. [iSalesDB close_db:db];
  1948. [ret setObject:detail1_section forKey:@"detail_1"];
  1949. return ret;
  1950. }
  1951. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1952. {
  1953. UIApplication * app = [UIApplication sharedApplication];
  1954. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1955. // NSArray* arr1 = [self get_user_all_price_type];
  1956. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1957. // NSSet *set1 = [NSSet setWithArray:arr1];
  1958. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1959. // if(appDelegate.contact_id==nil)
  1960. // set2=[set1 mutableCopy];
  1961. // else
  1962. // [set2 intersectsSet:set1];
  1963. // NSArray *retarr = [set2 allObjects];
  1964. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1965. // sqlite3 *db = [iSalesDB get_db];
  1966. NSString* sqlQuery = nil;
  1967. if(appDelegate.contact_id==nil)
  1968. {
  1969. if(!appDelegate.bLogin)
  1970. return nil;
  1971. 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];
  1972. }
  1973. else
  1974. 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];
  1975. sqlite3_stmt * statement;
  1976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1977. int count=0;
  1978. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1979. {
  1980. while (sqlite3_step(statement) == SQLITE_ROW)
  1981. {
  1982. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1983. char *name = (char*)sqlite3_column_text(statement, 0);
  1984. if(name==nil)
  1985. name="";
  1986. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1987. // double price = sqlite3_column_double(statement, 1);
  1988. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1989. // if(isnull==nil)
  1990. // item[nsname]= @"No Price";
  1991. // else
  1992. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1993. char *price = (char*)sqlite3_column_text(statement, 1);
  1994. if(price!=nil)
  1995. {
  1996. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1997. nsprice=[AESCrypt fastdecrypt:nsprice];
  1998. if(nsprice.length>0)
  1999. {
  2000. double dp= [nsprice doubleValue];
  2001. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2002. }
  2003. }
  2004. else
  2005. {
  2006. item[nsname]= @"No Price";
  2007. }
  2008. // int type= sqlite3_column_int(statement, 2);
  2009. //item[@"type"]=@"price";
  2010. // item[nsname]= nsprice;
  2011. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2012. count++;
  2013. }
  2014. ret[@"count"]= [NSNumber numberWithInt:count];
  2015. sqlite3_finalize(statement);
  2016. }
  2017. // [iSalesDB close_db:db];
  2018. return ret;
  2019. }
  2020. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2021. //{
  2022. // NSArray* arr1 = [self get_user_all_price_type:db];
  2023. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2024. //
  2025. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2026. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2027. // // [set2 intersectsSet:set1];
  2028. // //
  2029. // //
  2030. // // NSArray *retarr = [set2 allObjects];
  2031. //
  2032. // NSString* whereprice=nil;
  2033. // if(contact_id==nil)
  2034. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2035. // else
  2036. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2037. //
  2038. //
  2039. // // sqlite3 *db = [iSalesDB get_db];
  2040. //
  2041. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2042. // sqlite3_stmt * statement;
  2043. //
  2044. //
  2045. // NSNumber* ret = nil;
  2046. // double dprice=DBL_MAX;
  2047. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2048. // {
  2049. //
  2050. //
  2051. // while (sqlite3_step(statement) == SQLITE_ROW)
  2052. // {
  2053. //
  2054. // // double val = sqlite3_column_double(statement, 0);
  2055. // char *price = (char*)sqlite3_column_text(statement, 0);
  2056. // if(price!=nil)
  2057. // {
  2058. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2059. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2060. // if(nsprice.length>0)
  2061. // {
  2062. // double dp= [nsprice doubleValue];
  2063. // if(dp<dprice)
  2064. // dprice=dp;
  2065. // }
  2066. // }
  2067. // }
  2068. //
  2069. //
  2070. //
  2071. //
  2072. // sqlite3_finalize(statement);
  2073. //
  2074. //
  2075. //
  2076. //
  2077. // }
  2078. //
  2079. // // [iSalesDB close_db:db];
  2080. //
  2081. // if(dprice==DBL_MAX)
  2082. // ret= nil;
  2083. // else
  2084. // ret= [NSNumber numberWithDouble:dprice];
  2085. // return ret;
  2086. //}
  2087. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2088. {
  2089. NSArray* arr1 = [self get_user_all_price_type:db];
  2090. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2091. NSString* whereprice=nil;
  2092. if(contact_id==nil)
  2093. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2094. else
  2095. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2096. // sqlite3 *db = [iSalesDB get_db];
  2097. NSString *productIdCondition = @"1 = 1";
  2098. if (product_id) {
  2099. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2100. }
  2101. NSString *itemIdCondition = @"";
  2102. if (item_id) {
  2103. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2104. }
  2105. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2106. sqlite3_stmt * statement;
  2107. NSNumber* ret = nil;
  2108. double dprice=DBL_MAX;
  2109. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2110. {
  2111. while (sqlite3_step(statement) == SQLITE_ROW)
  2112. {
  2113. // double val = sqlite3_column_double(statement, 0);
  2114. char *price = (char*)sqlite3_column_text(statement, 0);
  2115. if(price!=nil)
  2116. {
  2117. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2118. nsprice=[AESCrypt fastdecrypt:nsprice];
  2119. if(nsprice.length>0)
  2120. {
  2121. double dp= [nsprice doubleValue];
  2122. if(dp<dprice)
  2123. dprice=dp;
  2124. }
  2125. }
  2126. }
  2127. sqlite3_finalize(statement);
  2128. }
  2129. // [iSalesDB close_db:db];
  2130. if(dprice==DBL_MAX)
  2131. ret= nil;
  2132. else
  2133. ret= [NSNumber numberWithDouble:dprice];
  2134. return ret;
  2135. }
  2136. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2137. {
  2138. NSArray* ret=nil;
  2139. // sqlite3 *db = [iSalesDB get_db];
  2140. // no customer assigned , use login user contact_id
  2141. UIApplication * app = [UIApplication sharedApplication];
  2142. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2143. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2144. sqlite3_stmt * statement;
  2145. // int count=0;
  2146. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2147. {
  2148. if (sqlite3_step(statement) == SQLITE_ROW)
  2149. {
  2150. char *val = (char*)sqlite3_column_text(statement, 0);
  2151. if(val==nil)
  2152. val="";
  2153. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2154. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2155. }
  2156. sqlite3_finalize(statement);
  2157. }
  2158. // [iSalesDB close_db:db];
  2159. return ret;
  2160. }
  2161. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2162. {
  2163. // sqlite3 *db = [iSalesDB get_db];
  2164. if(contact_id==nil)
  2165. {
  2166. // no customer assigned , use login user contact_id
  2167. UIApplication * app = [UIApplication sharedApplication];
  2168. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2169. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2170. sqlite3_stmt * statement;
  2171. // int count=0;
  2172. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2173. {
  2174. if (sqlite3_step(statement) == SQLITE_ROW)
  2175. {
  2176. char *val = (char*)sqlite3_column_text(statement, 0);
  2177. if(val==nil)
  2178. val="";
  2179. contact_id = [[NSString alloc]initWithUTF8String:val];
  2180. }
  2181. sqlite3_finalize(statement);
  2182. }
  2183. if(contact_id.length<=0)
  2184. {
  2185. // [iSalesDB close_db:db];
  2186. return nil;
  2187. }
  2188. }
  2189. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2190. sqlite3_stmt * statement;
  2191. NSArray* ret=nil;
  2192. // int count=0;
  2193. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2194. {
  2195. if (sqlite3_step(statement) == SQLITE_ROW)
  2196. {
  2197. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2198. if(price_type==nil)
  2199. price_type="";
  2200. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2201. if(nsprice_type.length>0)
  2202. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2203. else
  2204. ret=nil;
  2205. }
  2206. sqlite3_finalize(statement);
  2207. }
  2208. // [iSalesDB close_db:db];
  2209. return ret;
  2210. }
  2211. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2212. {
  2213. NSString* ret= nil;
  2214. // sqlite3 *db = [iSalesDB get_db];
  2215. NSString *sqlQuery = nil;
  2216. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2217. // 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;
  2218. // 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
  2219. if(product_id==nil && model_name)
  2220. 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;
  2221. else if (product_id)
  2222. 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
  2223. sqlite3_stmt * statement;
  2224. // int count=0;
  2225. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2226. {
  2227. if (sqlite3_step(statement) == SQLITE_ROW)
  2228. {
  2229. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2230. if(imgurl==nil)
  2231. imgurl="";
  2232. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2233. ret=nsimgurl;
  2234. }
  2235. sqlite3_finalize(statement);
  2236. }
  2237. else
  2238. {
  2239. [ret setValue:@"8" forKey:@"result"];
  2240. }
  2241. // [iSalesDB close_db:db];
  2242. DebugLog(@"data string: %@",ret );
  2243. return ret;
  2244. }
  2245. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2246. {
  2247. NSString* ret= nil;
  2248. sqlite3 *db = [iSalesDB get_db];
  2249. NSString *sqlQuery = nil;
  2250. if(product_id==nil)
  2251. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2252. else
  2253. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2254. sqlite3_stmt * statement;
  2255. // int count=0;
  2256. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2257. {
  2258. if (sqlite3_step(statement) == SQLITE_ROW)
  2259. {
  2260. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2261. if(default_category==nil)
  2262. default_category="";
  2263. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2264. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2265. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2266. ret=nsdefault_category;
  2267. }
  2268. sqlite3_finalize(statement);
  2269. }
  2270. else
  2271. {
  2272. [ret setValue:@"8" forKey:@"result"];
  2273. }
  2274. [iSalesDB close_db:db];
  2275. DebugLog(@"data string: %@",ret );
  2276. return ret;
  2277. }
  2278. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2279. {
  2280. NSString* model_name = [params valueForKey:@"product_name"];
  2281. NSString* product_id = [params valueForKey:@"product_id"];
  2282. NSString* category = [params valueForKey:@"category"];
  2283. if(category==nil)
  2284. category = [self model_default_category:product_id model_name:model_name];
  2285. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2286. sqlite3 *db = [iSalesDB get_db];
  2287. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2288. NSString *sqlQuery = nil;
  2289. if(product_id==nil)
  2290. 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='%@';
  2291. else
  2292. 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=%@;
  2293. sqlite3_stmt * statement;
  2294. [ret setValue:@"2" forKey:@"result"];
  2295. [ret setValue:@"3" forKey:@"detail_section_count"];
  2296. // int count=0;
  2297. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2298. {
  2299. if (sqlite3_step(statement) == SQLITE_ROW)
  2300. {
  2301. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2302. char *name = (char*)sqlite3_column_text(statement, 0);
  2303. if(name==nil)
  2304. name="";
  2305. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2306. char *description = (char*)sqlite3_column_text(statement, 1);
  2307. if(description==nil)
  2308. description="";
  2309. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2310. int product_id = sqlite3_column_int(statement, 2);
  2311. char *color = (char*)sqlite3_column_text(statement, 3);
  2312. if(color==nil)
  2313. color="";
  2314. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2315. //
  2316. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2317. // if(legcolor==nil)
  2318. // legcolor="";
  2319. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2320. //
  2321. //
  2322. int availability = sqlite3_column_int(statement, 5);
  2323. //
  2324. int incoming_stock = sqlite3_column_int(statement, 6);
  2325. char *demension = (char*)sqlite3_column_text(statement, 7);
  2326. if(demension==nil)
  2327. demension="";
  2328. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2329. // ,,,,,,,,,
  2330. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2331. if(seat_height==nil)
  2332. seat_height="";
  2333. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2334. char *material = (char*)sqlite3_column_text(statement, 9);
  2335. if(material==nil)
  2336. material="";
  2337. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2338. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2339. if(box_dim==nil)
  2340. box_dim="";
  2341. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2342. char *volume = (char*)sqlite3_column_text(statement, 11);
  2343. if(volume==nil)
  2344. volume="";
  2345. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2346. double weight = sqlite3_column_double(statement, 12);
  2347. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2348. if(model_set==nil)
  2349. model_set="";
  2350. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2351. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2352. if(load_ability==nil)
  2353. load_ability="";
  2354. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2355. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2356. if(default_category==nil)
  2357. default_category="";
  2358. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2359. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2360. if(fabric_content==nil)
  2361. fabric_content="";
  2362. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2363. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2364. if(assembling==nil)
  2365. assembling="";
  2366. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2367. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2368. if(made_in==nil)
  2369. made_in="";
  2370. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2371. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2372. if(special_remarks==nil)
  2373. special_remarks="";
  2374. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2375. int stockUcom = sqlite3_column_double(statement, 20);
  2376. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2377. if(product_group==nil)
  2378. product_group="";
  2379. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2380. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2381. // if(fashion_selector==nil)
  2382. // fashion_selector="";
  2383. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2384. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2385. if(selector_field==nil)
  2386. selector_field="";
  2387. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2388. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2389. if(property_field==nil)
  2390. property_field="";
  2391. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2392. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2393. if(packaging==nil)
  2394. packaging="";
  2395. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2396. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2397. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2398. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2399. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2400. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2401. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2402. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2403. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2404. NSString* Availability=nil;
  2405. if(availability>0)
  2406. Availability=[NSString stringWithFormat:@"%d",availability];
  2407. else
  2408. Availability = @"In Production";
  2409. [img_section setValue:Availability forKey:@"Availability"];
  2410. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2411. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2412. char *eta = (char*)sqlite3_column_text(statement, 25);
  2413. if(eta==nil)
  2414. eta="";
  2415. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2416. if ([nseta isEqualToString:@"null"]) {
  2417. nseta = @"";
  2418. }
  2419. if (availability <= 0) {
  2420. [img_section setValue:nseta forKey:@"ETA"];
  2421. }
  2422. int item_id = sqlite3_column_int(statement, 26);
  2423. NSString* Price=nil;
  2424. if(appDelegate.bLogin==false)
  2425. Price=@"Must Sign in.";
  2426. else
  2427. {
  2428. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2429. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2430. if(price==nil)
  2431. Price=@"No Price.";
  2432. else
  2433. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2434. }
  2435. [img_section setValue:Price forKey:@"price"];
  2436. [img_section setValue:nsname forKey:@"model_name"];
  2437. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2438. [ret setObject:img_section forKey:@"img_section"];
  2439. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2440. int detail0_item_count=0;
  2441. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2442. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2443. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2444. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2445. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2446. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2447. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2448. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2449. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2450. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2451. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2452. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2453. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2454. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2455. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2456. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2457. {
  2458. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2459. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2460. }
  2461. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2462. [detail0_section setValue:@"kv" forKey:@"type"];
  2463. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2464. [ret setObject:detail0_section forKey:@"detail_0"];
  2465. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2466. // [detail1_section setValue:@"detail" forKey:@"target"];
  2467. // [detail1_section setValue:@"popup" forKey:@"action"];
  2468. // [detail1_section setValue:@"content" forKey:@"type"];
  2469. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2470. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2471. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2472. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2473. [ret setObject:detail1_section forKey:@"detail_1"];
  2474. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2475. [detail2_section setValue:@"detail" forKey:@"target"];
  2476. [detail2_section setValue:@"popup" forKey:@"action"];
  2477. [detail2_section setValue:@"content" forKey:@"type"];
  2478. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2479. [detail2_section setValue:@"true" forKey:@"single_row"];
  2480. [detail2_section setValue:@"local" forKey:@"data"];
  2481. [ret setObject:detail2_section forKey:@"detail_2"];
  2482. }
  2483. sqlite3_finalize(statement);
  2484. }
  2485. else
  2486. {
  2487. [ret setValue:@"8" forKey:@"result"];
  2488. }
  2489. DebugLog(@"count:%d",count);
  2490. [iSalesDB close_db:db];
  2491. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2492. return ret;
  2493. }
  2494. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2495. NSString* orderCode = [params valueForKey:@"orderCode"];
  2496. NSString* keyword = [params valueForKey:@"keyword"];
  2497. keyword=keyword.lowercaseString;
  2498. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  2499. int limit = [[params valueForKey:@"limit"] intValue];
  2500. int offset = [[params valueForKey:@"offset"] intValue];
  2501. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2502. NSString *limit_str = @"";
  2503. if (limited) {
  2504. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2505. }
  2506. sqlite3 *db = [iSalesDB get_db];
  2507. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2508. UIApplication * app = [UIApplication sharedApplication];
  2509. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2510. NSString *sqlQuery = nil;
  2511. if(exactMatch )
  2512. 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 ;
  2513. else
  2514. 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
  2515. DebugLog(@"offline_search sql:%@",sqlQuery);
  2516. sqlite3_stmt * statement;
  2517. [ret setValue:@"2" forKey:@"result"];
  2518. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2519. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2520. // int count=0;
  2521. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2522. {
  2523. int i=0;
  2524. while (sqlite3_step(statement) == SQLITE_ROW)
  2525. {
  2526. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2527. // char *name = (char*)sqlite3_column_text(statement, 1);
  2528. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2529. char *name = (char*)sqlite3_column_text(statement, 0);
  2530. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2531. char *description = (char*)sqlite3_column_text(statement, 1);
  2532. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2533. int product_id = sqlite3_column_int(statement, 2);
  2534. // char *url = (char*)sqlite3_column_text(statement, 3);
  2535. // if(url==nil)
  2536. // url="";
  2537. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2538. int wid = sqlite3_column_int(statement, 3);
  2539. int closeout = sqlite3_column_int(statement, 4);
  2540. int cid = sqlite3_column_int(statement, 5);
  2541. int wisdelete = sqlite3_column_int(statement, 6);
  2542. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2543. if(wid !=0 && wisdelete != 1)
  2544. [item setValue:@"true" forKey:@"wish_exists"];
  2545. else
  2546. [item setValue:@"false" forKey:@"wish_exists"];
  2547. if(closeout==0)
  2548. [item setValue:@"false" forKey:@"is_closeout"];
  2549. else
  2550. [item setValue:@"true" forKey:@"is_closeout"];
  2551. if(cid==0)
  2552. [item setValue:@"false" forKey:@"cart_exists"];
  2553. else
  2554. [item setValue:@"true" forKey:@"cart_exists"];
  2555. [item addEntriesFromDictionary:imgjson];
  2556. // [item setValue:nsurl forKey:@"img"];
  2557. [item setValue:nsname forKey:@"fash_name"];
  2558. [item setValue:nsdescription forKey:@"description"];
  2559. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2560. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2561. i++;
  2562. }
  2563. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2564. [ret setObject:items forKey:@"items"];
  2565. sqlite3_finalize(statement);
  2566. }
  2567. DebugLog(@"count:%d",count);
  2568. [iSalesDB close_db:db];
  2569. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2570. return ret;
  2571. }
  2572. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2573. {
  2574. return [self search:params limited:YES];
  2575. }
  2576. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2577. {
  2578. UIApplication * app = [UIApplication sharedApplication];
  2579. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2580. appDelegate.disable_trigger=true;
  2581. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2582. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2583. appDelegate.disable_trigger=false;
  2584. //
  2585. // NSString* user = [params valueForKey:@"user"];
  2586. //
  2587. // NSString* password = [params valueForKey:@"password"];
  2588. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2589. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2590. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2591. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2592. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2593. [appDelegate update_count_mark];
  2594. appDelegate.can_show_price =false;
  2595. appDelegate.can_see_price =false;
  2596. appDelegate.can_create_portfolio =false;
  2597. appDelegate.can_create_order =false;
  2598. appDelegate.can_cancel_order =false;
  2599. appDelegate.can_set_cart_price =false;
  2600. appDelegate.can_delete_order =false;
  2601. appDelegate.can_submit_order =false;
  2602. appDelegate.can_set_tearsheet_price =false;
  2603. appDelegate.can_update_contact_info = false;
  2604. appDelegate.save_order_logout = false;
  2605. appDelegate.submit_order_logout = false;
  2606. appDelegate.alert_sold_in_quantities = false;
  2607. appDelegate.ipad_perm =nil ;
  2608. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2609. appDelegate.OrderFilter= nil;
  2610. [appDelegate SetSo:nil];
  2611. [appDelegate set_main_button_panel];
  2612. // sqlite3 *db = [iSalesDB get_db];
  2613. //
  2614. //
  2615. //
  2616. //
  2617. //
  2618. // 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"]];
  2619. //
  2620. //
  2621. //
  2622. //
  2623. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2624. // sqlite3_stmt * statement;
  2625. //
  2626. //
  2627. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2628. //
  2629. //
  2630. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2631. // {
  2632. //
  2633. //
  2634. // if (sqlite3_step(statement) == SQLITE_ROW)
  2635. // {
  2636. //
  2637. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2638. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2639. //
  2640. //
  2641. //
  2642. // int can_show_price = sqlite3_column_int(statement, 0);
  2643. // int can_see_price = sqlite3_column_int(statement, 1);
  2644. //
  2645. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2646. // if(contact_id==nil)
  2647. // contact_id="";
  2648. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2649. //
  2650. // int user_type = sqlite3_column_int(statement, 3);
  2651. //
  2652. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2653. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2654. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2655. // int can_delete_order = sqlite3_column_int(statement, 7);
  2656. // int can_submit_order = sqlite3_column_int(statement, 8);
  2657. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2658. // int can_create_order = sqlite3_column_int(statement, 10);
  2659. //
  2660. //
  2661. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2662. // if(mode==nil)
  2663. // mode="";
  2664. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2665. //
  2666. //
  2667. // char *username = (char*)sqlite3_column_text(statement, 12);
  2668. // if(username==nil)
  2669. // username="";
  2670. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2671. //
  2672. //
  2673. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2674. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2675. // [header setValue:nscontact_id forKey:@"contact_id"];
  2676. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2677. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2678. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2679. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2680. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2681. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2682. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2683. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2684. //
  2685. // [header setValue:nsusername forKey:@"username"];
  2686. //
  2687. //
  2688. // [ret setObject:header forKey:@"header"];
  2689. // [ret setValue:nsmode forKey:@"mode"];
  2690. //
  2691. //
  2692. // }
  2693. //
  2694. //
  2695. //
  2696. // sqlite3_finalize(statement);
  2697. // }
  2698. //
  2699. //
  2700. //
  2701. // [iSalesDB close_db:db];
  2702. //
  2703. //
  2704. //
  2705. //
  2706. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2707. return ret;
  2708. }
  2709. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2710. {
  2711. UIApplication * app = [UIApplication sharedApplication];
  2712. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2713. appDelegate.disable_trigger=true;
  2714. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2715. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2716. appDelegate.disable_trigger=false;
  2717. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2718. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2719. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2720. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2721. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2722. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2723. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2724. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2725. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2726. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2727. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2728. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2729. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2730. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2731. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2732. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2733. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2734. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2735. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2736. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2737. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2738. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2739. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2740. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2741. [arr_name addObject:customer_first_name];
  2742. [arr_name addObject:customer_last_name];
  2743. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2744. // default ship from
  2745. 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';";
  2746. __block NSString *cid = @"";
  2747. __block NSString *name = @"";
  2748. __block NSString *ext = @"";
  2749. __block NSString *contact = @"";
  2750. __block NSString *email = @"";
  2751. __block NSString *fax = @"";
  2752. __block NSString *phone = @"";
  2753. sqlite3 *db = [iSalesDB get_db];
  2754. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2755. cid = [self textAtColumn:0 statement:statment];
  2756. name = [self textAtColumn:1 statement:statment];
  2757. ext = [self textAtColumn:2 statement:statment];
  2758. contact = [self textAtColumn:3 statement:statment];
  2759. email = [self textAtColumn:4 statement:statment];
  2760. fax = [self textAtColumn:5 statement:statment];
  2761. phone = [self textAtColumn:6 statement:statment];
  2762. }];
  2763. NSString* so_id = [self get_offline_soid:db];
  2764. if(so_id==nil)
  2765. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2766. 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,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",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,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2767. int result =[iSalesDB execSql:sql_neworder db:db];
  2768. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2769. //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'
  2770. //soId
  2771. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2772. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2773. sqlite3_stmt * statement;
  2774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2775. {
  2776. if (sqlite3_step(statement) == SQLITE_ROW)
  2777. {
  2778. // char *name = (char*)sqlite3_column_text(statement, 1);
  2779. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2780. //ret = sqlite3_column_int(statement, 0);
  2781. char *soId = (char*)sqlite3_column_text(statement, 0);
  2782. if(soId==nil)
  2783. soId="";
  2784. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2785. [ret setValue:nssoId forKey:@"soId"];
  2786. [ret setValue:nssoId forKey:@"orderCode"];
  2787. }
  2788. sqlite3_finalize(statement);
  2789. }
  2790. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2791. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2792. [iSalesDB close_db:db];
  2793. return [RAUtils dict2data:ret];
  2794. }
  2795. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2796. {
  2797. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2798. NSString* orderCode = [params valueForKey:@"orderCode"];
  2799. UIApplication * app = [UIApplication sharedApplication];
  2800. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2801. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2802. {
  2803. appDelegate.disable_trigger=true;
  2804. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2805. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2806. appDelegate.disable_trigger=false;
  2807. }
  2808. sqlite3 *db = [iSalesDB get_db];
  2809. int cart_count=[self query_ordercartcount:orderCode db:db];
  2810. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2811. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2812. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2813. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2814. [iSalesDB close_db:db];
  2815. return [RAUtils dict2data:ret];
  2816. }
  2817. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2818. {
  2819. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2820. sqlite3 *db = [iSalesDB get_db];
  2821. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2822. int count =0;
  2823. if(params[@"count"]!=nil)
  2824. {
  2825. count = [params[@"count"] intValue];
  2826. }
  2827. NSString* product_id=params[@"product_id"];
  2828. NSString* orderCode=params[@"orderCode"];
  2829. NSString *qty = params[@"qty"];
  2830. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2831. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2832. for(int i=0;i<arr_id.count;i++)
  2833. {
  2834. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2835. int item_qty= count;
  2836. if (qty) {
  2837. item_qty = [qty_arr[i] integerValue];
  2838. }
  2839. if(item_qty==0)
  2840. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2841. NSString* sql=nil;
  2842. sqlite3_stmt *stmt;
  2843. if(_id<0)
  2844. {
  2845. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2846. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2847. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2848. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2849. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2850. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2851. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2852. }
  2853. else
  2854. {
  2855. if (qty) {
  2856. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2857. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2858. sqlite3_bind_int(stmt, 1, _id);
  2859. } else {
  2860. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2861. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2862. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2863. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2864. }
  2865. }
  2866. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2867. [iSalesDB execSql:@"ROLLBACK" db:db];
  2868. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2869. [iSalesDB close_db:db];
  2870. DebugLog(@"add to cart error");
  2871. return [RAUtils dict2data:ret];
  2872. }
  2873. }
  2874. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2875. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2876. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2877. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2878. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2879. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2880. [iSalesDB close_db:db];
  2881. return [RAUtils dict2data:ret];
  2882. }
  2883. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2884. {
  2885. UIApplication * app = [UIApplication sharedApplication];
  2886. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2887. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2888. 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];
  2889. sqlite3_stmt * statement;
  2890. int count=0;
  2891. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2892. {
  2893. while (sqlite3_step(statement) == SQLITE_ROW)
  2894. {
  2895. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2896. int bitem_id = sqlite3_column_int(statement, 0);
  2897. int bitem_qty = sqlite3_column_int(statement, 1);
  2898. char *name = (char*)sqlite3_column_text(statement, 2);
  2899. if(name==nil)
  2900. name="";
  2901. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2902. char *description = (char*)sqlite3_column_text(statement, 3);
  2903. if(description==nil)
  2904. description="";
  2905. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2906. double unit_price = sqlite3_column_double(statement, 4);
  2907. int use_unitprice = sqlite3_column_int(statement, 5);
  2908. if(use_unitprice!=1)
  2909. {
  2910. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2911. }
  2912. itemjson[@"model"]=nsname;
  2913. itemjson[@"description"]=nsdescription;
  2914. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2915. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2916. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2917. if(compute)
  2918. {
  2919. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2920. }
  2921. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2922. count++;
  2923. }
  2924. sqlite3_finalize(statement);
  2925. }
  2926. ret[@"count"]=@(count);
  2927. if(count==0)
  2928. return nil;
  2929. else
  2930. return ret;
  2931. }
  2932. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2933. {
  2934. //compute: add part to subtotal;
  2935. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2936. dict_item[@(item_id)]=@"1";
  2937. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2938. double cuft=0;
  2939. double weight=0;
  2940. int carton=0;
  2941. 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];
  2942. sqlite3_stmt * statement;
  2943. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2944. {
  2945. if (sqlite3_step(statement) == SQLITE_ROW)
  2946. {
  2947. double ulength = sqlite3_column_double(statement, 0);
  2948. double uwidth = sqlite3_column_double(statement, 1);
  2949. double uheight = sqlite3_column_double(statement, 2);
  2950. double uweight = sqlite3_column_double(statement, 3);
  2951. double mlength = sqlite3_column_double(statement, 4);
  2952. double mwidth = sqlite3_column_double(statement, 5);
  2953. double mheight = sqlite3_column_double(statement, 6);
  2954. double mweight = sqlite3_column_double(statement, 7);
  2955. double ilength = sqlite3_column_double(statement, 8);
  2956. double iwidth = sqlite3_column_double(statement, 9);
  2957. double iheight = sqlite3_column_double(statement, 10);
  2958. double iweight = sqlite3_column_double(statement, 11);
  2959. int pcs = sqlite3_column_int(statement,12);
  2960. int mpack = sqlite3_column_int(statement, 13);
  2961. int ipack = sqlite3_column_int(statement, 14);
  2962. double ucbf = sqlite3_column_double(statement, 15);
  2963. double icbf = sqlite3_column_double(statement, 16);
  2964. double mcbf = sqlite3_column_double(statement, 17);
  2965. if(ipack==0)
  2966. {
  2967. carton= count/mpack ;
  2968. weight = mweight*carton;
  2969. cuft= carton*(mlength*mwidth*mheight);
  2970. int remain=count%mpack;
  2971. if(remain==0)
  2972. {
  2973. //do nothing;
  2974. }
  2975. else
  2976. {
  2977. carton++;
  2978. weight += uweight*remain;
  2979. cuft += (ulength*uwidth*uheight)*remain;
  2980. }
  2981. }
  2982. else
  2983. {
  2984. carton = count/(mpack*ipack);
  2985. weight = mweight*carton;
  2986. cuft= carton*(mlength*mwidth*mheight);
  2987. int remain=count%(mpack*ipack);
  2988. if(remain==0)
  2989. {
  2990. // do nothing;
  2991. }
  2992. else
  2993. {
  2994. carton++;
  2995. int icarton =remain/ipack;
  2996. int iremain=remain%ipack;
  2997. weight += iweight*icarton;
  2998. cuft += (ilength*iwidth*iheight)*icarton;
  2999. if(iremain==0)
  3000. {
  3001. //do nothing;
  3002. }
  3003. else
  3004. {
  3005. weight += uweight*iremain;
  3006. cuft += (ulength*uwidth*uheight)*iremain;
  3007. }
  3008. }
  3009. }
  3010. #ifdef BUILD_NPD
  3011. cuft=ucbf*count;
  3012. weight= uweight*count;
  3013. #endif
  3014. }
  3015. sqlite3_finalize(statement);
  3016. }
  3017. if(compute)
  3018. {
  3019. NSArray * arr_count=nil;
  3020. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3021. for(int i=0;i<arr_bundle.count;i++)
  3022. {
  3023. dict_item[arr_bundle[i]]=@"1";
  3024. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3025. cuft+=[bundlejson[@"cuft"] doubleValue];
  3026. weight+=[bundlejson[@"weight"] doubleValue];
  3027. carton+=[bundlejson[@"carton"] intValue];
  3028. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3029. }
  3030. }
  3031. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3032. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3033. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3034. ret[@"items"]=dict_item;
  3035. return ret;
  3036. }
  3037. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3038. {
  3039. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3040. // 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 ];
  3041. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3042. DebugLog(@"offline_login sql:%@",sqlQuery);
  3043. sqlite3_stmt * statement;
  3044. int cart_count=0;
  3045. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3046. if ( dbresult== SQLITE_OK)
  3047. {
  3048. while (sqlite3_step(statement) == SQLITE_ROW)
  3049. {
  3050. int item_id = sqlite3_column_int(statement, 0);
  3051. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3052. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3053. }
  3054. sqlite3_finalize(statement);
  3055. }
  3056. return cart_count;
  3057. }
  3058. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3059. {
  3060. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3061. sqlite3 *db = [iSalesDB get_db];
  3062. UIApplication * app = [UIApplication sharedApplication];
  3063. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3064. NSString* orderCode=params[@"orderCode"];
  3065. int sort = [[params objectForKey:@"sort"] intValue];
  3066. NSString *sort_str = @"";
  3067. switch (sort) {
  3068. case 0:{
  3069. sort_str = @"order by c.modify_time desc";
  3070. }
  3071. break;
  3072. case 1:{
  3073. sort_str = @"order by c.modify_time asc";
  3074. }
  3075. break;
  3076. case 2:{
  3077. sort_str = @"order by m.name asc";
  3078. }
  3079. break;
  3080. case 3:{
  3081. sort_str = @"order by m.name desc";
  3082. }
  3083. break;
  3084. case 4:{
  3085. sort_str = @"order by m.description asc";
  3086. }
  3087. break;
  3088. default:
  3089. break;
  3090. }
  3091. 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 ];
  3092. // 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 ];
  3093. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3094. DebugLog(@"offline_login sql:%@",sqlQuery);
  3095. sqlite3_stmt * statement;
  3096. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3097. NSDate *date1 = [NSDate date];
  3098. int count=0;
  3099. int cart_count=0;
  3100. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3101. if ( dbresult== SQLITE_OK)
  3102. {
  3103. while (sqlite3_step(statement) == SQLITE_ROW)
  3104. {
  3105. NSDate *row_date = [NSDate date];
  3106. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3107. int product_id = sqlite3_column_int(statement, 0);
  3108. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3109. int item_id = sqlite3_column_int(statement, 7);
  3110. NSString* Price=nil;
  3111. if(str_price==nil)
  3112. {
  3113. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3114. // NSDate *price_date = [NSDate date];
  3115. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3116. // DebugLog(@"price time interval");
  3117. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3118. if(price==nil)
  3119. Price=@"No Price.";
  3120. else
  3121. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3122. }
  3123. else
  3124. {
  3125. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3126. }
  3127. double discount = sqlite3_column_double(statement, 2);
  3128. int item_count = sqlite3_column_int(statement, 3);
  3129. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3130. NSString *nsline_note=nil;
  3131. if(line_note!=nil)
  3132. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3133. char *name = (char*)sqlite3_column_text(statement, 5);
  3134. NSString *nsname=nil;
  3135. if(name!=nil)
  3136. nsname= [[NSString alloc]initWithUTF8String:name];
  3137. char *description = (char*)sqlite3_column_text(statement, 6);
  3138. NSString *nsdescription=nil;
  3139. if(description!=nil)
  3140. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3141. int stockUom = sqlite3_column_int(statement, 8);
  3142. int _id = sqlite3_column_int(statement, 9);
  3143. // NSDate *subtotal_date = [NSDate date];
  3144. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3145. // DebugLog(@"subtotal_date time interval");
  3146. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3147. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3148. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3149. int carton=[bsubtotaljson[@"carton"] intValue];
  3150. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3151. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3152. // NSDate *img_date = [NSDate date];
  3153. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3154. // DebugLog(@"img_date time interval");
  3155. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3156. itemjson[@"model"]=nsname;
  3157. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3158. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3159. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3160. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3161. itemjson[@"check"]=@"true";
  3162. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3163. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3164. itemjson[@"unit_price"]=Price;
  3165. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3166. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3167. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3168. itemjson[@"note"]=nsline_note;
  3169. // NSDate *date2 = [NSDate date];
  3170. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3171. // DebugLog(@"model_bundle time interval");
  3172. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3173. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3174. count++;
  3175. // DebugLog(@"row time interval");
  3176. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3177. }
  3178. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3179. sqlite3_finalize(statement);
  3180. }
  3181. DebugLog(@"request cart total time interval");
  3182. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3183. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3184. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3185. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3186. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3187. ret[@"count"]=[NSNumber numberWithInt:count ];
  3188. ret[@"mode"]=@"Regular Mode";
  3189. [iSalesDB close_db:db];
  3190. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3191. DebugLog(@"general notes :%@",general_note);
  3192. ret[@"general_note"]= general_note;
  3193. return [RAUtils dict2data:ret];
  3194. }
  3195. +(NSData*) offline_login :(NSMutableDictionary *) params
  3196. {
  3197. NSString* user = [params valueForKey:@"user"];
  3198. NSString* password = [params valueForKey:@"password"];
  3199. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3200. sqlite3 *db = [iSalesDB get_db];
  3201. 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"]];
  3202. DebugLog(@"offline_login sql:%@",sqlQuery);
  3203. sqlite3_stmt * statement;
  3204. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3205. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3206. {
  3207. if (sqlite3_step(statement) == SQLITE_ROW)
  3208. {
  3209. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3210. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3211. int can_show_price = sqlite3_column_int(statement, 0);
  3212. int can_see_price = sqlite3_column_int(statement, 1);
  3213. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3214. if(contact_id==nil)
  3215. contact_id="";
  3216. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3217. int user_type = sqlite3_column_int(statement, 3);
  3218. int can_cancel_order = sqlite3_column_int(statement, 4);
  3219. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3220. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3221. int can_delete_order = sqlite3_column_int(statement, 7);
  3222. int can_submit_order = sqlite3_column_int(statement, 8);
  3223. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3224. int can_create_order = sqlite3_column_int(statement, 10);
  3225. char *mode = (char*)sqlite3_column_text(statement, 11);
  3226. if(mode==nil)
  3227. mode="";
  3228. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3229. char *username = (char*)sqlite3_column_text(statement, 12);
  3230. if(username==nil)
  3231. username="";
  3232. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3233. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3234. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3235. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3236. [header setValue:nscontact_id forKey:@"contact_id"];
  3237. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3238. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3239. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3240. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3241. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3242. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3243. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3244. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3245. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3246. [header setValue:nsusername forKey:@"username"];
  3247. NSError* error=nil;
  3248. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3249. [header setValue:statusFilter forKey:@"statusFilter"];
  3250. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3251. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3252. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3253. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3254. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3255. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3256. [ret setObject:header forKey:@"header"];
  3257. [ret setValue:nsmode forKey:@"mode"];
  3258. }
  3259. sqlite3_finalize(statement);
  3260. }
  3261. [iSalesDB close_db:db];
  3262. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3263. return [RAUtils dict2data:ret];
  3264. }
  3265. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3266. {
  3267. NSString* contactId = [params valueForKey:@"contactId"];
  3268. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3269. sqlite3 *db = [iSalesDB get_db];
  3270. NSString *sqlQuery = nil;
  3271. {
  3272. 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];
  3273. }
  3274. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3275. sqlite3_stmt * statement;
  3276. [ret setValue:@"2" forKey:@"result"];
  3277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3278. {
  3279. //int i = 0;
  3280. if (sqlite3_step(statement) == SQLITE_ROW)
  3281. {
  3282. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3283. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3284. // int editable = sqlite3_column_int(statement, 0);
  3285. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3286. NSString *nscompany_name =nil;
  3287. if(company_name==nil)
  3288. nscompany_name=@"";
  3289. else
  3290. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3291. char *country = (char*)sqlite3_column_text(statement, 2);
  3292. if(country==nil)
  3293. country="";
  3294. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3295. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3296. // if(addr==nil)
  3297. // addr="";
  3298. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3299. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3300. if(zipcode==nil)
  3301. zipcode="";
  3302. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3303. char *state = (char*)sqlite3_column_text(statement, 5);
  3304. if(state==nil)
  3305. state="";
  3306. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3307. char *city = (char*)sqlite3_column_text(statement, 6);
  3308. if(city==nil)
  3309. city="";
  3310. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3311. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3312. // NSString *nscontact_name = nil;
  3313. // if(contact_name==nil)
  3314. // nscontact_name=@"";
  3315. // else
  3316. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3317. char *phone = (char*)sqlite3_column_text(statement, 8);
  3318. NSString *nsphone = nil;
  3319. if(phone==nil)
  3320. nsphone=@"";
  3321. else
  3322. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3323. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3324. if(contact_id==nil)
  3325. contact_id="";
  3326. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3327. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3328. if(addr_1==nil)
  3329. addr_1="";
  3330. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3331. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3332. if(addr_2==nil)
  3333. addr_2="";
  3334. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3335. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3336. if(addr_3==nil)
  3337. addr_3="";
  3338. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3339. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3340. if(addr_4==nil)
  3341. addr_4="";
  3342. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3343. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3344. if(first_name==nil)
  3345. first_name="";
  3346. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3347. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3348. if(last_name==nil)
  3349. last_name="";
  3350. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3351. char *fax = (char*)sqlite3_column_text(statement, 16);
  3352. NSString *nsfax = nil;
  3353. if(fax==nil)
  3354. nsfax=@"";
  3355. else
  3356. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3357. char *email = (char*)sqlite3_column_text(statement, 17);
  3358. NSString *nsemail = nil;
  3359. if(email==nil)
  3360. nsemail=@"";
  3361. else
  3362. nsemail= [[NSString alloc]initWithUTF8String:email];
  3363. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3364. NSString *nsimg_0 = nil;
  3365. if(img_0==nil)
  3366. nsimg_0=@"";
  3367. else
  3368. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3369. [self copy_bcardImg:nsimg_0];
  3370. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3371. NSString *nsimg_1 = nil;
  3372. if(img_1==nil)
  3373. nsimg_1=@"";
  3374. else
  3375. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3376. [self copy_bcardImg:nsimg_1];
  3377. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3378. NSString *nsimg_2 = nil;
  3379. if(img_2==nil)
  3380. nsimg_2=@"";
  3381. else
  3382. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3383. [self copy_bcardImg:nsimg_2];
  3384. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3385. NSString *nsprice_type = nil;
  3386. if(price_type==nil)
  3387. nsprice_type=@"";
  3388. else
  3389. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3390. char *notes = (char*)sqlite3_column_text(statement, 22);
  3391. NSString *nsnotes = nil;
  3392. if(notes==nil)
  3393. nsnotes=@"";
  3394. else
  3395. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3396. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3397. NSString *nssalesrep = nil;
  3398. if(salesrep==nil)
  3399. nssalesrep=@"";
  3400. else
  3401. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3402. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3403. {
  3404. // decrypt
  3405. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3406. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3407. nsphone=[AESCrypt fastdecrypt:nsphone];
  3408. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3409. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3410. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3411. }
  3412. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3413. [arr_name addObject:nsfirst_name];
  3414. [arr_name addObject:nslast_name];
  3415. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3416. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3417. [arr_ext addObject:nsaddr_1];
  3418. [arr_ext addObject:nsaddr_2];
  3419. [arr_ext addObject:nsaddr_3];
  3420. [arr_ext addObject:nsaddr_4];
  3421. [arr_ext addObject:@"\r\n"];
  3422. [arr_ext addObject:nscity];
  3423. [arr_ext addObject:nsstate];
  3424. [arr_ext addObject:nszipcode];
  3425. [arr_ext addObject:nscountry];
  3426. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3427. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3428. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3429. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3430. [item setValue:nscountry forKey:@"customer_country"];
  3431. [item setValue:nsphone forKey:@"customer_phone"];
  3432. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3433. [item setValue:nscompany_name forKey:@"customer_name"];
  3434. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3435. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3436. [item setValue:nsext forKey:@"customer_contact_ext"];
  3437. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3438. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3439. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3440. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3441. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3442. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3443. [item setValue:nslast_name forKey:@"customer_last_name"];
  3444. [item setValue:nscity forKey:@"customer_city"];
  3445. [item setValue:nsstate forKey:@"customer_state"];
  3446. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3447. [item setValue:contactId forKey:@"customer_cid"];
  3448. [item setValue:nscontact_name forKey:@"customer_contact"];
  3449. [item setValue:nsfax forKey:@"customer_fax"];
  3450. [item setValue:nsemail forKey:@"customer_email"];
  3451. [item setValue:contact_type forKey:@"customer_contact_type"];
  3452. [ret setObject:item forKey:@"customerInfo"];
  3453. // i++;
  3454. }
  3455. UIApplication * app = [UIApplication sharedApplication];
  3456. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3457. [ret setValue:appDelegate.mode forKey:@"mode"];
  3458. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3459. sqlite3_finalize(statement);
  3460. }
  3461. [iSalesDB close_db:db];
  3462. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3463. return ret;
  3464. }
  3465. + (bool) copy_bcardImg:(NSString*) filename
  3466. {
  3467. if(filename.length==0)
  3468. return false;
  3469. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3470. bool ret=false;
  3471. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3472. NSString *cachefolder = [paths objectAtIndex:0];
  3473. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3474. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3475. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3476. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3477. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3478. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3479. //
  3480. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3481. NSFileManager* fileManager = [NSFileManager defaultManager];
  3482. BOOL bdir=NO;
  3483. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3484. {
  3485. NSError *error = nil;
  3486. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3487. {
  3488. ret=false;
  3489. }
  3490. else
  3491. {
  3492. ret=true;
  3493. }
  3494. // NSError *error = nil;
  3495. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3496. //
  3497. // if(!bsuccess)
  3498. // {
  3499. // DebugLog(@"Create offline_createimg folder failed");
  3500. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3501. // return [RAUtils dict2data:ret];
  3502. // }
  3503. // if(bsuccess)
  3504. // {
  3505. // sqlite3 *db = [self get_db];
  3506. //
  3507. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3508. // [iSalesDB close_db:db];
  3509. // }
  3510. }
  3511. return ret;
  3512. //
  3513. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3514. // if(bsuccess)
  3515. // {
  3516. // NSError *error = nil;
  3517. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3518. // {
  3519. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3520. // }
  3521. // else
  3522. // {
  3523. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3524. //
  3525. // ret[@"img_url_aname"]=filename;
  3526. // ret[@"img_url"]=savedImagePath;
  3527. // }
  3528. // }
  3529. }
  3530. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3531. {
  3532. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3533. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3534. NSString *cachefolder = [paths objectAtIndex:0];
  3535. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3536. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3537. NSFileManager* fileManager = [NSFileManager defaultManager];
  3538. BOOL bdir=YES;
  3539. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3540. {
  3541. NSError *error = nil;
  3542. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3543. if(!bsuccess)
  3544. {
  3545. DebugLog(@"Create offline_createimg folder failed");
  3546. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3547. return [RAUtils dict2data:ret];
  3548. }
  3549. // if(bsuccess)
  3550. // {
  3551. // sqlite3 *db = [self get_db];
  3552. //
  3553. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3554. // [iSalesDB close_db:db];
  3555. // }
  3556. }
  3557. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3558. //JEPG格式
  3559. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3560. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3561. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3562. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3563. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3564. if(bsuccess)
  3565. {
  3566. NSError *error = nil;
  3567. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3568. {
  3569. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3570. }
  3571. else
  3572. {
  3573. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3574. ret[@"img_url_aname"]=filename;
  3575. ret[@"img_url"]=filename;
  3576. }
  3577. }
  3578. else
  3579. {
  3580. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3581. }
  3582. return [RAUtils dict2data:ret];
  3583. }
  3584. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3585. {
  3586. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3587. category = [category substringToIndex:3];
  3588. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3589. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3590. params[@"category"]= category;
  3591. ret[@"params"]= params;
  3592. [ret setValue:@"detail" forKey:@"target"];
  3593. [ret setValue:@"popup" forKey:@"action"];
  3594. [ret setValue:@"content" forKey:@"type"];
  3595. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3596. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3597. [ret setValue:@"true" forKey:@"single_row"];
  3598. [ret setValue:@"true" forKey:@"partial_refresh"];
  3599. // sqlite3 *db = [iSalesDB get_db];
  3600. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3601. 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 ;
  3602. sqlite3_stmt * statement;
  3603. int count = 0;
  3604. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3605. // int count=0;
  3606. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3607. {
  3608. int i=0;
  3609. while (sqlite3_step(statement) == SQLITE_ROW)
  3610. {
  3611. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3612. // char *name = (char*)sqlite3_column_text(statement, 1);
  3613. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3614. char *name = (char*)sqlite3_column_text(statement, 0);
  3615. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3616. int product_id = sqlite3_column_int(statement, 1);
  3617. char *url = (char*)sqlite3_column_text(statement, 2);
  3618. if(url==nil)
  3619. url="";
  3620. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3621. [item setValue:nsurl forKey:@"picture_path"];
  3622. [item setValue:nsname forKey:@"fash_name"];
  3623. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3624. [item setValue:category forKey:@"category"];
  3625. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3626. i++;
  3627. }
  3628. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3629. sqlite3_finalize(statement);
  3630. }
  3631. DebugLog(@"count:%d",count);
  3632. // [iSalesDB close_db:db];
  3633. return ret;
  3634. }
  3635. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3636. {
  3637. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3638. [ret setValue:key forKey:@"key"];
  3639. [ret setValue:value forKey:@"val"];
  3640. [ret setValue:@"price" forKey:@"type"];
  3641. return ret;
  3642. }
  3643. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3644. {
  3645. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3646. [ret setValue:key forKey:@"key"];
  3647. [ret setValue:value forKey:@"val"];
  3648. return ret;
  3649. }
  3650. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3651. {
  3652. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3653. [ret setValue:@"0" forKey:@"img_count"];
  3654. // sqlite3 *db = [iSalesDB get_db];
  3655. 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 ;
  3656. sqlite3_stmt * statement;
  3657. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3658. {
  3659. int i=0;
  3660. if (sqlite3_step(statement) == SQLITE_ROW)
  3661. {
  3662. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3663. // char *name = (char*)sqlite3_column_text(statement, 1);
  3664. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3665. char *value = (char*)sqlite3_column_text(statement, 0);
  3666. if(value==nil)
  3667. value="";
  3668. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3669. char *key = (char*)sqlite3_column_text(statement, 1);
  3670. if(key==nil)
  3671. key="";
  3672. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3673. [item setValue:nsvalue forKey:@"val"];
  3674. [item setValue:nskey forKey:@"key"];
  3675. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3676. [ret setValue:@"1" forKey:@"count"];
  3677. i++;
  3678. }
  3679. sqlite3_finalize(statement);
  3680. }
  3681. // [iSalesDB close_db:db];
  3682. return ret;
  3683. }
  3684. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3685. {
  3686. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3687. [ret setValue:@"0" forKey:@"count"];
  3688. // sqlite3 *db = [iSalesDB get_db];
  3689. 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;
  3690. sqlite3_stmt * statement;
  3691. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3692. {
  3693. int i=0;
  3694. while (sqlite3_step(statement) == SQLITE_ROW)
  3695. {
  3696. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3697. // char *name = (char*)sqlite3_column_text(statement, 1);
  3698. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3699. char *value = (char*)sqlite3_column_text(statement, 0);
  3700. if(value==nil)
  3701. value="";
  3702. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3703. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3704. if(selector_display==nil)
  3705. selector_display="";
  3706. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3707. int product_id = sqlite3_column_int(statement, 2);
  3708. char *category = (char*)sqlite3_column_text(statement, 3);
  3709. if(category==nil)
  3710. category="";
  3711. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3712. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3713. [item setValue:nsvalue forKey:@"title"];
  3714. [item setValue:url forKey:@"pic_url"];
  3715. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3716. [params setValue:@"2" forKey:@"count"];
  3717. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3718. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3719. [param0 setValue:@"product_id" forKey:@"name"];
  3720. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3721. [param1 setValue:nscategory forKey:@"val"];
  3722. [param1 setValue:@"category" forKey:@"name"];
  3723. [params setObject:param0 forKey:@"param_0"];
  3724. [params setObject:param1 forKey:@"param_1"];
  3725. [item setObject:params forKey:@"params"];
  3726. [ret setValue:nsselector_display forKey:@"name"];
  3727. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3728. i++;
  3729. }
  3730. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3731. [ret setValue:@"switch" forKey:@"action"];
  3732. sqlite3_finalize(statement);
  3733. }
  3734. // [iSalesDB close_db:db];
  3735. return ret;
  3736. }
  3737. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3738. {
  3739. // model 在 category search 显示的图片。
  3740. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3741. // sqlite3 *db = [iSalesDB get_db];
  3742. 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];
  3743. sqlite3_stmt * statement;
  3744. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3745. {
  3746. while (sqlite3_step(statement) == SQLITE_ROW)
  3747. {
  3748. char *url = (char*)sqlite3_column_text(statement, 0);
  3749. if(url==nil)
  3750. url="";
  3751. int type = sqlite3_column_int(statement, 1);
  3752. if(type==0)
  3753. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3754. else
  3755. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3756. }
  3757. sqlite3_finalize(statement);
  3758. }
  3759. // [iSalesDB close_db:db];
  3760. return ret;
  3761. }
  3762. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3763. {
  3764. int item_id=-1;
  3765. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3766. sqlite3_stmt * statement;
  3767. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3768. {
  3769. if (sqlite3_step(statement) == SQLITE_ROW)
  3770. {
  3771. item_id = sqlite3_column_int(statement, 0);
  3772. }
  3773. sqlite3_finalize(statement);
  3774. }
  3775. return item_id;
  3776. }
  3777. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3778. {
  3779. // NSString* ret = @"";
  3780. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3781. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3782. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3783. sqlite3_stmt * statement;
  3784. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3785. {
  3786. while (sqlite3_step(statement) == SQLITE_ROW)
  3787. {
  3788. int bitem_id = sqlite3_column_int(statement, 0);
  3789. int bitem_qty = sqlite3_column_int(statement, 1);
  3790. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3791. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3792. }
  3793. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3794. sqlite3_finalize(statement);
  3795. }
  3796. // if(ret==nil)
  3797. // ret=@"";
  3798. *count=arr_count;
  3799. return arr_bundle;
  3800. }
  3801. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3802. {
  3803. // get default sold qty, return -1 if model not found;
  3804. int ret = -1;
  3805. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3806. sqlite3_stmt * statement;
  3807. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3808. {
  3809. if (sqlite3_step(statement) == SQLITE_ROW)
  3810. {
  3811. ret = sqlite3_column_int(statement, 0);
  3812. }
  3813. sqlite3_finalize(statement);
  3814. }
  3815. return ret;
  3816. }
  3817. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3818. {
  3819. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3820. [ret setValue:@"0" forKey:@"img_count"];
  3821. // sqlite3 *db = [iSalesDB get_db];
  3822. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3823. sqlite3_stmt * statement;
  3824. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3825. {
  3826. int i=0;
  3827. while (sqlite3_step(statement) == SQLITE_ROW)
  3828. {
  3829. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3830. // char *name = (char*)sqlite3_column_text(statement, 1);
  3831. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3832. char *url = (char*)sqlite3_column_text(statement, 0);
  3833. if(url==nil)
  3834. url="";
  3835. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3836. [item setValue:nsurl forKey:@"s"];
  3837. [item setValue:nsurl forKey:@"l"];
  3838. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3839. i++;
  3840. }
  3841. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3842. sqlite3_finalize(statement);
  3843. }
  3844. // [iSalesDB close_db:db];
  3845. return ret;
  3846. }
  3847. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3848. UIApplication * app = [UIApplication sharedApplication];
  3849. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3850. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3851. sqlite3 *db = [iSalesDB get_db];
  3852. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3853. NSString* product_id=params[@"product_id"];
  3854. NSString *item_count_str = params[@"item_count"];
  3855. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3856. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3857. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3858. // NSString *sql = @"";
  3859. for(int i=0;i<arr.count;i++)
  3860. {
  3861. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3862. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3863. __block int cart_count = 0;
  3864. if (!item_count_str) {
  3865. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3866. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3867. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3868. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3869. cart_count = [[model_set_components lastObject] integerValue];
  3870. }];
  3871. }
  3872. if(count==0)
  3873. {
  3874. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3875. sqlite3_stmt *stmt;
  3876. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3877. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3878. if (item_count_arr) {
  3879. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3880. } else {
  3881. sqlite3_bind_int(stmt,2,cart_count);
  3882. }
  3883. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3884. [iSalesDB execSql:@"ROLLBACK" db:db];
  3885. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3886. [iSalesDB close_db:db];
  3887. DebugLog(@"add to wishlist error");
  3888. return ret;
  3889. }
  3890. } else {
  3891. int qty = 0;
  3892. if (item_count_arr) {
  3893. qty = [item_count_arr[i] integerValue];
  3894. } else {
  3895. qty = cart_count;
  3896. }
  3897. 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]];
  3898. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3899. [iSalesDB execSql:@"ROLLBACK" db:db];
  3900. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3901. [iSalesDB close_db:db];
  3902. DebugLog(@"add to wishlist error");
  3903. return ret;
  3904. }
  3905. }
  3906. }
  3907. // [iSalesDB execSql:sql db:db];
  3908. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3909. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3910. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3911. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3912. [iSalesDB close_db:db];
  3913. appDelegate.wish_count =count;
  3914. [appDelegate update_count_mark];
  3915. ret[@"result"]= [NSNumber numberWithInt:2];
  3916. return ret;
  3917. }
  3918. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3919. // 0 category
  3920. // 1 search
  3921. // 2 itemsearch
  3922. NSData *ret = nil;
  3923. NSDictionary *items = nil;
  3924. switch (from) {
  3925. case 0:{
  3926. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3927. }
  3928. break;
  3929. case 1:{
  3930. items = [[self search:params limited:NO] objectForKey:@"items"];
  3931. }
  3932. break;
  3933. case 2:{
  3934. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3935. }
  3936. break;
  3937. default:
  3938. break;
  3939. }
  3940. if (!items) {
  3941. return ret;
  3942. }
  3943. int count = [[items objectForKey:@"count"] intValue];
  3944. NSMutableString *product_id_str = [@"" mutableCopy];
  3945. for (int i = 0; i < count; i++) {
  3946. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3947. NSDictionary *item = [items objectForKey:key];
  3948. NSString *product_id = [item objectForKey:@"product_id"];
  3949. if (i == 0) {
  3950. [product_id_str appendString:product_id];
  3951. } else {
  3952. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3953. }
  3954. }
  3955. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3956. if ([add_to isEqualToString:@"cart"]) {
  3957. NSString *order_code = [params objectForKey:@"orderCode"];
  3958. if (order_code.length) {
  3959. NSDictionary *newParams = @{
  3960. @"product_id" : product_id_str,
  3961. @"orderCode" : order_code
  3962. };
  3963. ret = [self offline_add2cart:newParams.mutableCopy];
  3964. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3965. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3966. ret = [RAUtils dict2data:retDic];
  3967. }
  3968. } else if([add_to isEqualToString:@"wishlist"]) {
  3969. NSDictionary *newParams = @{
  3970. @"product_id" : product_id_str
  3971. };
  3972. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3973. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3974. ret = [RAUtils dict2data:retDic];
  3975. } else if([add_to isEqualToString:@"portfolio"]) {
  3976. NSDictionary *newParams = @{
  3977. @"product_id" : product_id_str
  3978. };
  3979. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  3980. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3981. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3982. ret = [RAUtils dict2data:retDic];
  3983. }
  3984. return ret;
  3985. }
  3986. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3987. {
  3988. return [self addAll:params from:0];
  3989. }
  3990. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3991. {
  3992. return [self addAll:params from:1];
  3993. }
  3994. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3995. {
  3996. return [self addAll:params from:2];
  3997. }
  3998. #pragma mark - Jack
  3999. #warning 做SQL操作时转义!!
  4000. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4001. // "val_227" : {
  4002. // "check" : 1,
  4003. // "value" : "US United States",
  4004. // "value_id" : "228"
  4005. // },
  4006. if (!countryCode) {
  4007. countryCode = @"US";
  4008. }
  4009. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4010. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4011. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4012. int code_id = sqlite3_column_int(stmt, 3); // id
  4013. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4014. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4015. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4016. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4017. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4018. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4019. }
  4020. long n = *count;
  4021. *count = n + 1;
  4022. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4023. [container setValue:countryDic forKey:key];
  4024. }] mutableCopy];
  4025. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4026. return ret;
  4027. }
  4028. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4029. countryCode = [self translateSingleQuote:countryCode];
  4030. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4031. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4032. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4033. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4034. if (name == NULL) {
  4035. name = "";
  4036. }
  4037. if (code == NULL) {
  4038. code = "";
  4039. }
  4040. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4041. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4042. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4043. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4044. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4045. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4046. }
  4047. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4048. [container setValue:stateDic forKey:key];
  4049. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4050. DebugLog(@"query all state error: %@",err_msg);
  4051. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4052. // [stateDic setValue:@"Other" forKey:@"value"];
  4053. // [stateDic setValue:@"" forKey:@"value_id"];
  4054. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4055. //
  4056. // if (state_code && [@"" isEqualToString:state_code]) {
  4057. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4058. // }
  4059. //
  4060. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4061. // [container setValue:stateDic forKey:key];
  4062. }] mutableCopy];
  4063. [ret removeObjectForKey:@"result"];
  4064. // failure 可以不用了,一样的
  4065. if (ret.allKeys.count == 0) {
  4066. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4067. [stateDic setValue:@"Other" forKey:@"value"];
  4068. [stateDic setValue:@"" forKey:@"value_id"];
  4069. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4070. if (state_code && [@"" isEqualToString:state_code]) {
  4071. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4072. }
  4073. NSString *key = [NSString stringWithFormat:@"val_0"];
  4074. [ret setValue:stateDic forKey:key];
  4075. }
  4076. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4077. return ret;
  4078. }
  4079. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4080. codeId = [self translateSingleQuote:codeId];
  4081. 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];
  4082. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4083. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4084. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4085. if (name == NULL) {
  4086. name = "";
  4087. }
  4088. if (code == NULL) {
  4089. code = "";
  4090. }
  4091. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4092. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4093. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4094. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4095. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4096. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4097. }
  4098. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4099. [container setValue:stateDic forKey:key];
  4100. }] mutableCopy];
  4101. [ret removeObjectForKey:@"result"];
  4102. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4103. return ret;
  4104. }
  4105. + (NSDictionary *)offline_getPrice {
  4106. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4107. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4108. char *name = (char *) sqlite3_column_text(stmt, 1);
  4109. int type = sqlite3_column_int(stmt, 2);
  4110. int orderBy = sqlite3_column_int(stmt, 3);
  4111. if (name == NULL) {
  4112. name = "";
  4113. }
  4114. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4115. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4116. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4117. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4118. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4119. if (orderBy == 0) {
  4120. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4121. }
  4122. [container setValue:priceDic forKey:key];
  4123. }] mutableCopy];
  4124. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4125. return ret;
  4126. }
  4127. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4128. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4129. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4130. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4131. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4132. int _id = sqlite3_column_int(stmt, 0);
  4133. NSString *name = [self textAtColumn:1 statement:stmt];
  4134. NSDictionary *typeDic = @{
  4135. @"value_id" : name,
  4136. @"value" : name,
  4137. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4138. };
  4139. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4140. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4141. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4142. }];
  4143. return ret;
  4144. }
  4145. + (NSDictionary *)offline_getSalesRep {
  4146. // 首先从offline_login表中取出sales_code
  4147. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4148. NSString *user = app.user;
  4149. user = [self translateSingleQuote:user];
  4150. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4151. __block NSString *user_code = @"";
  4152. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4153. char *code = (char *)sqlite3_column_text(stmt, 0);
  4154. if (code == NULL) {
  4155. code = "";
  4156. }
  4157. user_code = [NSString stringWithUTF8String:code];
  4158. }];
  4159. // 再取所有salesRep
  4160. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4161. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4162. // 1 name 2 code 3 salesrep_id
  4163. char *name = (char *)sqlite3_column_text(stmt, 1);
  4164. char *code = (char *)sqlite3_column_text(stmt, 2);
  4165. int salesrep_id = sqlite3_column_int(stmt, 3);
  4166. if (name == NULL) {
  4167. name = "";
  4168. }
  4169. if (code == NULL) {
  4170. code = "";
  4171. }
  4172. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4173. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4174. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4175. // 比较code 相等则check
  4176. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4177. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4178. }
  4179. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4180. }] mutableCopy];
  4181. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4182. return ret;
  4183. }
  4184. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4185. zipcode = [self translateSingleQuote:zipcode];
  4186. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4187. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4188. char *country = (char *)sqlite3_column_text(stmt, 0);
  4189. char *state = (char *)sqlite3_column_text(stmt, 1);
  4190. char *city = (char *)sqlite3_column_text(stmt, 2);
  4191. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4192. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4193. if (country == NULL) {
  4194. country = "";
  4195. }
  4196. if (state == NULL) {
  4197. state = "";
  4198. }
  4199. if (city == NULL) {
  4200. city = "";
  4201. }
  4202. if (country_code == NULL) {
  4203. country_code = "";
  4204. }
  4205. if (state_code == NULL) {
  4206. state_code = "";
  4207. }
  4208. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4209. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4210. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4211. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4212. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4213. }] mutableCopy];
  4214. return ret;
  4215. }
  4216. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4217. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4218. [item setValue:value forKey:valueKey];
  4219. [dic setValue:item forKey:itemKey];
  4220. }
  4221. + (NSString *)countryCodeByid:(NSString *)code_id {
  4222. NSString *ret = nil;
  4223. code_id = [self translateSingleQuote:code_id];
  4224. sqlite3 *db = [iSalesDB get_db];
  4225. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4226. sqlite3_stmt * statement;
  4227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4228. while (sqlite3_step(statement) == SQLITE_ROW) {
  4229. char *code = (char *)sqlite3_column_text(statement, 0);
  4230. if (code == NULL) {
  4231. code = "";
  4232. }
  4233. ret = [NSString stringWithUTF8String:code];
  4234. }
  4235. sqlite3_finalize(statement);
  4236. }
  4237. [iSalesDB close_db:db];
  4238. return ret;
  4239. }
  4240. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4241. NSString *ret = nil;
  4242. code = [self translateSingleQuote:code];
  4243. sqlite3 *db = [iSalesDB get_db];
  4244. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4245. sqlite3_stmt * statement;
  4246. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4247. while (sqlite3_step(statement) == SQLITE_ROW) {
  4248. char *_id = (char *)sqlite3_column_text(statement, 0);
  4249. if (_id == NULL) {
  4250. _id = "";
  4251. }
  4252. ret = [NSString stringWithFormat:@"%s",_id];
  4253. }
  4254. sqlite3_finalize(statement);
  4255. }
  4256. [iSalesDB close_db:db];
  4257. return ret;
  4258. }
  4259. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4260. NSString *name = nil;
  4261. codeId = [self translateSingleQuote:codeId];
  4262. sqlite3 *db = [iSalesDB get_db];
  4263. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4264. sqlite3_stmt * statement;
  4265. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4266. while (sqlite3_step(statement) == SQLITE_ROW) {
  4267. char *value = (char *)sqlite3_column_text(statement, 0);
  4268. if (value == NULL) {
  4269. value = "";
  4270. }
  4271. name = [NSString stringWithUTF8String:value];
  4272. }
  4273. sqlite3_finalize(statement);
  4274. }
  4275. [iSalesDB close_db:db];
  4276. return name;
  4277. }
  4278. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4279. NSString *ret = nil;
  4280. sqlite3 *db = [iSalesDB get_db];
  4281. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4282. sqlite3_stmt * statement;
  4283. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4284. while (sqlite3_step(statement) == SQLITE_ROW) {
  4285. char *name = (char *)sqlite3_column_text(statement, 0);
  4286. if (name == NULL) {
  4287. name = "";
  4288. }
  4289. ret = [NSString stringWithUTF8String:name];
  4290. }
  4291. sqlite3_finalize(statement);
  4292. }
  4293. [iSalesDB close_db:db];
  4294. return ret;
  4295. }
  4296. + (NSString *)salesRepCodeById:(NSString *)_id {
  4297. NSString *ret = nil;
  4298. _id = [self translateSingleQuote:_id];
  4299. sqlite3 *db = [iSalesDB get_db];
  4300. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4301. sqlite3_stmt * statement;
  4302. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4303. while (sqlite3_step(statement) == SQLITE_ROW) {
  4304. char *rep = (char *)sqlite3_column_text(statement, 0);
  4305. if (rep == NULL) {
  4306. rep = "";
  4307. }
  4308. ret = [NSString stringWithUTF8String:rep];
  4309. }
  4310. sqlite3_finalize(statement);
  4311. }
  4312. [iSalesDB close_db:db];
  4313. return ret;
  4314. }
  4315. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4316. char *tx = (char *)sqlite3_column_text(stmt, col);
  4317. if (tx == NULL) {
  4318. tx = "";
  4319. }
  4320. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4321. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4322. if (!text) {
  4323. text = @"";
  4324. }
  4325. // 将字符全部为' '的字符串干掉
  4326. int spaceCount = 0;
  4327. for (int i = 0; i < text.length; i++) {
  4328. if ([text characterAtIndex:i] == ' ') {
  4329. spaceCount++;
  4330. }
  4331. }
  4332. if (spaceCount == text.length) {
  4333. text = @"";
  4334. }
  4335. return text;
  4336. }
  4337. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4338. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4339. NSData *data = [NSData dataWithContentsOfFile:path];
  4340. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4341. return ret;
  4342. }
  4343. + (NSString *)textFileName:(NSString *)name {
  4344. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4345. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4346. if (!text) {
  4347. text = @"";
  4348. }
  4349. return text;
  4350. }
  4351. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4352. return [[dic objectForKey:key] mutableCopy];
  4353. }
  4354. + (id)translateSingleQuote:(NSString *)string {
  4355. if ([string isKindOfClass:[NSString class]])
  4356. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4357. return string;
  4358. }
  4359. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4360. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4361. }
  4362. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4363. NSString* ret= nil;
  4364. NSString *sqlQuery = nil;
  4365. // 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
  4366. 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];
  4367. sqlite3_stmt * statement;
  4368. // int count=0;
  4369. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4370. {
  4371. if (sqlite3_step(statement) == SQLITE_ROW)
  4372. {
  4373. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4374. if(imgurl==nil)
  4375. imgurl="";
  4376. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4377. ret=nsimgurl;
  4378. }
  4379. sqlite3_finalize(statement);
  4380. }
  4381. else
  4382. {
  4383. [ret setValue:@"8" forKey:@"result"];
  4384. }
  4385. // [iSalesDB close_db:db];
  4386. // DebugLog(@"data string: %@",ret );
  4387. return ret;
  4388. }
  4389. #pragma mark contact list
  4390. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4391. {
  4392. // contactType = "Sales_Order_Customer";
  4393. // limit = 25;
  4394. // offset = 0;
  4395. // password = 123456;
  4396. // "price_name" = 16;
  4397. // user = EvanK;
  4398. sqlite3 *db = [iSalesDB get_db];
  4399. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4400. if (contactType) {
  4401. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4402. } else {
  4403. contactType = @"1 = 1";
  4404. }
  4405. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4406. DebugLog(@"offline contact list keyword: %@",keyword);
  4407. // advanced search
  4408. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4409. if (contact_name) {
  4410. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4411. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4412. } else {
  4413. contact_name = @"";
  4414. }
  4415. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4416. if (customer_phone) {
  4417. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4418. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4419. } else {
  4420. customer_phone = @"";
  4421. }
  4422. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4423. if (customer_fax) {
  4424. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4425. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4426. } else {
  4427. customer_fax = @"";
  4428. }
  4429. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4430. if (customer_zipcode) {
  4431. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4432. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4433. } else {
  4434. customer_zipcode = @"";
  4435. }
  4436. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4437. if (customer_sales_rep) {
  4438. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4439. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4440. } else {
  4441. customer_sales_rep = @"";
  4442. }
  4443. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4444. if (customer_state) {
  4445. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4446. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4447. } else {
  4448. customer_state = @"";
  4449. }
  4450. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4451. if (customer_name) {
  4452. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4453. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4454. } else {
  4455. customer_name = @"";
  4456. }
  4457. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4458. if (customer_country) {
  4459. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4460. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4461. } else {
  4462. customer_country = @"";
  4463. }
  4464. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4465. if (customer_cid) {
  4466. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4467. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4468. } else {
  4469. customer_cid = @"";
  4470. }
  4471. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4472. if (customer_city) {
  4473. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4474. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4475. } else {
  4476. customer_city = @"";
  4477. }
  4478. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4479. if (customer_address) {
  4480. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4481. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4482. } else {
  4483. customer_address = @"";
  4484. }
  4485. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4486. if (customer_email) {
  4487. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4488. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4489. } else {
  4490. customer_email = @"";
  4491. }
  4492. NSString *price_name = [params valueForKey:@"price_name"];
  4493. if (price_name) {
  4494. if ([price_name containsString:@","]) {
  4495. // 首先从 price表中查处name
  4496. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4497. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4498. for (int i = 1;i < pArray.count;i++) {
  4499. NSString *p = pArray[i];
  4500. [mutablePStr appendFormat:@" or type = %@ ",p];
  4501. }
  4502. [mutablePStr appendString:@";"];
  4503. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4504. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4505. char *name = (char *)sqlite3_column_text(stmt, 0);
  4506. if (!name)
  4507. name = "";
  4508. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4509. }];
  4510. // 再根据name 拼sql
  4511. NSMutableString *mutable_price_name = [NSMutableString string];
  4512. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4513. for (int i = 1; i < price_name_array.count; i++) {
  4514. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4515. }
  4516. [mutable_price_name appendString:@")"];
  4517. price_name = mutable_price_name;
  4518. } else {
  4519. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4520. if ([price_name isEqualToString:@""]) {
  4521. price_name = @"";
  4522. } else {
  4523. __block NSString *price;
  4524. price_name = [self translateSingleQuote:price_name];
  4525. [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) {
  4526. char *p = (char *)sqlite3_column_text(stmt, 0);
  4527. if (p == NULL) {
  4528. p = "";
  4529. }
  4530. price = [NSString stringWithUTF8String:p];
  4531. }];
  4532. if ([price isEqualToString:@""]) {
  4533. price_name = @"";
  4534. } else {
  4535. price = [self translateSingleQuote:price];
  4536. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4537. }
  4538. }
  4539. }
  4540. } else {
  4541. price_name = @"";
  4542. }
  4543. int limit = [[params valueForKey:@"limit"] intValue];
  4544. int offset = [[params valueForKey:@"offset"] intValue];
  4545. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4546. 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];
  4547. 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];
  4548. // int result= [iSalesDB AddExFunction:db];
  4549. int count =0;
  4550. NSString *sqlQuery = nil;
  4551. if(keyword.length==0)
  4552. {
  4553. // 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];
  4554. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4555. sqlQuery = sql;
  4556. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4557. }
  4558. else
  4559. {
  4560. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4561. keyword = keyword.lowercaseString;
  4562. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4563. 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];
  4564. 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]];
  4565. }
  4566. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4567. sqlite3_stmt * statement;
  4568. [ret setValue:@"2" forKey:@"result"];
  4569. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4570. // sqlQuery = @"select editable,decrypt(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 Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4571. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4572. {
  4573. int i = 0;
  4574. while (sqlite3_step(statement) == SQLITE_ROW)
  4575. {
  4576. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4577. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4578. int editable = sqlite3_column_int(statement, 0);
  4579. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4580. NSString *nscompany_name =nil;
  4581. if(company_name==nil)
  4582. nscompany_name=@"";
  4583. else
  4584. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4585. char *country = (char*)sqlite3_column_text(statement, 2);
  4586. if(country==nil)
  4587. country="";
  4588. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4589. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4590. // if(addr==nil)
  4591. // addr="";
  4592. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4593. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4594. if(zipcode==nil)
  4595. zipcode="";
  4596. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4597. char *state = (char*)sqlite3_column_text(statement, 5);
  4598. if(state==nil)
  4599. state="";
  4600. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4601. char *city = (char*)sqlite3_column_text(statement, 6);
  4602. if(city==nil)
  4603. city="";
  4604. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4605. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4606. // NSString *nscontact_name = nil;
  4607. // if(contact_name==nil)
  4608. // nscontact_name=@"";
  4609. // else
  4610. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4611. char *phone = (char*)sqlite3_column_text(statement, 8);
  4612. NSString *nsphone = nil;
  4613. if(phone==nil)
  4614. nsphone=@"";
  4615. else
  4616. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4617. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4618. if(contact_id==nil)
  4619. contact_id="";
  4620. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4621. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4622. if(addr_1==nil)
  4623. addr_1="";
  4624. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4625. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4626. if(addr_2==nil)
  4627. addr_2="";
  4628. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4629. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4630. if(addr_3==nil)
  4631. addr_3="";
  4632. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4633. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4634. if(addr_4==nil)
  4635. addr_4="";
  4636. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4637. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4638. if(first_name==nil)
  4639. first_name="";
  4640. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4641. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4642. if(last_name==nil)
  4643. last_name="";
  4644. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4645. char *fax = (char*)sqlite3_column_text(statement, 16);
  4646. NSString *nsfax = nil;
  4647. if(fax==nil)
  4648. nsfax=@"";
  4649. else
  4650. {
  4651. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4652. if(nsfax.length>0)
  4653. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4654. }
  4655. char *email = (char*)sqlite3_column_text(statement, 17);
  4656. NSString *nsemail = nil;
  4657. if(email==nil)
  4658. nsemail=@"";
  4659. else
  4660. {
  4661. nsemail= [[NSString alloc]initWithUTF8String:email];
  4662. if(nsemail.length>0)
  4663. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4664. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4665. }
  4666. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4667. [arr_name addObject:nsfirst_name];
  4668. [arr_name addObject:nslast_name];
  4669. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4670. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4671. {
  4672. // decrypt
  4673. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4674. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4675. nsphone=[AESCrypt fastdecrypt:nsphone];
  4676. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4677. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4678. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4679. }
  4680. [arr_addr addObject:nscompany_name];
  4681. [arr_addr addObject:nscontact_name];
  4682. [arr_addr addObject:@"<br>"];
  4683. [arr_addr addObject:nsaddr_1];
  4684. [arr_addr addObject:nsaddr_2];
  4685. [arr_addr addObject:nsaddr_3];
  4686. [arr_addr addObject:nsaddr_4];
  4687. //[arr_addr addObject:nsaddr];
  4688. [arr_addr addObject:nszipcode];
  4689. [arr_addr addObject:nscity];
  4690. [arr_addr addObject:nsstate];
  4691. [arr_addr addObject:nscountry];
  4692. [arr_addr addObject:@"<br>"];
  4693. [arr_addr addObject:nsphone];
  4694. [arr_addr addObject:nsfax];
  4695. [arr_addr addObject:nsemail];
  4696. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4697. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4698. [item setValue:name forKey:@"name"];
  4699. [item setValue:nscontact_id forKey:@"contact_id"];
  4700. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4701. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4702. i++;
  4703. }
  4704. UIApplication * app = [UIApplication sharedApplication];
  4705. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4706. [ret setValue:appDelegate.mode forKey:@"mode"];
  4707. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4708. sqlite3_finalize(statement);
  4709. }
  4710. [iSalesDB close_db:db];
  4711. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4712. return ret;
  4713. }
  4714. #pragma mark contact Advanced search
  4715. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4716. {
  4717. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4718. return [RAUtils dict2data:contactAdvanceDic];
  4719. }
  4720. #pragma mark create new contact
  4721. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4722. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4723. NSData *data = [NSData dataWithContentsOfFile:path];
  4724. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4725. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4726. NSString *countryCode = nil;
  4727. NSString *countryCode_id = nil;
  4728. NSString *stateCode = nil;
  4729. NSString *city = nil;
  4730. NSString *zipCode = nil;
  4731. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4732. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4733. NSString *code_id = params[@"country"];
  4734. countryCode_id = code_id;
  4735. countryCode = [self countryCodeByid:code_id];
  4736. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4737. NSString *zip_code = params[@"zipcode"];
  4738. // 剔除全部为空格
  4739. int spaceCount = 0;
  4740. for (int i = 0; i < zip_code.length; i++) {
  4741. if ([zip_code characterAtIndex:i] == ' ') {
  4742. spaceCount++;
  4743. }
  4744. }
  4745. if (spaceCount == zip_code.length) {
  4746. zip_code = @"";
  4747. }
  4748. zipCode = zip_code;
  4749. if (zipCode.length > 0) {
  4750. countryCode_id = params[@"country"];
  4751. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4752. countryCode = [dic valueForKey:@"country_code"];
  4753. if (!countryCode) {
  4754. // countryCode = @"US";
  4755. NSString *code_id = params[@"country"];
  4756. countryCode = [self countryCodeByid:code_id];
  4757. }
  4758. stateCode = [dic valueForKey:@"state_code"];
  4759. if (!stateCode.length) {
  4760. stateCode = params[@"state"];
  4761. }
  4762. city = [dic valueForKey:@"city"];
  4763. if (!city.length) {
  4764. city = params[@"city"];
  4765. }
  4766. // zip code
  4767. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4768. [zipDic setValue:zipCode forKey:@"value"];
  4769. [section_0 setValue:zipDic forKey:@"item_11"];
  4770. } else {
  4771. NSString *code_id = params[@"country"];
  4772. countryCode = [self countryCodeByid:code_id];
  4773. stateCode = params[@"state"];
  4774. city = params[@"city"];
  4775. }
  4776. }
  4777. } else {
  4778. // default: US United States
  4779. countryCode = @"US";
  4780. countryCode_id = @"228";
  4781. }
  4782. // country
  4783. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4784. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4785. // state
  4786. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4787. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4788. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4789. // city
  4790. if (city) {
  4791. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4792. [cityDic setValue:city forKey:@"value"];
  4793. [section_0 setValue:cityDic forKey:@"item_13"];
  4794. }
  4795. // price type
  4796. NSDictionary *priceDic = [self offline_getPrice];
  4797. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4798. // contact type
  4799. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4800. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4801. // Sales Rep
  4802. NSDictionary *repDic = [self offline_getSalesRep];
  4803. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4804. [ret setValue:section_0 forKey:@"section_0"];
  4805. return [RAUtils dict2data:ret];
  4806. }
  4807. #pragma mark save
  4808. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4809. NSString *addr = nil;
  4810. NSString *contact_name = nil;
  4811. NSString *companyName = [params objectForKey:@"company"];
  4812. if (companyName) {
  4813. companyName = [AESCrypt fastencrypt:companyName];
  4814. } else {
  4815. companyName = @"";
  4816. }
  4817. DebugLog(@"company");
  4818. companyName = [self translateSingleQuote:companyName];
  4819. NSString *addr1 = [params objectForKey:@"address"];
  4820. NSString *addr2 = [params objectForKey:@"address2"];
  4821. NSString *addr3 = [params objectForKey:@"address_3"];
  4822. NSString *addr4 = [params objectForKey:@"address_4"];
  4823. if (!addr2) {
  4824. addr2 = @"";
  4825. }
  4826. if (!addr3) {
  4827. addr3 = @"";
  4828. }
  4829. if (!addr4) {
  4830. addr4 = @"";
  4831. }
  4832. if (!addr1) {
  4833. addr1 = @"";
  4834. }
  4835. DebugLog(@"addr");
  4836. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4837. addr = [AESCrypt fastencrypt:addr];
  4838. addr = [self translateSingleQuote:addr];
  4839. if (addr1 && ![addr1 isEqualToString:@""]) {
  4840. addr1 = [AESCrypt fastencrypt:addr1];
  4841. }
  4842. addr1 = [self translateSingleQuote:addr1];
  4843. addr2 = [self translateSingleQuote:addr2];
  4844. addr3 = [self translateSingleQuote:addr3];
  4845. addr4 = [self translateSingleQuote:addr4];
  4846. NSString *country = [params objectForKey:@"country"];
  4847. if (country) {
  4848. country = [self countryNameByCountryCodeId:country];
  4849. } else {
  4850. country = @"";
  4851. }
  4852. DebugLog(@"country");
  4853. country = [self translateSingleQuote:country];
  4854. NSString *state = [params objectForKey:@"state"];
  4855. if (!state) {
  4856. state = @"";
  4857. }
  4858. DebugLog(@"state");
  4859. state = [self translateSingleQuote:state];
  4860. NSString *city = [params objectForKey:@"city"];
  4861. if (!city) {
  4862. city = @"";
  4863. }
  4864. city = [self translateSingleQuote:city];
  4865. NSString *zipcode = [params objectForKey:@"zipcode"];
  4866. if (!zipcode) {
  4867. zipcode = @"";
  4868. }
  4869. DebugLog(@"zip");
  4870. zipcode = [self translateSingleQuote:zipcode];
  4871. NSString *fistName = [params objectForKey:@"firstname"];
  4872. if (!fistName) {
  4873. fistName = @"";
  4874. }
  4875. NSString *lastName = [params objectForKey:@"lastname"];
  4876. if (!lastName) {
  4877. lastName = @"";
  4878. }
  4879. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4880. DebugLog(@"contact_name");
  4881. contact_name = [self translateSingleQuote:contact_name];
  4882. fistName = [self translateSingleQuote:fistName];
  4883. lastName = [self translateSingleQuote:lastName];
  4884. NSString *phone = [params objectForKey:@"phone"];
  4885. if (phone) {
  4886. phone = [AESCrypt fastencrypt:phone];
  4887. } else {
  4888. phone = @"";
  4889. }
  4890. DebugLog(@"PHONE");
  4891. phone = [self translateSingleQuote:phone];
  4892. NSString *fax = [params objectForKey:@"fax"];
  4893. if (!fax) {
  4894. fax = @"";
  4895. }
  4896. DebugLog(@"FAX");
  4897. fax = [self translateSingleQuote:fax];
  4898. NSString *email = [params objectForKey:@"email"];
  4899. if (!email) {
  4900. email = @"";
  4901. }
  4902. DebugLog(@"EMAIL:%@",email);
  4903. email = [self translateSingleQuote:email];
  4904. NSString *notes = [params objectForKey:@"contact_notes"];
  4905. if (!notes) {
  4906. notes = @"";
  4907. }
  4908. DebugLog(@"NOTE:%@",notes);
  4909. notes = [self translateSingleQuote:notes];
  4910. NSString *price = [params objectForKey:@"price_name"];
  4911. if (price) {
  4912. price = [self priceNameByPriceId:price];
  4913. } else {
  4914. price = @"";
  4915. }
  4916. DebugLog(@"PRICE");
  4917. price = [self translateSingleQuote:price];
  4918. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4919. if (salesRep) {
  4920. salesRep = [self salesRepCodeById:salesRep];
  4921. } else {
  4922. salesRep = @"";
  4923. }
  4924. salesRep = [self translateSingleQuote:salesRep];
  4925. NSString *img = [params objectForKey:@"business_card"];
  4926. NSArray *array = [img componentsSeparatedByString:@","];
  4927. NSString *img_0 = array[0];
  4928. if (!img_0) {
  4929. img_0 = @"";
  4930. }
  4931. img_0 = [self translateSingleQuote:img_0];
  4932. NSString *img_1 = array[1];
  4933. if (!img_1) {
  4934. img_1 = @"";
  4935. }
  4936. img_1 = [self translateSingleQuote:img_1];
  4937. NSString *img_2 = array[2];
  4938. if (!img_2) {
  4939. img_2 = @"";
  4940. }
  4941. img_2 = [self translateSingleQuote:img_2];
  4942. NSString *contact_id = [NSUUID UUID].UUIDString;
  4943. NSString *contact_type = [params objectForKey:@"type_name"];
  4944. if (!contact_type) {
  4945. contact_type = @"";
  4946. }
  4947. contact_type = [self translateSingleQuote:contact_type];
  4948. // 判断更新时是否为customer
  4949. if (update) {
  4950. contact_id = [params objectForKey:@"contact_id"];
  4951. if (!contact_id) {
  4952. contact_id = @"";
  4953. }
  4954. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4955. __block int customer = 0;
  4956. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4957. customer = sqlite3_column_int(stmt, 0);
  4958. }];
  4959. isCustomer = customer ? YES : NO;
  4960. }
  4961. NSMutableDictionary *sync_dic = [params mutableCopy];
  4962. if (isCustomer) {
  4963. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4964. } else {
  4965. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4966. }
  4967. NSString *sync_data = nil;
  4968. NSString *sql = nil;
  4969. if (update){
  4970. contact_id = [params objectForKey:@"contact_id"];
  4971. if (!contact_id) {
  4972. contact_id = @"";
  4973. }
  4974. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4975. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4976. sync_data = [RAUtils dict2string:sync_dic];
  4977. sync_data = [self translateSingleQuote:sync_data];
  4978. 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];
  4979. } else {
  4980. contact_id = [self translateSingleQuote:contact_id];
  4981. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4982. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4983. sync_data = [RAUtils dict2string:sync_dic];
  4984. sync_data = [self translateSingleQuote:sync_data];
  4985. 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];
  4986. }
  4987. int result = [iSalesDB execSql:sql];
  4988. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4989. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4990. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4991. }
  4992. #pragma mark save new contact
  4993. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4994. {
  4995. return [self offline_saveContact:params update:NO isCustomer:YES];
  4996. }
  4997. #pragma mark edit contact
  4998. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4999. {
  5000. // {
  5001. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5002. // password = 123456;
  5003. // user = EvanK;
  5004. // }
  5005. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5006. NSData *data = [NSData dataWithContentsOfFile:path];
  5007. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5008. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5009. NSString *countryCode = nil;
  5010. NSString *countryCode_id = nil;
  5011. NSString *stateCode = nil;
  5012. /*------contact infor------*/
  5013. __block NSString *country = nil;
  5014. __block NSString *company_name = nil;
  5015. __block NSString *contact_id = params[@"contact_id"];
  5016. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5017. __block NSString *zipcode = nil;
  5018. __block NSString *state = nil; // state_code
  5019. __block NSString *city = nil; //
  5020. __block NSString *firt_name,*last_name;
  5021. __block NSString *phone,*fax,*email;
  5022. __block NSString *notes,*price_type,*sales_rep;
  5023. __block NSString *img_0,*img_1,*img_2;
  5024. __block NSString *contact_type;
  5025. contact_id = [self translateSingleQuote:contact_id];
  5026. 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];
  5027. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5028. country = [self textAtColumn:0 statement:stmt]; // country name
  5029. company_name = [self textAtColumn:1 statement:stmt];
  5030. addr_1 = [self textAtColumn:2 statement:stmt];
  5031. addr_2 = [self textAtColumn:3 statement:stmt];
  5032. addr_3 = [self textAtColumn:4 statement:stmt];
  5033. addr_4 = [self textAtColumn:5 statement:stmt];
  5034. zipcode = [self textAtColumn:6 statement:stmt];
  5035. state = [self textAtColumn:7 statement:stmt]; // state code
  5036. city = [self textAtColumn:8 statement:stmt];
  5037. firt_name = [self textAtColumn:9 statement:stmt];
  5038. last_name = [self textAtColumn:10 statement:stmt];
  5039. phone = [self textAtColumn:11 statement:stmt];
  5040. fax = [self textAtColumn:12 statement:stmt];
  5041. email = [self textAtColumn:13 statement:stmt];
  5042. notes = [self textAtColumn:14 statement:stmt];
  5043. price_type = [self textAtColumn:15 statement:stmt]; // name
  5044. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5045. img_0 = [self textAtColumn:17 statement:stmt];
  5046. img_1 = [self textAtColumn:18 statement:stmt];
  5047. img_2 = [self textAtColumn:19 statement:stmt];
  5048. contact_type = [self textAtColumn:20 statement:stmt];
  5049. }];
  5050. // decrypt
  5051. if (company_name) {
  5052. company_name = [AESCrypt fastdecrypt:company_name];
  5053. }
  5054. if (addr_1) {
  5055. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5056. }
  5057. if (phone) {
  5058. phone = [AESCrypt fastdecrypt:phone];
  5059. }
  5060. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5061. countryCode = [iSalesDB jk_queryText:countrySql];
  5062. stateCode = state;
  5063. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5064. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5065. NSString *code_id = params[@"country"];
  5066. countryCode_id = code_id;
  5067. countryCode = [self countryCodeByid:code_id];
  5068. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5069. NSString *zip_code = params[@"zipcode"];
  5070. // 剔除全部为空格
  5071. int spaceCount = 0;
  5072. for (int i = 0; i < zip_code.length; i++) {
  5073. if ([zip_code characterAtIndex:i] == ' ') {
  5074. spaceCount++;
  5075. }
  5076. }
  5077. if (spaceCount == zip_code.length) {
  5078. zip_code = @"";
  5079. }
  5080. if (zipcode.length > 0) {
  5081. zipcode = zip_code;
  5082. countryCode_id = params[@"country"];
  5083. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5084. countryCode = [dic valueForKey:@"country_code"];
  5085. if (!countryCode) {
  5086. NSString *code_id = params[@"country"];
  5087. countryCode = [self countryCodeByid:code_id];
  5088. }
  5089. stateCode = [dic valueForKey:@"state_code"];
  5090. if (!stateCode.length) {
  5091. stateCode = params[@"state"];
  5092. }
  5093. city = [dic valueForKey:@"city"];
  5094. if (!city.length) {
  5095. city = params[@"city"];
  5096. }
  5097. // zip code
  5098. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5099. [zipDic setValue:zipcode forKey:@"value"];
  5100. [section_0 setValue:zipDic forKey:@"item_8"];
  5101. } else {
  5102. NSString *code_id = params[@"country"];
  5103. countryCode = [self countryCodeByid:code_id];
  5104. stateCode = params[@"state"];
  5105. city = params[@"city"];
  5106. }
  5107. }
  5108. }
  5109. // 0 Country
  5110. // 1 Company Name
  5111. // 2 Contact ID
  5112. // 3 Picture
  5113. // 4 Address 1
  5114. // 5 Address 2
  5115. // 6 Address 3
  5116. // 7 Address 4
  5117. // 8 Zip Code
  5118. // 9 State/Province
  5119. // 10 City
  5120. // 11 Contact First Name
  5121. // 12 Contact Last Name
  5122. // 13 Phone
  5123. // 14 Fax
  5124. // 15 Email
  5125. // 16 Contact Notes
  5126. // 17 Price Type
  5127. // 18 Contact Type
  5128. // 19 Sales Rep
  5129. // country
  5130. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5131. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5132. // company
  5133. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5134. // contact_id
  5135. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5136. // picture
  5137. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5138. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5139. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5140. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5141. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5142. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5143. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5144. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5145. // addr 1 2 3 4
  5146. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5147. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5148. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5149. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5150. // zip code
  5151. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5152. // state
  5153. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5154. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5155. // city
  5156. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5157. // first last
  5158. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5159. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5160. // phone fax email
  5161. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5162. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5163. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5164. // notes
  5165. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5166. // price
  5167. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5168. for (NSString *key in priceDic.allKeys) {
  5169. if ([key containsString:@"val_"]) {
  5170. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5171. if ([dic[@"value"] isEqualToString:price_type]) {
  5172. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5173. [priceDic setValue:dic forKey:key];
  5174. }
  5175. }
  5176. }
  5177. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5178. // Contact Rep
  5179. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5180. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5181. // Sales Rep
  5182. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5183. for (NSString *key in repDic.allKeys) {
  5184. if ([key containsString:@"val_"]) {
  5185. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5186. NSString *value = dic[@"value"];
  5187. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5188. if (code && [code isEqualToString:sales_rep]) {
  5189. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5190. [repDic setValue:dic forKey:key];
  5191. }
  5192. }
  5193. }
  5194. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5195. [ret setValue:section_0 forKey:@"section_0"];
  5196. return [RAUtils dict2data:ret];
  5197. }
  5198. #pragma mark save contact
  5199. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5200. {
  5201. return [self offline_saveContact:params update:YES isCustomer:YES];
  5202. }
  5203. #pragma mark category
  5204. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5205. if (ck) {
  5206. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5207. for (NSString *key in res.allKeys) {
  5208. if (![key isEqualToString:@"count"]) {
  5209. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5210. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5211. if ([val[@"value"] isEqualToString:ck]) {
  5212. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5213. }
  5214. [res setValue:val forKey:key];
  5215. }
  5216. }
  5217. [dic setValue:res forKey:valueKey];
  5218. }
  5219. }
  5220. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5221. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5222. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5223. NSString* category = [params valueForKey:@"category"];
  5224. if (!category || [category isEqualToString:@""]) {
  5225. category = @"%";
  5226. }
  5227. category = [self translateSingleQuote:category];
  5228. int limit = [[params valueForKey:@"limit"] intValue];
  5229. int offset = [[params valueForKey:@"offset"] intValue];
  5230. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5231. NSString *limit_str = @"";
  5232. if (limited) {
  5233. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5234. }
  5235. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5236. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5237. sqlite3 *db = [iSalesDB get_db];
  5238. // [iSalesDB AddExFunction:db];
  5239. int count;
  5240. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5241. 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];
  5242. double price_min = 0;
  5243. double price_max = 0;
  5244. if ([params.allKeys containsObject:@"alert"]) {
  5245. // alert
  5246. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5247. NSString *alert = params[@"alert"];
  5248. if ([alert isEqualToString:@"Display All"]) {
  5249. alert = [NSString stringWithFormat:@""];
  5250. } else {
  5251. alert = [self translateSingleQuote:alert];
  5252. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5253. }
  5254. // available
  5255. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5256. NSString *available = params[@"available"];
  5257. NSString *available_condition;
  5258. if ([available isEqualToString:@"Display All"]) {
  5259. available_condition = @"";
  5260. } else if ([available isEqualToString:@"Available Now"]) {
  5261. available_condition = @"and availability > 0";
  5262. } else {
  5263. available_condition = @"and availability == 0";
  5264. }
  5265. // best seller
  5266. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5267. NSString *best_seller = @"";
  5268. NSString *order_best_seller = @"m.name asc";
  5269. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5270. best_seller = @"and best_seller > 0";
  5271. order_best_seller = @"m.best_seller desc,m.name asc";
  5272. }
  5273. // price
  5274. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5275. NSString *price = params[@"price"];
  5276. price_min = 0;
  5277. price_max = MAXFLOAT;
  5278. if (appDelegate.user) {
  5279. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5280. NSMutableString *priceName = [NSMutableString string];
  5281. for (int i = 0; i < priceTypeArray.count; i++) {
  5282. NSString *pricetype = priceTypeArray[i];
  5283. pricetype = [self translateSingleQuote:pricetype];
  5284. if (i == 0) {
  5285. [priceName appendFormat:@"'%@'",pricetype];
  5286. } else {
  5287. [priceName appendFormat:@",'%@'",pricetype];
  5288. }
  5289. }
  5290. if ([price isEqualToString:@"Display All"]) {
  5291. price = [NSString stringWithFormat:@""];
  5292. } else if([price containsString:@"+"]){
  5293. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5294. price_min = [price doubleValue];
  5295. 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];
  5296. } else {
  5297. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5298. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5299. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5300. 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];
  5301. }
  5302. } else {
  5303. price = @"";
  5304. }
  5305. // sold_by_qty : Sold in quantities of %@
  5306. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5307. NSString *qty = params[@"sold_by_qty"];
  5308. if ([qty isEqualToString:@"Display All"]) {
  5309. qty = @"";
  5310. } else {
  5311. qty = [self translateSingleQuote:qty];
  5312. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5313. }
  5314. // cate
  5315. category = [self translateSingleQuote:category];
  5316. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5317. // where bestseller > 0 order by bestseller desc
  5318. // sql query: alert availability(int) best_seller(int) price qty
  5319. 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];
  5320. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5321. }
  5322. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5323. if (!appDelegate.user) {
  5324. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5325. }
  5326. [ret setValue:filter forKey:@"filter"];
  5327. DebugLog(@"offline_category sql:%@",sqlQuery);
  5328. sqlite3_stmt * statement;
  5329. [ret setValue:@"2" forKey:@"result"];
  5330. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5331. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5332. // int count=0;
  5333. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5334. {
  5335. int i=0;
  5336. while (sqlite3_step(statement) == SQLITE_ROW)
  5337. {
  5338. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5339. char *name = (char*)sqlite3_column_text(statement, 0);
  5340. if(name==nil)
  5341. name="";
  5342. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5343. char *description = (char*)sqlite3_column_text(statement, 1);
  5344. if(description==nil)
  5345. description="";
  5346. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5347. int product_id = sqlite3_column_int(statement, 2);
  5348. int wid = sqlite3_column_int(statement, 3);
  5349. int closeout = sqlite3_column_int(statement, 4);
  5350. int cid = sqlite3_column_int(statement, 5);
  5351. int wisdelete = sqlite3_column_int(statement, 6);
  5352. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5353. if(wid !=0 && wisdelete != 1)
  5354. [item setValue:@"true" forKey:@"wish_exists"];
  5355. else
  5356. [item setValue:@"false" forKey:@"wish_exists"];
  5357. if(closeout==0)
  5358. [item setValue:@"false" forKey:@"is_closeout"];
  5359. else
  5360. [item setValue:@"true" forKey:@"is_closeout"];
  5361. if(cid==0)
  5362. [item setValue:@"false" forKey:@"cart_exists"];
  5363. else
  5364. [item setValue:@"true" forKey:@"cart_exists"];
  5365. [item addEntriesFromDictionary:imgjson];
  5366. // [item setValue:nsurl forKey:@"img"];
  5367. [item setValue:nsname forKey:@"name"];
  5368. [item setValue:nsdescription forKey:@"description"];
  5369. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5370. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5371. i++;
  5372. }
  5373. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5374. [ret setObject:items forKey:@"items"];
  5375. sqlite3_finalize(statement);
  5376. } else {
  5377. DebugLog(@"nothing...");
  5378. }
  5379. DebugLog(@"count:%d",count);
  5380. [iSalesDB close_db:db];
  5381. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5382. return ret;
  5383. }
  5384. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5385. {
  5386. return [self categoryList:params limited:YES];
  5387. }
  5388. # pragma mark item search
  5389. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5390. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5391. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5392. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5393. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5394. // category
  5395. NSDictionary *category_menu = [self offline_category_menu];
  5396. [filter setValue:category_menu forKey:@"category"];
  5397. NSString* where= nil;
  5398. NSString* orderby= @"m.name";
  5399. if (!filterSearch) {
  5400. int covertype = [[params valueForKey:@"covertype"] intValue];
  5401. switch (covertype) {
  5402. case 0:
  5403. {
  5404. where=@"m.category like'%%#005#%%'";
  5405. break;
  5406. }
  5407. case 1:
  5408. {
  5409. where=@"m.alert like '%QS%'";
  5410. break;
  5411. }
  5412. case 2:
  5413. {
  5414. where=@"m.availability>0";
  5415. break;
  5416. }
  5417. case 3:
  5418. {
  5419. where=@"m.best_seller>0";
  5420. orderby=@"m.best_seller desc,m.name asc";
  5421. break;
  5422. }
  5423. default:
  5424. where=@"1=1";
  5425. break;
  5426. }
  5427. }
  5428. int limit = [[params valueForKey:@"limit"] intValue];
  5429. int offset = [[params valueForKey:@"offset"] intValue];
  5430. NSString *limit_str = @"";
  5431. if (limited) {
  5432. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5433. }
  5434. sqlite3 *db = [iSalesDB get_db];
  5435. // [iSalesDB AddExFunction:db];
  5436. int count;
  5437. NSString *sqlQuery = nil;
  5438. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5439. 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];
  5440. double price_min = 0;
  5441. double price_max = 0;
  5442. if (filterSearch) {
  5443. // alert
  5444. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5445. NSString *alert = params[@"alert"];
  5446. if ([alert isEqualToString:@"Display All"]) {
  5447. alert = [NSString stringWithFormat:@""];
  5448. } else {
  5449. alert = [self translateSingleQuote:alert];
  5450. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5451. }
  5452. // available
  5453. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5454. NSString *available = params[@"available"];
  5455. NSString *available_condition;
  5456. if ([available isEqualToString:@"Display All"]) {
  5457. available_condition = @"";
  5458. } else if ([available isEqualToString:@"Available Now"]) {
  5459. available_condition = @"and availability > 0";
  5460. } else {
  5461. available_condition = @"and availability == 0";
  5462. }
  5463. // best seller
  5464. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5465. NSString *best_seller = @"";
  5466. NSString *order_best_seller = @"m.name asc";
  5467. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5468. best_seller = @"and best_seller > 0";
  5469. order_best_seller = @"m.best_seller desc,m.name asc";
  5470. }
  5471. // price
  5472. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5473. NSString *price = params[@"price"];
  5474. price_min = 0;
  5475. price_max = MAXFLOAT;
  5476. if (appDelegate.user) {
  5477. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5478. NSMutableString *priceName = [NSMutableString string];
  5479. for (int i = 0; i < priceTypeArray.count; i++) {
  5480. NSString *pricetype = priceTypeArray[i];
  5481. pricetype = [self translateSingleQuote:pricetype];
  5482. if (i == 0) {
  5483. [priceName appendFormat:@"'%@'",pricetype];
  5484. } else {
  5485. [priceName appendFormat:@",'%@'",pricetype];
  5486. }
  5487. }
  5488. if ([price isEqualToString:@"Display All"]) {
  5489. price = [NSString stringWithFormat:@""];
  5490. } else if([price containsString:@"+"]){
  5491. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5492. price_min = [price doubleValue];
  5493. 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];
  5494. } else {
  5495. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5496. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5497. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5498. 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];
  5499. }
  5500. } else {
  5501. price = @"";
  5502. }
  5503. // sold_by_qty : Sold in quantities of %@
  5504. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5505. NSString *qty = params[@"sold_by_qty"];
  5506. if ([qty isEqualToString:@"Display All"]) {
  5507. qty = @"";
  5508. } else {
  5509. qty = [self translateSingleQuote:qty];
  5510. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5511. }
  5512. // category
  5513. NSString *category_id = params[@"ctgId"];
  5514. NSMutableArray *cate_id_array = nil;
  5515. NSMutableString *cateWhere = [NSMutableString string];
  5516. if ([category_id isEqualToString:@""] || !category_id) {
  5517. [cateWhere appendString:@"1 = 1"];
  5518. } else {
  5519. if ([category_id containsString:@","]) {
  5520. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5521. } else {
  5522. cate_id_array = [@[category_id] mutableCopy];
  5523. }
  5524. /*-----------*/
  5525. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  5526. for (int i = 0; i < cate_id_array.count; i++) {
  5527. for (NSString *key0 in cateDic.allKeys) {
  5528. if ([key0 containsString:@"category_"]) {
  5529. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  5530. for (NSString *key1 in category0.allKeys) {
  5531. if ([key1 containsString:@"category_"]) {
  5532. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  5533. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5534. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5535. cate_id_array[i] = [category1 objectForKey:@"id"];
  5536. if (i == 0) {
  5537. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5538. } else {
  5539. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  5540. }
  5541. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5542. [category0 setValue:category1 forKey:key1];
  5543. [cateDic setValue:category0 forKey:key0];
  5544. }
  5545. }
  5546. }
  5547. }
  5548. }
  5549. }
  5550. [filter setValue:cateDic forKey:@"category"];
  5551. }
  5552. // where bestseller > 0 order by bestseller desc
  5553. // sql query: alert availability(int) best_seller(int) price qty
  5554. 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];
  5555. // count
  5556. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5557. }
  5558. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5559. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5560. if (!appDelegate.user) {
  5561. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5562. }
  5563. [ret setValue:filter forKey:@"filter"];
  5564. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5565. sqlite3_stmt * statement;
  5566. [ret setValue:@"2" forKey:@"result"];
  5567. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5568. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5569. // int count=0;
  5570. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5571. {
  5572. int i=0;
  5573. while (sqlite3_step(statement) == SQLITE_ROW)
  5574. {
  5575. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5576. char *name = (char*)sqlite3_column_text(statement, 0);
  5577. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5578. char *description = (char*)sqlite3_column_text(statement, 1);
  5579. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5580. int product_id = sqlite3_column_int(statement, 2);
  5581. int wid = sqlite3_column_int(statement, 3);
  5582. int closeout = sqlite3_column_int(statement, 4);
  5583. int cid = sqlite3_column_int(statement, 5);
  5584. int wisdelete = sqlite3_column_int(statement, 6);
  5585. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5586. if(wid !=0 && wisdelete != 1)
  5587. [item setValue:@"true" forKey:@"wish_exists"];
  5588. else
  5589. [item setValue:@"false" forKey:@"wish_exists"];
  5590. if(closeout==0)
  5591. [item setValue:@"false" forKey:@"is_closeout"];
  5592. else
  5593. [item setValue:@"true" forKey:@"is_closeout"];
  5594. if(cid==0)
  5595. [item setValue:@"false" forKey:@"cart_exists"];
  5596. else
  5597. [item setValue:@"true" forKey:@"cart_exists"];
  5598. [item addEntriesFromDictionary:imgjson];
  5599. // [item setValue:nsurl forKey:@"img"];
  5600. [item setValue:nsname forKey:@"fash_name"];
  5601. [item setValue:nsdescription forKey:@"description"];
  5602. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5603. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5604. i++;
  5605. }
  5606. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5607. [ret setObject:items forKey:@"items"];
  5608. sqlite3_finalize(statement);
  5609. }
  5610. [iSalesDB close_db:db];
  5611. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5612. return ret;
  5613. }
  5614. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5615. {
  5616. return [self itemsearch:params limited:YES];
  5617. }
  5618. #pragma mark order detail
  5619. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5620. if (str1.length == 0) {
  5621. str1 = @"&nbsp";
  5622. }
  5623. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5624. return str;
  5625. }
  5626. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5627. {
  5628. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5629. [fromformatter setDateFormat:from];
  5630. NSDate *date = [fromformatter dateFromString:datetime];
  5631. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5632. [toformatter setDateFormat:to];
  5633. NSString * ret = [toformatter stringFromDate:date];
  5634. return ret;
  5635. }
  5636. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5637. // 把毫秒去掉
  5638. if ([dateTime containsString:@"."]) {
  5639. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5640. }
  5641. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5642. formatter.dateFormat = formate;
  5643. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5644. NSDate *date = [formatter dateFromString:dateTime];
  5645. formatter.dateFormat = newFormate;
  5646. NSString *result = [formatter stringFromDate:date];
  5647. return result ? result : @"";
  5648. }
  5649. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5650. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5651. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5652. }
  5653. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5654. {
  5655. DebugLog(@"offline oderdetail params: %@",params);
  5656. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5657. int orderId = [params[@"orderId"] intValue];
  5658. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5659. // decrypt card number and card security code
  5660. // 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 ];
  5661. 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,c.city,c.state,c.zipcode,c.country 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];
  5662. sqlite3 *db = [iSalesDB get_db];
  5663. sqlite3_stmt * statement;
  5664. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5665. NSString *nssoid = @"";
  5666. NSString* orderinfo = @"";
  5667. NSString *moreInfo = @"";
  5668. double handlingFee = 0;
  5669. double payments_and_credist = 0;
  5670. double totalPrice = 0;
  5671. double shippingFee = 0;
  5672. double lift_gate = 0;
  5673. NSString *customer_contact = @"";
  5674. NSString *customer_email = @"";
  5675. NSString *customer_fax = @"";
  5676. NSString *customer_phone = @"";
  5677. NSString *customer_city = @"";
  5678. NSString *customer_state = @"";
  5679. NSString *customer_zipcode = @"";
  5680. NSString *customer_country = @"";
  5681. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5682. {
  5683. if (sqlite3_step(statement) == SQLITE_ROW)
  5684. {
  5685. int order_id = sqlite3_column_int(statement, 0);
  5686. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5687. ret[@"sign_url"] = sign_url;
  5688. customer_contact = [self textAtColumn:52 statement:statement];
  5689. customer_email = [self textAtColumn:53 statement:statement];
  5690. customer_phone = [self textAtColumn:54 statement:statement];
  5691. customer_fax = [self textAtColumn:55 statement:statement];
  5692. customer_city = [self textAtColumn:60 statement:statement];
  5693. customer_state = [self textAtColumn:61 statement:statement];
  5694. customer_zipcode = [self textAtColumn:62 statement:statement];
  5695. customer_country = [self textAtColumn:63 statement:statement];
  5696. int offline_edit=sqlite3_column_int(statement, 56);
  5697. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5698. char *soid = (char*)sqlite3_column_text(statement, 1);
  5699. if(soid==nil)
  5700. soid= "";
  5701. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5702. // so#
  5703. ret[@"so#"] = nssoid;
  5704. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5705. if(poNumber==nil)
  5706. poNumber= "";
  5707. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5708. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5709. if(create_time==nil)
  5710. create_time= "";
  5711. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5712. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5713. int status = sqlite3_column_int(statement, 4);
  5714. int erpStatus = sqlite3_column_int(statement, 49);
  5715. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5716. // status
  5717. if (status > 1 && status != 3) {
  5718. status = erpStatus;
  5719. } else if (status == 3) {
  5720. status = 15;
  5721. }
  5722. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5723. ret[@"order_status"] = nsstatus;
  5724. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5725. if(company_name==nil)
  5726. company_name= "";
  5727. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5728. // company name
  5729. ret[@"company_name"] = nscompany_name;
  5730. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5731. if(customer_contact==nil)
  5732. customer_contact= "";
  5733. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5734. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5735. if(addr_1==nil)
  5736. addr_1="";
  5737. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5738. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5739. if(addr_2==nil)
  5740. addr_2="";
  5741. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5742. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5743. if(addr_3==nil)
  5744. addr_3="";
  5745. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5746. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5747. if(addr_4==nil)
  5748. addr_4="";
  5749. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5750. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5751. [arr_addr addObject:nsaddr_1];
  5752. [arr_addr addObject:nsaddr_2];
  5753. [arr_addr addObject:nsaddr_3];
  5754. [arr_addr addObject:nsaddr_4];
  5755. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5756. [arr_addr addObject:customer_state];
  5757. [arr_addr addObject:customer_zipcode];
  5758. [arr_addr addObject:customer_country];
  5759. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@" " trim:true];
  5760. char *logist = (char*)sqlite3_column_text(statement, 11);
  5761. if(logist==nil)
  5762. logist= "";
  5763. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5764. if (/*status == -11 || */status == 10 || status == 11) {
  5765. nslogist = [self textAtColumn:59 statement:statement];
  5766. };
  5767. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5768. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5769. shipping = @"Shipping To Be Quoted";
  5770. } else {
  5771. shippingFee = sqlite3_column_double(statement, 12);
  5772. }
  5773. // Shipping
  5774. ret[@"Shipping"] = shipping;
  5775. int have_lift_gate = sqlite3_column_int(statement, 17);
  5776. lift_gate = sqlite3_column_double(statement, 13);
  5777. // Liftgate Fee(No loading dock)
  5778. if (!have_lift_gate) {
  5779. lift_gate = 0;
  5780. }
  5781. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5782. if (sqlite3_column_int(statement, 57)) {
  5783. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5784. }
  5785. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  5786. // [ret removeObjectForKey:@"Shipping"];
  5787. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5788. // }
  5789. //
  5790. // if (have_lift_gate) {
  5791. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5792. // }
  5793. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5794. if(general_notes==nil)
  5795. general_notes= "";
  5796. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5797. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5798. if(internal_notes==nil)
  5799. internal_notes= "";
  5800. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5801. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5802. if(payment_type==nil)
  5803. payment_type= "";
  5804. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5805. // order info
  5806. orderinfo = [self textFileName:@"order_info.html"];
  5807. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5808. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5809. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5810. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5811. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5812. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5813. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5814. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5815. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5816. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5817. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5818. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5819. NSString *payment = nil;
  5820. if([nspayment_type isEqualToString:@"Credit Card"])
  5821. {
  5822. payment = [self textFileName:@"creditcardpayment.html"];
  5823. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5824. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5825. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5826. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5827. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5828. NSString *card_type = [self textAtColumn:42 statement:statement];
  5829. if (card_type.length > 0) { // 显示星号
  5830. card_type = @"****";
  5831. }
  5832. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5833. if (card_number.length > 0 && card_number.length > 4) {
  5834. for (int i = 0; i < card_number.length - 4; i++) {
  5835. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5836. }
  5837. } else {
  5838. card_number = @"";
  5839. }
  5840. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5841. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5842. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5843. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5844. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5845. card_expiration = @"****";
  5846. }
  5847. NSString *card_city = [self textAtColumn:46 statement:statement];
  5848. NSString *card_state = [self textAtColumn:47 statement:statement];
  5849. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5850. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5851. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5852. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5853. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5854. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5855. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5856. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5857. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5858. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5859. }
  5860. else
  5861. {
  5862. payment=[self textFileName:@"normalpayment.html"];
  5863. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5864. }
  5865. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5866. ret[@"result"]= [NSNumber numberWithInt:2];
  5867. // more info
  5868. moreInfo = [self textFileName:@"more_info.html"];
  5869. /*****ship to******/
  5870. // ShipToCompany_or_&nbsp
  5871. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5872. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5873. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5874. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5875. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5876. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5877. /*****ship from******/
  5878. // ShipFromCompany_or_&nbsp
  5879. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5880. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5881. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5882. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5883. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5884. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5885. /*****freight to******/
  5886. // FreightBillToCompany_or_&nbsp
  5887. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5888. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5889. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5890. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5891. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5892. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5893. /*****merchandise to******/
  5894. // MerchandiseBillToCompany_or_&nbsp
  5895. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5896. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5897. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5898. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5899. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5900. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5901. /*****return to******/
  5902. // ReturnToCompany_or_&nbsp
  5903. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5904. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5905. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5906. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5907. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5908. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5909. //
  5910. // DebugLog(@"more info : %@",moreInfo);
  5911. // handling fee
  5912. handlingFee = sqlite3_column_double(statement, 33);
  5913. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5914. if (sqlite3_column_int(statement, 58)) {
  5915. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5916. }
  5917. //
  5918. // customer info
  5919. customerID = [self textAtColumn:36 statement:statement];
  5920. // mode
  5921. ret[@"mode"] = appDelegate.mode;
  5922. // model_count
  5923. ret[@"model_count"] = @(0);
  5924. }
  5925. sqlite3_finalize(statement);
  5926. }
  5927. [iSalesDB close_db:db];
  5928. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5929. // "customer_email" = "Shui Hu";
  5930. // "customer_fax" = "";
  5931. // "customer_first_name" = F;
  5932. // "customer_last_name" = L;
  5933. // "customer_name" = ",da He Xiang Dong Liu A";
  5934. // "customer_phone" = "Hey Xuan Feng";
  5935. contactInfo[@"customer_phone"] = customer_phone;
  5936. contactInfo[@"customer_fax"] = customer_fax;
  5937. contactInfo[@"customer_email"] = customer_email;
  5938. NSString *first_name = @"";
  5939. NSString *last_name = @"";
  5940. if ([customer_contact isEqualToString:@""]) {
  5941. } else if ([customer_contact containsString:@" "]) {
  5942. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5943. first_name = [customer_contact substringToIndex:first_space_index];
  5944. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5945. }
  5946. contactInfo[@"customer_first_name"] = first_name;
  5947. contactInfo[@"customer_last_name"] = last_name;
  5948. ret[@"customerInfo"] = contactInfo;
  5949. // models
  5950. if (nssoid) {
  5951. __block double TotalCuft = 0;
  5952. __block double TotalWeight = 0;
  5953. __block int TotalCarton = 0;
  5954. __block double allItemPrice = 0;
  5955. 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];
  5956. sqlite3 *db1 = [iSalesDB get_db];
  5957. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5958. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5959. int product_id = sqlite3_column_int(stmt, 0);
  5960. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5961. int item_id = sqlite3_column_int(stmt, 7);
  5962. NSString* Price=nil;
  5963. if(str_price==nil)
  5964. {
  5965. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  5966. if(price==nil)
  5967. Price=@"No Price.";
  5968. else
  5969. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5970. }
  5971. else
  5972. {
  5973. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5974. }
  5975. double discount = sqlite3_column_double(stmt, 2);
  5976. int item_count = sqlite3_column_int(stmt, 3);
  5977. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5978. NSString *nsline_note=nil;
  5979. if(line_note!=nil)
  5980. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5981. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5982. // NSString *nsname = nil;
  5983. // if(name!=nil)
  5984. // nsname= [[NSString alloc]initWithUTF8String:name];
  5985. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5986. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5987. // NSString *nsdescription=nil;
  5988. // if(description!=nil)
  5989. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5990. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5991. // int stockUom = sqlite3_column_int(stmt, 8);
  5992. // int _id = sqlite3_column_int(stmt, 9);
  5993. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5994. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5995. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5996. int carton=[bsubtotaljson[@"carton"] intValue];
  5997. TotalCuft += cuft;
  5998. TotalWeight += weight;
  5999. TotalCarton += carton;
  6000. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6001. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6002. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6003. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6004. itemjson[@"note"]=nsline_note;
  6005. itemjson[@"origin_price"] = Price;
  6006. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6007. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6008. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6009. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6010. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6011. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6012. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6013. if(itemjson[@"combine"] != nil)
  6014. {
  6015. // int citem=0;
  6016. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6017. for(int bc=0;bc<bcount;bc++)
  6018. {
  6019. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6020. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6021. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6022. subTotal += uprice * modulus * item_count;
  6023. }
  6024. }
  6025. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6026. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6027. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6028. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6029. allItemPrice += subTotal;
  6030. }];
  6031. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6032. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6033. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6034. // payments/Credits
  6035. // payments_and_credist = sqlite3_column_double(statement, 34);
  6036. payments_and_credist = allItemPrice;
  6037. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6038. // // total
  6039. // totalPrice = sqlite3_column_double(statement, 35);
  6040. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6041. } else {
  6042. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6043. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6044. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6045. // payments/Credits
  6046. payments_and_credist = 0;
  6047. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6048. }
  6049. // total
  6050. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6051. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6052. ret[@"order_info"]= orderinfo;
  6053. ret[@"more_order_info"] = moreInfo;
  6054. return [RAUtils dict2data:ret];
  6055. }
  6056. #pragma mark order list
  6057. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6058. NSString *nsstatus = @"";
  6059. switch (status) {
  6060. case 0:
  6061. {
  6062. nsstatus=@"Temp Order";
  6063. break;
  6064. }
  6065. case 1:
  6066. {
  6067. nsstatus=@"Saved Order";
  6068. break;
  6069. }
  6070. case 2: {
  6071. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6072. break;
  6073. }
  6074. case 3:
  6075. case 15:
  6076. {
  6077. nsstatus=@"Cancelled";
  6078. break;
  6079. }
  6080. case 10:
  6081. {
  6082. nsstatus=@"Quote Submitted";
  6083. break;
  6084. }
  6085. case 11:
  6086. {
  6087. nsstatus=@"Sales Order Submitted";
  6088. break;
  6089. }
  6090. case 12:
  6091. {
  6092. nsstatus=@"Processing";
  6093. break;
  6094. }
  6095. case 13:
  6096. {
  6097. nsstatus=@"Shipped";
  6098. break;
  6099. }
  6100. case 14:
  6101. {
  6102. nsstatus=@"Closed";
  6103. break;
  6104. }
  6105. case -11:
  6106. {
  6107. nsstatus = @"Ready For Submit";
  6108. break;
  6109. }
  6110. default:
  6111. break;
  6112. }
  6113. return nsstatus;
  6114. }
  6115. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6116. double total = 0;
  6117. __block double payments_and_credist = 0;
  6118. __block double allItemPrice = 0;
  6119. // sqlite3 *db1 = [iSalesDB get_db];
  6120. if (so_id) {
  6121. // 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];
  6122. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6123. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6124. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6125. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6126. int product_id = sqlite3_column_int(stmt, 0);
  6127. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6128. int discount = sqlite3_column_int(stmt, 2);
  6129. int item_count = sqlite3_column_int(stmt, 3);
  6130. // int item_id = sqlite3_column_int(stmt, 7);
  6131. int item_id = sqlite3_column_int(stmt, 4);
  6132. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6133. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6134. NSString* Price=nil;
  6135. if(str_price==nil)
  6136. {
  6137. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6138. if(price==nil)
  6139. Price=@"No Price.";
  6140. else
  6141. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6142. }
  6143. else
  6144. {
  6145. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6146. }
  6147. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6148. itemjson[@"The unit price"]=Price;
  6149. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6150. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6151. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6152. if(itemjson[@"combine"] != nil)
  6153. {
  6154. // int citem=0;
  6155. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6156. for(int bc=0;bc<bcount;bc++)
  6157. {
  6158. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6159. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6160. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6161. subTotal += uprice * modulus * item_count;
  6162. }
  6163. }
  6164. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6165. allItemPrice += subTotal;
  6166. }];
  6167. payments_and_credist = allItemPrice;
  6168. } else {
  6169. // payments/Credits
  6170. payments_and_credist = 0;
  6171. }
  6172. // lift_gate handlingFee shippingFee
  6173. __block double lift_gate = 0;
  6174. __block double handlingFee = 0;
  6175. __block double shippingFee = 0;
  6176. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6177. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6178. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6179. if (have_lift_gate) {
  6180. lift_gate = sqlite3_column_double(stmt, 1);
  6181. }
  6182. handlingFee = sqlite3_column_double(stmt, 2);
  6183. shippingFee = sqlite3_column_double(stmt, 3);
  6184. }];
  6185. // total
  6186. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  6187. if (close) {
  6188. [iSalesDB close_db:db1];
  6189. }
  6190. return total;
  6191. }
  6192. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6193. {
  6194. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6195. int limit = [[params valueForKey:@"limit"] intValue];
  6196. int offset = [[params valueForKey:@"offset"] intValue];
  6197. NSString* keyword = [params valueForKey:@"keyWord"];
  6198. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6199. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6200. NSString* where=@"1 = 1";
  6201. if(keyword.length>0)
  6202. 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]];
  6203. if (orderStatus.length > 0) {
  6204. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6205. if (order_status_array.count == 1) {
  6206. int status_value = [[order_status_array firstObject] integerValue];
  6207. if (status_value <= 1 || status_value == 3) {
  6208. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6209. } else {
  6210. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6211. }
  6212. } else if (order_status_array.count > 1) {
  6213. for (int i = 0; i < order_status_array.count;i++) {
  6214. NSString *status = order_status_array[i];
  6215. NSString *condition = @" or";
  6216. if (i == 0) {
  6217. condition = @" and (";
  6218. }
  6219. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6220. int status_value = [status integerValue];
  6221. if (status_value <= 1 || status_value == 3) {
  6222. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6223. } else {
  6224. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6225. }
  6226. }
  6227. where = [where stringByAppendingString:@" )"];
  6228. }
  6229. }
  6230. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6231. // DebugLog(@"order list sql: %@",sqlQuery);
  6232. sqlite3 *db = [iSalesDB get_db];
  6233. sqlite3_stmt * statement;
  6234. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6235. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6236. {
  6237. int count=0;
  6238. while (sqlite3_step(statement) == SQLITE_ROW)
  6239. {
  6240. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6241. int order_id = sqlite3_column_double(statement, 0);
  6242. char *soid = (char*)sqlite3_column_text(statement, 1);
  6243. if(soid==nil)
  6244. soid= "";
  6245. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6246. int status = sqlite3_column_double(statement, 2);
  6247. int erpStatus = sqlite3_column_double(statement, 9);
  6248. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6249. if(sales_rep==nil)
  6250. sales_rep= "";
  6251. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6252. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6253. if(create_by==nil)
  6254. create_by= "";
  6255. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6256. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6257. if(company_name==nil)
  6258. company_name= "";
  6259. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6260. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6261. if(create_time==nil)
  6262. create_time= "";
  6263. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6264. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6265. // double total_price = sqlite3_column_double(statement, 7);
  6266. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6267. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6268. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6269. int offline_edit = sqlite3_column_int(statement, 10);
  6270. // ": "JH",
  6271. // "": "$8307.00",
  6272. // "": "MOB1608050001",
  6273. // "": "ArpithaT",
  6274. // "": "1st Stage Property Transformations",
  6275. // "": "JANICE SUTTON",
  6276. // "": 2255,
  6277. // "": "08/02/2016 09:49:18",
  6278. // "": 1,
  6279. // "": "Saved Order"
  6280. // "": "1470384050483",
  6281. // "model_count": "6 / 28"
  6282. item[@"sales_rep"]= nssales_rep;
  6283. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6284. item[@"so#"]= nssoid;
  6285. item[@"create_by"]= nscreate_by;
  6286. item[@"customer_name"]= nscompany_name;
  6287. item[@"customer_contact"] = customer_contact;
  6288. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6289. item[@"purchase_time"]= nscreate_time;
  6290. int statusCode = status;
  6291. if (statusCode == 2) {
  6292. statusCode = erpStatus;
  6293. } else if (statusCode == 3) {
  6294. statusCode = 15;
  6295. }
  6296. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6297. item[@"order_status"]= nsstatus;
  6298. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6299. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6300. // item[@"model_count"]
  6301. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6302. count++;
  6303. }
  6304. ret[@"count"]= [NSNumber numberWithInt:count];
  6305. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6306. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6307. ret[@"result"]= [NSNumber numberWithInt:2];
  6308. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6309. ret[@"time_zone"] = @"PST";
  6310. sqlite3_finalize(statement);
  6311. }
  6312. 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];
  6313. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6314. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6315. [iSalesDB close_db:db];
  6316. return [RAUtils dict2data:ret];
  6317. }
  6318. #pragma mark update gnotes
  6319. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6320. {
  6321. DebugLog(@"params: %@",params);
  6322. // comments = Meyoyoyoyoyoyoy;
  6323. // orderCode = MOB1608110001;
  6324. // password = 123456;
  6325. // user = EvanK;
  6326. 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]];
  6327. int ret = [iSalesDB execSql:sql];
  6328. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6329. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6330. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6331. // [dic setValue:@"160409" forKey:@"min_ver"];
  6332. return [RAUtils dict2data:dic];
  6333. }
  6334. #pragma mark move to wishlist
  6335. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6336. {
  6337. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6338. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6339. _id = [NSString stringWithFormat:@"(%@)",_id];
  6340. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6341. sqlite3 *db = [iSalesDB get_db];
  6342. __block NSString *product_id = @"";
  6343. __block NSString *item_count_str = @"";
  6344. // __block NSString *item_id = nil;
  6345. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6346. // product_id = [self textAtColumn:0 statement:stmt];
  6347. int item_count = sqlite3_column_int(stmt, 1);
  6348. // item_id = [self textAtColumn:2 statement:stmt];
  6349. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6350. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6351. if (p_id.length) {
  6352. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6353. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6354. }
  6355. }];
  6356. [iSalesDB close_db:db];
  6357. // 去除第一个,
  6358. product_id = [product_id substringFromIndex:1];
  6359. item_count_str = [item_count_str substringFromIndex:1];
  6360. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6361. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6362. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6363. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6364. sqlite3 *db1 = [iSalesDB get_db];
  6365. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6366. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6367. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6368. // 删除
  6369. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6370. int ret = [iSalesDB execSql:deleteSql db:db1];
  6371. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6372. [iSalesDB close_db:db1];
  6373. return [RAUtils dict2data:dic];
  6374. }
  6375. #pragma mark cart delete
  6376. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6377. {
  6378. // cartItemId = 548;
  6379. // orderCode = MOB1608110001;
  6380. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6381. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6382. _id = [NSString stringWithFormat:@"(%@)",_id];
  6383. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6384. int ret = [iSalesDB execSql:sql];
  6385. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6386. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6387. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6388. // [dic setValue:@"160409" forKey:@"min_ver"];
  6389. return [RAUtils dict2data:dic];
  6390. }
  6391. #pragma mark set price
  6392. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6393. {
  6394. DebugLog(@"cart set price params: %@",params);
  6395. // "cartitem_id" = 1;
  6396. // discount = "0.000000";
  6397. // "item_note" = "";
  6398. // price = "269.000000";
  6399. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6400. NSString *notes = [self valueInParams:params key:@"item_note"];
  6401. NSString *discount = [self valueInParams:params key:@"discount"];
  6402. NSString *price = [self valueInParams:params key:@"price"];
  6403. // bool badd_price_changed=false;
  6404. // sqlite3* db=[iSalesDB get_db];
  6405. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6406. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6407. // NSRange range;
  6408. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6409. //
  6410. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6411. // badd_price_changed=true;
  6412. // [iSalesDB close_db:db];
  6413. //
  6414. // if(badd_price_changed)
  6415. // {
  6416. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6417. // }
  6418. //
  6419. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6420. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6421. int ret = [iSalesDB execSql:sql];
  6422. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6423. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6424. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6425. // [dic setValue:@"160409" forKey:@"min_ver"];
  6426. return [RAUtils dict2data:dic];
  6427. }
  6428. #pragma mark set line notes
  6429. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6430. {
  6431. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6432. NSString *notes = [self valueInParams:params key:@"notes"];
  6433. notes = [self translateSingleQuote:notes];
  6434. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6435. int ret = [iSalesDB execSql:sql];
  6436. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6437. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6438. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6439. // [dic setValue:@"160409" forKey:@"min_ver"];
  6440. return [RAUtils dict2data:dic];
  6441. }
  6442. #pragma mark set qty
  6443. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6444. {
  6445. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6446. int item_count = [params[@"inputInt"] integerValue];
  6447. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6448. int ret = [iSalesDB execSql:sql];
  6449. __block int item_id = 0;
  6450. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6451. item_id = sqlite3_column_int(stmt, 0);
  6452. }];
  6453. sqlite3 *db = [iSalesDB get_db];
  6454. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6455. [iSalesDB close_db:db];
  6456. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6457. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6458. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6459. // [dic setValue:@"160409" forKey:@"min_ver"];
  6460. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6461. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6462. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6463. return [RAUtils dict2data:dic];
  6464. }
  6465. #pragma mark place order
  6466. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6467. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6468. if (aname) {
  6469. [dic setValue:aname forKey:@"aname"];
  6470. }
  6471. if (control) {
  6472. [dic setValue:control forKey:@"control"];
  6473. }
  6474. if (keyboard) {
  6475. [dic setValue:keyboard forKey:@"keyboard"];
  6476. }
  6477. if (name) {
  6478. [dic setValue:name forKey:@"name"];
  6479. }
  6480. if (value) {
  6481. [dic setValue:value forKey:@"value"];
  6482. }
  6483. return dic;
  6484. }
  6485. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6486. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6487. orderCode = [self translateSingleQuote:orderCode];
  6488. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6489. 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];
  6490. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6491. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6492. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6493. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6494. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6495. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6496. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6497. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6498. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6499. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6500. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6501. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6502. NSString *email = [self textAtColumn:11 statement:stmt];
  6503. NSString *phone = [self textAtColumn:12 statement:stmt];
  6504. NSString *fax = [self textAtColumn:13 statement:stmt];
  6505. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6506. NSString *city = [self textAtColumn:15 statement:stmt];
  6507. NSString *state = [self textAtColumn:16 statement:stmt];
  6508. NSString *country = [self textAtColumn:17 statement:stmt];
  6509. NSString *name = [self textAtColumn:18 statement:stmt];
  6510. // contact id
  6511. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6512. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6513. [contact_id_dic setValue:@"text" forKey:@"control"];
  6514. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6515. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6516. [contact_id_dic setValue:@"true" forKey:@"required"];
  6517. [contact_id_dic setValue:contact_id forKey:@"value"];
  6518. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6519. // business card
  6520. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6521. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6522. [business_card_dic setValue:@"img" forKey:@"control"];
  6523. [business_card_dic setValue:@"1" forKey:@"disable"];
  6524. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6525. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6526. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6527. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6528. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6529. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6530. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6531. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6532. // fax
  6533. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6534. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6535. // zipcode
  6536. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6537. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6538. // city
  6539. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6540. [customer_dic setValue:city_dic forKey:@"item_12"];
  6541. // state
  6542. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6543. [customer_dic setValue:state_dic forKey:@"item_13"];
  6544. // country
  6545. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6546. [customer_dic setValue:country_dic forKey:@"item_14"];
  6547. // company name
  6548. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6549. [customer_dic setValue:company_dic forKey:@"item_2"];
  6550. // addr_1
  6551. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6552. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6553. // addr_2
  6554. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6555. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6556. // addr_3
  6557. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6558. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6559. // addr_4
  6560. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6561. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6562. // Contact
  6563. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6564. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6565. // email
  6566. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6567. [customer_dic setValue:email_dic forKey:@"item_8"];
  6568. // phone
  6569. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6570. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6571. // title
  6572. [customer_dic setValue:@"Customer" forKey:@"title"];
  6573. // count
  6574. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6575. }];
  6576. // setting
  6577. NSDictionary *setting = params[@"setting"];
  6578. NSNumber *hide = setting[@"CustomerHide"];
  6579. [customer_dic setValue:hide forKey:@"hide"];
  6580. return customer_dic;
  6581. }
  6582. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6583. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6584. // setting
  6585. NSDictionary *setting = params[@"setting"];
  6586. NSNumber *hide = setting[@"ShipToHide"];
  6587. [dic setValue:hide forKey:@"hide"];
  6588. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6589. orderCode = [self translateSingleQuote:orderCode];
  6590. 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];
  6591. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6592. NSString *cid = [self textAtColumn:0 statement:stmt];
  6593. NSString *name = [self textAtColumn:1 statement:stmt];
  6594. NSString *ext = [self textAtColumn:2 statement:stmt];
  6595. NSString *contact = [self textAtColumn:3 statement:stmt];
  6596. NSString *email = [self textAtColumn:4 statement:stmt];
  6597. NSString *fax = [self textAtColumn:5 statement:stmt];
  6598. NSString *phone = [self textAtColumn:6 statement:stmt];
  6599. // count
  6600. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6601. // title
  6602. [dic setValue:@"Ship To" forKey:@"title"];
  6603. // choose
  6604. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6605. [choose_dic setValue:@"choose" forKey:@"aname"];
  6606. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6607. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6608. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6609. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6610. @"receive_contact" : @"customer_contact",
  6611. @"receive_email" : @"customer_email",
  6612. @"receive_ext" : @"customer_contact_ext",
  6613. @"receive_fax" : @"customer_fax",
  6614. @"receive_name" : @"customer_name",
  6615. @"receive_phone" : @"customer_phone"},
  6616. @"refresh" : [NSNumber numberWithInteger:1],
  6617. @"type" : @"pull"};
  6618. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6619. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6620. @"name" : @"Add new address",
  6621. @"refresh" : [NSNumber numberWithInteger:1],
  6622. @"value" : @"new_addr"
  6623. };
  6624. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6625. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6626. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6627. @"receive_contact" : @"customer_contact",
  6628. @"receive_email" : @"customer_email",
  6629. @"receive_ext" : @"customer_contact_ext",
  6630. @"receive_fax" : @"customer_fax",
  6631. @"receive_name" : @"customer_name",
  6632. @"receive_phone" : @"customer_phone"},
  6633. @"name" : @"select_ship_to",
  6634. @"refresh" : [NSNumber numberWithInteger:1],
  6635. @"value" : @"Sales_Order_Ship_To"};
  6636. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6637. [dic setValue:choose_dic forKey:@"item_0"];
  6638. // contact id
  6639. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6640. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6641. [contact_id_dic setValue:@"true" forKey:@"required"];
  6642. [dic setValue:contact_id_dic forKey:@"item_1"];
  6643. // company name
  6644. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6645. [dic setValue:company_name_dic forKey:@"item_2"];
  6646. // address
  6647. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6648. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6649. if ([required integerValue]) {
  6650. [ext_dic setValue:@"true" forKey:@"required"];
  6651. }
  6652. [dic setValue:ext_dic forKey:@"item_3"];
  6653. // contact
  6654. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6655. [dic setValue:contact_dic forKey:@"item_4"];
  6656. // phone
  6657. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6658. [dic setValue:phone_dic forKey:@"item_5"];
  6659. // fax
  6660. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6661. [dic setValue:fax_dic forKey:@"item_6"];
  6662. // email
  6663. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6664. [dic setValue:email_dic forKey:@"item_7"];
  6665. }];
  6666. return dic;
  6667. }
  6668. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6669. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6670. // setting
  6671. NSDictionary *setting = params[@"setting"];
  6672. NSNumber *hide = setting[@"ShipFromHide"];
  6673. [dic setValue:hide forKey:@"hide"];
  6674. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6675. orderCode = [self translateSingleQuote:orderCode];
  6676. 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];
  6677. 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';";
  6678. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6679. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6680. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6681. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6682. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6683. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6684. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6685. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6686. if (!cid.length) {
  6687. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6688. cid = [self textAtColumn:0 statement:statment];
  6689. name = [self textAtColumn:1 statement:statment];
  6690. ext = [self textAtColumn:2 statement:statment];
  6691. contact = [self textAtColumn:3 statement:statment];
  6692. email = [self textAtColumn:4 statement:statment];
  6693. fax = [self textAtColumn:5 statement:statment];
  6694. phone = [self textAtColumn:6 statement:statment];
  6695. }];
  6696. }
  6697. // count
  6698. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6699. // title
  6700. [dic setValue:@"Ship From" forKey:@"title"];
  6701. // hide
  6702. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6703. // choose
  6704. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6705. [choose_dic setValue:@"choose" forKey:@"aname"];
  6706. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6707. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6708. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6709. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6710. @"sender_contact" : @"customer_contact",
  6711. @"sender_email" : @"customer_email",
  6712. @"sender_ext" : @"customer_contact_ext",
  6713. @"sender_fax" : @"customer_fax",
  6714. @"sender_name" : @"customer_name",
  6715. @"sender_phone" : @"customer_phone"},
  6716. @"name" : @"select_cid",
  6717. @"refresh" : [NSNumber numberWithInteger:0],
  6718. @"value" : @"Sales_Order_Ship_From"};
  6719. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6720. [dic setValue:choose_dic forKey:@"item_0"];
  6721. // contact id
  6722. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6723. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6724. [contact_id_dic setValue:@"true" forKey:@"required"];
  6725. [dic setValue:contact_id_dic forKey:@"item_1"];
  6726. // company name
  6727. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6728. [dic setValue:company_name_dic forKey:@"item_2"];
  6729. // address
  6730. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6731. [dic setValue:ext_dic forKey:@"item_3"];
  6732. // contact
  6733. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6734. [dic setValue:contact_dic forKey:@"item_4"];
  6735. // phone
  6736. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6737. [dic setValue:phone_dic forKey:@"item_5"];
  6738. // fax
  6739. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6740. [dic setValue:fax_dic forKey:@"item_6"];
  6741. // email
  6742. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6743. [dic setValue:email_dic forKey:@"item_7"];
  6744. }];
  6745. return dic;
  6746. }
  6747. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6748. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6749. // setting
  6750. NSDictionary *setting = params[@"setting"];
  6751. NSNumber *hide = setting[@"FreightBillToHide"];
  6752. [dic setValue:hide forKey:@"hide"];
  6753. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6754. orderCode = [self translateSingleQuote:orderCode];
  6755. 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];
  6756. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6757. NSString *cid = [self textAtColumn:0 statement:stmt];
  6758. NSString *name = [self textAtColumn:1 statement:stmt];
  6759. NSString *ext = [self textAtColumn:2 statement:stmt];
  6760. NSString *contact = [self textAtColumn:3 statement:stmt];
  6761. NSString *email = [self textAtColumn:4 statement:stmt];
  6762. NSString *fax = [self textAtColumn:5 statement:stmt];
  6763. NSString *phone = [self textAtColumn:6 statement:stmt];
  6764. // count
  6765. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6766. // title
  6767. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6768. // hide
  6769. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6770. // choose
  6771. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6772. [choose_dic setValue:@"choose" forKey:@"aname"];
  6773. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6774. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6775. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6776. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6777. @"shipping_billto_contact" : @"receive_contact",
  6778. @"shipping_billto_email" : @"receive_email",
  6779. @"shipping_billto_ext" : @"receive_ext",
  6780. @"shipping_billto_fax" : @"receive_fax",
  6781. @"shipping_billto_name" : @"receive_name",
  6782. @"shipping_billto_phone" : @"receive_phone"},
  6783. @"type" : @"pull"};
  6784. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6785. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6786. @"type" : @"pull",
  6787. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6788. @"shipping_billto_contact" : @"customer_contact",
  6789. @"shipping_billto_email" : @"customer_email",
  6790. @"shipping_billto_ext" : @"customer_contact_ext",
  6791. @"shipping_billto_fax" : @"customer_fax",
  6792. @"shipping_billto_name" : @"customer_name",
  6793. @"shipping_billto_phone" : @"customer_phone"}
  6794. };
  6795. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6796. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6797. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6798. @"shipping_billto_contact" : @"sender_contact",
  6799. @"shipping_billto_email" : @"sender_email",
  6800. @"shipping_billto_ext" : @"sender_ext",
  6801. @"shipping_billto_fax" : @"sender_fax",
  6802. @"shipping_billto_name" : @"sender_name",
  6803. @"shipping_billto_phone" : @"sender_phone"},
  6804. @"type" : @"pull"
  6805. };
  6806. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6807. NSDictionary *select_freight_bill_to_dic = @{
  6808. @"aname" : @"Select freight bill to",
  6809. @"name" : @"select_cid",
  6810. @"refresh" : [NSNumber numberWithInteger:0],
  6811. @"value" : @"Sales_Order_Freight_Bill_To",
  6812. @"key_map" : @{
  6813. @"shipping_billto_cid" : @"customer_cid",
  6814. @"shipping_billto_contact" : @"customer_contact",
  6815. @"shipping_billto_email" : @"customer_email",
  6816. @"shipping_billto_ext" : @"customer_contact_ext",
  6817. @"shipping_billto_fax" : @"customer_fax",
  6818. @"shipping_billto_name" : @"customer_name",
  6819. @"shipping_billto_phone" : @"customer_phone"
  6820. }
  6821. };
  6822. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6823. [dic setValue:choose_dic forKey:@"item_0"];
  6824. // contact id
  6825. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6826. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6827. [contact_id_dic setValue:@"true" forKey:@"required"];
  6828. [dic setValue:contact_id_dic forKey:@"item_1"];
  6829. // company name
  6830. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6831. [dic setValue:company_name_dic forKey:@"item_2"];
  6832. // address
  6833. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6834. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6835. if ([ext_required integerValue]) {
  6836. [ext_dic setValue:@"true" forKey:@"required"];
  6837. }
  6838. [dic setValue:ext_dic forKey:@"item_3"];
  6839. // contact
  6840. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6841. [dic setValue:contact_dic forKey:@"item_4"];
  6842. // phone
  6843. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6844. [dic setValue:phone_dic forKey:@"item_5"];
  6845. // fax
  6846. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6847. [dic setValue:fax_dic forKey:@"item_6"];
  6848. // email
  6849. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6850. [dic setValue:email_dic forKey:@"item_7"];
  6851. }];
  6852. return dic;
  6853. }
  6854. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6855. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6856. // setting
  6857. NSDictionary *setting = params[@"setting"];
  6858. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6859. [dic setValue:hide forKey:@"hide"];
  6860. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6861. orderCode = [self translateSingleQuote:orderCode];
  6862. 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];
  6863. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6864. NSString *cid = [self textAtColumn:0 statement:stmt];
  6865. NSString *name = [self textAtColumn:1 statement:stmt];
  6866. NSString *ext = [self textAtColumn:2 statement:stmt];
  6867. NSString *contact = [self textAtColumn:3 statement:stmt];
  6868. NSString *email = [self textAtColumn:4 statement:stmt];
  6869. NSString *fax = [self textAtColumn:5 statement:stmt];
  6870. NSString *phone = [self textAtColumn:6 statement:stmt];
  6871. // count
  6872. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6873. // title
  6874. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6875. // hide
  6876. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6877. // choose
  6878. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6879. [choose_dic setValue:@"choose" forKey:@"aname"];
  6880. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6881. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6882. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6883. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6884. @"billing_contact" : @"receive_contact",
  6885. @"billing_email" : @"receive_email",
  6886. @"billing_ext" : @"receive_ext",
  6887. @"billing_fax" : @"receive_fax",
  6888. @"billing_name" : @"receive_name",
  6889. @"billing_phone" : @"receive_phone"},
  6890. @"type" : @"pull"};
  6891. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6892. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6893. @"type" : @"pull",
  6894. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6895. @"billing_contact" : @"customer_contact",
  6896. @"billing_email" : @"customer_email",
  6897. @"billing_ext" : @"customer_contact_ext",
  6898. @"billing_fax" : @"customer_fax",
  6899. @"billing_name" : @"customer_name",
  6900. @"billing_phone" : @"customer_phone"}
  6901. };
  6902. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6903. NSDictionary *select_bill_to_dic = @{
  6904. @"aname" : @"Select bill to",
  6905. @"name" : @"select_cid",
  6906. @"refresh" : [NSNumber numberWithInteger:0],
  6907. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6908. @"key_map" : @{
  6909. @"billing_cid" : @"customer_cid",
  6910. @"billing_contact" : @"customer_contact",
  6911. @"billing_email" : @"customer_email",
  6912. @"billing_ext" : @"customer_contact_ext",
  6913. @"billing_fax" : @"customer_fax",
  6914. @"billing_name" : @"customer_name",
  6915. @"billing_phone" : @"customer_phone"
  6916. }
  6917. };
  6918. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6919. [dic setValue:choose_dic forKey:@"item_0"];
  6920. // contact id
  6921. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6922. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6923. [contact_id_dic setValue:@"true" forKey:@"required"];
  6924. [dic setValue:contact_id_dic forKey:@"item_1"];
  6925. // company name
  6926. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6927. [dic setValue:company_name_dic forKey:@"item_2"];
  6928. // address
  6929. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6930. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6931. if ([ext_required integerValue]) {
  6932. [ext_dic setValue:@"true" forKey:@"required"];
  6933. }
  6934. [dic setValue:ext_dic forKey:@"item_3"];
  6935. // contact
  6936. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6937. [dic setValue:contact_dic forKey:@"item_4"];
  6938. // phone
  6939. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6940. [dic setValue:phone_dic forKey:@"item_5"];
  6941. // fax
  6942. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6943. [dic setValue:fax_dic forKey:@"item_6"];
  6944. // email
  6945. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6946. [dic setValue:email_dic forKey:@"item_7"];
  6947. }];
  6948. return dic;
  6949. }
  6950. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6951. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6952. // setting
  6953. NSDictionary *setting = params[@"setting"];
  6954. NSNumber *hide = setting[@"ReturnToHide"];
  6955. [dic setValue:hide forKey:@"hide"];
  6956. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6957. orderCode = [self translateSingleQuote:orderCode];
  6958. 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];
  6959. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6960. NSString *cid = [self textAtColumn:0 statement:stmt];
  6961. NSString *name = [self textAtColumn:1 statement:stmt];
  6962. NSString *ext = [self textAtColumn:2 statement:stmt];
  6963. NSString *contact = [self textAtColumn:3 statement:stmt];
  6964. NSString *email = [self textAtColumn:4 statement:stmt];
  6965. NSString *fax = [self textAtColumn:5 statement:stmt];
  6966. NSString *phone = [self textAtColumn:6 statement:stmt];
  6967. // count
  6968. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6969. // title
  6970. [dic setValue:@"Return To" forKey:@"title"];
  6971. // hide
  6972. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6973. // choose
  6974. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6975. [choose_dic setValue:@"choose" forKey:@"aname"];
  6976. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6977. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6978. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6979. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6980. @"returnto_contact" : @"sender_contact",
  6981. @"returnto_email" : @"sender_email",
  6982. @"returnto_ext" : @"sender_ext",
  6983. @"returnto_fax" : @"sender_fax",
  6984. @"returnto_name" : @"sender_name",
  6985. @"returnto_phone" : @"sender_phone"},
  6986. @"type" : @"pull"};
  6987. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6988. NSDictionary *select_return_to_dic = @{
  6989. @"aname" : @"Select return to",
  6990. @"name" : @"select_cid",
  6991. @"refresh" : [NSNumber numberWithInteger:0],
  6992. @"value" : @"Contact_Return_To",
  6993. @"key_map" : @{
  6994. @"returnto_cid" : @"customer_cid",
  6995. @"returnto_contact" : @"customer_contact",
  6996. @"returnto_email" : @"customer_email",
  6997. @"returnto_ext" : @"customer_contact_ext",
  6998. @"returnto_fax" : @"customer_fax",
  6999. @"returnto_name" : @"customer_name",
  7000. @"returnto_phone" : @"customer_phone"
  7001. }
  7002. };
  7003. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7004. [dic setValue:choose_dic forKey:@"item_0"];
  7005. // contact id
  7006. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7007. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7008. [contact_id_dic setValue:@"true" forKey:@"required"];
  7009. [dic setValue:contact_id_dic forKey:@"item_1"];
  7010. // company name
  7011. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7012. [dic setValue:company_name_dic forKey:@"item_2"];
  7013. // address
  7014. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7015. [dic setValue:ext_dic forKey:@"item_3"];
  7016. // contact
  7017. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7018. [dic setValue:contact_dic forKey:@"item_4"];
  7019. // phone
  7020. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7021. [dic setValue:phone_dic forKey:@"item_5"];
  7022. // fax
  7023. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7024. [dic setValue:fax_dic forKey:@"item_6"];
  7025. // email
  7026. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7027. [dic setValue:email_dic forKey:@"item_7"];
  7028. }];
  7029. return dic;
  7030. }
  7031. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7032. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7033. orderCode = [self translateSingleQuote:orderCode];
  7034. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7035. __block double TotalCuft = 0;
  7036. __block double TotalWeight = 0;
  7037. __block int TotalCarton = 0;
  7038. __block double payments = 0;
  7039. // setting
  7040. NSDictionary *setting = params[@"setting"];
  7041. NSNumber *hide = setting[@"ModelInformationHide"];
  7042. [dic setValue:hide forKey:@"hide"];
  7043. 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];
  7044. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7045. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7046. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7047. // item id
  7048. int item_id = sqlite3_column_int(stmt, 0);
  7049. // count
  7050. int item_count = sqlite3_column_int(stmt, 1);
  7051. // stockUom
  7052. int stockUom = sqlite3_column_int(stmt, 2);
  7053. // unit price
  7054. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7055. NSString* Price=nil;
  7056. if([str_price isEqualToString:@""])
  7057. {
  7058. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7059. if(price==nil)
  7060. Price=@"No Price.";
  7061. else
  7062. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7063. }
  7064. else
  7065. {
  7066. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7067. }
  7068. // discount
  7069. double discount = sqlite3_column_double(stmt, 4);
  7070. // name
  7071. NSString *name = [self textAtColumn:5 statement:stmt];
  7072. // description
  7073. NSString *description = [self textAtColumn:6 statement:stmt];
  7074. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7075. // line note
  7076. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7077. int avaulability = sqlite3_column_int(stmt, 8);
  7078. // img
  7079. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7080. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7081. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7082. if(combine != nil)
  7083. {
  7084. // int citem=0;
  7085. int bcount=[[combine valueForKey:@"count"] intValue];
  7086. for(int bc=0;bc<bcount;bc++)
  7087. {
  7088. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7089. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7090. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7091. subTotal += uprice * modulus * item_count;
  7092. }
  7093. }
  7094. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7095. [model_dic setValue:@"model" forKey:@"control"];
  7096. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7097. [model_dic setValue:description forKey:@"description"];
  7098. [model_dic setValue:line_note forKey:@"note"];
  7099. [model_dic setValue:img forKey:@"img_url"];
  7100. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7101. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7102. [model_dic setValue:Price forKey:@"unit_price"];
  7103. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7104. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7105. if (combine) {
  7106. [model_dic setValue:combine forKey:@"combine"];
  7107. }
  7108. // well,what under the row is the info for total
  7109. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7110. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7111. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7112. int carton=[bsubtotaljson[@"carton"] intValue];
  7113. TotalCuft += cuft;
  7114. TotalWeight += weight;
  7115. TotalCarton += carton;
  7116. payments += subTotal;
  7117. //---------------------------
  7118. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7119. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7120. }];
  7121. [dic setValue:@"Model Information" forKey:@"title"];
  7122. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7123. return dic;
  7124. }
  7125. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7126. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7127. [dic setValue:@"Remarks Content" forKey:@"title"];
  7128. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7129. // setting
  7130. NSDictionary *setting = params[@"setting"];
  7131. NSNumber *hide = setting[@"RemarksContentHide"];
  7132. [dic setValue:hide forKey:@"hide"];
  7133. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7134. orderCode = [self translateSingleQuote:orderCode];
  7135. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7136. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7137. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7138. int mustCall = sqlite3_column_int(stmt, 1);
  7139. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7140. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7141. NSDictionary *po_dic = @{
  7142. @"aname" : @"PO#",
  7143. @"control" : @"edit",
  7144. @"keyboard" : @"text",
  7145. @"name" : @"poNumber",
  7146. @"value" : poNumber
  7147. };
  7148. NSDictionary *must_call_dic = @{
  7149. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7150. @"control" : @"switch",
  7151. @"name" : @"must_call",
  7152. @"value" : mustCall ? @"true" : @"false"
  7153. };
  7154. NSDictionary *general_notes_dic = @{
  7155. @"aname" : @"General notes",
  7156. @"control" : @"text_view",
  7157. @"keyboard" : @"text",
  7158. @"name" : @"comments",
  7159. @"value" : generalNotes
  7160. };
  7161. NSDictionary *internal_notes_dic = @{
  7162. @"aname" : @"Internal notes",
  7163. @"control" : @"text_view",
  7164. @"keyboard" : @"text",
  7165. @"name" : @"internal_notes",
  7166. @"value" : internalNotes
  7167. };
  7168. [dic setValue:po_dic forKey:@"item_0"];
  7169. [dic setValue:must_call_dic forKey:@"item_1"];
  7170. [dic setValue:general_notes_dic forKey:@"item_2"];
  7171. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7172. }];
  7173. return dic;
  7174. }
  7175. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7176. // params
  7177. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7178. orderCode = [self translateSingleQuote:orderCode];
  7179. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7180. // setting
  7181. NSDictionary *setting = params[@"setting"];
  7182. NSNumber *hide = setting[@"OrderTotalHide"];
  7183. [dic setValue:hide forKey:@"hide"];
  7184. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7185. __block double lift_gate_value = 0;
  7186. __block double handling_fee = 0;
  7187. __block double shipping = 0;
  7188. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7189. int lift_gate = sqlite3_column_int(stmt, 0);
  7190. lift_gate_value = sqlite3_column_double(stmt, 1);
  7191. shipping = sqlite3_column_double(stmt, 2);
  7192. handling_fee = sqlite3_column_double(stmt, 3);
  7193. if (!lift_gate) {
  7194. lift_gate_value = 0;
  7195. }
  7196. }];
  7197. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7198. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7199. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7200. double payments = [[total valueForKey:@"payments"] doubleValue];
  7201. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7202. double totalPrice = payments;
  7203. [dic setValue:@"Order Total" forKey:@"title"];
  7204. NSDictionary *payments_dic = @{
  7205. @"align" : @"right",
  7206. @"aname" : @"Payments/Credits",
  7207. @"control" : @"text",
  7208. @"name" : @"paymentsAndCredits",
  7209. @"type" : @"price",
  7210. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7211. };
  7212. [dic setValue:payments_dic forKey:@"item_0"];
  7213. NSDictionary *handling_fee_dic = @{
  7214. @"align" : @"right",
  7215. @"aname" : @"Handling Fee",
  7216. @"control" : @"text",
  7217. @"name" : @"handling_fee_value",
  7218. @"required" : @"true",
  7219. @"type" : @"price",
  7220. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7221. };
  7222. NSDictionary *shipping_dic = @{
  7223. @"align" : @"right",
  7224. @"aname" : @"Shipping*",
  7225. @"control" : @"text",
  7226. @"name" : @"shipping",
  7227. @"required" : @"true",
  7228. @"type" : @"price",
  7229. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7230. };
  7231. NSDictionary *lift_gate_dic = @{
  7232. @"align" : @"right",
  7233. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7234. @"control" : @"text",
  7235. @"name" : @"lift_gate_value",
  7236. @"required" : @"true",
  7237. @"type" : @"price",
  7238. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7239. };
  7240. int item_count = 1;
  7241. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7242. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7243. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7244. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7245. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7246. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7247. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7248. } else {
  7249. }
  7250. }
  7251. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7252. NSDictionary *total_price_dic = @{
  7253. @"align" : @"right",
  7254. @"aname" : @"Total",
  7255. @"control" : @"text",
  7256. @"name" : @"totalPrice",
  7257. @"type" : @"price",
  7258. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7259. };
  7260. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7261. NSDictionary *total_cuft_dic = @{
  7262. @"align" : @"right",
  7263. @"aname" : @"Total Cuft",
  7264. @"control" : @"text",
  7265. @"name" : @"",
  7266. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7267. };
  7268. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7269. NSDictionary *total_weight_dic = @{
  7270. @"align" : @"right",
  7271. @"aname" : @"Total Weight",
  7272. @"control" : @"text",
  7273. @"name" : @"",
  7274. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7275. };
  7276. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7277. NSDictionary *total_carton_dic = @{
  7278. @"align" : @"right",
  7279. @"aname" : @"Total Carton",
  7280. @"control" : @"text",
  7281. @"name" : @"",
  7282. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7283. };
  7284. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7285. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7286. return dic;
  7287. }
  7288. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7289. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7290. orderCode = [self translateSingleQuote:orderCode];
  7291. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7292. // setting
  7293. NSDictionary *setting = params[@"setting"];
  7294. NSNumber *hide = setting[@"SignatureHide"];
  7295. [dic setValue:hide forKey:@"hide"];
  7296. [dic setValue:@"Signature" forKey:@"title"];
  7297. __block NSString *pic_path = @"";
  7298. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7299. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7300. pic_path = [self textAtColumn:0 statement:stmt];
  7301. }];
  7302. NSDictionary *pic_dic = @{
  7303. @"aname" : @"Signature",
  7304. @"avalue" :pic_path,
  7305. @"control" : @"signature",
  7306. @"name" : @"sign_picpath",
  7307. @"value" : pic_path
  7308. };
  7309. [dic setValue:pic_dic forKey:@"item_0"];
  7310. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7311. return dic;
  7312. }
  7313. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7314. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7315. // setting
  7316. NSDictionary *setting = params[@"setting"];
  7317. NSNumber *hide = setting[@"ShippingMethodHide"];
  7318. [dic setValue:hide forKey:@"hide"];
  7319. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7320. orderCode = [self translateSingleQuote:orderCode];
  7321. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7322. __block NSString *logist = @"";
  7323. __block NSString *lift_gate = @"";
  7324. __block int lift_gate_integer = 0;
  7325. __block NSString *logistic_note = @"";
  7326. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7327. logist = [self textAtColumn:0 statement:stmt];
  7328. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7329. logistic_note = [self textAtColumn:2 statement:stmt];
  7330. }];
  7331. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7332. [dic setValue:@"Shipping Method" forKey:@"title"];
  7333. // val_0
  7334. int PERSONAL_PICK_UP_check = 0;
  7335. int USE_MY_CARRIER_check = 0;
  7336. NSString *logistic_note_text = @"";
  7337. int will_call_check = 0;
  7338. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7339. will_call_check = 1;
  7340. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7341. PERSONAL_PICK_UP_check = 1;
  7342. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7343. USE_MY_CARRIER_check = 1;
  7344. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7345. logistic_note = [logistic_note_array firstObject];
  7346. if (logistic_note_array.count > 1) {
  7347. logistic_note_text = [logistic_note_array lastObject];
  7348. }
  7349. }
  7350. }
  7351. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7352. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7353. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7354. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7355. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7356. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7357. NSDictionary *val0_subItem_item0 = @{
  7358. @"aname" : @"Option",
  7359. @"cadedate" : @{
  7360. @"count" : [NSNumber numberWithInteger:2],
  7361. @"val_0" : @{
  7362. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7363. @"refresh" : [NSNumber numberWithInteger:0],
  7364. @"value" : @"PERSONAL PICK UP",
  7365. @"value_id" : @"PERSONAL PICK UP"
  7366. },
  7367. @"val_1" : @{
  7368. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7369. @"refresh" : [NSNumber numberWithInteger:0],
  7370. @"sub_item" : @{
  7371. @"count" : [NSNumber numberWithInteger:1],
  7372. @"item_0" : @{
  7373. @"aname" : @"BOL",
  7374. @"control" : @"edit",
  7375. @"keyboard" : @"text",
  7376. @"name" : @"logist_note_text",
  7377. @"refresh" : [NSNumber numberWithInteger:0],
  7378. @"required" : @"false",
  7379. @"value" : logistic_note_text
  7380. }
  7381. },
  7382. @"value" : @"USE MY CARRIER",
  7383. @"value_id" : @"USE MY CARRIER"
  7384. }
  7385. },
  7386. @"control" : @"enum",
  7387. @"name" : @"logistic_note",
  7388. @"required" : @"true",
  7389. @"single_select" : @"true"
  7390. };
  7391. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7392. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7393. // val_1
  7394. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7395. if([lift_gate isEqualToString:@""]) {
  7396. if (lift_gate_integer == 1) {
  7397. lift_gate = @"true";
  7398. } else {
  7399. lift_gate = @"false";
  7400. }
  7401. }
  7402. int common_carrier_check = 0;
  7403. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7404. common_carrier_check = 1;
  7405. [params setValue:lift_gate forKey:@"lift_gate"];
  7406. }
  7407. NSDictionary *val_1 = @{
  7408. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7409. @"sub_item" : @{
  7410. @"count" : [NSNumber numberWithInteger:1],
  7411. @"item_0" : @{
  7412. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7413. @"control" : @"switch",
  7414. @"name" : @"lift_gate",
  7415. @"refresh" : [NSNumber numberWithInteger:1],
  7416. @"required" : @"true",
  7417. @"value" : lift_gate
  7418. }
  7419. },
  7420. @"value" : @"COMMON CARRIER",
  7421. @"value_id" : @"COMMON CARRIER"
  7422. };
  7423. // cadedate
  7424. NSDictionary *cadedate = @{
  7425. @"count" : [NSNumber numberWithInteger:2],
  7426. @"val_0" : val_0,
  7427. @"val_1" : val_1
  7428. };
  7429. // item_0
  7430. NSMutableDictionary *item_0 = @{
  7431. @"aname" : @"Shipping",
  7432. @"cadedate" : cadedate,
  7433. @"control" : @"enum",
  7434. @"name" : @"logist",
  7435. @"refresh" : [NSNumber numberWithInteger:1],
  7436. @"single_select" : @"true",
  7437. }.mutableCopy;
  7438. NSNumber *required = setting[@"ShippingMethodRequire"];
  7439. if ([required integerValue]) {
  7440. [item_0 setValue:@"true" forKey:@"required"];
  7441. }
  7442. [dic setValue:item_0 forKey:@"item_0"];
  7443. if ([logist isEqualToString:@"WILL CALL"]) {
  7444. [dic removeObjectForKey:@"lift_gate"];
  7445. }
  7446. return dic;
  7447. }
  7448. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7449. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7450. orderCode = [self translateSingleQuote:orderCode];
  7451. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7452. __block int submit_as_integer = 0;
  7453. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7454. submit_as_integer = sqlite3_column_int(stmt, 0);
  7455. }];
  7456. int check11 = 0;
  7457. int check10 = 0;
  7458. if (submit_as_integer == 11) {
  7459. check11 = 1;
  7460. }
  7461. if (submit_as_integer == 10) {
  7462. check10 = 1;
  7463. }
  7464. // section_0
  7465. NSMutableDictionary *dic = @{
  7466. @"count" : @(1),
  7467. @"item_0" : @{
  7468. @"aname" : @"Submit Order As",
  7469. @"cadedate" : @{
  7470. @"count" : @(2),
  7471. @"val_0" : @{
  7472. @"check" : [NSNumber numberWithInteger:check11],
  7473. @"value" : @"Sales Order",
  7474. @"value_id" : @(11)
  7475. },
  7476. @"val_1" : @{
  7477. @"check" : [NSNumber numberWithInteger:check10],
  7478. @"value" : @"Quote",
  7479. @"value_id" : @(10)
  7480. }
  7481. },
  7482. @"control" : @"enum",
  7483. @"name" : @"erpOrderStatus",
  7484. @"required" : @"true",
  7485. @"single_select" : @"true"
  7486. },
  7487. @"title" : @"Order Type"
  7488. }.mutableCopy;
  7489. // setting
  7490. NSDictionary *setting = params[@"setting"];
  7491. NSNumber *hide = setting[@"OrderTypeHide"];
  7492. [dic setValue:hide forKey:@"hide"];
  7493. return dic;
  7494. }
  7495. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7496. countryCode = [self translateSingleQuote:countryCode];
  7497. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7498. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7499. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7500. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7501. if (name == NULL) {
  7502. name = "";
  7503. }
  7504. if (code == NULL) {
  7505. code = "";
  7506. }
  7507. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7508. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7509. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7510. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7511. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7512. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7513. }
  7514. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7515. [container setValue:stateDic forKey:key];
  7516. }] mutableCopy];
  7517. [ret removeObjectForKey:@"result"];
  7518. // failure 可以不用了,一样的
  7519. if (ret.allKeys.count == 0) {
  7520. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7521. [stateDic setValue:@"Other" forKey:@"value"];
  7522. [stateDic setValue:@"" forKey:@"value_id"];
  7523. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7524. if (state_code && [@"" isEqualToString:state_code]) {
  7525. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7526. }
  7527. NSString *key = [NSString stringWithFormat:@"val_0"];
  7528. [ret setValue:stateDic forKey:key];
  7529. }
  7530. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7531. return ret;
  7532. }
  7533. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7534. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7535. orderCode = [self translateSingleQuote:orderCode];
  7536. 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];
  7537. __block NSString *payType = @"";
  7538. __block NSString *firstName = @"";
  7539. __block NSString *lastName = @"";
  7540. __block NSString *addr1 = @"";
  7541. __block NSString *addr2 = @"";
  7542. __block NSString *zipcode = @"";
  7543. __block NSString *cardType = @"";
  7544. __block NSString *cardNumber = @"";
  7545. __block NSString *securityCode = @"";
  7546. __block NSString *month = @"";
  7547. __block NSString *year = @"";
  7548. __block NSString *city = @"";
  7549. __block NSString *state = @"";
  7550. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7551. payType = [self textAtColumn:0 statement:stmt];
  7552. firstName = [self textAtColumn:1 statement:stmt];
  7553. lastName = [self textAtColumn:2 statement:stmt];
  7554. addr1 = [self textAtColumn:3 statement:stmt];
  7555. addr2 = [self textAtColumn:4 statement:stmt];
  7556. zipcode = [self textAtColumn:5 statement:stmt];
  7557. cardType = [self textAtColumn:6 statement:stmt];
  7558. cardNumber = [self textAtColumn:7 statement:stmt];
  7559. securityCode = [self textAtColumn:8 statement:stmt];
  7560. month = [self textAtColumn:9 statement:stmt];
  7561. year = [self textAtColumn:10 statement:stmt];
  7562. city = [self textAtColumn:11 statement:stmt];
  7563. state = [self textAtColumn:12 statement:stmt];
  7564. }];
  7565. NSString *required = @"true";
  7566. // setting
  7567. NSDictionary *setting = params[@"setting"];
  7568. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7569. if ([requiredNumber integerValue]) {
  7570. required = @"true";
  7571. } else {
  7572. required = @"false";
  7573. }
  7574. // "section_2"
  7575. NSMutableDictionary *dic = @{
  7576. @"count" : @(1),
  7577. @"item_0" : @{
  7578. @"aname" : @"Payment",
  7579. @"required" : required,
  7580. @"cadedate" : @{
  7581. @"count" : @(6),
  7582. @"val_0" : @{
  7583. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7584. @"value" : @"Check",
  7585. @"value_id" : @"Check"
  7586. },
  7587. @"val_1" : @{
  7588. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7589. @"value" : @"Cash",
  7590. @"value_id" : @"Cash"
  7591. },
  7592. @"val_2" : @{
  7593. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7594. @"value" : @"NET 30",
  7595. @"value_id" : @"NET 30"
  7596. },
  7597. @"val_3" : @{
  7598. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7599. @"value" : @"Wire Transfer",
  7600. @"value_id" : @"Wire Transfer"
  7601. },
  7602. @"val_4" : @{
  7603. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7604. @"sub_item" : @{
  7605. @"count" : @(3),
  7606. @"item_0" : @{
  7607. @"aname" : @"choose",
  7608. @"control" : @"multi_action",
  7609. @"count" : @(1),
  7610. @"item_0" : @{
  7611. @"aname" : @"Same as customer",
  7612. @"key_map" : @{
  7613. @"credit_card_address1" : @"customer_address1",
  7614. @"credit_card_address2" : @"customer_address2",
  7615. @"credit_card_city" : @"customer_city",
  7616. @"credit_card_first_name" : @"customer_first_name",
  7617. @"credit_card_last_name" : @"customer_last_name",
  7618. @"credit_card_state" : @"customer_state",
  7619. @"credit_card_zipcode" : @"customer_zipcode"
  7620. },
  7621. @"type" : @"pull"
  7622. }
  7623. },
  7624. @"item_1" : @{
  7625. @"aname" : @"",
  7626. @"color" : @"red",
  7627. @"control" : @"text",
  7628. @"name" : @"",
  7629. @"value" : @"USA Credit cards only"
  7630. },
  7631. @"item_2" : @{
  7632. @"aname" : @"Fill",
  7633. @"cadedate" : @{
  7634. @"count" : @(2),
  7635. @"val_0" : @{
  7636. @"check" : @(1),
  7637. @"sub_item" : @{
  7638. @"count" : @(11),
  7639. @"item_0" : @{
  7640. @"aname" : @"Type",
  7641. @"cadedate" : @{
  7642. @"count" : @(2),
  7643. @"val_0" : @{
  7644. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7645. @"value" : @"VISA",
  7646. @"value_id" : @(0)
  7647. },
  7648. @"val_1" : @{
  7649. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7650. @"value" : @"MASTER CARD",
  7651. @"value_id" : @(1)
  7652. }
  7653. },
  7654. @"control" : @"enum",
  7655. @"name" : @"credit_card_type",
  7656. @"required" : @"true",
  7657. @"single_select" : @"true"
  7658. },
  7659. @"item_1" : @{
  7660. @"aname" : @"Number",
  7661. @"control" : @"edit",
  7662. @"keyboard" : @"int",
  7663. @"length" : @"16",
  7664. @"name" : @"credit_card_number",
  7665. @"required" : @"true",
  7666. @"value" : cardNumber
  7667. },
  7668. @"item_10" : @{
  7669. @"aname" : @"State",
  7670. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7671. @"control" : @"enum",
  7672. @"enum" : @"true",
  7673. @"name" : @"credit_card_state",
  7674. @"required" : @"true",
  7675. @"single_select" : @"true"
  7676. },
  7677. @"item_2" : @{
  7678. @"aname" : @"Expiration Date",
  7679. @"control" : @"monthpicker",
  7680. @"name" : @"credit_card_expiration",
  7681. @"required" : @"true",
  7682. @"type" : @"date",
  7683. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7684. },
  7685. @"item_3" : @{
  7686. @"aname" : @"Security Code",
  7687. @"control" : @"edit",
  7688. @"keyboard" : @"int",
  7689. @"length" : @"3",
  7690. @"name" : @"credit_card_security_code",
  7691. @"required" : @"true",
  7692. @"value" : securityCode
  7693. },
  7694. @"item_4" : @{
  7695. @"aname" : @"First Name",
  7696. @"control" : @"edit",
  7697. @"keyboard" : @"text",
  7698. @"name" : @"credit_card_first_name",
  7699. @"required" : @"true",
  7700. @"value" : firstName
  7701. },
  7702. @"item_5" : @{
  7703. @"aname" : @"Last Name",
  7704. @"control" : @"edit",
  7705. @"keyboard" : @"text",
  7706. @"name" : @"credit_card_last_name",
  7707. @"required" : @"true",
  7708. @"value" : lastName
  7709. },
  7710. @"item_6" : @{
  7711. @"aname" : @"Address 1",
  7712. @"control" : @"edit",
  7713. @"keyboard" : @"text",
  7714. @"name" : @"credit_card_address1",
  7715. @"required" : @"true",
  7716. @"value" : addr1
  7717. },
  7718. @"item_7" : @{
  7719. @"aname" : @"Address 2",
  7720. @"control" : @"edit",
  7721. @"keyboard" : @"text",
  7722. @"name" : @"credit_card_address2",
  7723. @"value" : addr2
  7724. },
  7725. @"item_8" : @{
  7726. @"aname" : @"zip code",
  7727. @"control" : @"edit",
  7728. @"keyboard" : @"text",
  7729. @"name" : @"credit_card_zipcode",
  7730. @"required" : @"true",
  7731. @"value" : zipcode
  7732. },
  7733. @"item_9" : @{
  7734. @"aname" : @"City",
  7735. @"control" : @"edit",
  7736. @"keyboard" : @"text",
  7737. @"name" : @"credit_card_city",
  7738. @"required" : @"true",
  7739. @"value" : city
  7740. }
  7741. },
  7742. @"value" : @"Fill Now",
  7743. @"value_id" : @""
  7744. },
  7745. @"val_1" : @{
  7746. @"check" : @(0),
  7747. @"value" : @"Fill Later",
  7748. @"value_id" : @""
  7749. }
  7750. },
  7751. @"control" : @"enum",
  7752. @"name" : @"",
  7753. @"single_select" : @"true"
  7754. }
  7755. },
  7756. @"value" : @"Credit Card",
  7757. @"value_id" : @"Credit Card"
  7758. },
  7759. @"val_5" : @{
  7760. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7761. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7762. @"value_id" : @"FOLLOW EXISTING"
  7763. }
  7764. },
  7765. @"control" : @"enum",
  7766. @"name" : @"paymentType",
  7767. @"single_select" : @"true"
  7768. },
  7769. @"title" : @"Payment Information"
  7770. }.mutableCopy;
  7771. NSNumber *hide = setting[@"PaymentInformationHide"];
  7772. [dic setValue:hide forKey:@"hide"];
  7773. return dic;
  7774. }
  7775. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7776. {
  7777. sqlite3 *db = [iSalesDB get_db];
  7778. // UISetting
  7779. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7780. NSString *cachefolder = [paths objectAtIndex:0];
  7781. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7782. NSData* json =nil;
  7783. json=[NSData dataWithContentsOfFile:img_cache];
  7784. NSError *error=nil;
  7785. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7786. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7787. [params setObject:setting forKey:@"setting"];
  7788. int section_count = 0;
  7789. // params
  7790. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7791. orderCode = [self translateSingleQuote:orderCode];
  7792. // 0 Order Type 1 Shipping Method 2 Payment Information
  7793. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7794. // 0 Order Type
  7795. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7796. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7797. [ret setValue:order_type_dic forKey:key0];
  7798. // 1 Shipping Method
  7799. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7800. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7801. [ret setValue:shipping_method_dic forKey:key1];
  7802. // 2 Payment Information
  7803. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7804. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7805. [ret setValue:payment_info_dic forKey:key2];
  7806. // 3 Customer
  7807. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7808. NSDictionary *customer_dic = [self customerDic:params db:db];
  7809. [ret setValue:customer_dic forKey:key3];
  7810. // 4 Ship To
  7811. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7812. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7813. [ret setValue:ship_to_dic forKey:key4];
  7814. // 5 Ship From
  7815. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7816. if (![shipFromDisable integerValue]) {
  7817. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7818. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7819. [ret setValue:ship_from_dic forKey:key5];
  7820. }
  7821. // 6 Freight Bill To
  7822. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7823. if (![freightBillToDisable integerValue]) {
  7824. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7825. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7826. [ret setValue:freight_bill_to forKey:key6];
  7827. }
  7828. // 7 Merchandise Bill To
  7829. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7830. if (![merchandiseBillToDisable integerValue]) {
  7831. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7832. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7833. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7834. }
  7835. // 8 Return To
  7836. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7837. if (![returnToDisable integerValue]) {
  7838. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7839. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7840. [ret setValue:return_to_dic forKey:key8];
  7841. }
  7842. // 9 Model Information
  7843. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7844. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7845. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7846. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7847. [ret setValue:model_info_dic forKey:key9];
  7848. // 10 Remarks Content
  7849. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7850. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7851. [ret setValue:remarks_content_dic forKey:key10];
  7852. // 11 Order Total
  7853. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7854. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7855. [ret setValue:order_total_dic forKey:key11];
  7856. // 12 Signature
  7857. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7858. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7859. [ret setValue:sign_dic forKey:key12];
  7860. [ret setValue:@(section_count) forKey:@"section_count"];
  7861. [iSalesDB close_db:db];
  7862. return [RAUtils dict2data:ret];
  7863. // return nil;
  7864. }
  7865. #pragma mark addr editor
  7866. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7867. NSMutableDictionary *new_item = [item mutableCopy];
  7868. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7869. return new_item;
  7870. }
  7871. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7872. {
  7873. // "is_subaction" = true;
  7874. // orderCode = MOB1608240002;
  7875. // password = 123456;
  7876. // "subaction_tag" = 1;
  7877. // user = EvanK;
  7878. // {
  7879. // "is_subaction" = true;
  7880. // orderCode = MOB1608240002;
  7881. // password = 123456;
  7882. // "refresh_trigger" = zipcode;
  7883. // "subaction_tag" = 1;
  7884. // user = EvanK;
  7885. // }
  7886. NSString *country_code = nil;
  7887. NSString *zipCode = nil;
  7888. NSString *stateCode = nil;
  7889. NSString *city = nil;
  7890. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  7891. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  7892. NSString *code_id = params[@"country"];
  7893. country_code = [self countryCodeByid:code_id];
  7894. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  7895. NSString *zip_code = params[@"zipcode"];
  7896. // 剔除全部为空格
  7897. int spaceCount = 0;
  7898. for (int i = 0; i < zip_code.length; i++) {
  7899. if ([zip_code characterAtIndex:i] == ' ') {
  7900. spaceCount++;
  7901. }
  7902. }
  7903. if (spaceCount == zip_code.length) {
  7904. zip_code = @"";
  7905. }
  7906. zipCode = zip_code;
  7907. if (zipCode.length > 0) {
  7908. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  7909. country_code = [dic valueForKey:@"country_code"];
  7910. if (!country_code) {
  7911. // country_code = @"US";
  7912. NSString *code_id = params[@"country"];
  7913. country_code = [self countryCodeByid:code_id];
  7914. }
  7915. stateCode = [dic valueForKey:@"state_code"];
  7916. if(!stateCode.length) {
  7917. stateCode = params[@"state"];
  7918. }
  7919. city = [dic valueForKey:@"city"];
  7920. if (!city.length) {
  7921. city = params[@"city"];
  7922. }
  7923. // zip code
  7924. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  7925. // [zipDic setValue:zipCode forKey:@"value"];
  7926. // [section_0 setValue:zipDic forKey:@"item_11"];
  7927. } else {
  7928. NSString *code_id = params[@"country"];
  7929. country_code = [self countryCodeByid:code_id];
  7930. stateCode = params[@"state"];
  7931. city = params[@"city"];
  7932. }
  7933. }
  7934. }
  7935. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7936. // [ret removeObjectForKey:@"up_params"];
  7937. [ret setObject:@"New Address" forKey:@"title"];
  7938. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7939. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7940. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7941. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7942. // [country_dic removeObjectForKey:@"refresh"];
  7943. // [country_dic removeObjectForKey:@"restore"];
  7944. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7945. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7946. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7947. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7948. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7949. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7950. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7951. // [zip_code_dic removeObjectForKey:@"refresh"];
  7952. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  7953. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7954. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7955. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7956. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  7957. [cityDic setValue:city ? city : @"" forKey:@"value"];
  7958. [new_section_0 setObject:cityDic forKey:@"item_8"];
  7959. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7960. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7961. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7962. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7963. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7964. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7965. // country
  7966. NSString *countryCode = country_code == nil ? @"US" : country_code;
  7967. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7968. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7969. // state
  7970. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  7971. // NSDictionary *tmpDic = @{
  7972. // @"value" : @"Other",
  7973. // @"value_id" : @"",
  7974. // @"check" : [NSNumber numberWithInteger:0]
  7975. // };
  7976. //
  7977. // for (int i = 0; i < allState.allKeys.count; i++) {
  7978. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7979. //
  7980. // NSDictionary *tmp = allState[key];
  7981. // [allState setValue:tmpDic forKey:key];
  7982. // tmpDic = tmp;
  7983. // }
  7984. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7985. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7986. [ret setValue:new_section_0 forKey:@"section_0"];
  7987. return [RAUtils dict2data:ret];
  7988. }
  7989. #pragma mark save addr
  7990. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7991. {
  7992. // NSString *companyName = [self valueInParams:params key:@"company"];
  7993. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7994. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7995. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7996. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7997. // NSString *countryId = [self valueInParams:params key:@"country"];
  7998. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7999. // NSString *city = [self valueInParams:params key:@"city"];
  8000. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8001. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8002. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8003. // NSString *phone = [self valueInParams:params key:@"phone"];
  8004. // NSString *fax = [self valueInParams:params key:@"fax"];
  8005. // NSString *email = [self valueInParams:params key:@"email"];
  8006. return [self offline_saveContact:params update:NO isCustomer:NO];
  8007. }
  8008. #pragma mark cancel order
  8009. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8010. {
  8011. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8012. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8013. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8014. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8015. int ret = [iSalesDB execSql:sql];
  8016. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8017. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8018. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8019. // [dic setValue:@"160409" forKey:@"min_ver"];
  8020. return [RAUtils dict2data:dic];
  8021. }
  8022. #pragma mark sign order
  8023. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8024. {
  8025. //参考 offline_saveBusinesscard
  8026. DebugLog(@"sign order params: %@",params);
  8027. // orderCode = MOB1608240002;
  8028. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8029. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8030. orderCode = [self translateSingleQuote:orderCode];
  8031. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8032. picPath = [self translateSingleQuote:picPath];
  8033. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8034. int ret = [iSalesDB execSql:sql];
  8035. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8036. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8037. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8038. // [dic setValue:@"160409" forKey:@"min_ver"];
  8039. return [RAUtils dict2data:dic];
  8040. }
  8041. #pragma mark save order
  8042. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8043. NSString *ret = [self valueInParams:params key:key];
  8044. if (translate) {
  8045. ret = [self translateSingleQuote:ret];
  8046. }
  8047. return ret;
  8048. }
  8049. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8050. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8051. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8052. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8053. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8054. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8055. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8056. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8057. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8058. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8059. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8060. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8061. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8062. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8063. if (![total_price isEqualToString:@""]) {
  8064. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8065. } else {
  8066. total_price = @"";
  8067. }
  8068. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8069. if ([paymentsAndCredits isEqualToString:@""]) {
  8070. paymentsAndCredits = @"";
  8071. } else {
  8072. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8073. }
  8074. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8075. if ([handling_fee_value isEqualToString:@""]) {
  8076. handling_fee_value = @"";
  8077. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8078. handling_fee_value = @"";
  8079. } else {
  8080. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8081. }
  8082. NSString *handling_fee_placeholder = handling_fee_value;
  8083. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8084. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8085. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8086. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8087. NSString *lift_gate_placeholder = @"";
  8088. if ([lift_gate_value isEqualToString:@""]) {
  8089. lift_gate_placeholder = @"";
  8090. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8091. lift_gate_placeholder = @"";
  8092. } else {
  8093. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8094. }
  8095. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8096. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8097. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8098. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8099. 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];
  8100. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8101. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8102. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8103. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8104. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8105. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8106. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8107. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8108. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8109. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8110. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8111. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8112. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8113. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8114. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8115. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8116. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8117. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8118. if (!number_nil) {
  8119. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8120. }
  8121. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8122. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8123. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8124. if (!security_code_nil) {
  8125. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8126. }
  8127. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8128. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8129. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8130. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8131. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8132. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8133. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8134. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8135. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8136. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8137. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8138. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8139. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8140. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8141. NSString *contact_id = customer_cid;
  8142. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8143. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8144. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8145. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8146. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8147. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8148. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8149. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8150. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8151. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8152. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8153. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8154. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8155. //
  8156. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8157. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8158. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8159. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8160. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8161. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8162. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8163. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8164. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8165. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8166. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8167. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8168. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8169. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8170. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8171. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8172. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8173. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8174. 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];
  8175. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8176. if (receive_cid.length) {
  8177. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8178. }
  8179. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8180. if (receive_name.length) {
  8181. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8182. }
  8183. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8184. if (receive_ext.length) {
  8185. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8186. }
  8187. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8188. if (receive_contact.length) {
  8189. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8190. }
  8191. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8192. if (receive_phone.length) {
  8193. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8194. }
  8195. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8196. if (receive_email.length) {
  8197. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8198. }
  8199. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8200. if (receive_fax.length) {
  8201. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8202. }
  8203. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8204. if (sender_cid.length) {
  8205. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8206. }
  8207. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8208. if (sender_name.length) {
  8209. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8210. }
  8211. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8212. if (sender_ext.length) {
  8213. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8214. }
  8215. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8216. if(sender_contact.length) {
  8217. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8218. }
  8219. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8220. if (sender_phone.length) {
  8221. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8222. }
  8223. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8224. if (sender_fax.length) {
  8225. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8226. }
  8227. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8228. if (sender_email.length) {
  8229. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8230. }
  8231. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8232. if (shipping_billto_cid.length) {
  8233. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8234. }
  8235. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8236. if (shipping_billto_name.length) {
  8237. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8238. }
  8239. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8240. if (shipping_billto_ext.length) {
  8241. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8242. }
  8243. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8244. if (shipping_billto_contact.length) {
  8245. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8246. }
  8247. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8248. if (shipping_billto_phone.length) {
  8249. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8250. }
  8251. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8252. if (shipping_billto_fax.length) {
  8253. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8254. }
  8255. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8256. if (shipping_billto_email.length) {
  8257. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8258. }
  8259. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8260. if (billing_cid.length) {
  8261. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8262. }
  8263. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8264. if (billing_name.length) {
  8265. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8266. }
  8267. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8268. if (billing_ext.length) {
  8269. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8270. }
  8271. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8272. if (billing_contact.length) {
  8273. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8274. }
  8275. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8276. if (billing_phone.length) {
  8277. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8278. }
  8279. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8280. if (billing_fax.length) {
  8281. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8282. }
  8283. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8284. if (billing_email.length) {
  8285. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8286. }
  8287. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8288. if (returnto_cid.length) {
  8289. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8290. }
  8291. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8292. if (returnto_name.length) {
  8293. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8294. }
  8295. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8296. if (returnto_ext.length) {
  8297. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8298. }
  8299. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8300. if (returnto_contact.length) {
  8301. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8302. }
  8303. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8304. if (returnto_phone.length) {
  8305. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8306. }
  8307. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8308. if (returnto_fax.length) {
  8309. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8310. }
  8311. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8312. if (returnto_email.length) {
  8313. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8314. }
  8315. NSString *order_status = @"status = 1,";
  8316. if (submit) {
  8317. order_status = @"status = -11,";
  8318. }
  8319. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8320. NSString *sync_sql = @"";
  8321. if (submit) {
  8322. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8323. [param setValue:sales_rep forKey:@"sales_rep"];
  8324. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8325. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8326. }
  8327. 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];
  8328. DebugLog(@"save order contactSql: %@",contactSql);
  8329. DebugLog(@"save order orderSql: %@",orderSql);
  8330. // int contact_ret = [iSalesDB execSql:contactSql];
  8331. int order_ret = [iSalesDB execSql:orderSql];
  8332. int ret = order_ret;
  8333. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8334. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8335. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8336. // [dic setValue:@"160409" forKey:@"min_ver"];
  8337. [dic setObject:so_id forKey:@"so#"];
  8338. return [RAUtils dict2data:dic];
  8339. }
  8340. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8341. {
  8342. // id foo = nil;
  8343. // NSMutableArray *a = @[].mutableCopy;
  8344. // [a addObject:foo];
  8345. return [self saveorder:param submit:NO];
  8346. }
  8347. #pragma mark add to cart by name
  8348. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8349. {
  8350. NSString *orderCode = [params objectForKey:@"orderCode"];
  8351. NSString *product_name = [params objectForKey:@"product_name"];
  8352. product_name = [self translateSingleQuote:product_name];
  8353. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8354. sqlite3 *db = [iSalesDB get_db];
  8355. __block NSMutableString *product_id_string = [NSMutableString string];
  8356. for (int i = 0; i < product_name_array.count; i++) {
  8357. NSString *name = [product_name_array objectAtIndex:i];
  8358. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8359. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8360. int product_id = sqlite3_column_int(stmt, 0);
  8361. if (i == product_name_array.count - 1) {
  8362. [product_id_string appendFormat:@"%d",product_id];
  8363. } else {
  8364. [product_id_string appendFormat:@"%d,",product_id];
  8365. }
  8366. }];
  8367. }
  8368. NSDictionary *newParams = @{
  8369. @"product_id" : product_id_string,
  8370. @"orderCode" : orderCode
  8371. };
  8372. [iSalesDB close_db:db];
  8373. return [self offline_add2cart:[newParams mutableCopy]];
  8374. }
  8375. #pragma mark reset order
  8376. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8377. {
  8378. UIApplication * app = [UIApplication sharedApplication];
  8379. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8380. appDelegate.disable_trigger=true;
  8381. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8382. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8383. appDelegate.disable_trigger=false;
  8384. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8385. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8386. return [RAUtils dict2data:dic];
  8387. }
  8388. #pragma mark submit order
  8389. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8390. {
  8391. return [self saveorder:params submit:YES];
  8392. }
  8393. #pragma mark copy order
  8394. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8395. {
  8396. NSMutableDictionary *ret = @{}.mutableCopy;
  8397. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8398. sqlite3 *db = [iSalesDB get_db];
  8399. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8400. // 首先查看联系人是否active
  8401. 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];
  8402. __block int customer_is_active = 1;
  8403. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8404. customer_is_active = sqlite3_column_int(stmt, 0);
  8405. }];
  8406. if (!customer_is_active) {
  8407. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8408. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8409. [iSalesDB close_db:db];
  8410. return [RAUtils dict2data:ret];
  8411. }
  8412. // new order
  8413. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8414. NSString *new_order_code = [self get_offline_soid:db];
  8415. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8416. user = [self translateSingleQuote:user];
  8417. 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,sales_terms) 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,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8418. __block int result = 1;
  8419. result = [iSalesDB execSql:insert_order_sql db:db];
  8420. if (!result) {
  8421. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8422. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8423. [iSalesDB close_db:db];
  8424. return [RAUtils dict2data:ret];
  8425. }
  8426. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8427. // __block NSString *product_id = @"";
  8428. __weak typeof(self) weakSelf = self;
  8429. 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];
  8430. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8431. int is_active = sqlite3_column_int(stmt, 1);
  8432. if (is_active) {
  8433. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8434. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8435. 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];
  8436. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8437. }
  8438. }];
  8439. // product_id = [product_id substringFromIndex:1];
  8440. //
  8441. // [self offline_add2cart:@{}.mutableCopy];
  8442. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8443. [iSalesDB close_db:db];
  8444. if (result) {
  8445. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8446. } else {
  8447. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8448. }
  8449. [ret setObject:new_order_code forKey:@"so_id"];
  8450. return [RAUtils dict2data:ret];
  8451. }
  8452. #pragma mark move wish list to cart
  8453. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8454. NSString *collectId = params[@"collectId"];
  8455. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  8456. __block NSString *product_id = @"";
  8457. __block NSString *qty = @"";
  8458. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8459. int productId = sqlite3_column_int(stmt, 0);
  8460. int item_qty = sqlite3_column_int(stmt, 1);
  8461. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8462. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8463. }];
  8464. product_id = [product_id substringFromIndex:1];
  8465. qty = [qty substringFromIndex:1];
  8466. NSDictionary *newParams = @{
  8467. @"product_id" : product_id,
  8468. @"orderCode" : params[@"orderCode"],
  8469. @"qty" : qty
  8470. };
  8471. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8472. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8473. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8474. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  8475. //
  8476. // int ret = [iSalesDB execSql:deleteSQL];
  8477. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  8478. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8479. }
  8480. return [RAUtils dict2data:retDic];
  8481. }
  8482. #pragma mark - portfolio
  8483. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8484. int sort = [[params valueForKey:@"sort"] integerValue];
  8485. int offset = [[params valueForKey:@"offset"] integerValue];
  8486. int limit = [[params valueForKey:@"limit"] integerValue];
  8487. NSString *orderBy = @"";
  8488. switch (sort) {
  8489. case 0:{
  8490. orderBy = @"modify_time desc";
  8491. }
  8492. break;
  8493. case 1:{
  8494. orderBy = @"modify_time asc";
  8495. }
  8496. break;
  8497. case 2:{
  8498. orderBy = @"name asc";
  8499. }
  8500. break;
  8501. case 3:{
  8502. orderBy = @"name desc";
  8503. }
  8504. break;
  8505. case 4:{
  8506. orderBy = @"description asc";
  8507. }
  8508. break;
  8509. default:
  8510. break;
  8511. }
  8512. 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];
  8513. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8514. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8515. sqlite3 *db = [iSalesDB get_db];
  8516. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8517. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8518. int product_id = sqlite3_column_int(stmt, 0);
  8519. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8520. NSString *name = [self textAtColumn:1 statement:stmt];
  8521. NSString *description = [self textAtColumn:2 statement:stmt];
  8522. double price = sqlite3_column_double(stmt, 3);
  8523. double discount = sqlite3_column_double(stmt,4);
  8524. int qty = sqlite3_column_int(stmt, 5);
  8525. int percentage = sqlite3_column_int(stmt, 6);
  8526. int item_id = sqlite3_column_int(stmt, 7);
  8527. // int fashion_id = sqlite3_column_int(stmt, 8);
  8528. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8529. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8530. double percent = sqlite3_column_double(stmt, 11);
  8531. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8532. if ([price_null isEqualToString:@"null"]) {
  8533. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8534. }
  8535. NSMutableDictionary *item = @{
  8536. @"linenotes": line_note,
  8537. @"check": @(1),
  8538. @"product_id": product_id_string,
  8539. @"available_qty": @(qty),
  8540. @"available_percent" : @(percent),
  8541. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8542. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8543. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8544. @"img": img_path,
  8545. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8546. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8547. }.mutableCopy;
  8548. if (percentage) {
  8549. [item removeObjectForKey:@"available_qty"];
  8550. } else {
  8551. [item removeObjectForKey:@"available_percent"];
  8552. }
  8553. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8554. if ([qty_null isEqualToString:@"null"]) {
  8555. [item removeObjectForKey:@"available_qty"];
  8556. }
  8557. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8558. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8559. }];
  8560. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8561. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8562. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8563. }
  8564. [iSalesDB close_db:db];
  8565. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8566. [dic setValue:@"" forKey:@"email_content"];
  8567. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8568. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8569. return [RAUtils dict2data:dic];
  8570. }
  8571. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8572. __block int qty = 0;
  8573. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8574. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8575. qty = sqlite3_column_int(stmt, 0);
  8576. }];
  8577. return qty;
  8578. }
  8579. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8580. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8581. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8582. NSMutableDictionary * values = params[@"replaceValue"];
  8583. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8584. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8585. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8586. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8587. NSString *pdf_path = @"";
  8588. if (direct) {
  8589. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8590. } else {
  8591. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8592. }
  8593. NSString *create_user = [self valueInParams:params key:@"user"];
  8594. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8595. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8596. // model info
  8597. 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];
  8598. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8599. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8600. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8601. sqlite3 *db = [iSalesDB get_db];
  8602. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8603. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8604. int product_id = sqlite3_column_int(stmt, 0);
  8605. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8606. double price = sqlite3_column_double(stmt, 1);
  8607. double discount = sqlite3_column_double(stmt,2);
  8608. int qty = sqlite3_column_int(stmt, 3);
  8609. int percentage = sqlite3_column_int(stmt, 4);
  8610. int item_id = sqlite3_column_int(stmt, 5);
  8611. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8612. double percent = sqlite3_column_double(stmt, 7);
  8613. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8614. /* if ([price_null isEqualToString:@"null"]) {
  8615. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8616. }
  8617. */
  8618. [product_ids_string appendFormat:@"%@,",product_id_string];
  8619. // Regular Price
  8620. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8621. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8622. // QTY
  8623. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8624. if ([qty_null isEqualToString:@"null"]) {
  8625. // 查available
  8626. qty = [self model_QTY:product_id_string db:db];
  8627. }
  8628. if (percentage) {
  8629. qty = qty * percent / 100;
  8630. }
  8631. // Special Price
  8632. if ([price_null isEqualToString:@"null"]) {
  8633. // price = regular price
  8634. price = [regular_price_str doubleValue];
  8635. }
  8636. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8637. if (![discount_null isEqualToString:@"null"]) {
  8638. price = price * (1 - discount / 100.0);
  8639. }
  8640. NSMutableDictionary *item = @{
  8641. @"line_note": line_note,
  8642. @"product_id": product_id_string,
  8643. @"available_qty": @(qty),
  8644. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8645. @"regular_price" : regular_price_str,
  8646. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8647. }.mutableCopy;
  8648. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8649. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8650. }];
  8651. [iSalesDB close_db:db];
  8652. if (product_ids_string.length > 0) {
  8653. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8654. }
  8655. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8656. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8657. return [RAUtils dict2data:resultDictionary];
  8658. }
  8659. NSString *model_info = [RAUtils dict2string:dic];
  8660. // 创建PDF
  8661. // 在preview情况下保存,则不需要新建了
  8662. if (direct) {
  8663. NSMutableDictionary *tear_sheet_params = params;
  8664. // if (tear_sheet_id) {
  8665. // tear_sheet_params = values;
  8666. // }
  8667. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8668. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8669. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8670. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8671. resultDictionary = pdfInfo.mutableCopy;
  8672. } else { // 创建PDF失败
  8673. return pdfData;
  8674. }
  8675. } else {
  8676. // pdf_path 就是本地路径
  8677. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8678. }
  8679. // 将文件移动到PDF Cache文件夹
  8680. NSString *newPath = [pdf_path lastPathComponent];
  8681. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8682. NSString *cachefolder = [paths objectAtIndex:0];
  8683. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8684. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8685. NSFileManager *fileManager = [NSFileManager defaultManager];
  8686. NSError *error = nil;
  8687. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8688. if (error) { // 移动文件失败
  8689. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8690. return [RAUtils dict2data:resultDictionary];
  8691. }
  8692. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8693. pdf_path = [newPath lastPathComponent];
  8694. // 保存信息
  8695. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8696. NSString *off_params = [RAUtils dict2string:params];
  8697. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8698. pdf_path = [self translateSingleQuote:pdf_path];
  8699. create_user = [self translateSingleQuote:create_user];
  8700. tear_note = [self translateSingleQuote:tear_note];
  8701. tear_name = [self translateSingleQuote:tear_name];
  8702. model_info = [self translateSingleQuote:model_info];
  8703. configureParams = [self translateSingleQuote:configureParams];
  8704. off_params = [self translateSingleQuote:off_params];
  8705. 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];
  8706. if (tear_sheet_id) {
  8707. int _id = [tear_sheet_id integerValue];
  8708. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8709. }
  8710. int result = [iSalesDB execSql:save_pdf_sql];
  8711. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8712. //
  8713. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8714. if (remove_Item) {
  8715. // portfolioId
  8716. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8717. }
  8718. return [RAUtils dict2data:resultDictionary];
  8719. }
  8720. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8721. return [self offline_savePDF:params direct:YES];
  8722. }
  8723. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8724. return [self offline_savePDF:params direct:NO];
  8725. }
  8726. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8727. int offset = [[params valueForKey:@"offset"] integerValue];
  8728. int limit = [[params valueForKey:@"limit"] integerValue];
  8729. NSString *keyword = [params valueForKey:@"keyWord"];
  8730. NSString *where = @"where is_delete is null or is_delete = 0";
  8731. if (keyword.length) {
  8732. keyword = [self translateSingleQuote:keyword];
  8733. 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];
  8734. }
  8735. 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
  8736. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8737. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8738. NSString *cachefolder = [paths objectAtIndex:0];
  8739. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8740. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8741. NSString *name = [self textAtColumn:0 statement:stmt];
  8742. NSString *note = [self textAtColumn:1 statement:stmt];
  8743. NSString *time = [self textAtColumn:2 statement:stmt];
  8744. NSString *user = [self textAtColumn:3 statement:stmt];
  8745. NSString *path = [self textAtColumn:4 statement:stmt];
  8746. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8747. NSString *off_params = [self textAtColumn:7 statement:stmt];
  8748. path = [pdfFolder stringByAppendingPathComponent:path];
  8749. BOOL bdir=NO;
  8750. NSFileManager* fileManager = [NSFileManager defaultManager];
  8751. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8752. {
  8753. //pdf文件不存在
  8754. path=nil;
  8755. }
  8756. time = [self changeDateTimeFormate:time];
  8757. time = [time stringByAppendingString:@" PST"];
  8758. int sheet_id = sqlite3_column_int(stmt, 5);
  8759. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8760. item[@"tearsheetsId"]=@(sheet_id);
  8761. item[@"pdf_path"]=path;
  8762. item[@"create_time"]=time;
  8763. item[@"create_user"]=user;
  8764. item[@"tear_note"]=note;
  8765. item[@"tear_name"]=name;
  8766. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  8767. item[@"model_info"]=model_info;
  8768. item[@"off_params"]=off_params;
  8769. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8770. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8771. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8772. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8773. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8774. }];
  8775. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8776. return [RAUtils dict2data:dic];
  8777. }
  8778. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8779. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8780. // NSString *user = [params objectForKey:@"user"];
  8781. // 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];
  8782. 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];
  8783. int result = [iSalesDB execSql:sql];
  8784. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8785. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8786. if (result == RESULT_TRUE) {
  8787. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8788. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8789. sqlite3 *db = [iSalesDB get_db];
  8790. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8791. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8792. [iSalesDB close_db:db];
  8793. }
  8794. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8795. return [RAUtils dict2data:dic];
  8796. }
  8797. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8798. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8799. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8800. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8801. // Regurlar Price
  8802. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8803. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8804. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8805. __block long val_count = 0;
  8806. NSString *sql = @"select name,type,order_by from price order by order_by";
  8807. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8808. NSString *name = [self textAtColumn:0 statement:stmt];
  8809. int type = sqlite3_column_int(stmt, 1);
  8810. int order_by = sqlite3_column_int(stmt, 2);
  8811. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8812. NSDictionary *price_dic = @{
  8813. @"value" : name,
  8814. @"value_id" : [NSNumber numberWithInteger:type],
  8815. @"check" : order_by == 0 ? @(1) : @(0)
  8816. };
  8817. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8818. val_count = ++(*count);
  8819. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8820. val_count = 0;
  8821. }];
  8822. [cadedate setObject:@{@"value" : @"None",
  8823. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8824. val_count++;
  8825. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8826. [price setObject:cadedate forKey:@"cadedate"];
  8827. [section1 setObject:price forKey:@"item_2"];
  8828. [dic setObject:section1 forKey:@"section_1"];
  8829. return [RAUtils dict2data:dic];
  8830. }
  8831. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8832. NSString *product_id = [params objectForKey:@"fashionId"];
  8833. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8834. __block int result = RESULT_TRUE;
  8835. __block int qty = 0;
  8836. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8837. qty = sqlite3_column_int(stmt, 0);
  8838. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8839. result = RESULT_FALSE;
  8840. }];
  8841. NSMutableDictionary *dic = @{
  8842. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8843. @"mode" : @"Regular Mode",
  8844. @"quantity_available" : @(qty),
  8845. @"result" : @(result),
  8846. }.mutableCopy;
  8847. return [RAUtils dict2data:dic];
  8848. }
  8849. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8850. NSString *item_ids = [params objectForKey:@"item_id"];
  8851. NSString *line_notes = [params objectForKey:@"notes"];
  8852. NSString *price_str = [params objectForKey:@"price"];
  8853. NSString *discount_str = [params objectForKey:@"discount"];
  8854. NSString *percent = [params objectForKey:@"available_percent"];
  8855. NSString *qty = [params objectForKey:@"available_qty"];
  8856. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8857. int dot = 0;
  8858. if (line_notes) {
  8859. line_notes = [self translateSingleQuote:line_notes];
  8860. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8861. sql = [sql stringByAppendingString:line_notes];
  8862. dot = 1;
  8863. } else {
  8864. line_notes = @"";
  8865. }
  8866. if (price_str) {
  8867. if (dot) {
  8868. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8869. } else {
  8870. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8871. dot = 1;
  8872. }
  8873. sql = [sql stringByAppendingString:price_str];
  8874. } else {
  8875. price_str = @"";
  8876. }
  8877. if (discount_str) {
  8878. if (dot) {
  8879. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8880. } else {
  8881. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8882. dot = 1;
  8883. }
  8884. sql = [sql stringByAppendingString:discount_str];
  8885. } else {
  8886. discount_str = @"";
  8887. }
  8888. if (percent) {
  8889. if (dot) {
  8890. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8891. } else {
  8892. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8893. dot = 1;
  8894. }
  8895. sql = [sql stringByAppendingString:percent];
  8896. } else {
  8897. percent = @"";
  8898. }
  8899. if (qty) {
  8900. if (dot) {
  8901. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8902. } else {
  8903. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8904. dot = 1;
  8905. }
  8906. sql = [sql stringByAppendingString:qty];
  8907. } else {
  8908. qty = @"";
  8909. }
  8910. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8911. sql = [sql stringByAppendingString:where];
  8912. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8913. int result = [iSalesDB execSql:sql];
  8914. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8915. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8916. return [RAUtils dict2data:dic];
  8917. }
  8918. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8919. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8920. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8921. sqlite3 *db = [iSalesDB get_db];
  8922. 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];
  8923. int result = [iSalesDB execSql:sql db:db];
  8924. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8925. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8926. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8927. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8928. [iSalesDB close_db:db];
  8929. return [RAUtils dict2data:dic];
  8930. }
  8931. + (void)offline_removePDFWithName:(NSString *)name {
  8932. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8933. NSString *cachefolder = [paths objectAtIndex:0];
  8934. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8935. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8936. NSFileManager *fileManager = [NSFileManager defaultManager];
  8937. [fileManager removeItemAtPath:path error:nil];
  8938. }
  8939. + (void)offline_clear_PDFCache {
  8940. NSFileManager *fileManager = [NSFileManager defaultManager];
  8941. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8942. NSString *cachefolder = [paths objectAtIndex:0];
  8943. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8944. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8945. for (NSString *path in pdf_files) {
  8946. [self offline_removePDFWithName:[path lastPathComponent]];
  8947. }
  8948. }
  8949. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8950. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8951. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8952. NSString *user = [params objectForKey:@"user"];
  8953. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8954. if (![create_user isEqualToString:user]) {
  8955. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8956. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8957. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8958. return [RAUtils dict2data:dic];
  8959. }
  8960. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8961. __block int is_local = 0;
  8962. __block NSString *path = @"";
  8963. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8964. is_local = sqlite3_column_int(stmt, 0);
  8965. path = [self textAtColumn:1 statement:stmt];
  8966. }];
  8967. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8968. if (is_local == 1) {
  8969. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8970. // 删除文件
  8971. [self offline_removePDFWithName:path];
  8972. }
  8973. int result = [iSalesDB execSql:sql];
  8974. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8975. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8976. return [RAUtils dict2data:dic];
  8977. }
  8978. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8979. {
  8980. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8981. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8982. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8983. NSString* where=@"1=1";
  8984. if (ver!=nil) {
  8985. where=@"is_dirty=1";
  8986. }
  8987. 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];
  8988. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8989. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8990. sqlite3 *db = [iSalesDB get_db];
  8991. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8992. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8993. NSInteger _id = sqlite3_column_int(stmt, 0);
  8994. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8995. NSString *name = [self textAtColumn:2 statement:stmt];
  8996. NSString *desc = [self textAtColumn:3 statement:stmt];
  8997. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8998. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8999. NSInteger qty = sqlite3_column_int(stmt, 6);
  9000. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9001. double percent = sqlite3_column_double(stmt, 8);
  9002. double price = sqlite3_column_double(stmt, 9);
  9003. double discount = sqlite3_column_double(stmt, 10);
  9004. NSString *img = [self textAtColumn:11 statement:stmt];
  9005. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9006. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9007. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9008. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9009. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9010. if ([price_null isEqualToString:@"null"]) {
  9011. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9012. }
  9013. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9014. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9015. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9016. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9017. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9018. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9019. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9020. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9021. if ([qty_null isEqualToString:@"null"]) {
  9022. // [item setValue:@"null" forKey:@"available_qty"];
  9023. } else {
  9024. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9025. }
  9026. if ([is_percent_null isEqualToString:@"null"]) {
  9027. // [item setValue:@"null" forKey:@"percentage"];
  9028. } else {
  9029. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9030. }
  9031. if ([percent_null isEqualToString:@"null"]) {
  9032. // [item setValue:@"null" forKey:@"percent"];
  9033. } else {
  9034. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9035. }
  9036. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9037. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9038. [item setValue:name forKey:@"name"];
  9039. [item setValue:desc forKey:@"description"];
  9040. [item setValue:img forKey:@"img"];
  9041. [item setValue:line_note forKey:@"line_note"];
  9042. [item setValue:create_time forKey:@"createtime"];
  9043. [item setValue:modify_time forKey:@"modifytime"];
  9044. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9045. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9046. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9047. }];
  9048. [iSalesDB close_db:db];
  9049. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9050. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9051. }
  9052. return ret;
  9053. }
  9054. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9055. {
  9056. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9057. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9058. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9059. NSString* where=@"1=1";
  9060. if (ver!=nil) {
  9061. where=@"is_dirty=1";
  9062. }
  9063. 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];
  9064. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9065. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9066. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9067. NSInteger _id = sqlite3_column_int(stmt, 0);
  9068. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9069. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9070. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9071. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9072. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9073. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9074. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9075. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9076. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9077. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9078. NSString *uuid = [NSUUID UUID].UUIDString;
  9079. int is_delete = sqlite3_column_int(stmt, 11);
  9080. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9081. int is_local = sqlite3_column_int(stmt, 12);
  9082. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9083. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9084. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9085. }
  9086. [item setObject:pdf_path forKey:@"pdf_path"];
  9087. [item setObject:create_user forKey:@"create_user"];
  9088. [item setObject:tear_note forKey:@"tear_note"];
  9089. [item setObject:tear_name forKey:@"tear_name"];
  9090. [item setObject:model_info forKey:@"model_info"];
  9091. [item setObject:createtime forKey:@"createtime"];
  9092. [item setObject:modifytime forKey:@"modifytime"];
  9093. [item setObject:urlParams forKey:@"urlParams"];
  9094. [item setObject:off_params forKey:@"off_params"];
  9095. [item setObject:uuid forKey:@"pdf_token"];
  9096. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9097. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9098. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9099. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9100. } else {
  9101. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9102. }
  9103. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9104. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9105. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9106. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9107. }];
  9108. return ret;
  9109. }
  9110. @end