OLDataProvider.m 482 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  57. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. NSMutableDictionary* values=params[@"replaceValue"];
  83. //生成portfolio pdf需要的数据
  84. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  85. data[@"npd_url"]=@"www.newpacificdirect.com";
  86. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  87. [formatter setDateFormat:@"MMMM yyyy"];
  88. NSString* date = [formatter stringFromDate:[NSDate date]];
  89. data[@"create_month"]=date;
  90. data[@"company_name"]=@"New Pacific Direct,Inc.";
  91. data[@"catalog_name"]=params[@"catalog_name"];
  92. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  93. data[@"TOTAL_PAGE"]=0;
  94. data[@"CURRENT_PAGE"]=0;
  95. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  96. bool show_stockout=[params[@"show_stockout"] boolValue];
  97. NSString *sql = nil;
  98. if(values==nil)
  99. {
  100. 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"]];
  101. }
  102. else
  103. {
  104. 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"]];
  105. }
  106. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  107. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  108. sqlite3 *db = [iSalesDB get_db];
  109. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  110. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  111. int product_id = sqlite3_column_int(stmt, 0);
  112. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  113. NSString *name = [self textAtColumn:1 statement:stmt];
  114. NSString *description = [self textAtColumn:2 statement:stmt];
  115. double price = sqlite3_column_double(stmt, 3);
  116. NSString *s_price = [self textAtColumn:3 statement:stmt];
  117. double discount = sqlite3_column_double(stmt,4);
  118. int qty = sqlite3_column_int(stmt, 5);
  119. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  120. int is_percentage = sqlite3_column_int(stmt, 6);
  121. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  122. int item_id = sqlite3_column_int(stmt, 7);
  123. // int fashion_id = sqlite3_column_int(stmt, 8);
  124. NSString *line_note = [self textAtColumn:9 statement:stmt];
  125. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  126. double percent = sqlite3_column_double(stmt, 10);
  127. // int portfolio_id = sqlite3_column_int(stmt, 11);
  128. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  129. int availability = sqlite3_column_int(stmt, 12);
  130. NSString *color = [self textAtColumn:13 statement:stmt];
  131. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  132. NSString *demension = [self textAtColumn:15 statement:stmt];
  133. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  134. NSString *material = [self textAtColumn:17 statement:stmt];
  135. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  136. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  137. double volume = sqlite3_column_double(stmt, 19);
  138. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  139. double weight = sqlite3_column_double(stmt, 20);
  140. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  141. int model_set = sqlite3_column_int(stmt, 21);
  142. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  143. double load_ability = sqlite3_column_double(stmt, 22);
  144. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  145. NSString *assembling = [self textAtColumn:24 statement:stmt];
  146. NSString *made_in = [self textAtColumn:25 statement:stmt];
  147. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  148. if(is_percentage==0&&qty==0&&!show_stockout)
  149. {
  150. return;
  151. }
  152. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  153. {
  154. return;
  155. }
  156. if(s_percent.length==0||is_percentage==0)
  157. {
  158. percent=100.0;
  159. if([s_qty isEqualToString:@"null"])
  160. qty=availability;
  161. }
  162. else
  163. {
  164. qty=availability;
  165. }
  166. qty=qty*percent/100+0.5;
  167. for(int i=0;i<[values[@"count"] intValue];i++)
  168. {
  169. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  170. if([valueitem[@"product_id"] intValue]==product_id)
  171. {
  172. line_note=valueitem[@"line_note"];
  173. qty=[valueitem[@"available_qty"] intValue];
  174. if([[valueitem allKeys] containsObject:@"available_qty"])
  175. s_qty=[NSString stringWithFormat:@"%d",qty];
  176. else
  177. s_qty=@"null";
  178. if(valueitem[@"regular_price"]!=nil)
  179. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  180. s_price=valueitem[@"special_price"];
  181. price=[valueitem[@"special_price"] floatValue];
  182. discount=0;
  183. }
  184. }
  185. NSString* set_price=@"";
  186. if([params[@"entered_price"] boolValue])
  187. {
  188. if (s_price==nil ) {
  189. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  190. }
  191. else
  192. {
  193. if(price*(1-discount/100.0)!=gprice.floatValue)
  194. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  195. }
  196. }
  197. NSString* get_price=@"";
  198. {
  199. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  200. // DebugLog(@"price time interval");
  201. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  202. if(gprice==nil)
  203. get_price=@"Price:No Price.";
  204. else
  205. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  206. }
  207. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  208. // if ([qty_null isEqualToString:@"null"]) {
  209. // qty=availability;
  210. // }
  211. //
  212. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  213. // if ([qty_percent_null isEqualToString:@"null"]) {
  214. // percentage=1;
  215. // }
  216. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  217. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  218. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  219. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  220. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  221. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  222. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  223. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  224. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  225. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  226. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  227. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  228. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  229. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  230. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  231. if([params[@"availability"] boolValue]==false)
  232. str_availability=@"";
  233. else
  234. {
  235. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  236. str_availability=@"<b>Availability:</b> In Production";
  237. }
  238. if([params[@"color"] boolValue]==false || color.length==0 )
  239. str_color=@"";
  240. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  241. str_model_set=@"";
  242. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  243. str_legcolor=@"";
  244. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  245. str_demension=@"";
  246. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  247. str_seat_height=@"";
  248. if([params[@"material"] boolValue]==false || material.length==0 )
  249. str_meterial=@"";
  250. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  251. str_box_dim=@"";
  252. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  253. str_volume=@"";
  254. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  255. str_weight=@"";
  256. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  257. str_load_ability=@"";
  258. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  259. str_fabric_content=@"";
  260. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  261. str_assembling=@"";
  262. if([params[@"made"] boolValue]==false || made_in.length==0 )
  263. str_made_in=@"";
  264. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  265. str_line_note=@"";
  266. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  267. [arr_detail addObject:str_availability];
  268. [arr_detail addObject:str_color];
  269. [arr_detail addObject:str_model_set];
  270. [arr_detail addObject:str_legcolor];
  271. [arr_detail addObject:str_demension];
  272. [arr_detail addObject:str_seat_height];
  273. [arr_detail addObject:str_meterial];
  274. [arr_detail addObject:str_box_dim];
  275. [arr_detail addObject:str_volume];
  276. [arr_detail addObject:str_weight];
  277. [arr_detail addObject:str_load_ability];
  278. [arr_detail addObject:str_fabric_content];
  279. [arr_detail addObject:str_assembling];
  280. [arr_detail addObject:str_made_in];
  281. [arr_detail addObject:str_line_note];
  282. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  283. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  284. //model image;
  285. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  286. // qr image
  287. NSString* qrpath=nil;
  288. if([params[@"show_barcode"] boolValue])
  289. {
  290. NSString* temp = NSTemporaryDirectory();
  291. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  292. qrpath=[temp stringByAppendingPathComponent:filename];
  293. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  294. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  295. }
  296. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  297. item[@"img"]=img_path;
  298. item[@"img_qr"]=qrpath;
  299. item[@"special_price"]=set_price;
  300. item[@"price"]=get_price;
  301. item[@"name"]=name;
  302. item[@"description"]=description;
  303. item[@"detail"]=detail;
  304. item[@"img"]=img_path;
  305. // @{
  306. // //@"linenotes": line_note,
  307. //// @"product_id": product_id_string,
  308. //// @"available_qty": @(qty),
  309. //// @"available_percent" : @(percent),
  310. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  311. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  312. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  313. // @"": ,
  314. // @"": ,
  315. // @"": ,
  316. // @"": ,
  317. // @"": ,
  318. // @"": ,
  319. // @"":
  320. // }.mutableCopy;
  321. // if (percentage) {
  322. // [item removeObjectForKey:@"available_qty"];
  323. // } else {
  324. // [item removeObjectForKey:@"available_percent"];
  325. // }
  326. //
  327. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  328. // if ([qty_null isEqualToString:@"null"]) {
  329. // [item removeObjectForKey:@"available_qty"];
  330. // }
  331. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  332. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  333. }];
  334. [iSalesDB close_db:db];
  335. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  336. // [dic setValue:@"" forKey:@"email_content"];
  337. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  338. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  339. //
  340. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  341. // cell[@"count"]=[NSNumber numberWithInt:10];
  342. grid[@"cell0"]=dic;
  343. data[@"grid0"]=grid;
  344. return data;
  345. }
  346. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  347. {
  348. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  349. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  350. NSString *cache_folder=[paths objectAtIndex:0];
  351. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  352. BOOL bdir=NO;
  353. NSFileManager* fileManager = [NSFileManager defaultManager];
  354. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir])
  355. {
  356. NSError * error=nil;
  357. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  358. {
  359. return nil;
  360. }
  361. }
  362. NSData *data = [NSData dataWithContentsOfFile:template_path];
  363. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  364. return ret;
  365. }
  366. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  367. {
  368. /*
  369. DebugLog(@"offline oderdetail params: %@",params);
  370. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  371. int orderId = [params[@"orderId"] intValue];
  372. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  373. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  374. sqlite3 *db = [iSalesDB get_db];
  375. sqlite3_stmt * statement;
  376. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  377. NSString *nssoid = @"";
  378. NSString* orderinfo = @"";
  379. NSString *moreInfo = @"";
  380. double handlingFee = 0;
  381. double payments_and_credist = 0;
  382. double totalPrice = 0;
  383. double shippingFee = 0;
  384. double lift_gate = 0;
  385. NSString *customer_contact = @"";
  386. NSString *customer_email = @"";
  387. NSString *customer_fax = @"";
  388. NSString *customer_phone = @"";
  389. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  390. {
  391. if (sqlite3_step(statement) == SQLITE_ROW)
  392. {
  393. int order_id = sqlite3_column_int(statement, 0);
  394. NSString *sign_url = [self textAtColumn:51 statement:statement];
  395. ret[@"sign_url"] = sign_url;
  396. customer_contact = [self textAtColumn:52 statement:statement];
  397. customer_email = [self textAtColumn:53 statement:statement];
  398. customer_phone = [self textAtColumn:54 statement:statement];
  399. customer_fax = [self textAtColumn:55 statement:statement];
  400. int offline_edit=sqlite3_column_int(statement, 56);
  401. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  402. char *soid = (char*)sqlite3_column_text(statement, 1);
  403. if(soid==nil)
  404. soid= "";
  405. nssoid= [[NSString alloc]initWithUTF8String:soid];
  406. // so#
  407. ret[@"so#"] = nssoid;
  408. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  409. if(poNumber==nil)
  410. poNumber= "";
  411. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  412. char *create_time = (char*)sqlite3_column_text(statement, 3);
  413. if(create_time==nil)
  414. create_time= "";
  415. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  416. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  417. int status = sqlite3_column_int(statement, 4);
  418. int erpStatus = sqlite3_column_int(statement, 49);
  419. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  420. // status
  421. if (status > 1 && status != 3) {
  422. status = erpStatus;
  423. } else if (status == 3) {
  424. status = 15;
  425. }
  426. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  427. ret[@"order_status"] = nsstatus;
  428. char *company_name = (char*)sqlite3_column_text(statement, 5);
  429. if(company_name==nil)
  430. company_name= "";
  431. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  432. // company name
  433. ret[@"company_name"] = nscompany_name;
  434. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  435. if(customer_contact==nil)
  436. customer_contact= "";
  437. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  438. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  439. if(addr_1==nil)
  440. addr_1="";
  441. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  442. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  443. if(addr_2==nil)
  444. addr_2="";
  445. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  446. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  447. if(addr_3==nil)
  448. addr_3="";
  449. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  450. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  451. if(addr_4==nil)
  452. addr_4="";
  453. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  454. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  455. [arr_addr addObject:nsaddr_1];
  456. [arr_addr addObject:nsaddr_2];
  457. [arr_addr addObject:nsaddr_3];
  458. [arr_addr addObject:nsaddr_4];
  459. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  460. char *logist = (char*)sqlite3_column_text(statement, 11);
  461. if(logist==nil)
  462. logist= "";
  463. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  464. if (status == -11 || status == 10 || status == 11) {
  465. nslogist = [self textAtColumn:59 statement:statement];
  466. };
  467. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  468. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  469. shipping = @"Shipping To Be Quoted";
  470. } else {
  471. shippingFee = sqlite3_column_double(statement, 12);
  472. }
  473. // Shipping
  474. ret[@"Shipping"] = shipping;
  475. int have_lift_gate = sqlite3_column_int(statement, 17);
  476. lift_gate = sqlite3_column_double(statement, 13);
  477. // Liftgate Fee(No loading dock)
  478. if (!have_lift_gate) {
  479. lift_gate = 0;
  480. }
  481. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  482. if (sqlite3_column_int(statement, 57)) {
  483. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  484. }
  485. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  486. if(general_notes==nil)
  487. general_notes= "";
  488. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  489. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  490. if(internal_notes==nil)
  491. internal_notes= "";
  492. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  493. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  494. if(payment_type==nil)
  495. payment_type= "";
  496. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  497. // order info
  498. orderinfo = [self textFileName:@"order_info.html"];
  499. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  500. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  501. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  502. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  503. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  504. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  505. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  506. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  507. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  508. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  509. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  510. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  511. NSString *payment = nil;
  512. if([nspayment_type isEqualToString:@"Credit Card"])
  513. {
  514. payment = [self textFileName:@"creditcardpayment.html"];
  515. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  516. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  517. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  518. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  519. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  520. NSString *card_type = [self textAtColumn:42 statement:statement];
  521. if (card_type.length > 0) { // 显示星号
  522. card_type = @"****";
  523. }
  524. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  525. if (card_number.length > 0 && card_number.length > 4) {
  526. for (int i = 0; i < card_number.length - 4; i++) {
  527. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  528. }
  529. } else {
  530. card_number = @"";
  531. }
  532. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  533. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  534. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  535. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  536. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  537. card_expiration = @"****";
  538. }
  539. NSString *card_city = [self textAtColumn:46 statement:statement];
  540. NSString *card_state = [self textAtColumn:47 statement:statement];
  541. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  542. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  543. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  544. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  545. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  546. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  547. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  548. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  549. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  550. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  551. }
  552. else
  553. {
  554. payment=[self textFileName:@"normalpayment.html"];
  555. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  556. }
  557. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  558. ret[@"result"]= [NSNumber numberWithInt:2];
  559. // more info
  560. moreInfo = [self textFileName:@"more_info.html"];
  561. // ShipToCompany_or_&nbsp
  562. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  563. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  564. NSString *shipToName = [self textAtColumn:19 statement:statement];
  565. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  566. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  567. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  568. // ShipFromCompany_or_&nbsp
  569. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  570. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  571. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  572. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  573. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  574. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  575. // FreightBillToCompany_or_&nbsp
  576. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  577. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  578. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  579. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  580. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  581. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  582. // MerchandiseBillToCompany_or_&nbsp
  583. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  584. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  585. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  586. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  587. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  588. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  589. // ReturnToCompany_or_&nbsp
  590. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  591. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  592. NSString *returnToName = [self textAtColumn:31 statement:statement];
  593. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  594. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  595. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  596. //
  597. // DebugLog(@"more info : %@",moreInfo);
  598. // handling fee
  599. handlingFee = sqlite3_column_double(statement, 33);
  600. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  601. if (sqlite3_column_int(statement, 58)) {
  602. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  603. }
  604. //
  605. // customer info
  606. customerID = [self textAtColumn:36 statement:statement];
  607. // mode
  608. ret[@"mode"] = appDelegate.mode;
  609. // model_count
  610. ret[@"model_count"] = @(0);
  611. }
  612. sqlite3_finalize(statement);
  613. }
  614. [iSalesDB close_db:db];
  615. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  616. // "customer_email" = "Shui Hu";
  617. // "customer_fax" = "";
  618. // "customer_first_name" = F;
  619. // "customer_last_name" = L;
  620. // "customer_name" = ",da He Xiang Dong Liu A";
  621. // "customer_phone" = "Hey Xuan Feng";
  622. contactInfo[@"customer_phone"] = customer_phone;
  623. contactInfo[@"customer_fax"] = customer_fax;
  624. contactInfo[@"customer_email"] = customer_email;
  625. NSString *first_name = @"";
  626. NSString *last_name = @"";
  627. if ([customer_contact isEqualToString:@""]) {
  628. } else if ([customer_contact containsString:@" "]) {
  629. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  630. first_name = [customer_contact substringToIndex:first_space_index];
  631. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  632. }
  633. contactInfo[@"customer_first_name"] = first_name;
  634. contactInfo[@"customer_last_name"] = last_name;
  635. ret[@"customerInfo"] = contactInfo;
  636. // models
  637. if (nssoid) {
  638. __block double TotalCuft = 0;
  639. __block double TotalWeight = 0;
  640. __block int TotalCarton = 0;
  641. __block double allItemPrice = 0;
  642. 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];
  643. sqlite3 *db1 = [iSalesDB get_db];
  644. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  645. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  646. int product_id = sqlite3_column_int(stmt, 0);
  647. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  648. int item_id = sqlite3_column_int(stmt, 7);
  649. NSString* Price=nil;
  650. if(str_price==nil)
  651. {
  652. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  653. if(price==nil)
  654. Price=@"No Price.";
  655. else
  656. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  657. }
  658. else
  659. {
  660. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  661. }
  662. double discount = sqlite3_column_double(stmt, 2);
  663. int item_count = sqlite3_column_int(stmt, 3);
  664. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  665. NSString *nsline_note=nil;
  666. if(line_note!=nil)
  667. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  668. // char *name = (char*)sqlite3_column_text(stmt, 5);
  669. // NSString *nsname = nil;
  670. // if(name!=nil)
  671. // nsname= [[NSString alloc]initWithUTF8String:name];
  672. NSString *nsname = [self textAtColumn:5 statement:stmt];
  673. // char *description = (char*)sqlite3_column_text(stmt, 6);
  674. // NSString *nsdescription=nil;
  675. // if(description!=nil)
  676. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  677. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  678. // int stockUom = sqlite3_column_int(stmt, 8);
  679. // int _id = sqlite3_column_int(stmt, 9);
  680. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  681. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  682. double weight=[bsubtotaljson[@"weight"] doubleValue];
  683. int carton=[bsubtotaljson[@"carton"] intValue];
  684. TotalCuft += cuft;
  685. TotalWeight += weight;
  686. TotalCarton += carton;
  687. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  688. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  689. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  690. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  691. itemjson[@"note"]=nsline_note;
  692. itemjson[@"origin_price"] = Price;
  693. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  694. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  695. itemjson[@"order_item_status"] = @""; // 暂时不处理
  696. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  697. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  698. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  699. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  700. if(itemjson[@"combine"] != nil)
  701. {
  702. // int citem=0;
  703. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  704. for(int bc=0;bc<bcount;bc++)
  705. {
  706. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  707. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  708. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  709. subTotal += uprice * modulus * item_count;
  710. }
  711. }
  712. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  713. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  714. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  715. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  716. allItemPrice += subTotal;
  717. }];
  718. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  719. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  720. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  721. // payments/Credits
  722. // payments_and_credist = sqlite3_column_double(statement, 34);
  723. payments_and_credist = allItemPrice;
  724. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  725. // // total
  726. // totalPrice = sqlite3_column_double(statement, 35);
  727. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  728. } else {
  729. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  730. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  731. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  732. // payments/Credits
  733. payments_and_credist = 0;
  734. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  735. }
  736. // total
  737. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  738. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  739. ret[@"order_info"]= orderinfo;
  740. ret[@"more_order_info"] = moreInfo;
  741. return [RAUtils dict2data:ret];
  742. */
  743. return nil;
  744. }
  745. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  746. {
  747. NSString* template_file=nil;
  748. switch([params[@"pdf_style"] intValue])
  749. {
  750. case 0:
  751. template_file= @"so.json";
  752. break;
  753. case 1:
  754. template_file= @"so_thumb.json";
  755. break;
  756. default:
  757. template_file= @"so.json";
  758. }
  759. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"pdf_style"] intValue]];
  760. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  761. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  762. DebugLog(@"%@",file);
  763. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  764. [dic setObject:@"Regular Mode" forKey:@"mode"];
  765. if (file) {
  766. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  767. [dic setObject:file forKey:@"pdf_path"];
  768. dic[@"isLocalFile"]=@"true";
  769. return [RAUtils dict2data:dic];
  770. }
  771. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  772. dic[@"isLocalFile"]=@"true";
  773. return [RAUtils dict2data:dic];
  774. }
  775. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  776. {
  777. NSString* template_file=nil;
  778. switch([params[@"pdf_style"] intValue])
  779. {
  780. case 0:
  781. template_file= @"portfolio_2x3.json";
  782. break;
  783. case 1:
  784. template_file= @"portfolio_3x2.json";
  785. break;
  786. default:
  787. template_file= @"portfolio_3x2.json";
  788. }
  789. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  790. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  791. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  792. DebugLog(@"%@",file);
  793. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  794. [dic setObject:@"Regular Mode" forKey:@"mode"];
  795. if (file) {
  796. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  797. [dic setObject:file forKey:@"pdf_path"];
  798. dic[@"isLocalFile"]=@"true";
  799. return [RAUtils dict2data:dic];
  800. }
  801. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  802. dic[@"isLocalFile"]=@"true";
  803. return [RAUtils dict2data:dic];
  804. }
  805. +(NSString*) get_offline_soid:(sqlite3*)db
  806. {
  807. NSString* soid=nil;
  808. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  809. [formatter setDateFormat:@"yyMMdd"];
  810. NSString* date = [formatter stringFromDate:[NSDate date]];
  811. for(int i=1;i<999;i++)
  812. {
  813. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  814. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  815. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  816. if(count==0)
  817. return soid;
  818. }
  819. return nil;
  820. }
  821. +(NSArray*) enumOfflineOrder
  822. {
  823. NSMutableArray* ret = [[NSMutableArray alloc] init];
  824. sqlite3 *db = [iSalesDB get_db];
  825. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  826. sqlite3_stmt * statement;
  827. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  828. if ( dbresult== SQLITE_OK)
  829. {
  830. while (sqlite3_step(statement) == SQLITE_ROW)
  831. {
  832. char *so_id = (char*)sqlite3_column_text(statement, 0);
  833. NSString* nsso_id=nil;
  834. if(so_id!=nil)
  835. {
  836. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  837. }
  838. [ret addObject:nsso_id];
  839. }
  840. sqlite3_finalize(statement);
  841. }
  842. [iSalesDB close_db:db];
  843. return ret;
  844. }
  845. +(void) uploadFile:(NSString*) file
  846. {
  847. NSData* data = [NSData dataWithContentsOfFile: file];
  848. UIApplication * app = [UIApplication sharedApplication];
  849. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  850. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  851. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  852. if(appDelegate.user!=nil)
  853. [params setValue:appDelegate.user forKey:@"user"];
  854. // if(appDelegate.contact_id!=nil)
  855. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  856. if(appDelegate.password!=nil)
  857. [params setValue:appDelegate.password forKey:@"password"];
  858. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  859. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  860. } error:nil];
  861. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  862. NSProgress *progress = nil;
  863. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  864. if (error) {
  865. NSString* err_msg = [error localizedDescription];
  866. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  867. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  868. NSLog(@"data string: %@",str);
  869. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  870. } else {
  871. NSLog(@"response ");
  872. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  873. // 再将NSData转为字符串
  874. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  875. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  876. NSLog(@"data string: %@",jsonStr);
  877. NSDictionary* json = responseObject;
  878. if([[json valueForKey:@"result"] intValue]==2)
  879. {
  880. // NSString* img_url_down = json[@"img_url_aname"];
  881. // NSString* img_url_up = json[@"img_url"];
  882. }
  883. else
  884. {
  885. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  886. }
  887. }
  888. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  889. }];
  890. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  891. //
  892. [uploadTask resume];
  893. }
  894. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  895. {
  896. NSString* serial= [[NSUUID UUID] UUIDString];
  897. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  898. NSString *cachefolder = [paths objectAtIndex:0];
  899. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  900. upfolder = [upfolder stringByAppendingPathComponent:serial];
  901. NSFileManager* fileManager = [NSFileManager defaultManager];
  902. BOOL bdir=YES;
  903. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  904. {
  905. NSError *error = nil;
  906. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  907. if(!bsuccess)
  908. DebugLog(@"Create UPLOAD folder failed");
  909. }
  910. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  911. ret[@"contact"]=[self prepareContact:serial];
  912. ret[@"wishlist"]=[self prepareWishlist:serial];
  913. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  914. ret[@"portfolio"] = [self preparePortfolio:serial];
  915. ret[@"view_portfolio"] = [self preparePDF:serial];
  916. NSString* str= [RAUtils dict2string:ret];
  917. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  918. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  919. NSError *error=nil;
  920. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  921. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  922. ZipArchive* zip = [[ZipArchive alloc] init];
  923. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  924. for(int i=0;i<arr_files.count;i++)
  925. {
  926. NSString* file=arr_files[i];
  927. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  928. }
  929. if( ![zip CloseZipFile2] )
  930. {
  931. zippath = @"";
  932. }
  933. ret[@"file"]=zippath;
  934. return ret;
  935. }
  936. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  937. {
  938. if(filename.length==0)
  939. return false;
  940. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  941. bool ret=false;
  942. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  943. NSString *cachefolder = [paths objectAtIndex:0];
  944. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  945. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  946. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  947. upfolder = [upfolder stringByAppendingPathComponent:serial];
  948. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  949. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  950. NSFileManager* fileManager = [NSFileManager defaultManager];
  951. BOOL bdir=NO;
  952. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  953. {
  954. NSError *error = nil;
  955. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  956. {
  957. ret=false;
  958. }
  959. else
  960. {
  961. ret=true;
  962. }
  963. }
  964. return ret;
  965. }
  966. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  967. {
  968. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  969. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  970. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  971. NSString* where=@" 1=1";
  972. if (ver!=nil) {
  973. where=@"is_dirty=1";
  974. }
  975. sqlite3 *db = [iSalesDB get_db];
  976. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  977. sqlite3_stmt * statement;
  978. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  979. int count=0;
  980. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  981. if ( dbresult== SQLITE_OK)
  982. {
  983. while (sqlite3_step(statement) == SQLITE_ROW)
  984. {
  985. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  986. int _id = sqlite3_column_int(statement, 0);
  987. int product_id = sqlite3_column_int(statement, 1);
  988. int item_id = sqlite3_column_int(statement, 2);
  989. int qty = sqlite3_column_int(statement, 3);
  990. char *create_time = (char*)sqlite3_column_text(statement, 4);
  991. NSString* nscreate_time=nil;
  992. if(create_time!=nil)
  993. {
  994. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  995. }
  996. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  997. NSString* nsmodify_time=nil;
  998. if(modify_time!=nil)
  999. {
  1000. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1001. }
  1002. int is_delete = sqlite3_column_int(statement, 6);
  1003. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1004. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1005. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1006. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1007. itemjson[@"create_time"]=nscreate_time;
  1008. itemjson[@"modify_time"]=nsmodify_time;
  1009. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1010. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1011. count++;
  1012. }
  1013. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1014. sqlite3_finalize(statement);
  1015. }
  1016. ret[@"count"]=[NSNumber numberWithInt:count ];
  1017. [iSalesDB close_db:db];
  1018. return ret;
  1019. }
  1020. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1021. {
  1022. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1023. UIApplication * app = [UIApplication sharedApplication];
  1024. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1025. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  1026. // 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 ];
  1027. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1028. DebugLog(@"offline_login sql:%@",sqlQuery);
  1029. sqlite3_stmt * statement;
  1030. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1031. int count=0;
  1032. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1033. if ( dbresult== SQLITE_OK)
  1034. {
  1035. while (sqlite3_step(statement) == SQLITE_ROW)
  1036. {
  1037. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1038. int product_id = sqlite3_column_int(statement, 0);
  1039. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1040. int item_id = sqlite3_column_int(statement, 7);
  1041. NSString* Price=nil;
  1042. if(str_price==nil)
  1043. {
  1044. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1045. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1046. if(price==nil)
  1047. Price=@"No Price.";
  1048. else
  1049. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1050. }
  1051. else
  1052. {
  1053. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1054. }
  1055. double discount = sqlite3_column_double(statement, 2);
  1056. int item_count = sqlite3_column_int(statement, 3);
  1057. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1058. NSString *nsline_note=nil;
  1059. if(line_note!=nil)
  1060. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1061. char *name = (char*)sqlite3_column_text(statement, 5);
  1062. NSString *nsname=nil;
  1063. if(name!=nil)
  1064. nsname= [[NSString alloc]initWithUTF8String:name];
  1065. char *description = (char*)sqlite3_column_text(statement, 6);
  1066. NSString *nsdescription=nil;
  1067. if(description!=nil)
  1068. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1069. // int stockUom = sqlite3_column_int(statement, 8);
  1070. // int _id = sqlite3_column_int(statement, 9);
  1071. //
  1072. //
  1073. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1074. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1075. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1076. // int carton=[bsubtotaljson[@"carton"] intValue];
  1077. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1078. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1079. itemjson[@"model"]=nsname;
  1080. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1081. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1082. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1083. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1084. // itemjson[@"check"]=@"true";
  1085. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1086. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1087. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1088. itemjson[@"unit_price"]=Price;
  1089. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1090. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1091. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1092. itemjson[@"note"]=nsline_note;
  1093. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1094. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1095. count++;
  1096. }
  1097. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1098. sqlite3_finalize(statement);
  1099. }
  1100. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1101. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1102. return ret;
  1103. }
  1104. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1105. {
  1106. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1107. sqlite3 *db = [iSalesDB get_db];
  1108. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1109. // for(int i=0;i<arr_soid.count;i++)
  1110. sqlite3_stmt * statement;
  1111. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1112. //int count=0;
  1113. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1114. if ( dbresult== SQLITE_OK)
  1115. {
  1116. int count=0;
  1117. while (sqlite3_step(statement) == SQLITE_ROW)
  1118. {
  1119. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1120. int _id = sqlite3_column_int(statement, 0);
  1121. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1122. NSString* nssync_data=nil;
  1123. if(sync_data!=nil)
  1124. {
  1125. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1126. }
  1127. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1128. NSString* nsimg_1=nil;
  1129. if(img_1!=nil)
  1130. {
  1131. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1132. [self copy_upImg:serial file:nsimg_1];
  1133. }
  1134. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1135. NSString* nsso_no=nil;
  1136. if(so_no!=nil)
  1137. {
  1138. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1139. }
  1140. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1141. itemjson[@"order_type"]=@"submit order";
  1142. else
  1143. itemjson[@"order_type"]=@"archive order";
  1144. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1145. itemjson[@"json_data"]= nssync_data;
  1146. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1147. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1148. count++;
  1149. }
  1150. ret[@"count"]=[NSNumber numberWithInt:count ];
  1151. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1152. }
  1153. sqlite3_finalize(statement);
  1154. [iSalesDB close_db:db];
  1155. return ret;
  1156. }
  1157. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1158. {
  1159. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1160. sqlite3 *db = [iSalesDB get_db];
  1161. // UIApplication * app = [UIApplication sharedApplication];
  1162. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1163. 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";
  1164. sqlite3_stmt * statement;
  1165. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1166. int count=0;
  1167. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1168. if ( dbresult== SQLITE_OK)
  1169. {
  1170. while (sqlite3_step(statement) == SQLITE_ROW)
  1171. {
  1172. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1173. int _id = sqlite3_column_int(statement, 0);
  1174. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1175. NSString* nsimg_0=nil;
  1176. if(img_0!=nil)
  1177. {
  1178. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1179. [self copy_upImg:serial file:nsimg_0];
  1180. }
  1181. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1182. NSString* nsimg_1=nil;
  1183. if(img_1!=nil)
  1184. {
  1185. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1186. [self copy_upImg:serial file:nsimg_1];
  1187. }
  1188. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1189. NSString* nsimg_2=nil;
  1190. if(img_2!=nil)
  1191. {
  1192. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1193. [self copy_upImg:serial file:nsimg_2];
  1194. }
  1195. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1196. NSString* nssync_data=nil;
  1197. if(sync_data!=nil)
  1198. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1199. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1200. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1201. itemjson[@"json_data"]= nssync_data;
  1202. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1203. count++;
  1204. }
  1205. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1206. sqlite3_finalize(statement);
  1207. }
  1208. ret[@"count"]=[NSNumber numberWithInt:count ];
  1209. [iSalesDB close_db:db];
  1210. return ret;
  1211. }
  1212. +(bool) check_offlinedata
  1213. {
  1214. UIApplication * app = [UIApplication sharedApplication];
  1215. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1216. sqlite3 *db = [iSalesDB get_db];
  1217. NSString * where=@"1=1";
  1218. // if(appDelegate.user!=nil)
  1219. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1220. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1221. [iSalesDB close_db:db];
  1222. if(count==0)
  1223. {
  1224. return false;
  1225. }
  1226. return true;
  1227. //
  1228. //[iSalesDB close_db:db];
  1229. }
  1230. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1231. {
  1232. UIApplication * app = [UIApplication sharedApplication];
  1233. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1234. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1235. sqlite3 *db = [iSalesDB get_db];
  1236. NSString* collectId=params[@"collectId"];
  1237. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1238. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1239. [iSalesDB execSql:sqlQuery db:db];
  1240. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1241. [iSalesDB close_db:db];
  1242. appDelegate.wish_count =count;
  1243. [appDelegate update_count_mark];
  1244. ret[@"result"]= [NSNumber numberWithInt:2];
  1245. return ret;
  1246. }
  1247. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1248. //{
  1249. //
  1250. // UIApplication * app = [UIApplication sharedApplication];
  1251. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1252. //
  1253. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1254. // sqlite3 *db = [iSalesDB get_db];
  1255. // NSString* product_id=params[@"product_id"];
  1256. //
  1257. //
  1258. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1259. //
  1260. //
  1261. // for(int i=0;i<arr.count;i++)
  1262. // {
  1263. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1264. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1265. // if(count==0)
  1266. // {
  1267. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1268. // [iSalesDB execSql:sqlQuery db:db];
  1269. // }
  1270. // }
  1271. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1272. // [iSalesDB close_db:db];
  1273. //
  1274. // appDelegate.wish_count =count;
  1275. //
  1276. // [appDelegate update_count_mark];
  1277. // ret[@"result"]= [NSNumber numberWithInt:2];
  1278. // return ret;
  1279. // //
  1280. // //return ret;
  1281. //}
  1282. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1283. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1284. DebugLog(@"time interval: %lf",interval);
  1285. }
  1286. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1287. {
  1288. UIApplication * app = [UIApplication sharedApplication];
  1289. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1290. int sort = [[params objectForKey:@"sort"] intValue];
  1291. NSString *sort_str = @"";
  1292. switch (sort) {
  1293. case 0:{
  1294. sort_str = @"order by w.modify_time desc";
  1295. }
  1296. break;
  1297. case 1:{
  1298. sort_str = @"order by w.modify_time asc";
  1299. }
  1300. break;
  1301. case 2:{
  1302. sort_str = @"order by m.name asc";
  1303. }
  1304. break;
  1305. case 3:{
  1306. sort_str = @"order by m.name desc";
  1307. }
  1308. break;
  1309. case 4:{
  1310. sort_str = @"order by m.description asc";
  1311. }
  1312. break;
  1313. default:
  1314. break;
  1315. }
  1316. NSString* user = appDelegate.user;
  1317. sqlite3 *db = [iSalesDB get_db];
  1318. // order by w.create_time
  1319. 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];
  1320. // 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];
  1321. sqlite3_stmt * statement;
  1322. NSDate *date1 = [NSDate date];
  1323. NSDate *date2 = nil;
  1324. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1325. int count=0;
  1326. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1327. {
  1328. while (sqlite3_step(statement) == SQLITE_ROW)
  1329. {
  1330. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1331. int product_id = sqlite3_column_double(statement, 0);
  1332. char *description = (char*)sqlite3_column_text(statement, 1);
  1333. if(description==nil)
  1334. description= "";
  1335. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1336. int item_id = sqlite3_column_double(statement, 2);
  1337. NSDate *date_image = [NSDate date];
  1338. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1339. printf("image : ");
  1340. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1341. // char *url = (char*)sqlite3_column_text(statement, 3);
  1342. // if(url==nil)
  1343. // url="";
  1344. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1345. int qty = sqlite3_column_int(statement, 3);
  1346. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1347. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1348. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1349. item[@"description"]= nsdescription;
  1350. item[@"img"]= nsurl;
  1351. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1352. count++;
  1353. }
  1354. printf("total time:");
  1355. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1356. ret[@"count"]= [NSNumber numberWithInt:count];
  1357. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1358. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1359. ret[@"result"]= [NSNumber numberWithInt:2];
  1360. appDelegate.wish_count =count;
  1361. [appDelegate update_count_mark];
  1362. sqlite3_finalize(statement);
  1363. }
  1364. [iSalesDB close_db:db];
  1365. return ret;
  1366. }
  1367. +(NSDictionary*) offline_notimpl
  1368. {
  1369. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1370. ret[@"result"]=@"8";
  1371. ret[@"err_msg"]=@"offline mode does not support this function.";
  1372. return ret;
  1373. }
  1374. +(NSDictionary*) offline_home
  1375. {
  1376. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1377. NSString *cachefolder = [paths objectAtIndex:0];
  1378. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1379. NSData* json =nil;
  1380. json=[NSData dataWithContentsOfFile:img_cache];
  1381. NSError *error=nil;
  1382. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1383. return menu;
  1384. }
  1385. +(NSDictionary*) offline_category_menu
  1386. {
  1387. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1388. NSString *cachefolder = [paths objectAtIndex:0];
  1389. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1390. NSData* json =nil;
  1391. json=[NSData dataWithContentsOfFile:img_cache];
  1392. if(json==nil)
  1393. return nil;
  1394. NSError *error=nil;
  1395. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1396. return menu;
  1397. }
  1398. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1399. {
  1400. NSString* offline_command=params[@"offline_Command"];
  1401. NSDictionary* ret=nil;
  1402. if([offline_command isEqualToString:@"model_NIYMAL"])
  1403. {
  1404. NSString* category = params[@"category"];
  1405. ret = [self refresh_model_NIYMAL:category];
  1406. }
  1407. return ret;
  1408. }
  1409. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1410. {
  1411. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1412. [ret setValue:@"2" forKey:@"result"];
  1413. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1414. sqlite3* db= [iSalesDB get_db];
  1415. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1416. [iSalesDB close_db:db];
  1417. [ret setObject:detail1_section forKey:@"detail_1"];
  1418. return ret;
  1419. }
  1420. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1421. {
  1422. UIApplication * app = [UIApplication sharedApplication];
  1423. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1424. // NSArray* arr1 = [self get_user_all_price_type];
  1425. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1426. // NSSet *set1 = [NSSet setWithArray:arr1];
  1427. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1428. // if(appDelegate.contact_id==nil)
  1429. // set2=[set1 mutableCopy];
  1430. // else
  1431. // [set2 intersectsSet:set1];
  1432. // NSArray *retarr = [set2 allObjects];
  1433. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1434. // sqlite3 *db = [iSalesDB get_db];
  1435. NSString* sqlQuery = nil;
  1436. if(appDelegate.contact_id==nil)
  1437. 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];
  1438. else
  1439. 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];
  1440. sqlite3_stmt * statement;
  1441. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1442. int count=0;
  1443. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1444. {
  1445. while (sqlite3_step(statement) == SQLITE_ROW)
  1446. {
  1447. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1448. char *name = (char*)sqlite3_column_text(statement, 0);
  1449. if(name==nil)
  1450. name="";
  1451. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1452. // double price = sqlite3_column_double(statement, 1);
  1453. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1454. // if(isnull==nil)
  1455. // item[nsname]= @"No Price";
  1456. // else
  1457. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1458. char *price = (char*)sqlite3_column_text(statement, 1);
  1459. if(price!=nil)
  1460. {
  1461. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1462. nsprice=[AESCrypt fastdecrypt:nsprice];
  1463. if(nsprice.length>0)
  1464. {
  1465. double dp= [nsprice doubleValue];
  1466. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1467. }
  1468. }
  1469. else
  1470. {
  1471. item[nsname]= @"No Price";
  1472. }
  1473. // int type= sqlite3_column_int(statement, 2);
  1474. // item[@"type"]=[NSNumber numberWithInt:type ];
  1475. // item[nsname]= nsprice;
  1476. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1477. count++;
  1478. }
  1479. ret[@"count"]= [NSNumber numberWithInt:count];
  1480. sqlite3_finalize(statement);
  1481. }
  1482. // [iSalesDB close_db:db];
  1483. return ret;
  1484. }
  1485. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1486. //{
  1487. // NSArray* arr1 = [self get_user_all_price_type:db];
  1488. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1489. //
  1490. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1491. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1492. // // [set2 intersectsSet:set1];
  1493. // //
  1494. // //
  1495. // // NSArray *retarr = [set2 allObjects];
  1496. //
  1497. // NSString* whereprice=nil;
  1498. // if(contact_id==nil)
  1499. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1500. // else
  1501. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1502. //
  1503. //
  1504. // // sqlite3 *db = [iSalesDB get_db];
  1505. //
  1506. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1507. // sqlite3_stmt * statement;
  1508. //
  1509. //
  1510. // NSNumber* ret = nil;
  1511. // double dprice=DBL_MAX;
  1512. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1513. // {
  1514. //
  1515. //
  1516. // while (sqlite3_step(statement) == SQLITE_ROW)
  1517. // {
  1518. //
  1519. // // double val = sqlite3_column_double(statement, 0);
  1520. // char *price = (char*)sqlite3_column_text(statement, 0);
  1521. // if(price!=nil)
  1522. // {
  1523. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1524. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1525. // if(nsprice.length>0)
  1526. // {
  1527. // double dp= [nsprice doubleValue];
  1528. // if(dp<dprice)
  1529. // dprice=dp;
  1530. // }
  1531. // }
  1532. // }
  1533. //
  1534. //
  1535. //
  1536. //
  1537. // sqlite3_finalize(statement);
  1538. //
  1539. //
  1540. //
  1541. //
  1542. // }
  1543. //
  1544. // // [iSalesDB close_db:db];
  1545. //
  1546. // if(dprice==DBL_MAX)
  1547. // ret= nil;
  1548. // else
  1549. // ret= [NSNumber numberWithDouble:dprice];
  1550. // return ret;
  1551. //}
  1552. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1553. {
  1554. NSArray* arr1 = [self get_user_all_price_type:db];
  1555. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1556. NSString* whereprice=nil;
  1557. if(contact_id==nil)
  1558. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1559. else
  1560. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1561. // sqlite3 *db = [iSalesDB get_db];
  1562. NSString *productIdCondition = @"1 = 1";
  1563. if (product_id) {
  1564. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1565. }
  1566. NSString *itemIdCondition = @"";
  1567. if (item_id) {
  1568. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1569. }
  1570. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1571. sqlite3_stmt * statement;
  1572. NSNumber* ret = nil;
  1573. double dprice=DBL_MAX;
  1574. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1575. {
  1576. while (sqlite3_step(statement) == SQLITE_ROW)
  1577. {
  1578. // double val = sqlite3_column_double(statement, 0);
  1579. char *price = (char*)sqlite3_column_text(statement, 0);
  1580. if(price!=nil)
  1581. {
  1582. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1583. nsprice=[AESCrypt fastdecrypt:nsprice];
  1584. if(nsprice.length>0)
  1585. {
  1586. double dp= [nsprice doubleValue];
  1587. if(dp<dprice)
  1588. dprice=dp;
  1589. }
  1590. }
  1591. }
  1592. sqlite3_finalize(statement);
  1593. }
  1594. // [iSalesDB close_db:db];
  1595. if(dprice==DBL_MAX)
  1596. ret= nil;
  1597. else
  1598. ret= [NSNumber numberWithDouble:dprice];
  1599. return ret;
  1600. }
  1601. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1602. {
  1603. NSArray* ret=nil;
  1604. // sqlite3 *db = [iSalesDB get_db];
  1605. // no customer assigned , use login user contact_id
  1606. UIApplication * app = [UIApplication sharedApplication];
  1607. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1608. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1609. sqlite3_stmt * statement;
  1610. // int count=0;
  1611. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1612. {
  1613. if (sqlite3_step(statement) == SQLITE_ROW)
  1614. {
  1615. char *val = (char*)sqlite3_column_text(statement, 0);
  1616. if(val==nil)
  1617. val="";
  1618. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1619. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1620. }
  1621. sqlite3_finalize(statement);
  1622. }
  1623. // [iSalesDB close_db:db];
  1624. return ret;
  1625. }
  1626. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1627. {
  1628. // sqlite3 *db = [iSalesDB get_db];
  1629. if(contact_id==nil)
  1630. {
  1631. // no customer assigned , use login user contact_id
  1632. UIApplication * app = [UIApplication sharedApplication];
  1633. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1634. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1635. sqlite3_stmt * statement;
  1636. // int count=0;
  1637. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1638. {
  1639. if (sqlite3_step(statement) == SQLITE_ROW)
  1640. {
  1641. char *val = (char*)sqlite3_column_text(statement, 0);
  1642. if(val==nil)
  1643. val="";
  1644. contact_id = [[NSString alloc]initWithUTF8String:val];
  1645. }
  1646. sqlite3_finalize(statement);
  1647. }
  1648. if(contact_id.length<=0)
  1649. {
  1650. // [iSalesDB close_db:db];
  1651. return nil;
  1652. }
  1653. }
  1654. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1655. sqlite3_stmt * statement;
  1656. NSArray* ret=nil;
  1657. // int count=0;
  1658. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1659. {
  1660. if (sqlite3_step(statement) == SQLITE_ROW)
  1661. {
  1662. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1663. if(price_type==nil)
  1664. price_type="";
  1665. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1666. if(nsprice_type.length>0)
  1667. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1668. else
  1669. ret=nil;
  1670. }
  1671. sqlite3_finalize(statement);
  1672. }
  1673. // [iSalesDB close_db:db];
  1674. return ret;
  1675. }
  1676. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1677. {
  1678. NSString* ret= nil;
  1679. // sqlite3 *db = [iSalesDB get_db];
  1680. NSString *sqlQuery = nil;
  1681. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1682. // 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;
  1683. // 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
  1684. if(product_id==nil && model_name)
  1685. 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;
  1686. else if (product_id)
  1687. 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
  1688. sqlite3_stmt * statement;
  1689. // int count=0;
  1690. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1691. {
  1692. if (sqlite3_step(statement) == SQLITE_ROW)
  1693. {
  1694. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1695. if(imgurl==nil)
  1696. imgurl="";
  1697. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1698. ret=nsimgurl;
  1699. }
  1700. sqlite3_finalize(statement);
  1701. }
  1702. else
  1703. {
  1704. [ret setValue:@"8" forKey:@"result"];
  1705. }
  1706. // [iSalesDB close_db:db];
  1707. DebugLog(@"data string: %@",ret );
  1708. return ret;
  1709. }
  1710. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1711. {
  1712. NSString* ret= nil;
  1713. sqlite3 *db = [iSalesDB get_db];
  1714. NSString *sqlQuery = nil;
  1715. if(product_id==nil)
  1716. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1717. else
  1718. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1719. sqlite3_stmt * statement;
  1720. // int count=0;
  1721. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1722. {
  1723. if (sqlite3_step(statement) == SQLITE_ROW)
  1724. {
  1725. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1726. if(default_category==nil)
  1727. default_category="";
  1728. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1729. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1730. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1731. ret=nsdefault_category;
  1732. }
  1733. sqlite3_finalize(statement);
  1734. }
  1735. else
  1736. {
  1737. [ret setValue:@"8" forKey:@"result"];
  1738. }
  1739. [iSalesDB close_db:db];
  1740. DebugLog(@"data string: %@",ret );
  1741. return ret;
  1742. }
  1743. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1744. {
  1745. NSString* model_name = [params valueForKey:@"product_name"];
  1746. NSString* product_id = [params valueForKey:@"product_id"];
  1747. NSString* category = [params valueForKey:@"category"];
  1748. if(category==nil)
  1749. category = [self model_default_category:product_id model_name:model_name];
  1750. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1751. sqlite3 *db = [iSalesDB get_db];
  1752. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1753. NSString *sqlQuery = nil;
  1754. if(product_id==nil)
  1755. 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='%@';
  1756. else
  1757. 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=%@;
  1758. sqlite3_stmt * statement;
  1759. [ret setValue:@"2" forKey:@"result"];
  1760. [ret setValue:@"3" forKey:@"detail_section_count"];
  1761. // int count=0;
  1762. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1763. {
  1764. if (sqlite3_step(statement) == SQLITE_ROW)
  1765. {
  1766. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1767. char *name = (char*)sqlite3_column_text(statement, 0);
  1768. if(name==nil)
  1769. name="";
  1770. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1771. char *description = (char*)sqlite3_column_text(statement, 1);
  1772. if(description==nil)
  1773. description="";
  1774. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1775. int product_id = sqlite3_column_int(statement, 2);
  1776. char *color = (char*)sqlite3_column_text(statement, 3);
  1777. if(color==nil)
  1778. color="";
  1779. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1780. //
  1781. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1782. // if(legcolor==nil)
  1783. // legcolor="";
  1784. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1785. //
  1786. //
  1787. int availability = sqlite3_column_int(statement, 5);
  1788. //
  1789. int incoming_stock = sqlite3_column_int(statement, 6);
  1790. char *demension = (char*)sqlite3_column_text(statement, 7);
  1791. if(demension==nil)
  1792. demension="";
  1793. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1794. // ,,,,,,,,,
  1795. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1796. if(seat_height==nil)
  1797. seat_height="";
  1798. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1799. char *material = (char*)sqlite3_column_text(statement, 9);
  1800. if(material==nil)
  1801. material="";
  1802. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1803. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1804. if(box_dim==nil)
  1805. box_dim="";
  1806. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1807. char *volume = (char*)sqlite3_column_text(statement, 11);
  1808. if(volume==nil)
  1809. volume="";
  1810. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1811. double weight = sqlite3_column_double(statement, 12);
  1812. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1813. if(model_set==nil)
  1814. model_set="";
  1815. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1816. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1817. if(load_ability==nil)
  1818. load_ability="";
  1819. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1820. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1821. if(default_category==nil)
  1822. default_category="";
  1823. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1824. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1825. if(fabric_content==nil)
  1826. fabric_content="";
  1827. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1828. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1829. if(assembling==nil)
  1830. assembling="";
  1831. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1832. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1833. if(made_in==nil)
  1834. made_in="";
  1835. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1836. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1837. if(special_remarks==nil)
  1838. special_remarks="";
  1839. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1840. int stockUcom = sqlite3_column_double(statement, 20);
  1841. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1842. if(product_group==nil)
  1843. product_group="";
  1844. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1845. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1846. // if(fashion_selector==nil)
  1847. // fashion_selector="";
  1848. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1849. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1850. if(selector_field==nil)
  1851. selector_field="";
  1852. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1853. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1854. if(property_field==nil)
  1855. property_field="";
  1856. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1857. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1858. if(packaging==nil)
  1859. packaging="";
  1860. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1861. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1862. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1863. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1864. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1865. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1866. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1867. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1868. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1869. NSString* Availability=nil;
  1870. if(availability>0)
  1871. Availability=[NSString stringWithFormat:@"%d",availability];
  1872. else
  1873. Availability = @"In Production";
  1874. [img_section setValue:Availability forKey:@"Availability"];
  1875. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1876. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1877. char *eta = (char*)sqlite3_column_text(statement, 25);
  1878. if(eta==nil)
  1879. eta="";
  1880. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1881. [img_section setValue:nseta forKey:@"ETA"];
  1882. int item_id = sqlite3_column_int(statement, 26);
  1883. NSString* Price=nil;
  1884. if(appDelegate.bLogin==false)
  1885. Price=@"Must Sign in.";
  1886. else
  1887. {
  1888. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1889. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1890. if(price==nil)
  1891. Price=@"No Price.";
  1892. else
  1893. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1894. }
  1895. [img_section setValue:Price forKey:@"price"];
  1896. [img_section setValue:nsname forKey:@"model_name"];
  1897. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1898. [ret setObject:img_section forKey:@"img_section"];
  1899. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1900. int detail0_item_count=0;
  1901. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1902. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1903. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1904. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1905. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1906. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1907. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1908. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1909. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1910. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1911. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1912. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1913. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1914. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1915. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1916. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1917. {
  1918. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1919. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1920. }
  1921. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1922. [detail0_section setValue:@"kv" forKey:@"type"];
  1923. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1924. [ret setObject:detail0_section forKey:@"detail_0"];
  1925. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1926. // [detail1_section setValue:@"detail" forKey:@"target"];
  1927. // [detail1_section setValue:@"popup" forKey:@"action"];
  1928. // [detail1_section setValue:@"content" forKey:@"type"];
  1929. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1930. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1931. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1932. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1933. [ret setObject:detail1_section forKey:@"detail_1"];
  1934. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1935. [detail2_section setValue:@"detail" forKey:@"target"];
  1936. [detail2_section setValue:@"popup" forKey:@"action"];
  1937. [detail2_section setValue:@"content" forKey:@"type"];
  1938. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1939. [detail2_section setValue:@"true" forKey:@"single_row"];
  1940. [detail2_section setValue:@"local" forKey:@"data"];
  1941. [ret setObject:detail2_section forKey:@"detail_2"];
  1942. }
  1943. sqlite3_finalize(statement);
  1944. }
  1945. else
  1946. {
  1947. [ret setValue:@"8" forKey:@"result"];
  1948. }
  1949. NSLog(@"count:%d",count);
  1950. [iSalesDB close_db:db];
  1951. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1952. return ret;
  1953. }
  1954. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1955. NSString* orderCode = [params valueForKey:@"orderCode"];
  1956. NSString* keyword = [params valueForKey:@"keyword"];
  1957. keyword=keyword.lowercaseString;
  1958. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1959. int limit = [[params valueForKey:@"limit"] intValue];
  1960. int offset = [[params valueForKey:@"offset"] intValue];
  1961. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1962. NSString *limit_str = @"";
  1963. if (limited) {
  1964. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1965. }
  1966. sqlite3 *db = [iSalesDB get_db];
  1967. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1968. UIApplication * app = [UIApplication sharedApplication];
  1969. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1970. NSString *sqlQuery = nil;
  1971. if(exactMatch )
  1972. 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 ;
  1973. else
  1974. 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
  1975. DebugLog(@"offline_search sql:%@",sqlQuery);
  1976. sqlite3_stmt * statement;
  1977. [ret setValue:@"2" forKey:@"result"];
  1978. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1979. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1980. // int count=0;
  1981. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1982. {
  1983. int i=0;
  1984. while (sqlite3_step(statement) == SQLITE_ROW)
  1985. {
  1986. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1987. // char *name = (char*)sqlite3_column_text(statement, 1);
  1988. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1989. char *name = (char*)sqlite3_column_text(statement, 0);
  1990. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1991. char *description = (char*)sqlite3_column_text(statement, 1);
  1992. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1993. int product_id = sqlite3_column_int(statement, 2);
  1994. // char *url = (char*)sqlite3_column_text(statement, 3);
  1995. // if(url==nil)
  1996. // url="";
  1997. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1998. int wid = sqlite3_column_int(statement, 3);
  1999. int closeout = sqlite3_column_int(statement, 4);
  2000. int cid = sqlite3_column_int(statement, 5);
  2001. int wisdelete = sqlite3_column_int(statement, 6);
  2002. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2003. if(wid !=0 && wisdelete != 1)
  2004. [item setValue:@"true" forKey:@"wish_exists"];
  2005. else
  2006. [item setValue:@"false" forKey:@"wish_exists"];
  2007. if(closeout==0)
  2008. [item setValue:@"false" forKey:@"is_closeout"];
  2009. else
  2010. [item setValue:@"true" forKey:@"is_closeout"];
  2011. if(cid==0)
  2012. [item setValue:@"false" forKey:@"cart_exists"];
  2013. else
  2014. [item setValue:@"true" forKey:@"cart_exists"];
  2015. [item addEntriesFromDictionary:imgjson];
  2016. // [item setValue:nsurl forKey:@"img"];
  2017. [item setValue:nsname forKey:@"fash_name"];
  2018. [item setValue:nsdescription forKey:@"description"];
  2019. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2020. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2021. i++;
  2022. }
  2023. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2024. [ret setObject:items forKey:@"items"];
  2025. sqlite3_finalize(statement);
  2026. }
  2027. NSLog(@"count:%d",count);
  2028. [iSalesDB close_db:db];
  2029. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2030. return ret;
  2031. }
  2032. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2033. {
  2034. return [self search:params limited:YES];
  2035. }
  2036. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2037. {
  2038. UIApplication * app = [UIApplication sharedApplication];
  2039. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2040. appDelegate.disable_trigger=true;
  2041. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2042. appDelegate.disable_trigger=false;
  2043. //
  2044. // NSString* user = [params valueForKey:@"user"];
  2045. //
  2046. // NSString* password = [params valueForKey:@"password"];
  2047. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2048. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2049. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2050. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2051. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2052. [appDelegate update_count_mark];
  2053. appDelegate.can_show_price =false;
  2054. appDelegate.can_see_price =false;
  2055. appDelegate.can_create_portfolio =false;
  2056. appDelegate.can_create_order =false;
  2057. appDelegate.can_cancel_order =false;
  2058. appDelegate.can_set_cart_price =false;
  2059. appDelegate.can_delete_order =false;
  2060. appDelegate.can_submit_order =false;
  2061. appDelegate.can_set_tearsheet_price =false;
  2062. appDelegate.can_update_contact_info = false;
  2063. appDelegate.save_order_logout = false;
  2064. appDelegate.submit_order_logout = false;
  2065. appDelegate.alert_sold_in_quantities = false;
  2066. appDelegate.ipad_perm =nil ;
  2067. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2068. appDelegate.OrderFilter= nil;
  2069. [appDelegate SetSo:nil];
  2070. [appDelegate set_main_button_panel];
  2071. // sqlite3 *db = [iSalesDB get_db];
  2072. //
  2073. //
  2074. //
  2075. //
  2076. //
  2077. // 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"]];
  2078. //
  2079. //
  2080. //
  2081. //
  2082. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2083. // sqlite3_stmt * statement;
  2084. //
  2085. //
  2086. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2087. //
  2088. //
  2089. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2090. // {
  2091. //
  2092. //
  2093. // if (sqlite3_step(statement) == SQLITE_ROW)
  2094. // {
  2095. //
  2096. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2097. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2098. //
  2099. //
  2100. //
  2101. // int can_show_price = sqlite3_column_int(statement, 0);
  2102. // int can_see_price = sqlite3_column_int(statement, 1);
  2103. //
  2104. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2105. // if(contact_id==nil)
  2106. // contact_id="";
  2107. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2108. //
  2109. // int user_type = sqlite3_column_int(statement, 3);
  2110. //
  2111. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2112. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2113. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2114. // int can_delete_order = sqlite3_column_int(statement, 7);
  2115. // int can_submit_order = sqlite3_column_int(statement, 8);
  2116. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2117. // int can_create_order = sqlite3_column_int(statement, 10);
  2118. //
  2119. //
  2120. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2121. // if(mode==nil)
  2122. // mode="";
  2123. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2124. //
  2125. //
  2126. // char *username = (char*)sqlite3_column_text(statement, 12);
  2127. // if(username==nil)
  2128. // username="";
  2129. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2130. //
  2131. //
  2132. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2133. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2134. // [header setValue:nscontact_id forKey:@"contact_id"];
  2135. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2136. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2137. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2138. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2139. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2140. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2141. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2142. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2143. //
  2144. // [header setValue:nsusername forKey:@"username"];
  2145. //
  2146. //
  2147. // [ret setObject:header forKey:@"header"];
  2148. // [ret setValue:nsmode forKey:@"mode"];
  2149. //
  2150. //
  2151. // }
  2152. //
  2153. //
  2154. //
  2155. // sqlite3_finalize(statement);
  2156. // }
  2157. //
  2158. //
  2159. //
  2160. // [iSalesDB close_db:db];
  2161. //
  2162. //
  2163. //
  2164. //
  2165. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2166. return ret;
  2167. }
  2168. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2169. {
  2170. UIApplication * app = [UIApplication sharedApplication];
  2171. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2172. appDelegate.disable_trigger=true;
  2173. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2174. appDelegate.disable_trigger=false;
  2175. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2176. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2177. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2178. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2179. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2180. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2181. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2182. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2183. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2184. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2185. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2186. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2187. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2188. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2189. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2190. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2191. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2192. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2193. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2194. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2195. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2196. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2197. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2198. [arr_name addObject:customer_first_name];
  2199. [arr_name addObject:customer_last_name];
  2200. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2201. // default ship from
  2202. 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';";
  2203. __block NSString *cid = @"";
  2204. __block NSString *name = @"";
  2205. __block NSString *ext = @"";
  2206. __block NSString *contact = @"";
  2207. __block NSString *email = @"";
  2208. __block NSString *fax = @"";
  2209. __block NSString *phone = @"";
  2210. sqlite3 *db = [iSalesDB get_db];
  2211. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2212. cid = [self textAtColumn:0 statement:statment];
  2213. name = [self textAtColumn:1 statement:statment];
  2214. ext = [self textAtColumn:2 statement:statment];
  2215. contact = [self textAtColumn:3 statement:statment];
  2216. email = [self textAtColumn:4 statement:statment];
  2217. fax = [self textAtColumn:5 statement:statment];
  2218. phone = [self textAtColumn:6 statement:statment];
  2219. }];
  2220. NSString* so_id = [self get_offline_soid:db];
  2221. if(so_id==nil)
  2222. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2223. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email];
  2224. int result =[iSalesDB execSql:sql_neworder db:db];
  2225. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2226. //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'
  2227. //soId
  2228. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2229. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2230. sqlite3_stmt * statement;
  2231. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2232. {
  2233. if (sqlite3_step(statement) == SQLITE_ROW)
  2234. {
  2235. // char *name = (char*)sqlite3_column_text(statement, 1);
  2236. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2237. //ret = sqlite3_column_int(statement, 0);
  2238. char *soId = (char*)sqlite3_column_text(statement, 0);
  2239. if(soId==nil)
  2240. soId="";
  2241. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2242. [ret setValue:nssoId forKey:@"soId"];
  2243. [ret setValue:nssoId forKey:@"orderCode"];
  2244. }
  2245. sqlite3_finalize(statement);
  2246. }
  2247. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2248. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2249. [iSalesDB close_db:db];
  2250. return [RAUtils dict2data:ret];
  2251. }
  2252. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2253. {
  2254. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2255. NSString* orderCode = [params valueForKey:@"orderCode"];
  2256. UIApplication * app = [UIApplication sharedApplication];
  2257. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2258. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2259. {
  2260. appDelegate.disable_trigger=true;
  2261. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2262. appDelegate.disable_trigger=false;
  2263. }
  2264. sqlite3 *db = [iSalesDB get_db];
  2265. int cart_count=[self query_ordercartcount:orderCode db:db];
  2266. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2267. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2268. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2269. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2270. [iSalesDB close_db:db];
  2271. return [RAUtils dict2data:ret];
  2272. }
  2273. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2274. {
  2275. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2276. sqlite3 *db = [iSalesDB get_db];
  2277. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2278. int count =0;
  2279. if(params[@"count"]!=nil)
  2280. {
  2281. count = [params[@"count"] intValue];
  2282. }
  2283. NSString* product_id=params[@"product_id"];
  2284. NSString* orderCode=params[@"orderCode"];
  2285. NSString *qty = params[@"qty"];
  2286. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2287. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2288. for(int i=0;i<arr_id.count;i++)
  2289. {
  2290. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2291. int item_qty= count;
  2292. if (qty) {
  2293. item_qty = [qty_arr[i] integerValue];
  2294. }
  2295. if(item_qty==0)
  2296. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2297. NSString* sql=nil;
  2298. sqlite3_stmt *stmt;
  2299. if(_id<0)
  2300. {
  2301. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2302. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2303. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2304. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  2305. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  2306. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2307. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2308. }
  2309. else
  2310. {
  2311. if (qty) {
  2312. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2313. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2314. sqlite3_bind_int(stmt, 1, _id);
  2315. } else {
  2316. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2317. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2318. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  2319. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2320. }
  2321. }
  2322. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2323. [iSalesDB execSql:@"ROLLBACK" db:db];
  2324. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2325. [iSalesDB close_db:db];
  2326. DebugLog(@"add to cart error");
  2327. return [RAUtils dict2data:ret];
  2328. }
  2329. }
  2330. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2331. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2332. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2333. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2334. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2335. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2336. [iSalesDB close_db:db];
  2337. return [RAUtils dict2data:ret];
  2338. }
  2339. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  2340. {
  2341. UIApplication * app = [UIApplication sharedApplication];
  2342. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2343. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2344. 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];
  2345. sqlite3_stmt * statement;
  2346. int count=0;
  2347. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2348. {
  2349. while (sqlite3_step(statement) == SQLITE_ROW)
  2350. {
  2351. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2352. int bitem_id = sqlite3_column_int(statement, 0);
  2353. int bitem_qty = sqlite3_column_int(statement, 1);
  2354. char *name = (char*)sqlite3_column_text(statement, 2);
  2355. if(name==nil)
  2356. name="";
  2357. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2358. char *description = (char*)sqlite3_column_text(statement, 3);
  2359. if(description==nil)
  2360. description="";
  2361. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2362. double unit_price = sqlite3_column_double(statement, 4);
  2363. int use_unitprice = sqlite3_column_int(statement, 5);
  2364. if(use_unitprice!=1)
  2365. {
  2366. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  2367. }
  2368. itemjson[@"model"]=nsname;
  2369. itemjson[@"description"]=nsdescription;
  2370. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2371. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2372. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2373. if(compute)
  2374. {
  2375. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  2376. }
  2377. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2378. count++;
  2379. }
  2380. sqlite3_finalize(statement);
  2381. }
  2382. ret[@"count"]=@(count);
  2383. if(count==0)
  2384. return nil;
  2385. else
  2386. return ret;
  2387. }
  2388. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  2389. {
  2390. //compute: add part to subtotal;
  2391. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  2392. dict_item[@(item_id)]=@"1";
  2393. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2394. double cuft=0;
  2395. double weight=0;
  2396. int carton=0;
  2397. 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];
  2398. sqlite3_stmt * statement;
  2399. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2400. {
  2401. if (sqlite3_step(statement) == SQLITE_ROW)
  2402. {
  2403. double ulength = sqlite3_column_double(statement, 0);
  2404. double uwidth = sqlite3_column_double(statement, 1);
  2405. double uheight = sqlite3_column_double(statement, 2);
  2406. double uweight = sqlite3_column_double(statement, 3);
  2407. double mlength = sqlite3_column_double(statement, 4);
  2408. double mwidth = sqlite3_column_double(statement, 5);
  2409. double mheight = sqlite3_column_double(statement, 6);
  2410. double mweight = sqlite3_column_double(statement, 7);
  2411. double ilength = sqlite3_column_double(statement, 8);
  2412. double iwidth = sqlite3_column_double(statement, 9);
  2413. double iheight = sqlite3_column_double(statement, 10);
  2414. double iweight = sqlite3_column_double(statement, 11);
  2415. int pcs = sqlite3_column_int(statement,12);
  2416. int mpack = sqlite3_column_int(statement, 13);
  2417. int ipack = sqlite3_column_int(statement, 14);
  2418. double ucbf = sqlite3_column_double(statement, 15);
  2419. double icbf = sqlite3_column_double(statement, 16);
  2420. double mcbf = sqlite3_column_double(statement, 17);
  2421. if(ipack==0)
  2422. {
  2423. carton= count/mpack ;
  2424. weight = mweight*carton;
  2425. cuft= carton*(mlength*mwidth*mheight);
  2426. int remain=count%mpack;
  2427. if(remain==0)
  2428. {
  2429. //do nothing;
  2430. }
  2431. else
  2432. {
  2433. carton++;
  2434. weight += uweight*remain;
  2435. cuft += (ulength*uwidth*uheight)*remain;
  2436. }
  2437. }
  2438. else
  2439. {
  2440. carton = count/(mpack*ipack);
  2441. weight = mweight*carton;
  2442. cuft= carton*(mlength*mwidth*mheight);
  2443. int remain=count%(mpack*ipack);
  2444. if(remain==0)
  2445. {
  2446. // do nothing;
  2447. }
  2448. else
  2449. {
  2450. carton++;
  2451. int icarton =remain/ipack;
  2452. int iremain=remain%ipack;
  2453. weight += iweight*icarton;
  2454. cuft += (ilength*iwidth*iheight)*icarton;
  2455. if(iremain==0)
  2456. {
  2457. //do nothing;
  2458. }
  2459. else
  2460. {
  2461. weight += uweight*iremain;
  2462. cuft += (ulength*uwidth*uheight)*iremain;
  2463. }
  2464. }
  2465. }
  2466. #ifdef BUILD_NPD
  2467. cuft=ucbf*count;
  2468. weight= uweight*count;
  2469. #endif
  2470. }
  2471. sqlite3_finalize(statement);
  2472. }
  2473. if(compute)
  2474. {
  2475. NSArray * arr_count=nil;
  2476. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2477. for(int i=0;i<arr_bundle.count;i++)
  2478. {
  2479. dict_item[arr_bundle[i]]=@"1";
  2480. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2481. cuft+=[bundlejson[@"cuft"] doubleValue];
  2482. weight+=[bundlejson[@"weight"] doubleValue];
  2483. carton+=[bundlejson[@"carton"] intValue];
  2484. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2485. }
  2486. }
  2487. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2488. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2489. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2490. ret[@"items"]=dict_item;
  2491. return ret;
  2492. }
  2493. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2494. {
  2495. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2496. // 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 ];
  2497. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2498. DebugLog(@"offline_login sql:%@",sqlQuery);
  2499. sqlite3_stmt * statement;
  2500. int cart_count=0;
  2501. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2502. if ( dbresult== SQLITE_OK)
  2503. {
  2504. while (sqlite3_step(statement) == SQLITE_ROW)
  2505. {
  2506. int item_id = sqlite3_column_int(statement, 0);
  2507. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2508. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2509. }
  2510. sqlite3_finalize(statement);
  2511. }
  2512. return cart_count;
  2513. }
  2514. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2515. {
  2516. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2517. sqlite3 *db = [iSalesDB get_db];
  2518. UIApplication * app = [UIApplication sharedApplication];
  2519. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2520. NSString* orderCode=params[@"orderCode"];
  2521. int sort = [[params objectForKey:@"sort"] intValue];
  2522. NSString *sort_str = @"";
  2523. switch (sort) {
  2524. case 0:{
  2525. sort_str = @"order by c.modify_time desc";
  2526. }
  2527. break;
  2528. case 1:{
  2529. sort_str = @"order by c.modify_time asc";
  2530. }
  2531. break;
  2532. case 2:{
  2533. sort_str = @"order by m.name asc";
  2534. }
  2535. break;
  2536. case 3:{
  2537. sort_str = @"order by m.name desc";
  2538. }
  2539. break;
  2540. case 4:{
  2541. sort_str = @"order by m.description asc";
  2542. }
  2543. break;
  2544. default:
  2545. break;
  2546. }
  2547. 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 ];
  2548. // 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 ];
  2549. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2550. DebugLog(@"offline_login sql:%@",sqlQuery);
  2551. sqlite3_stmt * statement;
  2552. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2553. NSDate *date1 = [NSDate date];
  2554. int count=0;
  2555. int cart_count=0;
  2556. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2557. if ( dbresult== SQLITE_OK)
  2558. {
  2559. while (sqlite3_step(statement) == SQLITE_ROW)
  2560. {
  2561. NSDate *row_date = [NSDate date];
  2562. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2563. int product_id = sqlite3_column_int(statement, 0);
  2564. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2565. int item_id = sqlite3_column_int(statement, 7);
  2566. NSString* Price=nil;
  2567. if(str_price==nil)
  2568. {
  2569. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2570. // NSDate *price_date = [NSDate date];
  2571. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2572. // DebugLog(@"price time interval");
  2573. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2574. if(price==nil)
  2575. Price=@"No Price.";
  2576. else
  2577. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2578. }
  2579. else
  2580. {
  2581. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2582. }
  2583. double discount = sqlite3_column_double(statement, 2);
  2584. int item_count = sqlite3_column_int(statement, 3);
  2585. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2586. NSString *nsline_note=nil;
  2587. if(line_note!=nil)
  2588. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2589. char *name = (char*)sqlite3_column_text(statement, 5);
  2590. NSString *nsname=nil;
  2591. if(name!=nil)
  2592. nsname= [[NSString alloc]initWithUTF8String:name];
  2593. char *description = (char*)sqlite3_column_text(statement, 6);
  2594. NSString *nsdescription=nil;
  2595. if(description!=nil)
  2596. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2597. int stockUom = sqlite3_column_int(statement, 8);
  2598. int _id = sqlite3_column_int(statement, 9);
  2599. // NSDate *subtotal_date = [NSDate date];
  2600. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2601. // DebugLog(@"subtotal_date time interval");
  2602. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2603. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2604. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2605. int carton=[bsubtotaljson[@"carton"] intValue];
  2606. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2607. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2608. // NSDate *img_date = [NSDate date];
  2609. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2610. // DebugLog(@"img_date time interval");
  2611. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2612. itemjson[@"model"]=nsname;
  2613. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2614. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2615. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2616. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2617. itemjson[@"check"]=@"true";
  2618. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2619. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2620. itemjson[@"unit_price"]=Price;
  2621. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2622. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2623. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2624. itemjson[@"note"]=nsline_note;
  2625. // NSDate *date2 = [NSDate date];
  2626. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2627. // DebugLog(@"model_bundle time interval");
  2628. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2629. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2630. count++;
  2631. // DebugLog(@"row time interval");
  2632. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2633. }
  2634. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2635. sqlite3_finalize(statement);
  2636. }
  2637. DebugLog(@"request cart total time interval");
  2638. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2639. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2640. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2641. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2642. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2643. ret[@"count"]=[NSNumber numberWithInt:count ];
  2644. ret[@"mode"]=@"Regular Mode";
  2645. [iSalesDB close_db:db];
  2646. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2647. DebugLog(@"general notes :%@",general_note);
  2648. ret[@"general_note"]= general_note;
  2649. return [RAUtils dict2data:ret];
  2650. }
  2651. +(NSData*) offline_login :(NSMutableDictionary *) params
  2652. {
  2653. NSString* user = [params valueForKey:@"user"];
  2654. NSString* password = [params valueForKey:@"password"];
  2655. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2656. sqlite3 *db = [iSalesDB get_db];
  2657. 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"]];
  2658. DebugLog(@"offline_login sql:%@",sqlQuery);
  2659. sqlite3_stmt * statement;
  2660. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2661. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2662. {
  2663. if (sqlite3_step(statement) == SQLITE_ROW)
  2664. {
  2665. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2666. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2667. int can_show_price = sqlite3_column_int(statement, 0);
  2668. int can_see_price = sqlite3_column_int(statement, 1);
  2669. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2670. if(contact_id==nil)
  2671. contact_id="";
  2672. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2673. int user_type = sqlite3_column_int(statement, 3);
  2674. int can_cancel_order = sqlite3_column_int(statement, 4);
  2675. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2676. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2677. int can_delete_order = sqlite3_column_int(statement, 7);
  2678. int can_submit_order = sqlite3_column_int(statement, 8);
  2679. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2680. int can_create_order = sqlite3_column_int(statement, 10);
  2681. char *mode = (char*)sqlite3_column_text(statement, 11);
  2682. if(mode==nil)
  2683. mode="";
  2684. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2685. char *username = (char*)sqlite3_column_text(statement, 12);
  2686. if(username==nil)
  2687. username="";
  2688. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2689. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2690. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2691. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2692. [header setValue:nscontact_id forKey:@"contact_id"];
  2693. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2694. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2695. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2696. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2697. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2698. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2699. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2700. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2701. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2702. [header setValue:nsusername forKey:@"username"];
  2703. NSError* error=nil;
  2704. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2705. [header setValue:statusFilter forKey:@"statusFilter"];
  2706. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2707. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2708. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2709. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2710. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2711. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2712. [ret setObject:header forKey:@"header"];
  2713. [ret setValue:nsmode forKey:@"mode"];
  2714. }
  2715. sqlite3_finalize(statement);
  2716. }
  2717. [iSalesDB close_db:db];
  2718. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2719. return [RAUtils dict2data:ret];
  2720. }
  2721. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2722. {
  2723. NSString* contactId = [params valueForKey:@"contactId"];
  2724. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2725. sqlite3 *db = [iSalesDB get_db];
  2726. NSString *sqlQuery = nil;
  2727. {
  2728. 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];
  2729. }
  2730. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2731. sqlite3_stmt * statement;
  2732. [ret setValue:@"2" forKey:@"result"];
  2733. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2734. {
  2735. //int i = 0;
  2736. if (sqlite3_step(statement) == SQLITE_ROW)
  2737. {
  2738. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2739. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2740. // int editable = sqlite3_column_int(statement, 0);
  2741. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2742. NSString *nscompany_name =nil;
  2743. if(company_name==nil)
  2744. nscompany_name=@"";
  2745. else
  2746. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2747. char *country = (char*)sqlite3_column_text(statement, 2);
  2748. if(country==nil)
  2749. country="";
  2750. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2751. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2752. // if(addr==nil)
  2753. // addr="";
  2754. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2755. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2756. if(zipcode==nil)
  2757. zipcode="";
  2758. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2759. char *state = (char*)sqlite3_column_text(statement, 5);
  2760. if(state==nil)
  2761. state="";
  2762. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2763. char *city = (char*)sqlite3_column_text(statement, 6);
  2764. if(city==nil)
  2765. city="";
  2766. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2767. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2768. // NSString *nscontact_name = nil;
  2769. // if(contact_name==nil)
  2770. // nscontact_name=@"";
  2771. // else
  2772. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2773. char *phone = (char*)sqlite3_column_text(statement, 8);
  2774. NSString *nsphone = nil;
  2775. if(phone==nil)
  2776. nsphone=@"";
  2777. else
  2778. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2779. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2780. if(contact_id==nil)
  2781. contact_id="";
  2782. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2783. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2784. if(addr_1==nil)
  2785. addr_1="";
  2786. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2787. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2788. if(addr_2==nil)
  2789. addr_2="";
  2790. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2791. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2792. if(addr_3==nil)
  2793. addr_3="";
  2794. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2795. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2796. if(addr_4==nil)
  2797. addr_4="";
  2798. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2799. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2800. if(first_name==nil)
  2801. first_name="";
  2802. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2803. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2804. if(last_name==nil)
  2805. last_name="";
  2806. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2807. char *fax = (char*)sqlite3_column_text(statement, 16);
  2808. NSString *nsfax = nil;
  2809. if(fax==nil)
  2810. nsfax=@"";
  2811. else
  2812. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2813. char *email = (char*)sqlite3_column_text(statement, 17);
  2814. NSString *nsemail = nil;
  2815. if(email==nil)
  2816. nsemail=@"";
  2817. else
  2818. nsemail= [[NSString alloc]initWithUTF8String:email];
  2819. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2820. NSString *nsimg_0 = nil;
  2821. if(img_0==nil)
  2822. nsimg_0=@"";
  2823. else
  2824. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2825. [self copy_bcardImg:nsimg_0];
  2826. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2827. NSString *nsimg_1 = nil;
  2828. if(img_1==nil)
  2829. nsimg_1=@"";
  2830. else
  2831. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2832. [self copy_bcardImg:nsimg_1];
  2833. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2834. NSString *nsimg_2 = nil;
  2835. if(img_2==nil)
  2836. nsimg_2=@"";
  2837. else
  2838. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2839. [self copy_bcardImg:nsimg_2];
  2840. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2841. NSString *nsprice_type = nil;
  2842. if(price_type==nil)
  2843. nsprice_type=@"";
  2844. else
  2845. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2846. char *notes = (char*)sqlite3_column_text(statement, 22);
  2847. NSString *nsnotes = nil;
  2848. if(notes==nil)
  2849. nsnotes=@"";
  2850. else
  2851. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2852. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2853. NSString *nssalesrep = nil;
  2854. if(salesrep==nil)
  2855. nssalesrep=@"";
  2856. else
  2857. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2858. NSString *contact_type = [self textAtColumn:24 statement:statement];
  2859. {
  2860. // decrypt
  2861. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2862. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2863. nsphone=[AESCrypt fastdecrypt:nsphone];
  2864. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2865. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2866. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2867. }
  2868. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2869. [arr_name addObject:nsfirst_name];
  2870. [arr_name addObject:nslast_name];
  2871. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2872. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2873. [arr_ext addObject:nsaddr_1];
  2874. [arr_ext addObject:nsaddr_2];
  2875. [arr_ext addObject:nsaddr_3];
  2876. [arr_ext addObject:nsaddr_4];
  2877. [arr_ext addObject:@"\r\n"];
  2878. [arr_ext addObject:nscity];
  2879. [arr_ext addObject:nsstate];
  2880. [arr_ext addObject:nszipcode];
  2881. [arr_ext addObject:nscountry];
  2882. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2883. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2884. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2885. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2886. [item setValue:nscountry forKey:@"customer_country"];
  2887. [item setValue:nsphone forKey:@"customer_phone"];
  2888. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2889. [item setValue:nscompany_name forKey:@"customer_name"];
  2890. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2891. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2892. [item setValue:nsext forKey:@"customer_contact_ext"];
  2893. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2894. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2895. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2896. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2897. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2898. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2899. [item setValue:nslast_name forKey:@"customer_last_name"];
  2900. [item setValue:nscity forKey:@"customer_city"];
  2901. [item setValue:nsstate forKey:@"customer_state"];
  2902. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2903. [item setValue:contactId forKey:@"customer_cid"];
  2904. [item setValue:nscontact_name forKey:@"customer_contact"];
  2905. [item setValue:nsfax forKey:@"customer_fax"];
  2906. [item setValue:nsemail forKey:@"customer_email"];
  2907. [item setValue:contact_type forKey:@"customer_contact_type"];
  2908. [ret setObject:item forKey:@"customerInfo"];
  2909. // i++;
  2910. }
  2911. UIApplication * app = [UIApplication sharedApplication];
  2912. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2913. [ret setValue:appDelegate.mode forKey:@"mode"];
  2914. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2915. sqlite3_finalize(statement);
  2916. }
  2917. [iSalesDB close_db:db];
  2918. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2919. return ret;
  2920. }
  2921. + (bool) copy_bcardImg:(NSString*) filename
  2922. {
  2923. if(filename.length==0)
  2924. return false;
  2925. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2926. bool ret=false;
  2927. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2928. NSString *cachefolder = [paths objectAtIndex:0];
  2929. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2930. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2931. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2932. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2933. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2934. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2935. //
  2936. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2937. NSFileManager* fileManager = [NSFileManager defaultManager];
  2938. BOOL bdir=NO;
  2939. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2940. {
  2941. NSError *error = nil;
  2942. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2943. {
  2944. ret=false;
  2945. }
  2946. else
  2947. {
  2948. ret=true;
  2949. }
  2950. // NSError *error = nil;
  2951. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2952. //
  2953. // if(!bsuccess)
  2954. // {
  2955. // DebugLog(@"Create offline_createimg folder failed");
  2956. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2957. // return [RAUtils dict2data:ret];
  2958. // }
  2959. // if(bsuccess)
  2960. // {
  2961. // sqlite3 *db = [self get_db];
  2962. //
  2963. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2964. // [iSalesDB close_db:db];
  2965. // }
  2966. }
  2967. return ret;
  2968. //
  2969. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2970. // if(bsuccess)
  2971. // {
  2972. // NSError *error = nil;
  2973. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2974. // {
  2975. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2976. // }
  2977. // else
  2978. // {
  2979. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2980. //
  2981. // ret[@"img_url_aname"]=filename;
  2982. // ret[@"img_url"]=savedImagePath;
  2983. // }
  2984. // }
  2985. }
  2986. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2987. {
  2988. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2989. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2990. NSString *cachefolder = [paths objectAtIndex:0];
  2991. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2992. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2993. NSFileManager* fileManager = [NSFileManager defaultManager];
  2994. BOOL bdir=YES;
  2995. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2996. {
  2997. NSError *error = nil;
  2998. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2999. if(!bsuccess)
  3000. {
  3001. DebugLog(@"Create offline_createimg folder failed");
  3002. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3003. return [RAUtils dict2data:ret];
  3004. }
  3005. // if(bsuccess)
  3006. // {
  3007. // sqlite3 *db = [self get_db];
  3008. //
  3009. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3010. // [iSalesDB close_db:db];
  3011. // }
  3012. }
  3013. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3014. //JEPG格式
  3015. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3016. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3017. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3018. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3019. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3020. if(bsuccess)
  3021. {
  3022. NSError *error = nil;
  3023. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3024. {
  3025. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3026. }
  3027. else
  3028. {
  3029. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3030. ret[@"img_url_aname"]=filename;
  3031. ret[@"img_url"]=filename;
  3032. }
  3033. }
  3034. else
  3035. {
  3036. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3037. }
  3038. return [RAUtils dict2data:ret];
  3039. }
  3040. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3041. {
  3042. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3043. category = [category substringToIndex:3];
  3044. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3045. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3046. params[@"category"]= category;
  3047. ret[@"params"]= params;
  3048. [ret setValue:@"detail" forKey:@"target"];
  3049. [ret setValue:@"popup" forKey:@"action"];
  3050. [ret setValue:@"content" forKey:@"type"];
  3051. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3052. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3053. [ret setValue:@"true" forKey:@"single_row"];
  3054. [ret setValue:@"true" forKey:@"partial_refresh"];
  3055. // sqlite3 *db = [iSalesDB get_db];
  3056. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3057. 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 ;
  3058. sqlite3_stmt * statement;
  3059. int count = 0;
  3060. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3061. // int count=0;
  3062. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3063. {
  3064. int i=0;
  3065. while (sqlite3_step(statement) == SQLITE_ROW)
  3066. {
  3067. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3068. // char *name = (char*)sqlite3_column_text(statement, 1);
  3069. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3070. char *name = (char*)sqlite3_column_text(statement, 0);
  3071. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3072. int product_id = sqlite3_column_int(statement, 1);
  3073. char *url = (char*)sqlite3_column_text(statement, 2);
  3074. if(url==nil)
  3075. url="";
  3076. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3077. [item setValue:nsurl forKey:@"picture_path"];
  3078. [item setValue:nsname forKey:@"fash_name"];
  3079. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3080. [item setValue:category forKey:@"category"];
  3081. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3082. i++;
  3083. }
  3084. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3085. sqlite3_finalize(statement);
  3086. }
  3087. NSLog(@"count:%d",count);
  3088. // [iSalesDB close_db:db];
  3089. return ret;
  3090. }
  3091. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3092. {
  3093. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3094. [ret setValue:key forKey:@"key"];
  3095. [ret setValue:value forKey:@"val"];
  3096. return ret;
  3097. }
  3098. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3099. {
  3100. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3101. [ret setValue:@"0" forKey:@"img_count"];
  3102. // sqlite3 *db = [iSalesDB get_db];
  3103. 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 ;
  3104. sqlite3_stmt * statement;
  3105. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3106. {
  3107. int i=0;
  3108. if (sqlite3_step(statement) == SQLITE_ROW)
  3109. {
  3110. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3111. // char *name = (char*)sqlite3_column_text(statement, 1);
  3112. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3113. char *value = (char*)sqlite3_column_text(statement, 0);
  3114. if(value==nil)
  3115. value="";
  3116. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3117. char *key = (char*)sqlite3_column_text(statement, 1);
  3118. if(key==nil)
  3119. key="";
  3120. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3121. [item setValue:nsvalue forKey:@"val"];
  3122. [item setValue:nskey forKey:@"key"];
  3123. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3124. [ret setValue:@"1" forKey:@"count"];
  3125. i++;
  3126. }
  3127. sqlite3_finalize(statement);
  3128. }
  3129. // [iSalesDB close_db:db];
  3130. return ret;
  3131. }
  3132. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3133. {
  3134. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3135. [ret setValue:@"0" forKey:@"count"];
  3136. // sqlite3 *db = [iSalesDB get_db];
  3137. 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;
  3138. sqlite3_stmt * statement;
  3139. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3140. {
  3141. int i=0;
  3142. while (sqlite3_step(statement) == SQLITE_ROW)
  3143. {
  3144. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3145. // char *name = (char*)sqlite3_column_text(statement, 1);
  3146. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3147. char *value = (char*)sqlite3_column_text(statement, 0);
  3148. if(value==nil)
  3149. value="";
  3150. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3151. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3152. if(selector_display==nil)
  3153. selector_display="";
  3154. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3155. int product_id = sqlite3_column_int(statement, 2);
  3156. char *category = (char*)sqlite3_column_text(statement, 3);
  3157. if(category==nil)
  3158. category="";
  3159. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3160. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3161. [item setValue:nsvalue forKey:@"title"];
  3162. [item setValue:url forKey:@"pic_url"];
  3163. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3164. [params setValue:@"2" forKey:@"count"];
  3165. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3166. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3167. [param0 setValue:@"product_id" forKey:@"name"];
  3168. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3169. [param1 setValue:nscategory forKey:@"val"];
  3170. [param1 setValue:@"category" forKey:@"name"];
  3171. [params setObject:param0 forKey:@"param_0"];
  3172. [params setObject:param1 forKey:@"param_1"];
  3173. [item setObject:params forKey:@"params"];
  3174. [ret setValue:nsselector_display forKey:@"name"];
  3175. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3176. i++;
  3177. }
  3178. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3179. [ret setValue:@"switch" forKey:@"action"];
  3180. sqlite3_finalize(statement);
  3181. }
  3182. // [iSalesDB close_db:db];
  3183. return ret;
  3184. }
  3185. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3186. {
  3187. // model 在 category search 显示的图片。
  3188. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3189. // sqlite3 *db = [iSalesDB get_db];
  3190. 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];
  3191. sqlite3_stmt * statement;
  3192. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3193. {
  3194. while (sqlite3_step(statement) == SQLITE_ROW)
  3195. {
  3196. char *url = (char*)sqlite3_column_text(statement, 0);
  3197. if(url==nil)
  3198. url="";
  3199. int type = sqlite3_column_int(statement, 1);
  3200. if(type==0)
  3201. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3202. else
  3203. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3204. }
  3205. sqlite3_finalize(statement);
  3206. }
  3207. // [iSalesDB close_db:db];
  3208. return ret;
  3209. }
  3210. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3211. {
  3212. int item_id=-1;
  3213. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3214. sqlite3_stmt * statement;
  3215. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3216. {
  3217. if (sqlite3_step(statement) == SQLITE_ROW)
  3218. {
  3219. item_id = sqlite3_column_int(statement, 0);
  3220. }
  3221. sqlite3_finalize(statement);
  3222. }
  3223. return item_id;
  3224. }
  3225. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3226. {
  3227. // NSString* ret = @"";
  3228. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3229. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3230. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3231. sqlite3_stmt * statement;
  3232. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3233. {
  3234. while (sqlite3_step(statement) == SQLITE_ROW)
  3235. {
  3236. int bitem_id = sqlite3_column_int(statement, 0);
  3237. int bitem_qty = sqlite3_column_int(statement, 1);
  3238. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3239. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3240. }
  3241. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3242. sqlite3_finalize(statement);
  3243. }
  3244. // if(ret==nil)
  3245. // ret=@"";
  3246. *count=arr_count;
  3247. return arr_bundle;
  3248. }
  3249. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3250. {
  3251. // get default sold qty, return -1 if model not found;
  3252. int ret = -1;
  3253. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3254. sqlite3_stmt * statement;
  3255. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3256. {
  3257. if (sqlite3_step(statement) == SQLITE_ROW)
  3258. {
  3259. ret = sqlite3_column_int(statement, 0);
  3260. }
  3261. sqlite3_finalize(statement);
  3262. }
  3263. return ret;
  3264. }
  3265. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3266. {
  3267. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3268. [ret setValue:@"0" forKey:@"img_count"];
  3269. // sqlite3 *db = [iSalesDB get_db];
  3270. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3271. sqlite3_stmt * statement;
  3272. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3273. {
  3274. int i=0;
  3275. while (sqlite3_step(statement) == SQLITE_ROW)
  3276. {
  3277. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3278. // char *name = (char*)sqlite3_column_text(statement, 1);
  3279. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3280. char *url = (char*)sqlite3_column_text(statement, 0);
  3281. if(url==nil)
  3282. url="";
  3283. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3284. [item setValue:nsurl forKey:@"s"];
  3285. [item setValue:nsurl forKey:@"l"];
  3286. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3287. i++;
  3288. }
  3289. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3290. sqlite3_finalize(statement);
  3291. }
  3292. // [iSalesDB close_db:db];
  3293. return ret;
  3294. }
  3295. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3296. UIApplication * app = [UIApplication sharedApplication];
  3297. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3298. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3299. sqlite3 *db = [iSalesDB get_db];
  3300. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3301. NSString* product_id=params[@"product_id"];
  3302. NSString *item_count_str = params[@"item_count"];
  3303. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3304. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3305. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3306. // NSString *sql = @"";
  3307. for(int i=0;i<arr.count;i++)
  3308. {
  3309. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3310. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3311. __block int cart_count = 0;
  3312. if (!item_count_str) {
  3313. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3314. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3315. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3316. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3317. cart_count = [[model_set_components lastObject] integerValue];
  3318. }];
  3319. }
  3320. if(count==0)
  3321. {
  3322. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3323. sqlite3_stmt *stmt;
  3324. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3325. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3326. if (item_count_arr) {
  3327. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3328. } else {
  3329. sqlite3_bind_int(stmt,2,cart_count);
  3330. }
  3331. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3332. [iSalesDB execSql:@"ROLLBACK" db:db];
  3333. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3334. [iSalesDB close_db:db];
  3335. DebugLog(@"add to wishlist error");
  3336. return ret;
  3337. }
  3338. } else {
  3339. int qty = 0;
  3340. if (item_count_arr) {
  3341. qty = [item_count_arr[i] integerValue];
  3342. } else {
  3343. qty = cart_count;
  3344. }
  3345. 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]];
  3346. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3347. [iSalesDB execSql:@"ROLLBACK" db:db];
  3348. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3349. [iSalesDB close_db:db];
  3350. DebugLog(@"add to wishlist error");
  3351. return ret;
  3352. }
  3353. }
  3354. }
  3355. // [iSalesDB execSql:sql db:db];
  3356. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3357. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3358. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3359. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3360. [iSalesDB close_db:db];
  3361. appDelegate.wish_count =count;
  3362. [appDelegate update_count_mark];
  3363. ret[@"result"]= [NSNumber numberWithInt:2];
  3364. return ret;
  3365. }
  3366. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3367. // 0 category
  3368. // 1 search
  3369. // 2 itemsearch
  3370. NSData *ret = nil;
  3371. NSDictionary *items = nil;
  3372. switch (from) {
  3373. case 0:{
  3374. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  3375. }
  3376. break;
  3377. case 1:{
  3378. items = [[self search:params limited:NO] objectForKey:@"items"];
  3379. }
  3380. break;
  3381. case 2:{
  3382. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  3383. }
  3384. break;
  3385. default:
  3386. break;
  3387. }
  3388. if (!items) {
  3389. return ret;
  3390. }
  3391. int count = [[items objectForKey:@"count"] intValue];
  3392. NSMutableString *product_id_str = [@"" mutableCopy];
  3393. for (int i = 0; i < count; i++) {
  3394. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  3395. NSDictionary *item = [items objectForKey:key];
  3396. NSString *product_id = [item objectForKey:@"product_id"];
  3397. if (i == 0) {
  3398. [product_id_str appendString:product_id];
  3399. } else {
  3400. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  3401. }
  3402. }
  3403. NSString *add_to = [self valueInParams:params key:@"addTo"];
  3404. if ([add_to isEqualToString:@"cart"]) {
  3405. NSString *order_code = [params objectForKey:@"orderCode"];
  3406. if (order_code.length) {
  3407. NSDictionary *newParams = @{
  3408. @"product_id" : product_id_str,
  3409. @"orderCode" : order_code
  3410. };
  3411. ret = [self offline_add2cart:newParams.mutableCopy];
  3412. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3413. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3414. ret = [RAUtils dict2data:retDic];
  3415. }
  3416. } else if([add_to isEqualToString:@"wishlist"]) {
  3417. NSDictionary *newParams = @{
  3418. @"product_id" : product_id_str
  3419. };
  3420. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  3421. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  3422. ret = [RAUtils dict2data:retDic];
  3423. } else if([add_to isEqualToString:@"portfolio"]) {
  3424. }
  3425. return ret;
  3426. }
  3427. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  3428. {
  3429. return [self addAll:params from:0];
  3430. }
  3431. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  3432. {
  3433. return [self addAll:params from:1];
  3434. }
  3435. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3436. {
  3437. return [self addAll:params from:2];
  3438. }
  3439. #pragma mark - Jack
  3440. #warning 做SQL操作时转义!!
  3441. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3442. // "val_227" : {
  3443. // "check" : 1,
  3444. // "value" : "US United States",
  3445. // "value_id" : "228"
  3446. // },
  3447. if (!countryCode) {
  3448. countryCode = @"US";
  3449. }
  3450. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3451. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3452. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3453. int code_id = sqlite3_column_int(stmt, 3); // id
  3454. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3455. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3456. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3457. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3458. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3459. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3460. }
  3461. long n = *count;
  3462. *count = n + 1;
  3463. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3464. [container setValue:countryDic forKey:key];
  3465. }] mutableCopy];
  3466. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3467. return ret;
  3468. }
  3469. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3470. countryCode = [self translateSingleQuote:countryCode];
  3471. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3472. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3473. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3474. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3475. if (name == NULL) {
  3476. name = "";
  3477. }
  3478. if (code == NULL) {
  3479. code = "";
  3480. }
  3481. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3482. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3483. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3484. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3485. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3486. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3487. }
  3488. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3489. [container setValue:stateDic forKey:key];
  3490. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3491. DebugLog(@"query all state error: %@",err_msg);
  3492. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3493. // [stateDic setValue:@"Other" forKey:@"value"];
  3494. // [stateDic setValue:@"" forKey:@"value_id"];
  3495. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3496. //
  3497. // if (state_code && [@"" isEqualToString:state_code]) {
  3498. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3499. // }
  3500. //
  3501. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3502. // [container setValue:stateDic forKey:key];
  3503. }] mutableCopy];
  3504. // failure 可以不用了,一样的
  3505. if (ret.allKeys.count == 0) {
  3506. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3507. [stateDic setValue:@"Other" forKey:@"value"];
  3508. [stateDic setValue:@"" forKey:@"value_id"];
  3509. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3510. if (state_code && [@"" isEqualToString:state_code]) {
  3511. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3512. }
  3513. NSString *key = [NSString stringWithFormat:@"val_0"];
  3514. [ret setValue:stateDic forKey:key];
  3515. }
  3516. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3517. return ret;
  3518. }
  3519. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3520. codeId = [self translateSingleQuote:codeId];
  3521. 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];
  3522. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3523. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3524. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3525. if (name == NULL) {
  3526. name = "";
  3527. }
  3528. if (code == NULL) {
  3529. code = "";
  3530. }
  3531. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3532. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3533. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3534. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3535. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3536. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3537. }
  3538. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3539. [container setValue:stateDic forKey:key];
  3540. }] mutableCopy];
  3541. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3542. return ret;
  3543. }
  3544. + (NSDictionary *)offline_getPrice {
  3545. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3546. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3547. char *name = (char *) sqlite3_column_text(stmt, 1);
  3548. int type = sqlite3_column_int(stmt, 2);
  3549. int orderBy = sqlite3_column_int(stmt, 3);
  3550. if (name == NULL) {
  3551. name = "";
  3552. }
  3553. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3554. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3555. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3556. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3557. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3558. if (orderBy == 0) {
  3559. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3560. }
  3561. [container setValue:priceDic forKey:key];
  3562. }] mutableCopy];
  3563. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3564. return ret;
  3565. }
  3566. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  3567. NSString *sql = @"select _id,type_name from contact_type";
  3568. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  3569. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  3570. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3571. int _id = sqlite3_column_int(stmt, 0);
  3572. NSString *name = [self textAtColumn:1 statement:stmt];
  3573. NSDictionary *typeDic = @{
  3574. @"value_id" : name,
  3575. @"value" : name,
  3576. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  3577. };
  3578. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  3579. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  3580. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  3581. }];
  3582. return ret;
  3583. }
  3584. + (NSDictionary *)offline_getSalesRep {
  3585. // 首先从offline_login表中取出sales_code
  3586. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3587. NSString *user = app.user;
  3588. user = [self translateSingleQuote:user];
  3589. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3590. __block NSString *user_code = @"";
  3591. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3592. char *code = (char *)sqlite3_column_text(stmt, 0);
  3593. if (code == NULL) {
  3594. code = "";
  3595. }
  3596. user_code = [NSString stringWithUTF8String:code];
  3597. }];
  3598. // 再取所有salesRep
  3599. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3600. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3601. // 1 name 2 code 3 salesrep_id
  3602. char *name = (char *)sqlite3_column_text(stmt, 1);
  3603. char *code = (char *)sqlite3_column_text(stmt, 2);
  3604. int salesrep_id = sqlite3_column_int(stmt, 3);
  3605. if (name == NULL) {
  3606. name = "";
  3607. }
  3608. if (code == NULL) {
  3609. code = "";
  3610. }
  3611. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3612. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3613. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3614. // 比较code 相等则check
  3615. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3616. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3617. }
  3618. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3619. }] mutableCopy];
  3620. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3621. return ret;
  3622. }
  3623. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3624. zipcode = [self translateSingleQuote:zipcode];
  3625. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3626. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3627. char *country = (char *)sqlite3_column_text(stmt, 0);
  3628. char *state = (char *)sqlite3_column_text(stmt, 1);
  3629. char *city = (char *)sqlite3_column_text(stmt, 2);
  3630. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3631. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3632. if (country == NULL) {
  3633. country = "";
  3634. }
  3635. if (state == NULL) {
  3636. state = "";
  3637. }
  3638. if (city == NULL) {
  3639. city = "";
  3640. }
  3641. if (country_code == NULL) {
  3642. country_code = "";
  3643. }
  3644. if (state_code == NULL) {
  3645. state_code = "";
  3646. }
  3647. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3648. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3649. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3650. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3651. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3652. }] mutableCopy];
  3653. return ret;
  3654. }
  3655. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3656. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3657. [item setValue:value forKey:valueKey];
  3658. [dic setValue:item forKey:itemKey];
  3659. }
  3660. + (NSString *)countryCodeByid:(NSString *)code_id {
  3661. NSString *ret = nil;
  3662. code_id = [self translateSingleQuote:code_id];
  3663. sqlite3 *db = [iSalesDB get_db];
  3664. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3665. sqlite3_stmt * statement;
  3666. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3667. while (sqlite3_step(statement) == SQLITE_ROW) {
  3668. char *code = (char *)sqlite3_column_text(statement, 0);
  3669. if (code == NULL) {
  3670. code = "";
  3671. }
  3672. ret = [NSString stringWithUTF8String:code];
  3673. }
  3674. sqlite3_finalize(statement);
  3675. }
  3676. [iSalesDB close_db:db];
  3677. return ret;
  3678. }
  3679. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3680. NSString *ret = nil;
  3681. code = [self translateSingleQuote:code];
  3682. sqlite3 *db = [iSalesDB get_db];
  3683. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3684. sqlite3_stmt * statement;
  3685. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3686. while (sqlite3_step(statement) == SQLITE_ROW) {
  3687. char *_id = (char *)sqlite3_column_text(statement, 0);
  3688. if (_id == NULL) {
  3689. _id = "";
  3690. }
  3691. ret = [NSString stringWithFormat:@"%s",_id];
  3692. }
  3693. sqlite3_finalize(statement);
  3694. }
  3695. [iSalesDB close_db:db];
  3696. return ret;
  3697. }
  3698. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3699. NSString *name = nil;
  3700. codeId = [self translateSingleQuote:codeId];
  3701. sqlite3 *db = [iSalesDB get_db];
  3702. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3703. sqlite3_stmt * statement;
  3704. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3705. while (sqlite3_step(statement) == SQLITE_ROW) {
  3706. char *value = (char *)sqlite3_column_text(statement, 0);
  3707. if (value == NULL) {
  3708. value = "";
  3709. }
  3710. name = [NSString stringWithUTF8String:value];
  3711. }
  3712. sqlite3_finalize(statement);
  3713. }
  3714. [iSalesDB close_db:db];
  3715. return name;
  3716. }
  3717. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3718. NSString *ret = nil;
  3719. sqlite3 *db = [iSalesDB get_db];
  3720. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3721. sqlite3_stmt * statement;
  3722. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3723. while (sqlite3_step(statement) == SQLITE_ROW) {
  3724. char *name = (char *)sqlite3_column_text(statement, 0);
  3725. if (name == NULL) {
  3726. name = "";
  3727. }
  3728. ret = [NSString stringWithUTF8String:name];
  3729. }
  3730. sqlite3_finalize(statement);
  3731. }
  3732. [iSalesDB close_db:db];
  3733. return ret;
  3734. }
  3735. + (NSString *)salesRepCodeById:(NSString *)_id {
  3736. NSString *ret = nil;
  3737. _id = [self translateSingleQuote:_id];
  3738. sqlite3 *db = [iSalesDB get_db];
  3739. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3740. sqlite3_stmt * statement;
  3741. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3742. while (sqlite3_step(statement) == SQLITE_ROW) {
  3743. char *rep = (char *)sqlite3_column_text(statement, 0);
  3744. if (rep == NULL) {
  3745. rep = "";
  3746. }
  3747. ret = [NSString stringWithUTF8String:rep];
  3748. }
  3749. sqlite3_finalize(statement);
  3750. }
  3751. [iSalesDB close_db:db];
  3752. return ret;
  3753. }
  3754. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3755. char *tx = (char *)sqlite3_column_text(stmt, col);
  3756. if (tx == NULL) {
  3757. tx = "";
  3758. }
  3759. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3760. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3761. if (!text) {
  3762. text = @"";
  3763. }
  3764. // 将字符全部为' '的字符串干掉
  3765. int spaceCount = 0;
  3766. for (int i = 0; i < text.length; i++) {
  3767. if ([text characterAtIndex:i] == ' ') {
  3768. spaceCount++;
  3769. }
  3770. }
  3771. if (spaceCount == text.length) {
  3772. text = @"";
  3773. }
  3774. return text;
  3775. }
  3776. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3777. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3778. NSData *data = [NSData dataWithContentsOfFile:path];
  3779. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3780. return ret;
  3781. }
  3782. + (NSString *)textFileName:(NSString *)name {
  3783. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3784. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3785. if (!text) {
  3786. text = @"";
  3787. }
  3788. return text;
  3789. }
  3790. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3791. return [[dic objectForKey:key] mutableCopy];
  3792. }
  3793. + (id)translateSingleQuote:(NSString *)string {
  3794. if ([string isKindOfClass:[NSString class]])
  3795. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3796. return string;
  3797. }
  3798. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3799. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3800. }
  3801. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3802. NSString* ret= nil;
  3803. NSString *sqlQuery = nil;
  3804. // 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
  3805. 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];
  3806. sqlite3_stmt * statement;
  3807. // int count=0;
  3808. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3809. {
  3810. if (sqlite3_step(statement) == SQLITE_ROW)
  3811. {
  3812. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3813. if(imgurl==nil)
  3814. imgurl="";
  3815. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3816. ret=nsimgurl;
  3817. }
  3818. sqlite3_finalize(statement);
  3819. }
  3820. else
  3821. {
  3822. [ret setValue:@"8" forKey:@"result"];
  3823. }
  3824. // [iSalesDB close_db:db];
  3825. // DebugLog(@"data string: %@",ret );
  3826. return ret;
  3827. }
  3828. #pragma mark contact list
  3829. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3830. {
  3831. // contactType = "Sales_Order_Customer";
  3832. // limit = 25;
  3833. // offset = 0;
  3834. // password = 123456;
  3835. // "price_name" = 16;
  3836. // user = EvanK;
  3837. sqlite3 *db = [iSalesDB get_db];
  3838. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3839. if (contactType) {
  3840. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3841. } else {
  3842. contactType = @"1 = 1";
  3843. }
  3844. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3845. DebugLog(@"offline contact list keyword: %@",keyword);
  3846. // advanced search
  3847. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3848. if (contact_name) {
  3849. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3850. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3851. } else {
  3852. contact_name = @"";
  3853. }
  3854. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3855. if (customer_phone) {
  3856. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3857. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3858. } else {
  3859. customer_phone = @"";
  3860. }
  3861. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3862. if (customer_fax) {
  3863. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3864. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3865. } else {
  3866. customer_fax = @"";
  3867. }
  3868. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3869. if (customer_zipcode) {
  3870. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3871. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3872. } else {
  3873. customer_zipcode = @"";
  3874. }
  3875. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3876. if (customer_sales_rep) {
  3877. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3878. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3879. } else {
  3880. customer_sales_rep = @"";
  3881. }
  3882. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3883. if (customer_state) {
  3884. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3885. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3886. } else {
  3887. customer_state = @"";
  3888. }
  3889. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3890. if (customer_name) {
  3891. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3892. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3893. } else {
  3894. customer_name = @"";
  3895. }
  3896. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3897. if (customer_country) {
  3898. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3899. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3900. } else {
  3901. customer_country = @"";
  3902. }
  3903. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3904. if (customer_cid) {
  3905. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3906. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3907. } else {
  3908. customer_cid = @"";
  3909. }
  3910. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3911. if (customer_city) {
  3912. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3913. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3914. } else {
  3915. customer_city = @"";
  3916. }
  3917. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3918. if (customer_address) {
  3919. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3920. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3921. } else {
  3922. customer_address = @"";
  3923. }
  3924. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3925. if (customer_email) {
  3926. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3927. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3928. } else {
  3929. customer_email = @"";
  3930. }
  3931. NSString *price_name = [params valueForKey:@"price_name"];
  3932. if (price_name) {
  3933. if ([price_name containsString:@","]) {
  3934. // 首先从 price表中查处name
  3935. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3936. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3937. for (int i = 1;i < pArray.count;i++) {
  3938. NSString *p = pArray[i];
  3939. [mutablePStr appendFormat:@" or type = %@ ",p];
  3940. }
  3941. [mutablePStr appendString:@";"];
  3942. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3943. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3944. char *name = (char *)sqlite3_column_text(stmt, 0);
  3945. if (!name)
  3946. name = "";
  3947. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3948. }];
  3949. // 再根据name 拼sql
  3950. NSMutableString *mutable_price_name = [NSMutableString string];
  3951. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3952. for (int i = 1; i < price_name_array.count; i++) {
  3953. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3954. }
  3955. [mutable_price_name appendString:@")"];
  3956. price_name = mutable_price_name;
  3957. } else {
  3958. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3959. if ([price_name isEqualToString:@""]) {
  3960. price_name = @"";
  3961. } else {
  3962. __block NSString *price;
  3963. price_name = [self translateSingleQuote:price_name];
  3964. [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) {
  3965. char *p = (char *)sqlite3_column_text(stmt, 0);
  3966. if (p == NULL) {
  3967. p = "";
  3968. }
  3969. price = [NSString stringWithUTF8String:p];
  3970. }];
  3971. if ([price isEqualToString:@""]) {
  3972. price_name = @"";
  3973. } else {
  3974. price = [self translateSingleQuote:price];
  3975. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3976. }
  3977. }
  3978. }
  3979. } else {
  3980. price_name = @"";
  3981. }
  3982. int limit = [[params valueForKey:@"limit"] intValue];
  3983. int offset = [[params valueForKey:@"offset"] intValue];
  3984. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3985. 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];
  3986. 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];
  3987. // int result= [iSalesDB AddExFunction:db];
  3988. int count =0;
  3989. NSString *sqlQuery = nil;
  3990. if(keyword.length==0)
  3991. {
  3992. // 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];
  3993. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3994. sqlQuery = sql;
  3995. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3996. }
  3997. else
  3998. {
  3999. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4000. keyword = keyword.lowercaseString;
  4001. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4002. 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];
  4003. 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]];
  4004. }
  4005. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4006. sqlite3_stmt * statement;
  4007. [ret setValue:@"2" forKey:@"result"];
  4008. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4009. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4010. {
  4011. int i = 0;
  4012. while (sqlite3_step(statement) == SQLITE_ROW)
  4013. {
  4014. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4015. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4016. int editable = sqlite3_column_int(statement, 0);
  4017. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4018. NSString *nscompany_name =nil;
  4019. if(company_name==nil)
  4020. nscompany_name=@"";
  4021. else
  4022. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4023. char *country = (char*)sqlite3_column_text(statement, 2);
  4024. if(country==nil)
  4025. country="";
  4026. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4027. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4028. // if(addr==nil)
  4029. // addr="";
  4030. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4031. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4032. if(zipcode==nil)
  4033. zipcode="";
  4034. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4035. char *state = (char*)sqlite3_column_text(statement, 5);
  4036. if(state==nil)
  4037. state="";
  4038. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4039. char *city = (char*)sqlite3_column_text(statement, 6);
  4040. if(city==nil)
  4041. city="";
  4042. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4043. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4044. // NSString *nscontact_name = nil;
  4045. // if(contact_name==nil)
  4046. // nscontact_name=@"";
  4047. // else
  4048. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4049. char *phone = (char*)sqlite3_column_text(statement, 8);
  4050. NSString *nsphone = nil;
  4051. if(phone==nil)
  4052. nsphone=@"";
  4053. else
  4054. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4055. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4056. if(contact_id==nil)
  4057. contact_id="";
  4058. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4059. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4060. if(addr_1==nil)
  4061. addr_1="";
  4062. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4063. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4064. if(addr_2==nil)
  4065. addr_2="";
  4066. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4067. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4068. if(addr_3==nil)
  4069. addr_3="";
  4070. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4071. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4072. if(addr_4==nil)
  4073. addr_4="";
  4074. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4075. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4076. if(first_name==nil)
  4077. first_name="";
  4078. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4079. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4080. if(last_name==nil)
  4081. last_name="";
  4082. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4083. char *fax = (char*)sqlite3_column_text(statement, 16);
  4084. NSString *nsfax = nil;
  4085. if(fax==nil)
  4086. nsfax=@"";
  4087. else
  4088. {
  4089. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4090. if(nsfax.length>0)
  4091. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4092. }
  4093. char *email = (char*)sqlite3_column_text(statement, 17);
  4094. NSString *nsemail = nil;
  4095. if(email==nil)
  4096. nsemail=@"";
  4097. else
  4098. {
  4099. nsemail= [[NSString alloc]initWithUTF8String:email];
  4100. if(nsemail.length>0)
  4101. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4102. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4103. }
  4104. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4105. [arr_name addObject:nsfirst_name];
  4106. [arr_name addObject:nslast_name];
  4107. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4108. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4109. {
  4110. // decrypt
  4111. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4112. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4113. nsphone=[AESCrypt fastdecrypt:nsphone];
  4114. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4115. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4116. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4117. }
  4118. [arr_addr addObject:nscompany_name];
  4119. [arr_addr addObject:nscontact_name];
  4120. [arr_addr addObject:@"<br>"];
  4121. [arr_addr addObject:nsaddr_1];
  4122. [arr_addr addObject:nsaddr_2];
  4123. [arr_addr addObject:nsaddr_3];
  4124. [arr_addr addObject:nsaddr_4];
  4125. //[arr_addr addObject:nsaddr];
  4126. [arr_addr addObject:nszipcode];
  4127. [arr_addr addObject:nscity];
  4128. [arr_addr addObject:nsstate];
  4129. [arr_addr addObject:nscountry];
  4130. [arr_addr addObject:@"<br>"];
  4131. [arr_addr addObject:nsphone];
  4132. [arr_addr addObject:nsfax];
  4133. [arr_addr addObject:nsemail];
  4134. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4135. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4136. [item setValue:name forKey:@"name"];
  4137. [item setValue:nscontact_id forKey:@"contact_id"];
  4138. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4139. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4140. i++;
  4141. }
  4142. UIApplication * app = [UIApplication sharedApplication];
  4143. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4144. [ret setValue:appDelegate.mode forKey:@"mode"];
  4145. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4146. sqlite3_finalize(statement);
  4147. }
  4148. [iSalesDB close_db:db];
  4149. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4150. return ret;
  4151. }
  4152. #pragma mark contact Advanced search
  4153. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4154. {
  4155. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4156. return [RAUtils dict2data:contactAdvanceDic];
  4157. }
  4158. #pragma mark create new contact
  4159. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4160. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4161. NSData *data = [NSData dataWithContentsOfFile:path];
  4162. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4163. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4164. NSString *countryCode = nil;
  4165. NSString *countryCode_id = nil;
  4166. NSString *stateCode = nil;
  4167. NSString *city = nil;
  4168. NSString *zipCode = nil;
  4169. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4170. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4171. NSString *code_id = params[@"country"];
  4172. countryCode_id = code_id;
  4173. countryCode = [self countryCodeByid:code_id];
  4174. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4175. NSString *zip_code = params[@"zipcode"];
  4176. // 剔除全部为空格
  4177. int spaceCount = 0;
  4178. for (int i = 0; i < zip_code.length; i++) {
  4179. if ([zip_code characterAtIndex:i] == ' ') {
  4180. spaceCount++;
  4181. }
  4182. }
  4183. if (spaceCount == zip_code.length) {
  4184. zip_code = @"";
  4185. }
  4186. zipCode = zip_code;
  4187. if (zipCode.length > 0) {
  4188. countryCode_id = params[@"country"];
  4189. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4190. countryCode = [dic valueForKey:@"country_code"];
  4191. if (!countryCode) {
  4192. countryCode = @"US";
  4193. }
  4194. stateCode = [dic valueForKey:@"state_code"];
  4195. city = [dic valueForKey:@"city"];
  4196. // zip code
  4197. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4198. [zipDic setValue:zipCode forKey:@"value"];
  4199. [section_0 setValue:zipDic forKey:@"item_11"];
  4200. }
  4201. }
  4202. } else {
  4203. // default: US United States
  4204. countryCode = @"US";
  4205. countryCode_id = @"228";
  4206. }
  4207. // country
  4208. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4209. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4210. // state
  4211. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4212. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4213. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4214. // city
  4215. if (city) {
  4216. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4217. [cityDic setValue:city forKey:@"value"];
  4218. [section_0 setValue:cityDic forKey:@"item_13"];
  4219. }
  4220. // price type
  4221. NSDictionary *priceDic = [self offline_getPrice];
  4222. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4223. // contact type
  4224. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4225. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4226. // Sales Rep
  4227. NSDictionary *repDic = [self offline_getSalesRep];
  4228. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4229. [ret setValue:section_0 forKey:@"section_0"];
  4230. return [RAUtils dict2data:ret];
  4231. }
  4232. #pragma mark save
  4233. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4234. NSString *addr = nil;
  4235. NSString *contact_name = nil;
  4236. NSString *companyName = [params objectForKey:@"company"];
  4237. if (companyName) {
  4238. companyName = [AESCrypt fastencrypt:companyName];
  4239. } else {
  4240. companyName = @"";
  4241. }
  4242. DebugLog(@"company");
  4243. companyName = [self translateSingleQuote:companyName];
  4244. NSString *addr1 = [params objectForKey:@"address"];
  4245. NSString *addr2 = [params objectForKey:@"address2"];
  4246. NSString *addr3 = [params objectForKey:@"address_3"];
  4247. NSString *addr4 = [params objectForKey:@"address_4"];
  4248. if (!addr2) {
  4249. addr2 = @"";
  4250. }
  4251. if (!addr3) {
  4252. addr3 = @"";
  4253. }
  4254. if (!addr4) {
  4255. addr4 = @"";
  4256. }
  4257. if (!addr1) {
  4258. addr1 = @"";
  4259. }
  4260. DebugLog(@"addr");
  4261. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4262. addr = [AESCrypt fastencrypt:addr];
  4263. addr = [self translateSingleQuote:addr];
  4264. if (addr1 && ![addr1 isEqualToString:@""]) {
  4265. addr1 = [AESCrypt fastencrypt:addr1];
  4266. }
  4267. addr1 = [self translateSingleQuote:addr1];
  4268. addr2 = [self translateSingleQuote:addr2];
  4269. addr3 = [self translateSingleQuote:addr3];
  4270. addr4 = [self translateSingleQuote:addr4];
  4271. NSString *country = [params objectForKey:@"country"];
  4272. if (country) {
  4273. country = [self countryNameByCountryCodeId:country];
  4274. } else {
  4275. country = @"";
  4276. }
  4277. DebugLog(@"country");
  4278. country = [self translateSingleQuote:country];
  4279. NSString *state = [params objectForKey:@"state"];
  4280. if (!state) {
  4281. state = @"";
  4282. }
  4283. DebugLog(@"state");
  4284. state = [self translateSingleQuote:state];
  4285. NSString *city = [params objectForKey:@"city"];
  4286. if (!city) {
  4287. city = @"";
  4288. }
  4289. city = [self translateSingleQuote:city];
  4290. NSString *zipcode = [params objectForKey:@"zipcode"];
  4291. if (!zipcode) {
  4292. zipcode = @"";
  4293. }
  4294. DebugLog(@"zip");
  4295. zipcode = [self translateSingleQuote:zipcode];
  4296. NSString *fistName = [params objectForKey:@"firstname"];
  4297. if (!fistName) {
  4298. fistName = @"";
  4299. }
  4300. NSString *lastName = [params objectForKey:@"lastname"];
  4301. if (!lastName) {
  4302. lastName = @"";
  4303. }
  4304. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4305. DebugLog(@"contact_name");
  4306. contact_name = [self translateSingleQuote:contact_name];
  4307. fistName = [self translateSingleQuote:fistName];
  4308. lastName = [self translateSingleQuote:lastName];
  4309. NSString *phone = [params objectForKey:@"phone"];
  4310. if (phone) {
  4311. phone = [AESCrypt fastencrypt:phone];
  4312. } else {
  4313. phone = @"";
  4314. }
  4315. DebugLog(@"PHONE");
  4316. phone = [self translateSingleQuote:phone];
  4317. NSString *fax = [params objectForKey:@"fax"];
  4318. if (!fax) {
  4319. fax = @"";
  4320. }
  4321. DebugLog(@"FAX");
  4322. fax = [self translateSingleQuote:fax];
  4323. NSString *email = [params objectForKey:@"email"];
  4324. if (!email) {
  4325. email = @"";
  4326. }
  4327. DebugLog(@"EMAIL:%@",email);
  4328. email = [self translateSingleQuote:email];
  4329. NSString *notes = [params objectForKey:@"contact_notes"];
  4330. if (!notes) {
  4331. notes = @"";
  4332. }
  4333. DebugLog(@"NOTE:%@",notes);
  4334. notes = [self translateSingleQuote:notes];
  4335. NSString *price = [params objectForKey:@"price_name"];
  4336. if (price) {
  4337. price = [self priceNameByPriceId:price];
  4338. } else {
  4339. price = @"";
  4340. }
  4341. DebugLog(@"PRICE");
  4342. price = [self translateSingleQuote:price];
  4343. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4344. if (salesRep) {
  4345. salesRep = [self salesRepCodeById:salesRep];
  4346. } else {
  4347. salesRep = @"";
  4348. }
  4349. salesRep = [self translateSingleQuote:salesRep];
  4350. NSString *img = [params objectForKey:@"business_card"];
  4351. NSArray *array = [img componentsSeparatedByString:@","];
  4352. NSString *img_0 = array[0];
  4353. if (!img_0) {
  4354. img_0 = @"";
  4355. }
  4356. img_0 = [self translateSingleQuote:img_0];
  4357. NSString *img_1 = array[1];
  4358. if (!img_1) {
  4359. img_1 = @"";
  4360. }
  4361. img_1 = [self translateSingleQuote:img_1];
  4362. NSString *img_2 = array[2];
  4363. if (!img_2) {
  4364. img_2 = @"";
  4365. }
  4366. img_2 = [self translateSingleQuote:img_2];
  4367. NSString *contact_id = [NSUUID UUID].UUIDString;
  4368. NSString *contact_type = [params objectForKey:@"type_name"];
  4369. if (!contact_type) {
  4370. contact_type = @"";
  4371. }
  4372. contact_type = [self translateSingleQuote:contact_type];
  4373. // 判断更新时是否为customer
  4374. if (update) {
  4375. contact_id = [params objectForKey:@"contact_id"];
  4376. if (!contact_id) {
  4377. contact_id = @"";
  4378. }
  4379. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  4380. __block int customer = 0;
  4381. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4382. customer = sqlite3_column_int(stmt, 0);
  4383. }];
  4384. isCustomer = customer ? YES : NO;
  4385. }
  4386. NSMutableDictionary *sync_dic = [params mutableCopy];
  4387. if (isCustomer) {
  4388. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  4389. } else {
  4390. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  4391. }
  4392. NSString *sync_data = nil;
  4393. NSString *sql = nil;
  4394. if (update){
  4395. contact_id = [params objectForKey:@"contact_id"];
  4396. if (!contact_id) {
  4397. contact_id = @"";
  4398. }
  4399. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4400. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4401. sync_data = [RAUtils dict2string:sync_dic];
  4402. sync_data = [self translateSingleQuote:sync_data];
  4403. 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];
  4404. } else {
  4405. contact_id = [self translateSingleQuote:contact_id];
  4406. [sync_dic setValue:contact_id forKey:@"contact_id"];
  4407. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  4408. sync_data = [RAUtils dict2string:sync_dic];
  4409. sync_data = [self translateSingleQuote:sync_data];
  4410. 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];
  4411. }
  4412. int result = [iSalesDB execSql:sql];
  4413. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  4414. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  4415. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  4416. }
  4417. #pragma mark save new contact
  4418. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  4419. {
  4420. return [self offline_saveContact:params update:NO isCustomer:YES];
  4421. }
  4422. #pragma mark edit contact
  4423. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  4424. {
  4425. // {
  4426. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  4427. // password = 123456;
  4428. // user = EvanK;
  4429. // }
  4430. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  4431. NSData *data = [NSData dataWithContentsOfFile:path];
  4432. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4433. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4434. NSString *countryCode = nil;
  4435. NSString *countryCode_id = nil;
  4436. NSString *stateCode = nil;
  4437. /*------contact infor------*/
  4438. __block NSString *country = nil;
  4439. __block NSString *company_name = nil;
  4440. __block NSString *contact_id = params[@"contact_id"];
  4441. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  4442. __block NSString *zipcode = nil;
  4443. __block NSString *state = nil; // state_code
  4444. __block NSString *city = nil; //
  4445. __block NSString *firt_name,*last_name;
  4446. __block NSString *phone,*fax,*email;
  4447. __block NSString *notes,*price_type,*sales_rep;
  4448. __block NSString *img_0,*img_1,*img_2;
  4449. __block NSString *contact_type;
  4450. contact_id = [self translateSingleQuote:contact_id];
  4451. 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];
  4452. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4453. country = [self textAtColumn:0 statement:stmt]; // country name
  4454. company_name = [self textAtColumn:1 statement:stmt];
  4455. addr_1 = [self textAtColumn:2 statement:stmt];
  4456. addr_2 = [self textAtColumn:3 statement:stmt];
  4457. addr_3 = [self textAtColumn:4 statement:stmt];
  4458. addr_4 = [self textAtColumn:5 statement:stmt];
  4459. zipcode = [self textAtColumn:6 statement:stmt];
  4460. state = [self textAtColumn:7 statement:stmt]; // state code
  4461. city = [self textAtColumn:8 statement:stmt];
  4462. firt_name = [self textAtColumn:9 statement:stmt];
  4463. last_name = [self textAtColumn:10 statement:stmt];
  4464. phone = [self textAtColumn:11 statement:stmt];
  4465. fax = [self textAtColumn:12 statement:stmt];
  4466. email = [self textAtColumn:13 statement:stmt];
  4467. notes = [self textAtColumn:14 statement:stmt];
  4468. price_type = [self textAtColumn:15 statement:stmt]; // name
  4469. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4470. img_0 = [self textAtColumn:17 statement:stmt];
  4471. img_1 = [self textAtColumn:18 statement:stmt];
  4472. img_2 = [self textAtColumn:19 statement:stmt];
  4473. contact_type = [self textAtColumn:20 statement:stmt];
  4474. }];
  4475. // decrypt
  4476. if (company_name) {
  4477. company_name = [AESCrypt fastdecrypt:company_name];
  4478. }
  4479. if (addr_1) {
  4480. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4481. }
  4482. if (phone) {
  4483. phone = [AESCrypt fastdecrypt:phone];
  4484. }
  4485. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4486. countryCode = [iSalesDB jk_queryText:countrySql];
  4487. stateCode = state;
  4488. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4489. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4490. NSString *code_id = params[@"country"];
  4491. countryCode_id = code_id;
  4492. countryCode = [self countryCodeByid:code_id];
  4493. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4494. NSString *zip_code = params[@"zipcode"];
  4495. // 剔除全部为空格
  4496. int spaceCount = 0;
  4497. for (int i = 0; i < zip_code.length; i++) {
  4498. if ([zip_code characterAtIndex:i] == ' ') {
  4499. spaceCount++;
  4500. }
  4501. }
  4502. if (spaceCount == zip_code.length) {
  4503. zip_code = @"";
  4504. }
  4505. if (zipcode.length > 0) {
  4506. zipcode = zip_code;
  4507. countryCode_id = params[@"country"];
  4508. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4509. countryCode = [dic valueForKey:@"country_code"];
  4510. stateCode = [dic valueForKey:@"state_code"];
  4511. city = [dic valueForKey:@"city"];
  4512. // zip code
  4513. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4514. [zipDic setValue:zipcode forKey:@"value"];
  4515. [section_0 setValue:zipDic forKey:@"item_8"];
  4516. }
  4517. }
  4518. }
  4519. // 0 Country
  4520. // 1 Company Name
  4521. // 2 Contact ID
  4522. // 3 Picture
  4523. // 4 Address 1
  4524. // 5 Address 2
  4525. // 6 Address 3
  4526. // 7 Address 4
  4527. // 8 Zip Code
  4528. // 9 State/Province
  4529. // 10 City
  4530. // 11 Contact First Name
  4531. // 12 Contact Last Name
  4532. // 13 Phone
  4533. // 14 Fax
  4534. // 15 Email
  4535. // 16 Contact Notes
  4536. // 17 Price Type
  4537. // 18 Contact Type
  4538. // 19 Sales Rep
  4539. // country
  4540. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4541. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4542. // company
  4543. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4544. // contact_id
  4545. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4546. // picture
  4547. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4548. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4549. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4550. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4551. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4552. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4553. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4554. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4555. // addr 1 2 3 4
  4556. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4557. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4558. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4559. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4560. // zip code
  4561. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4562. // state
  4563. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4564. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4565. // city
  4566. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4567. // first last
  4568. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4569. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4570. // phone fax email
  4571. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4572. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4573. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4574. // notes
  4575. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4576. // price
  4577. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4578. for (NSString *key in priceDic.allKeys) {
  4579. if ([key containsString:@"val_"]) {
  4580. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4581. if ([dic[@"value"] isEqualToString:price_type]) {
  4582. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4583. [priceDic setValue:dic forKey:key];
  4584. }
  4585. }
  4586. }
  4587. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4588. // Contact Rep
  4589. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  4590. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4591. // Sales Rep
  4592. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4593. for (NSString *key in repDic.allKeys) {
  4594. if ([key containsString:@"val_"]) {
  4595. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4596. NSString *value = dic[@"value"];
  4597. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4598. if (code && [code isEqualToString:sales_rep]) {
  4599. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4600. [repDic setValue:dic forKey:key];
  4601. }
  4602. }
  4603. }
  4604. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  4605. [ret setValue:section_0 forKey:@"section_0"];
  4606. return [RAUtils dict2data:ret];
  4607. }
  4608. #pragma mark save contact
  4609. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4610. {
  4611. return [self offline_saveContact:params update:YES isCustomer:YES];
  4612. }
  4613. #pragma mark category
  4614. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4615. if (ck) {
  4616. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4617. for (NSString *key in res.allKeys) {
  4618. if (![key isEqualToString:@"count"]) {
  4619. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4620. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4621. if ([val[@"value"] isEqualToString:ck]) {
  4622. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4623. }
  4624. [res setValue:val forKey:key];
  4625. }
  4626. }
  4627. [dic setValue:res forKey:valueKey];
  4628. }
  4629. }
  4630. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4631. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4632. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4633. NSString* category = [params valueForKey:@"category"];
  4634. if (!category || [category isEqualToString:@""]) {
  4635. category = @"%";
  4636. }
  4637. category = [self translateSingleQuote:category];
  4638. int limit = [[params valueForKey:@"limit"] intValue];
  4639. int offset = [[params valueForKey:@"offset"] intValue];
  4640. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4641. NSString *limit_str = @"";
  4642. if (limited) {
  4643. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4644. }
  4645. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4646. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4647. sqlite3 *db = [iSalesDB get_db];
  4648. // [iSalesDB AddExFunction:db];
  4649. int count;
  4650. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4651. 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];
  4652. double price_min = 0;
  4653. double price_max = 0;
  4654. if ([params.allKeys containsObject:@"alert"]) {
  4655. // alert
  4656. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4657. NSString *alert = params[@"alert"];
  4658. if ([alert isEqualToString:@"Display All"]) {
  4659. alert = [NSString stringWithFormat:@""];
  4660. } else {
  4661. alert = [self translateSingleQuote:alert];
  4662. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4663. }
  4664. // available
  4665. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4666. NSString *available = params[@"available"];
  4667. NSString *available_condition;
  4668. if ([available isEqualToString:@"Display All"]) {
  4669. available_condition = @"";
  4670. } else if ([available isEqualToString:@"Available Now"]) {
  4671. available_condition = @"and availability > 0";
  4672. } else {
  4673. available_condition = @"and availability == 0";
  4674. }
  4675. // best seller
  4676. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4677. NSString *best_seller = @"";
  4678. NSString *order_best_seller = @"m.name asc";
  4679. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4680. best_seller = @"and best_seller > 0";
  4681. order_best_seller = @"m.best_seller desc,m.name asc";
  4682. }
  4683. // price
  4684. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4685. NSString *price = params[@"price"];
  4686. price_min = 0;
  4687. price_max = MAXFLOAT;
  4688. if (appDelegate.user) {
  4689. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4690. NSMutableString *priceName = [NSMutableString string];
  4691. for (int i = 0; i < priceTypeArray.count; i++) {
  4692. NSString *pricetype = priceTypeArray[i];
  4693. pricetype = [self translateSingleQuote:pricetype];
  4694. if (i == 0) {
  4695. [priceName appendFormat:@"'%@'",pricetype];
  4696. } else {
  4697. [priceName appendFormat:@",'%@'",pricetype];
  4698. }
  4699. }
  4700. if ([price isEqualToString:@"Display All"]) {
  4701. price = [NSString stringWithFormat:@""];
  4702. } else if([price containsString:@"+"]){
  4703. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4704. price_min = [price doubleValue];
  4705. 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];
  4706. } else {
  4707. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4708. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4709. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4710. 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];
  4711. }
  4712. } else {
  4713. price = @"";
  4714. }
  4715. // sold_by_qty : Sold in quantities of %@
  4716. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4717. NSString *qty = params[@"sold_by_qty"];
  4718. if ([qty isEqualToString:@"Display All"]) {
  4719. qty = @"";
  4720. } else {
  4721. qty = [self translateSingleQuote:qty];
  4722. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4723. }
  4724. // cate
  4725. category = [self translateSingleQuote:category];
  4726. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4727. // where bestseller > 0 order by bestseller desc
  4728. // sql query: alert availability(int) best_seller(int) price qty
  4729. 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];
  4730. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4731. }
  4732. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4733. if (!appDelegate.user) {
  4734. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4735. }
  4736. [ret setValue:filter forKey:@"filter"];
  4737. DebugLog(@"offline_category sql:%@",sqlQuery);
  4738. sqlite3_stmt * statement;
  4739. [ret setValue:@"2" forKey:@"result"];
  4740. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4741. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4742. // int count=0;
  4743. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4744. {
  4745. int i=0;
  4746. while (sqlite3_step(statement) == SQLITE_ROW)
  4747. {
  4748. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4749. char *name = (char*)sqlite3_column_text(statement, 0);
  4750. if(name==nil)
  4751. name="";
  4752. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4753. char *description = (char*)sqlite3_column_text(statement, 1);
  4754. if(description==nil)
  4755. description="";
  4756. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4757. int product_id = sqlite3_column_int(statement, 2);
  4758. int wid = sqlite3_column_int(statement, 3);
  4759. int closeout = sqlite3_column_int(statement, 4);
  4760. int cid = sqlite3_column_int(statement, 5);
  4761. int wisdelete = sqlite3_column_int(statement, 6);
  4762. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4763. if(wid !=0 && wisdelete != 1)
  4764. [item setValue:@"true" forKey:@"wish_exists"];
  4765. else
  4766. [item setValue:@"false" forKey:@"wish_exists"];
  4767. if(closeout==0)
  4768. [item setValue:@"false" forKey:@"is_closeout"];
  4769. else
  4770. [item setValue:@"true" forKey:@"is_closeout"];
  4771. if(cid==0)
  4772. [item setValue:@"false" forKey:@"cart_exists"];
  4773. else
  4774. [item setValue:@"true" forKey:@"cart_exists"];
  4775. [item addEntriesFromDictionary:imgjson];
  4776. // [item setValue:nsurl forKey:@"img"];
  4777. [item setValue:nsname forKey:@"name"];
  4778. [item setValue:nsdescription forKey:@"description"];
  4779. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4780. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4781. i++;
  4782. }
  4783. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4784. [ret setObject:items forKey:@"items"];
  4785. sqlite3_finalize(statement);
  4786. } else {
  4787. DebugLog(@"nothing...");
  4788. }
  4789. DebugLog(@"count:%d",count);
  4790. [iSalesDB close_db:db];
  4791. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4792. return ret;
  4793. }
  4794. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4795. {
  4796. return [self categoryList:params limited:YES];
  4797. }
  4798. # pragma mark item search
  4799. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4800. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4801. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4802. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4803. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4804. // category
  4805. NSDictionary *category_menu = [self offline_category_menu];
  4806. [filter setValue:category_menu forKey:@"category"];
  4807. NSString* where= nil;
  4808. NSString* orderby= @"m.name";
  4809. if (!filterSearch) {
  4810. int covertype = [[params valueForKey:@"covertype"] intValue];
  4811. switch (covertype) {
  4812. case 0:
  4813. {
  4814. where=@"m.category like'%%#005#%%'";
  4815. break;
  4816. }
  4817. case 1:
  4818. {
  4819. where=@"m.alert like '%QS%'";
  4820. break;
  4821. }
  4822. case 2:
  4823. {
  4824. where=@"m.availability>0";
  4825. break;
  4826. }
  4827. case 3:
  4828. {
  4829. where=@"m.best_seller>0";
  4830. orderby=@"m.best_seller desc,m.name asc";
  4831. break;
  4832. }
  4833. default:
  4834. where=@"1=1";
  4835. break;
  4836. }
  4837. }
  4838. int limit = [[params valueForKey:@"limit"] intValue];
  4839. int offset = [[params valueForKey:@"offset"] intValue];
  4840. NSString *limit_str = @"";
  4841. if (limited) {
  4842. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4843. }
  4844. sqlite3 *db = [iSalesDB get_db];
  4845. // [iSalesDB AddExFunction:db];
  4846. int count;
  4847. NSString *sqlQuery = nil;
  4848. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4849. 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];
  4850. double price_min = 0;
  4851. double price_max = 0;
  4852. if (filterSearch) {
  4853. // alert
  4854. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4855. NSString *alert = params[@"alert"];
  4856. if ([alert isEqualToString:@"Display All"]) {
  4857. alert = [NSString stringWithFormat:@""];
  4858. } else {
  4859. alert = [self translateSingleQuote:alert];
  4860. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4861. }
  4862. // available
  4863. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4864. NSString *available = params[@"available"];
  4865. NSString *available_condition;
  4866. if ([available isEqualToString:@"Display All"]) {
  4867. available_condition = @"";
  4868. } else if ([available isEqualToString:@"Available Now"]) {
  4869. available_condition = @"and availability > 0";
  4870. } else {
  4871. available_condition = @"and availability == 0";
  4872. }
  4873. // best seller
  4874. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4875. NSString *best_seller = @"";
  4876. NSString *order_best_seller = @"m.name asc";
  4877. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4878. best_seller = @"and best_seller > 0";
  4879. order_best_seller = @"m.best_seller desc,m.name asc";
  4880. }
  4881. // price
  4882. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4883. NSString *price = params[@"price"];
  4884. price_min = 0;
  4885. price_max = MAXFLOAT;
  4886. if (appDelegate.user) {
  4887. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4888. NSMutableString *priceName = [NSMutableString string];
  4889. for (int i = 0; i < priceTypeArray.count; i++) {
  4890. NSString *pricetype = priceTypeArray[i];
  4891. pricetype = [self translateSingleQuote:pricetype];
  4892. if (i == 0) {
  4893. [priceName appendFormat:@"'%@'",pricetype];
  4894. } else {
  4895. [priceName appendFormat:@",'%@'",pricetype];
  4896. }
  4897. }
  4898. if ([price isEqualToString:@"Display All"]) {
  4899. price = [NSString stringWithFormat:@""];
  4900. } else if([price containsString:@"+"]){
  4901. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4902. price_min = [price doubleValue];
  4903. 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];
  4904. } else {
  4905. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4906. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4907. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4908. 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];
  4909. }
  4910. } else {
  4911. price = @"";
  4912. }
  4913. // sold_by_qty : Sold in quantities of %@
  4914. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4915. NSString *qty = params[@"sold_by_qty"];
  4916. if ([qty isEqualToString:@"Display All"]) {
  4917. qty = @"";
  4918. } else {
  4919. qty = [self translateSingleQuote:qty];
  4920. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4921. }
  4922. // category
  4923. NSString *category_id = params[@"ctgId"];
  4924. NSMutableArray *cate_id_array = nil;
  4925. NSMutableString *cateWhere = [NSMutableString string];
  4926. if ([category_id isEqualToString:@""] || !category_id) {
  4927. [cateWhere appendString:@"1 = 1"];
  4928. } else {
  4929. if ([category_id containsString:@","]) {
  4930. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4931. } else {
  4932. cate_id_array = [@[category_id] mutableCopy];
  4933. }
  4934. /*-----------*/
  4935. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4936. for (int i = 0; i < cate_id_array.count; i++) {
  4937. for (NSString *key0 in cateDic.allKeys) {
  4938. if ([key0 containsString:@"category_"]) {
  4939. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4940. for (NSString *key1 in category0.allKeys) {
  4941. if ([key1 containsString:@"category_"]) {
  4942. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4943. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4944. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4945. cate_id_array[i] = [category1 objectForKey:@"id"];
  4946. if (i == 0) {
  4947. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4948. } else {
  4949. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4950. }
  4951. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4952. [category0 setValue:category1 forKey:key1];
  4953. [cateDic setValue:category0 forKey:key0];
  4954. }
  4955. }
  4956. }
  4957. }
  4958. }
  4959. }
  4960. [filter setValue:cateDic forKey:@"category"];
  4961. }
  4962. // where bestseller > 0 order by bestseller desc
  4963. // sql query: alert availability(int) best_seller(int) price qty
  4964. 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];
  4965. // count
  4966. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4967. }
  4968. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4969. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4970. if (!appDelegate.user) {
  4971. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4972. }
  4973. [ret setValue:filter forKey:@"filter"];
  4974. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4975. sqlite3_stmt * statement;
  4976. [ret setValue:@"2" forKey:@"result"];
  4977. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4978. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4979. // int count=0;
  4980. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4981. {
  4982. int i=0;
  4983. while (sqlite3_step(statement) == SQLITE_ROW)
  4984. {
  4985. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4986. char *name = (char*)sqlite3_column_text(statement, 0);
  4987. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4988. char *description = (char*)sqlite3_column_text(statement, 1);
  4989. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4990. int product_id = sqlite3_column_int(statement, 2);
  4991. int wid = sqlite3_column_int(statement, 3);
  4992. int closeout = sqlite3_column_int(statement, 4);
  4993. int cid = sqlite3_column_int(statement, 5);
  4994. int wisdelete = sqlite3_column_int(statement, 6);
  4995. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4996. if(wid !=0 && wisdelete != 1)
  4997. [item setValue:@"true" forKey:@"wish_exists"];
  4998. else
  4999. [item setValue:@"false" forKey:@"wish_exists"];
  5000. if(closeout==0)
  5001. [item setValue:@"false" forKey:@"is_closeout"];
  5002. else
  5003. [item setValue:@"true" forKey:@"is_closeout"];
  5004. if(cid==0)
  5005. [item setValue:@"false" forKey:@"cart_exists"];
  5006. else
  5007. [item setValue:@"true" forKey:@"cart_exists"];
  5008. [item addEntriesFromDictionary:imgjson];
  5009. // [item setValue:nsurl forKey:@"img"];
  5010. [item setValue:nsname forKey:@"fash_name"];
  5011. [item setValue:nsdescription forKey:@"description"];
  5012. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5013. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5014. i++;
  5015. }
  5016. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5017. [ret setObject:items forKey:@"items"];
  5018. sqlite3_finalize(statement);
  5019. }
  5020. [iSalesDB close_db:db];
  5021. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5022. return ret;
  5023. }
  5024. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5025. {
  5026. return [self itemsearch:params limited:YES];
  5027. }
  5028. #pragma mark order detail
  5029. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5030. if (str1.length == 0) {
  5031. str1 = @"&nbsp";
  5032. }
  5033. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5034. return str;
  5035. }
  5036. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5037. {
  5038. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5039. [fromformatter setDateFormat:from];
  5040. NSDate *date = [fromformatter dateFromString:datetime];
  5041. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5042. [toformatter setDateFormat:to];
  5043. NSString * ret = [toformatter stringFromDate:date];
  5044. return ret;
  5045. }
  5046. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5047. // 把毫秒去掉
  5048. if ([dateTime containsString:@"."]) {
  5049. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5050. }
  5051. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5052. formatter.dateFormat = formate;
  5053. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5054. NSDate *date = [formatter dateFromString:dateTime];
  5055. formatter.dateFormat = newFormate;
  5056. NSString *result = [formatter stringFromDate:date];
  5057. return result ? result : @"";
  5058. }
  5059. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5060. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5061. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5062. }
  5063. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5064. {
  5065. DebugLog(@"offline oderdetail params: %@",params);
  5066. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5067. int orderId = [params[@"orderId"] intValue];
  5068. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5069. // decrypt card number and card security code
  5070. // 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 ];
  5071. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5072. sqlite3 *db = [iSalesDB get_db];
  5073. sqlite3_stmt * statement;
  5074. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5075. NSString *nssoid = @"";
  5076. NSString* orderinfo = @"";
  5077. NSString *moreInfo = @"";
  5078. double handlingFee = 0;
  5079. double payments_and_credist = 0;
  5080. double totalPrice = 0;
  5081. double shippingFee = 0;
  5082. double lift_gate = 0;
  5083. NSString *customer_contact = @"";
  5084. NSString *customer_email = @"";
  5085. NSString *customer_fax = @"";
  5086. NSString *customer_phone = @"";
  5087. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5088. {
  5089. if (sqlite3_step(statement) == SQLITE_ROW)
  5090. {
  5091. int order_id = sqlite3_column_int(statement, 0);
  5092. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5093. ret[@"sign_url"] = sign_url;
  5094. customer_contact = [self textAtColumn:52 statement:statement];
  5095. customer_email = [self textAtColumn:53 statement:statement];
  5096. customer_phone = [self textAtColumn:54 statement:statement];
  5097. customer_fax = [self textAtColumn:55 statement:statement];
  5098. int offline_edit=sqlite3_column_int(statement, 56);
  5099. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5100. char *soid = (char*)sqlite3_column_text(statement, 1);
  5101. if(soid==nil)
  5102. soid= "";
  5103. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5104. // so#
  5105. ret[@"so#"] = nssoid;
  5106. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5107. if(poNumber==nil)
  5108. poNumber= "";
  5109. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5110. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5111. if(create_time==nil)
  5112. create_time= "";
  5113. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5114. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5115. int status = sqlite3_column_int(statement, 4);
  5116. int erpStatus = sqlite3_column_int(statement, 49);
  5117. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5118. // status
  5119. if (status > 1 && status != 3) {
  5120. status = erpStatus;
  5121. } else if (status == 3) {
  5122. status = 15;
  5123. }
  5124. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5125. ret[@"order_status"] = nsstatus;
  5126. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5127. if(company_name==nil)
  5128. company_name= "";
  5129. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5130. // company name
  5131. ret[@"company_name"] = nscompany_name;
  5132. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5133. if(customer_contact==nil)
  5134. customer_contact= "";
  5135. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5136. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5137. if(addr_1==nil)
  5138. addr_1="";
  5139. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5140. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5141. if(addr_2==nil)
  5142. addr_2="";
  5143. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5144. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5145. if(addr_3==nil)
  5146. addr_3="";
  5147. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5148. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5149. if(addr_4==nil)
  5150. addr_4="";
  5151. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5152. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5153. [arr_addr addObject:nsaddr_1];
  5154. [arr_addr addObject:nsaddr_2];
  5155. [arr_addr addObject:nsaddr_3];
  5156. [arr_addr addObject:nsaddr_4];
  5157. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  5158. char *logist = (char*)sqlite3_column_text(statement, 11);
  5159. if(logist==nil)
  5160. logist= "";
  5161. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5162. if (status == -11 || status == 10 || status == 11) {
  5163. nslogist = [self textAtColumn:59 statement:statement];
  5164. };
  5165. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5166. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5167. shipping = @"Shipping To Be Quoted";
  5168. } else {
  5169. shippingFee = sqlite3_column_double(statement, 12);
  5170. }
  5171. // Shipping
  5172. ret[@"Shipping"] = shipping;
  5173. int have_lift_gate = sqlite3_column_int(statement, 17);
  5174. lift_gate = sqlite3_column_double(statement, 13);
  5175. // Liftgate Fee(No loading dock)
  5176. if (!have_lift_gate) {
  5177. lift_gate = 0;
  5178. }
  5179. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5180. if (sqlite3_column_int(statement, 57)) {
  5181. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5182. }
  5183. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5184. if(general_notes==nil)
  5185. general_notes= "";
  5186. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5187. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5188. if(internal_notes==nil)
  5189. internal_notes= "";
  5190. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5191. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5192. if(payment_type==nil)
  5193. payment_type= "";
  5194. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5195. // order info
  5196. orderinfo = [self textFileName:@"order_info.html"];
  5197. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5198. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5199. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5200. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5201. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5202. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5203. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5204. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5205. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5206. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5207. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5208. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5209. NSString *payment = nil;
  5210. if([nspayment_type isEqualToString:@"Credit Card"])
  5211. {
  5212. payment = [self textFileName:@"creditcardpayment.html"];
  5213. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5214. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5215. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5216. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5217. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5218. NSString *card_type = [self textAtColumn:42 statement:statement];
  5219. if (card_type.length > 0) { // 显示星号
  5220. card_type = @"****";
  5221. }
  5222. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5223. if (card_number.length > 0 && card_number.length > 4) {
  5224. for (int i = 0; i < card_number.length - 4; i++) {
  5225. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5226. }
  5227. } else {
  5228. card_number = @"";
  5229. }
  5230. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  5231. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  5232. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  5233. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  5234. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  5235. card_expiration = @"****";
  5236. }
  5237. NSString *card_city = [self textAtColumn:46 statement:statement];
  5238. NSString *card_state = [self textAtColumn:47 statement:statement];
  5239. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  5240. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  5241. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  5242. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  5243. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  5244. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  5245. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  5246. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  5247. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  5248. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  5249. }
  5250. else
  5251. {
  5252. payment=[self textFileName:@"normalpayment.html"];
  5253. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  5254. }
  5255. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  5256. ret[@"result"]= [NSNumber numberWithInt:2];
  5257. // more info
  5258. moreInfo = [self textFileName:@"more_info.html"];
  5259. /*****ship to******/
  5260. // ShipToCompany_or_&nbsp
  5261. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  5262. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  5263. NSString *shipToName = [self textAtColumn:19 statement:statement];
  5264. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  5265. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  5266. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  5267. /*****ship from******/
  5268. // ShipFromCompany_or_&nbsp
  5269. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  5270. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  5271. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  5272. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  5273. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  5274. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  5275. /*****freight to******/
  5276. // FreightBillToCompany_or_&nbsp
  5277. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  5278. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  5279. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  5280. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  5281. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  5282. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  5283. /*****merchandise to******/
  5284. // MerchandiseBillToCompany_or_&nbsp
  5285. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  5286. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  5287. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  5288. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  5289. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  5290. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  5291. /*****return to******/
  5292. // ReturnToCompany_or_&nbsp
  5293. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  5294. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  5295. NSString *returnToName = [self textAtColumn:31 statement:statement];
  5296. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  5297. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  5298. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  5299. //
  5300. // DebugLog(@"more info : %@",moreInfo);
  5301. // handling fee
  5302. handlingFee = sqlite3_column_double(statement, 33);
  5303. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  5304. if (sqlite3_column_int(statement, 58)) {
  5305. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  5306. }
  5307. //
  5308. // customer info
  5309. customerID = [self textAtColumn:36 statement:statement];
  5310. // mode
  5311. ret[@"mode"] = appDelegate.mode;
  5312. // model_count
  5313. ret[@"model_count"] = @(0);
  5314. }
  5315. sqlite3_finalize(statement);
  5316. }
  5317. [iSalesDB close_db:db];
  5318. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  5319. // "customer_email" = "Shui Hu";
  5320. // "customer_fax" = "";
  5321. // "customer_first_name" = F;
  5322. // "customer_last_name" = L;
  5323. // "customer_name" = ",da He Xiang Dong Liu A";
  5324. // "customer_phone" = "Hey Xuan Feng";
  5325. contactInfo[@"customer_phone"] = customer_phone;
  5326. contactInfo[@"customer_fax"] = customer_fax;
  5327. contactInfo[@"customer_email"] = customer_email;
  5328. NSString *first_name = @"";
  5329. NSString *last_name = @"";
  5330. if ([customer_contact isEqualToString:@""]) {
  5331. } else if ([customer_contact containsString:@" "]) {
  5332. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  5333. first_name = [customer_contact substringToIndex:first_space_index];
  5334. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  5335. }
  5336. contactInfo[@"customer_first_name"] = first_name;
  5337. contactInfo[@"customer_last_name"] = last_name;
  5338. ret[@"customerInfo"] = contactInfo;
  5339. // models
  5340. if (nssoid) {
  5341. __block double TotalCuft = 0;
  5342. __block double TotalWeight = 0;
  5343. __block int TotalCarton = 0;
  5344. __block double allItemPrice = 0;
  5345. 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];
  5346. sqlite3 *db1 = [iSalesDB get_db];
  5347. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5348. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5349. int product_id = sqlite3_column_int(stmt, 0);
  5350. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5351. int item_id = sqlite3_column_int(stmt, 7);
  5352. NSString* Price=nil;
  5353. if(str_price==nil)
  5354. {
  5355. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5356. if(price==nil)
  5357. Price=@"No Price.";
  5358. else
  5359. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5360. }
  5361. else
  5362. {
  5363. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5364. }
  5365. double discount = sqlite3_column_double(stmt, 2);
  5366. int item_count = sqlite3_column_int(stmt, 3);
  5367. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  5368. NSString *nsline_note=nil;
  5369. if(line_note!=nil)
  5370. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  5371. // char *name = (char*)sqlite3_column_text(stmt, 5);
  5372. // NSString *nsname = nil;
  5373. // if(name!=nil)
  5374. // nsname= [[NSString alloc]initWithUTF8String:name];
  5375. NSString *nsname = [self textAtColumn:5 statement:stmt];
  5376. // char *description = (char*)sqlite3_column_text(stmt, 6);
  5377. // NSString *nsdescription=nil;
  5378. // if(description!=nil)
  5379. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  5380. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  5381. // int stockUom = sqlite3_column_int(stmt, 8);
  5382. // int _id = sqlite3_column_int(stmt, 9);
  5383. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  5384. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5385. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5386. int carton=[bsubtotaljson[@"carton"] intValue];
  5387. TotalCuft += cuft;
  5388. TotalWeight += weight;
  5389. TotalCarton += carton;
  5390. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5391. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  5392. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  5393. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  5394. itemjson[@"note"]=nsline_note;
  5395. itemjson[@"origin_price"] = Price;
  5396. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5397. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  5398. itemjson[@"order_item_status"] = @""; // 暂时不处理
  5399. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5400. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5401. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  5402. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5403. if(itemjson[@"combine"] != nil)
  5404. {
  5405. // int citem=0;
  5406. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5407. for(int bc=0;bc<bcount;bc++)
  5408. {
  5409. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5410. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5411. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5412. subTotal += uprice * modulus * item_count;
  5413. }
  5414. }
  5415. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5416. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  5417. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  5418. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  5419. allItemPrice += subTotal;
  5420. }];
  5421. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  5422. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  5423. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  5424. // payments/Credits
  5425. // payments_and_credist = sqlite3_column_double(statement, 34);
  5426. payments_and_credist = allItemPrice;
  5427. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5428. // // total
  5429. // totalPrice = sqlite3_column_double(statement, 35);
  5430. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5431. } else {
  5432. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  5433. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  5434. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  5435. // payments/Credits
  5436. payments_and_credist = 0;
  5437. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  5438. }
  5439. // total
  5440. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5441. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  5442. ret[@"order_info"]= orderinfo;
  5443. ret[@"more_order_info"] = moreInfo;
  5444. return [RAUtils dict2data:ret];
  5445. }
  5446. #pragma mark order list
  5447. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  5448. NSString *nsstatus = @"";
  5449. switch (status) {
  5450. case 0:
  5451. {
  5452. nsstatus=@"Temp Order";
  5453. break;
  5454. }
  5455. case 1:
  5456. {
  5457. nsstatus=@"Saved Order";
  5458. break;
  5459. }
  5460. case 2: {
  5461. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  5462. break;
  5463. }
  5464. case 3:
  5465. case 15:
  5466. {
  5467. nsstatus=@"Cancelled";
  5468. break;
  5469. }
  5470. case 10:
  5471. {
  5472. nsstatus=@"Quote Submitted";
  5473. break;
  5474. }
  5475. case 11:
  5476. {
  5477. nsstatus=@"Sales Order Submitted";
  5478. break;
  5479. }
  5480. case 12:
  5481. {
  5482. nsstatus=@"Processing";
  5483. break;
  5484. }
  5485. case 13:
  5486. {
  5487. nsstatus=@"Shipped";
  5488. break;
  5489. }
  5490. case 14:
  5491. {
  5492. nsstatus=@"Closed";
  5493. break;
  5494. }
  5495. case -11:
  5496. {
  5497. nsstatus = @"Ready For Submit";
  5498. break;
  5499. }
  5500. default:
  5501. break;
  5502. }
  5503. return nsstatus;
  5504. }
  5505. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5506. double total = 0;
  5507. __block double payments_and_credist = 0;
  5508. __block double allItemPrice = 0;
  5509. // sqlite3 *db1 = [iSalesDB get_db];
  5510. if (so_id) {
  5511. // 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];
  5512. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  5513. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5514. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5515. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5516. int product_id = sqlite3_column_int(stmt, 0);
  5517. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5518. int discount = sqlite3_column_int(stmt, 2);
  5519. int item_count = sqlite3_column_int(stmt, 3);
  5520. // int item_id = sqlite3_column_int(stmt, 7);
  5521. int item_id = sqlite3_column_int(stmt, 4);
  5522. NSString* Price=nil;
  5523. if(str_price==nil)
  5524. {
  5525. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5526. if(price==nil)
  5527. Price=@"No Price.";
  5528. else
  5529. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5530. }
  5531. else
  5532. {
  5533. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5534. }
  5535. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5536. itemjson[@"The unit price"]=Price;
  5537. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5538. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5539. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5540. if(itemjson[@"combine"] != nil)
  5541. {
  5542. // int citem=0;
  5543. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5544. for(int bc=0;bc<bcount;bc++)
  5545. {
  5546. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5547. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5548. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5549. subTotal += uprice * modulus * item_count;
  5550. }
  5551. }
  5552. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5553. allItemPrice += subTotal;
  5554. }];
  5555. payments_and_credist = allItemPrice;
  5556. } else {
  5557. // payments/Credits
  5558. payments_and_credist = 0;
  5559. }
  5560. // lift_gate handlingFee shippingFee
  5561. __block double lift_gate = 0;
  5562. __block double handlingFee = 0;
  5563. __block double shippingFee = 0;
  5564. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5565. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5566. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5567. if (have_lift_gate) {
  5568. lift_gate = sqlite3_column_double(stmt, 1);
  5569. }
  5570. handlingFee = sqlite3_column_double(stmt, 2);
  5571. shippingFee = sqlite3_column_double(stmt, 3);
  5572. }];
  5573. // total
  5574. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5575. if (close) {
  5576. [iSalesDB close_db:db1];
  5577. }
  5578. return total;
  5579. }
  5580. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5581. {
  5582. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5583. int limit = [[params valueForKey:@"limit"] intValue];
  5584. int offset = [[params valueForKey:@"offset"] intValue];
  5585. NSString* keyword = [params valueForKey:@"keyWord"];
  5586. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5587. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5588. NSString* where=@"1 = 1";
  5589. if(keyword.length>0)
  5590. 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]];
  5591. if (orderStatus.length > 0) {
  5592. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5593. if (order_status_array.count == 1) {
  5594. int status_value = [[order_status_array firstObject] integerValue];
  5595. if (status_value <= 1 || status_value == 3) {
  5596. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5597. } else {
  5598. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5599. }
  5600. } else if (order_status_array.count > 1) {
  5601. for (int i = 0; i < order_status_array.count;i++) {
  5602. NSString *status = order_status_array[i];
  5603. NSString *condition = @" or";
  5604. if (i == 0) {
  5605. condition = @" and (";
  5606. }
  5607. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5608. int status_value = [status integerValue];
  5609. if (status_value <= 1 || status_value == 3) {
  5610. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5611. } else {
  5612. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5613. }
  5614. }
  5615. where = [where stringByAppendingString:@" )"];
  5616. }
  5617. }
  5618. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5619. // DebugLog(@"order list sql: %@",sqlQuery);
  5620. sqlite3 *db = [iSalesDB get_db];
  5621. sqlite3_stmt * statement;
  5622. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5623. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5624. {
  5625. int count=0;
  5626. while (sqlite3_step(statement) == SQLITE_ROW)
  5627. {
  5628. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5629. int order_id = sqlite3_column_double(statement, 0);
  5630. char *soid = (char*)sqlite3_column_text(statement, 1);
  5631. if(soid==nil)
  5632. soid= "";
  5633. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5634. int status = sqlite3_column_double(statement, 2);
  5635. int erpStatus = sqlite3_column_double(statement, 9);
  5636. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5637. if(sales_rep==nil)
  5638. sales_rep= "";
  5639. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5640. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5641. if(create_by==nil)
  5642. create_by= "";
  5643. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5644. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5645. if(company_name==nil)
  5646. company_name= "";
  5647. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5648. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5649. if(create_time==nil)
  5650. create_time= "";
  5651. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5652. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5653. // double total_price = sqlite3_column_double(statement, 7);
  5654. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5655. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5656. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5657. int offline_edit = sqlite3_column_int(statement, 10);
  5658. // ": "JH",
  5659. // "": "$8307.00",
  5660. // "": "MOB1608050001",
  5661. // "": "ArpithaT",
  5662. // "": "1st Stage Property Transformations",
  5663. // "": "JANICE SUTTON",
  5664. // "": 2255,
  5665. // "": "08/02/2016 09:49:18",
  5666. // "": 1,
  5667. // "": "Saved Order"
  5668. // "": "1470384050483",
  5669. // "model_count": "6 / 28"
  5670. item[@"sales_rep"]= nssales_rep;
  5671. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5672. item[@"so#"]= nssoid;
  5673. item[@"create_by"]= nscreate_by;
  5674. item[@"customer_name"]= nscompany_name;
  5675. item[@"customer_contact"] = customer_contact;
  5676. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5677. item[@"purchase_time"]= nscreate_time;
  5678. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5679. item[@"order_status"]= nsstatus;
  5680. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5681. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5682. // item[@"model_count"]
  5683. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5684. count++;
  5685. }
  5686. ret[@"count"]= [NSNumber numberWithInt:count];
  5687. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5688. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5689. ret[@"result"]= [NSNumber numberWithInt:2];
  5690. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5691. ret[@"time_zone"] = @"PST";
  5692. sqlite3_finalize(statement);
  5693. }
  5694. 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];
  5695. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5696. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5697. [iSalesDB close_db:db];
  5698. return [RAUtils dict2data:ret];
  5699. }
  5700. #pragma mark update gnotes
  5701. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5702. {
  5703. DebugLog(@"params: %@",params);
  5704. // comments = Meyoyoyoyoyoyoy;
  5705. // orderCode = MOB1608110001;
  5706. // password = 123456;
  5707. // user = EvanK;
  5708. 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]];
  5709. int ret = [iSalesDB execSql:sql];
  5710. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5711. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5712. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5713. // [dic setValue:@"160409" forKey:@"min_ver"];
  5714. return [RAUtils dict2data:dic];
  5715. }
  5716. #pragma mark move to wishlist
  5717. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5718. {
  5719. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5720. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5721. _id = [NSString stringWithFormat:@"(%@)",_id];
  5722. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5723. sqlite3 *db = [iSalesDB get_db];
  5724. __block NSString *product_id = @"";
  5725. __block NSString *item_count_str = @"";
  5726. // __block NSString *item_id = nil;
  5727. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5728. // product_id = [self textAtColumn:0 statement:stmt];
  5729. int item_count = sqlite3_column_int(stmt, 1);
  5730. // item_id = [self textAtColumn:2 statement:stmt];
  5731. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5732. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5733. if (p_id.length) {
  5734. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5735. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5736. }
  5737. }];
  5738. [iSalesDB close_db:db];
  5739. // 去除第一个,
  5740. product_id = [product_id substringFromIndex:1];
  5741. item_count_str = [item_count_str substringFromIndex:1];
  5742. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5743. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5744. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5745. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5746. sqlite3 *db1 = [iSalesDB get_db];
  5747. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5748. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5749. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5750. // 删除
  5751. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5752. int ret = [iSalesDB execSql:deleteSql db:db1];
  5753. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5754. [iSalesDB close_db:db1];
  5755. return [RAUtils dict2data:dic];
  5756. }
  5757. #pragma mark cart delete
  5758. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5759. {
  5760. // cartItemId = 548;
  5761. // orderCode = MOB1608110001;
  5762. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5763. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5764. _id = [NSString stringWithFormat:@"(%@)",_id];
  5765. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5766. int ret = [iSalesDB execSql:sql];
  5767. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5768. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5769. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5770. // [dic setValue:@"160409" forKey:@"min_ver"];
  5771. return [RAUtils dict2data:dic];
  5772. }
  5773. #pragma mark set price
  5774. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5775. {
  5776. DebugLog(@"cart set price params: %@",params);
  5777. // "cartitem_id" = 1;
  5778. // discount = "0.000000";
  5779. // "item_note" = "";
  5780. // price = "269.000000";
  5781. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5782. NSString *notes = [self valueInParams:params key:@"item_note"];
  5783. NSString *discount = [self valueInParams:params key:@"discount"];
  5784. NSString *price = [self valueInParams:params key:@"price"];
  5785. // bool badd_price_changed=false;
  5786. // sqlite3* db=[iSalesDB get_db];
  5787. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5788. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5789. // NSRange range;
  5790. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5791. //
  5792. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5793. // badd_price_changed=true;
  5794. // [iSalesDB close_db:db];
  5795. //
  5796. // if(badd_price_changed)
  5797. // {
  5798. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5799. // }
  5800. //
  5801. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5802. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5803. int ret = [iSalesDB execSql:sql];
  5804. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5805. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5806. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5807. // [dic setValue:@"160409" forKey:@"min_ver"];
  5808. return [RAUtils dict2data:dic];
  5809. }
  5810. #pragma mark set line notes
  5811. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5812. {
  5813. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5814. NSString *notes = [self valueInParams:params key:@"notes"];
  5815. notes = [self translateSingleQuote:notes];
  5816. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5817. int ret = [iSalesDB execSql:sql];
  5818. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5819. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5820. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5821. // [dic setValue:@"160409" forKey:@"min_ver"];
  5822. return [RAUtils dict2data:dic];
  5823. }
  5824. #pragma mark set qty
  5825. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5826. {
  5827. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5828. int item_count = [params[@"inputInt"] integerValue];
  5829. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5830. int ret = [iSalesDB execSql:sql];
  5831. __block int item_id = 0;
  5832. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5833. item_id = sqlite3_column_int(stmt, 0);
  5834. }];
  5835. sqlite3 *db = [iSalesDB get_db];
  5836. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5837. [iSalesDB close_db:db];
  5838. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5839. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5840. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5841. // [dic setValue:@"160409" forKey:@"min_ver"];
  5842. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5843. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5844. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5845. return [RAUtils dict2data:dic];
  5846. }
  5847. #pragma mark place order
  5848. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5849. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5850. if (aname) {
  5851. [dic setValue:aname forKey:@"aname"];
  5852. }
  5853. if (control) {
  5854. [dic setValue:control forKey:@"control"];
  5855. }
  5856. if (keyboard) {
  5857. [dic setValue:keyboard forKey:@"keyboard"];
  5858. }
  5859. if (name) {
  5860. [dic setValue:name forKey:@"name"];
  5861. }
  5862. if (value) {
  5863. [dic setValue:value forKey:@"value"];
  5864. }
  5865. return dic;
  5866. }
  5867. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5868. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5869. orderCode = [self translateSingleQuote:orderCode];
  5870. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5871. 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];
  5872. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5873. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5874. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5875. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5876. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5877. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5878. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5879. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5880. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5881. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5882. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5883. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5884. NSString *email = [self textAtColumn:11 statement:stmt];
  5885. NSString *phone = [self textAtColumn:12 statement:stmt];
  5886. NSString *fax = [self textAtColumn:13 statement:stmt];
  5887. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5888. NSString *city = [self textAtColumn:15 statement:stmt];
  5889. NSString *state = [self textAtColumn:16 statement:stmt];
  5890. NSString *country = [self textAtColumn:17 statement:stmt];
  5891. NSString *name = [self textAtColumn:18 statement:stmt];
  5892. // contact id
  5893. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5894. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5895. [contact_id_dic setValue:@"text" forKey:@"control"];
  5896. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5897. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5898. [contact_id_dic setValue:@"true" forKey:@"required"];
  5899. [contact_id_dic setValue:contact_id forKey:@"value"];
  5900. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5901. // business card
  5902. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5903. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5904. [business_card_dic setValue:@"img" forKey:@"control"];
  5905. [business_card_dic setValue:@"1" forKey:@"disable"];
  5906. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5907. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5908. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5909. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5910. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5911. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5912. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5913. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5914. // fax
  5915. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5916. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5917. // zipcode
  5918. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5919. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5920. // city
  5921. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5922. [customer_dic setValue:city_dic forKey:@"item_12"];
  5923. // state
  5924. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5925. [customer_dic setValue:state_dic forKey:@"item_13"];
  5926. // country
  5927. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5928. [customer_dic setValue:country_dic forKey:@"item_14"];
  5929. // company name
  5930. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5931. [customer_dic setValue:company_dic forKey:@"item_2"];
  5932. // addr_1
  5933. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5934. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5935. // addr_2
  5936. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5937. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5938. // addr_3
  5939. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5940. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5941. // addr_4
  5942. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5943. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5944. // Contact
  5945. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5946. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5947. // email
  5948. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5949. [customer_dic setValue:email_dic forKey:@"item_8"];
  5950. // phone
  5951. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5952. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5953. // title
  5954. [customer_dic setValue:@"Customer" forKey:@"title"];
  5955. // count
  5956. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5957. }];
  5958. // setting
  5959. NSDictionary *setting = params[@"setting"];
  5960. NSNumber *hide = setting[@"CustomerHide"];
  5961. [customer_dic setValue:hide forKey:@"hide"];
  5962. return customer_dic;
  5963. }
  5964. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5965. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5966. // setting
  5967. NSDictionary *setting = params[@"setting"];
  5968. NSNumber *hide = setting[@"ShipToHide"];
  5969. [dic setValue:hide forKey:@"hide"];
  5970. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5971. orderCode = [self translateSingleQuote:orderCode];
  5972. 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];
  5973. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5974. NSString *cid = [self textAtColumn:0 statement:stmt];
  5975. NSString *name = [self textAtColumn:1 statement:stmt];
  5976. NSString *ext = [self textAtColumn:2 statement:stmt];
  5977. NSString *contact = [self textAtColumn:3 statement:stmt];
  5978. NSString *email = [self textAtColumn:4 statement:stmt];
  5979. NSString *fax = [self textAtColumn:5 statement:stmt];
  5980. NSString *phone = [self textAtColumn:6 statement:stmt];
  5981. // count
  5982. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5983. // title
  5984. [dic setValue:@"Ship To" forKey:@"title"];
  5985. // choose
  5986. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5987. [choose_dic setValue:@"choose" forKey:@"aname"];
  5988. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5989. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5990. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5991. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5992. @"receive_contact" : @"customer_contact",
  5993. @"receive_email" : @"customer_email",
  5994. @"receive_ext" : @"customer_contact_ext",
  5995. @"receive_fax" : @"customer_fax",
  5996. @"receive_name" : @"customer_name",
  5997. @"receive_phone" : @"customer_phone"},
  5998. @"refresh" : [NSNumber numberWithInteger:1],
  5999. @"type" : @"pull"};
  6000. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6001. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6002. @"name" : @"Add new address",
  6003. @"refresh" : [NSNumber numberWithInteger:1],
  6004. @"value" : @"new_addr"
  6005. };
  6006. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6007. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6008. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6009. @"receive_contact" : @"customer_contact",
  6010. @"receive_email" : @"customer_email",
  6011. @"receive_ext" : @"customer_contact_ext",
  6012. @"receive_fax" : @"customer_fax",
  6013. @"receive_name" : @"customer_name",
  6014. @"receive_phone" : @"customer_phone"},
  6015. @"name" : @"select_ship_to",
  6016. @"refresh" : [NSNumber numberWithInteger:1],
  6017. @"value" : @"Sales_Order_Ship_To"};
  6018. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6019. [dic setValue:choose_dic forKey:@"item_0"];
  6020. // contact id
  6021. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6022. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6023. [contact_id_dic setValue:@"true" forKey:@"required"];
  6024. [dic setValue:contact_id_dic forKey:@"item_1"];
  6025. // company name
  6026. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6027. [dic setValue:company_name_dic forKey:@"item_2"];
  6028. // address
  6029. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6030. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6031. if ([required integerValue]) {
  6032. [ext_dic setValue:@"true" forKey:@"required"];
  6033. }
  6034. [dic setValue:ext_dic forKey:@"item_3"];
  6035. // contact
  6036. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6037. [dic setValue:contact_dic forKey:@"item_4"];
  6038. // phone
  6039. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6040. [dic setValue:phone_dic forKey:@"item_5"];
  6041. // fax
  6042. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6043. [dic setValue:fax_dic forKey:@"item_6"];
  6044. // email
  6045. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6046. [dic setValue:email_dic forKey:@"item_7"];
  6047. }];
  6048. return dic;
  6049. }
  6050. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6051. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6052. // setting
  6053. NSDictionary *setting = params[@"setting"];
  6054. NSNumber *hide = setting[@"ShipFromHide"];
  6055. [dic setValue:hide forKey:@"hide"];
  6056. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6057. orderCode = [self translateSingleQuote:orderCode];
  6058. 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];
  6059. 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';";
  6060. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6061. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6062. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6063. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6064. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6065. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6066. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6067. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6068. if (!cid.length) {
  6069. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6070. cid = [self textAtColumn:0 statement:statment];
  6071. name = [self textAtColumn:1 statement:statment];
  6072. ext = [self textAtColumn:2 statement:statment];
  6073. contact = [self textAtColumn:3 statement:statment];
  6074. email = [self textAtColumn:4 statement:statment];
  6075. fax = [self textAtColumn:5 statement:statment];
  6076. phone = [self textAtColumn:6 statement:statment];
  6077. }];
  6078. }
  6079. // count
  6080. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6081. // title
  6082. [dic setValue:@"Ship From" forKey:@"title"];
  6083. // hide
  6084. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6085. // choose
  6086. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6087. [choose_dic setValue:@"choose" forKey:@"aname"];
  6088. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6089. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6090. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6091. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6092. @"sender_contact" : @"customer_contact",
  6093. @"sender_email" : @"customer_email",
  6094. @"sender_ext" : @"customer_contact_ext",
  6095. @"sender_fax" : @"customer_fax",
  6096. @"sender_name" : @"customer_name",
  6097. @"sender_phone" : @"customer_phone"},
  6098. @"name" : @"select_cid",
  6099. @"refresh" : [NSNumber numberWithInteger:0],
  6100. @"value" : @"Sales_Order_Ship_From"};
  6101. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6102. [dic setValue:choose_dic forKey:@"item_0"];
  6103. // contact id
  6104. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6105. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6106. [contact_id_dic setValue:@"true" forKey:@"required"];
  6107. [dic setValue:contact_id_dic forKey:@"item_1"];
  6108. // company name
  6109. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6110. [dic setValue:company_name_dic forKey:@"item_2"];
  6111. // address
  6112. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6113. [dic setValue:ext_dic forKey:@"item_3"];
  6114. // contact
  6115. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6116. [dic setValue:contact_dic forKey:@"item_4"];
  6117. // phone
  6118. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6119. [dic setValue:phone_dic forKey:@"item_5"];
  6120. // fax
  6121. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6122. [dic setValue:fax_dic forKey:@"item_6"];
  6123. // email
  6124. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6125. [dic setValue:email_dic forKey:@"item_7"];
  6126. }];
  6127. return dic;
  6128. }
  6129. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6130. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6131. // setting
  6132. NSDictionary *setting = params[@"setting"];
  6133. NSNumber *hide = setting[@"FreightBillToHide"];
  6134. [dic setValue:hide forKey:@"hide"];
  6135. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6136. orderCode = [self translateSingleQuote:orderCode];
  6137. 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];
  6138. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6139. NSString *cid = [self textAtColumn:0 statement:stmt];
  6140. NSString *name = [self textAtColumn:1 statement:stmt];
  6141. NSString *ext = [self textAtColumn:2 statement:stmt];
  6142. NSString *contact = [self textAtColumn:3 statement:stmt];
  6143. NSString *email = [self textAtColumn:4 statement:stmt];
  6144. NSString *fax = [self textAtColumn:5 statement:stmt];
  6145. NSString *phone = [self textAtColumn:6 statement:stmt];
  6146. // count
  6147. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6148. // title
  6149. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6150. // hide
  6151. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6152. // choose
  6153. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6154. [choose_dic setValue:@"choose" forKey:@"aname"];
  6155. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6156. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6157. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6158. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6159. @"shipping_billto_contact" : @"receive_contact",
  6160. @"shipping_billto_email" : @"receive_email",
  6161. @"shipping_billto_ext" : @"receive_ext",
  6162. @"shipping_billto_fax" : @"receive_fax",
  6163. @"shipping_billto_name" : @"receive_name",
  6164. @"shipping_billto_phone" : @"receive_phone"},
  6165. @"type" : @"pull"};
  6166. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6167. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6168. @"type" : @"pull",
  6169. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6170. @"shipping_billto_contact" : @"customer_contact",
  6171. @"shipping_billto_email" : @"customer_email",
  6172. @"shipping_billto_ext" : @"customer_contact_ext",
  6173. @"shipping_billto_fax" : @"customer_fax",
  6174. @"shipping_billto_name" : @"customer_name",
  6175. @"shipping_billto_phone" : @"customer_phone"}
  6176. };
  6177. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6178. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6179. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6180. @"shipping_billto_contact" : @"sender_contact",
  6181. @"shipping_billto_email" : @"sender_email",
  6182. @"shipping_billto_ext" : @"sender_ext",
  6183. @"shipping_billto_fax" : @"sender_fax",
  6184. @"shipping_billto_name" : @"sender_name",
  6185. @"shipping_billto_phone" : @"sender_phone"},
  6186. @"type" : @"pull"
  6187. };
  6188. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6189. NSDictionary *select_freight_bill_to_dic = @{
  6190. @"aname" : @"Select freight bill to",
  6191. @"name" : @"select_cid",
  6192. @"refresh" : [NSNumber numberWithInteger:0],
  6193. @"value" : @"Sales_Order_Freight_Bill_To",
  6194. @"key_map" : @{
  6195. @"shipping_billto_cid" : @"customer_cid",
  6196. @"shipping_billto_contact" : @"customer_contact",
  6197. @"shipping_billto_email" : @"customer_email",
  6198. @"shipping_billto_ext" : @"customer_contact_ext",
  6199. @"shipping_billto_fax" : @"customer_fax",
  6200. @"shipping_billto_name" : @"customer_name",
  6201. @"shipping_billto_phone" : @"customer_phone"
  6202. }
  6203. };
  6204. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  6205. [dic setValue:choose_dic forKey:@"item_0"];
  6206. // contact id
  6207. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6208. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6209. [contact_id_dic setValue:@"true" forKey:@"required"];
  6210. [dic setValue:contact_id_dic forKey:@"item_1"];
  6211. // company name
  6212. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6213. [dic setValue:company_name_dic forKey:@"item_2"];
  6214. // address
  6215. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6216. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  6217. if ([ext_required integerValue]) {
  6218. [ext_dic setValue:@"true" forKey:@"required"];
  6219. }
  6220. [dic setValue:ext_dic forKey:@"item_3"];
  6221. // contact
  6222. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6223. [dic setValue:contact_dic forKey:@"item_4"];
  6224. // phone
  6225. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6226. [dic setValue:phone_dic forKey:@"item_5"];
  6227. // fax
  6228. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6229. [dic setValue:fax_dic forKey:@"item_6"];
  6230. // email
  6231. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6232. [dic setValue:email_dic forKey:@"item_7"];
  6233. }];
  6234. return dic;
  6235. }
  6236. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6237. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6238. // setting
  6239. NSDictionary *setting = params[@"setting"];
  6240. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  6241. [dic setValue:hide forKey:@"hide"];
  6242. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6243. orderCode = [self translateSingleQuote:orderCode];
  6244. 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];
  6245. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6246. NSString *cid = [self textAtColumn:0 statement:stmt];
  6247. NSString *name = [self textAtColumn:1 statement:stmt];
  6248. NSString *ext = [self textAtColumn:2 statement:stmt];
  6249. NSString *contact = [self textAtColumn:3 statement:stmt];
  6250. NSString *email = [self textAtColumn:4 statement:stmt];
  6251. NSString *fax = [self textAtColumn:5 statement:stmt];
  6252. NSString *phone = [self textAtColumn:6 statement:stmt];
  6253. // count
  6254. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6255. // title
  6256. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  6257. // hide
  6258. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6259. // choose
  6260. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6261. [choose_dic setValue:@"choose" forKey:@"aname"];
  6262. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6263. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6264. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6265. @"key_map" : @{@"billing_cid" : @"receive_cid",
  6266. @"billing_contact" : @"receive_contact",
  6267. @"billing_email" : @"receive_email",
  6268. @"billing_ext" : @"receive_ext",
  6269. @"billing_fax" : @"receive_fax",
  6270. @"billing_name" : @"receive_name",
  6271. @"billing_phone" : @"receive_phone"},
  6272. @"type" : @"pull"};
  6273. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6274. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6275. @"type" : @"pull",
  6276. @"key_map" : @{@"billing_cid" : @"customer_cid",
  6277. @"billing_contact" : @"customer_contact",
  6278. @"billing_email" : @"customer_email",
  6279. @"billing_ext" : @"customer_contact_ext",
  6280. @"billing_fax" : @"customer_fax",
  6281. @"billing_name" : @"customer_name",
  6282. @"billing_phone" : @"customer_phone"}
  6283. };
  6284. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6285. NSDictionary *select_bill_to_dic = @{
  6286. @"aname" : @"Select bill to",
  6287. @"name" : @"select_cid",
  6288. @"refresh" : [NSNumber numberWithInteger:0],
  6289. @"value" : @"Sales_Order_Merchandise_Bill_To",
  6290. @"key_map" : @{
  6291. @"billing_cid" : @"customer_cid",
  6292. @"billing_contact" : @"customer_contact",
  6293. @"billing_email" : @"customer_email",
  6294. @"billing_ext" : @"customer_contact_ext",
  6295. @"billing_fax" : @"customer_fax",
  6296. @"billing_name" : @"customer_name",
  6297. @"billing_phone" : @"customer_phone"
  6298. }
  6299. };
  6300. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  6301. [dic setValue:choose_dic forKey:@"item_0"];
  6302. // contact id
  6303. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6304. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6305. [contact_id_dic setValue:@"true" forKey:@"required"];
  6306. [dic setValue:contact_id_dic forKey:@"item_1"];
  6307. // company name
  6308. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6309. [dic setValue:company_name_dic forKey:@"item_2"];
  6310. // address
  6311. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6312. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  6313. if ([ext_required integerValue]) {
  6314. [ext_dic setValue:@"true" forKey:@"required"];
  6315. }
  6316. [dic setValue:ext_dic forKey:@"item_3"];
  6317. // contact
  6318. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6319. [dic setValue:contact_dic forKey:@"item_4"];
  6320. // phone
  6321. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6322. [dic setValue:phone_dic forKey:@"item_5"];
  6323. // fax
  6324. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6325. [dic setValue:fax_dic forKey:@"item_6"];
  6326. // email
  6327. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6328. [dic setValue:email_dic forKey:@"item_7"];
  6329. }];
  6330. return dic;
  6331. }
  6332. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6333. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6334. // setting
  6335. NSDictionary *setting = params[@"setting"];
  6336. NSNumber *hide = setting[@"ReturnToHide"];
  6337. [dic setValue:hide forKey:@"hide"];
  6338. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6339. orderCode = [self translateSingleQuote:orderCode];
  6340. 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];
  6341. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6342. NSString *cid = [self textAtColumn:0 statement:stmt];
  6343. NSString *name = [self textAtColumn:1 statement:stmt];
  6344. NSString *ext = [self textAtColumn:2 statement:stmt];
  6345. NSString *contact = [self textAtColumn:3 statement:stmt];
  6346. NSString *email = [self textAtColumn:4 statement:stmt];
  6347. NSString *fax = [self textAtColumn:5 statement:stmt];
  6348. NSString *phone = [self textAtColumn:6 statement:stmt];
  6349. // count
  6350. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6351. // title
  6352. [dic setValue:@"Return To" forKey:@"title"];
  6353. // hide
  6354. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6355. // choose
  6356. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6357. [choose_dic setValue:@"choose" forKey:@"aname"];
  6358. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6359. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  6360. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6361. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  6362. @"returnto_contact" : @"sender_contact",
  6363. @"returnto_email" : @"sender_email",
  6364. @"returnto_ext" : @"sender_ext",
  6365. @"returnto_fax" : @"sender_fax",
  6366. @"returnto_name" : @"sender_name",
  6367. @"returnto_phone" : @"sender_phone"},
  6368. @"type" : @"pull"};
  6369. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  6370. NSDictionary *select_return_to_dic = @{
  6371. @"aname" : @"Select return to",
  6372. @"name" : @"select_cid",
  6373. @"refresh" : [NSNumber numberWithInteger:0],
  6374. @"value" : @"Contact_Return_To",
  6375. @"key_map" : @{
  6376. @"returnto_cid" : @"customer_cid",
  6377. @"returnto_contact" : @"customer_contact",
  6378. @"returnto_email" : @"customer_email",
  6379. @"returnto_ext" : @"customer_contact_ext",
  6380. @"returnto_fax" : @"customer_fax",
  6381. @"returnto_name" : @"customer_name",
  6382. @"returnto_phone" : @"customer_phone"
  6383. }
  6384. };
  6385. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  6386. [dic setValue:choose_dic forKey:@"item_0"];
  6387. // contact id
  6388. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6389. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6390. [contact_id_dic setValue:@"true" forKey:@"required"];
  6391. [dic setValue:contact_id_dic forKey:@"item_1"];
  6392. // company name
  6393. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6394. [dic setValue:company_name_dic forKey:@"item_2"];
  6395. // address
  6396. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6397. [dic setValue:ext_dic forKey:@"item_3"];
  6398. // contact
  6399. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6400. [dic setValue:contact_dic forKey:@"item_4"];
  6401. // phone
  6402. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6403. [dic setValue:phone_dic forKey:@"item_5"];
  6404. // fax
  6405. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6406. [dic setValue:fax_dic forKey:@"item_6"];
  6407. // email
  6408. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6409. [dic setValue:email_dic forKey:@"item_7"];
  6410. }];
  6411. return dic;
  6412. }
  6413. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6414. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6415. orderCode = [self translateSingleQuote:orderCode];
  6416. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6417. __block double TotalCuft = 0;
  6418. __block double TotalWeight = 0;
  6419. __block int TotalCarton = 0;
  6420. __block double payments = 0;
  6421. // setting
  6422. NSDictionary *setting = params[@"setting"];
  6423. NSNumber *hide = setting[@"ModelInformationHide"];
  6424. [dic setValue:hide forKey:@"hide"];
  6425. 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];
  6426. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6427. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6428. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  6429. // item id
  6430. int item_id = sqlite3_column_int(stmt, 0);
  6431. // count
  6432. int item_count = sqlite3_column_int(stmt, 1);
  6433. // stockUom
  6434. int stockUom = sqlite3_column_int(stmt, 2);
  6435. // unit price
  6436. NSString *str_price = [self textAtColumn:3 statement:stmt];
  6437. NSString* Price=nil;
  6438. if([str_price isEqualToString:@""])
  6439. {
  6440. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  6441. if(price==nil)
  6442. Price=@"No Price.";
  6443. else
  6444. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6445. }
  6446. else
  6447. {
  6448. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  6449. }
  6450. // discount
  6451. double discount = sqlite3_column_double(stmt, 4);
  6452. // name
  6453. NSString *name = [self textAtColumn:5 statement:stmt];
  6454. // description
  6455. NSString *description = [self textAtColumn:6 statement:stmt];
  6456. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  6457. // line note
  6458. NSString *line_note = [self textAtColumn:7 statement:stmt];
  6459. int avaulability = sqlite3_column_int(stmt, 8);
  6460. // img
  6461. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  6462. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  6463. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6464. if(combine != nil)
  6465. {
  6466. // int citem=0;
  6467. int bcount=[[combine valueForKey:@"count"] intValue];
  6468. for(int bc=0;bc<bcount;bc++)
  6469. {
  6470. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6471. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6472. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6473. subTotal += uprice * modulus * item_count;
  6474. }
  6475. }
  6476. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6477. [model_dic setValue:@"model" forKey:@"control"];
  6478. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6479. [model_dic setValue:description forKey:@"description"];
  6480. [model_dic setValue:line_note forKey:@"note"];
  6481. [model_dic setValue:img forKey:@"img_url"];
  6482. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6483. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6484. [model_dic setValue:Price forKey:@"unit_price"];
  6485. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6486. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6487. if (combine) {
  6488. [model_dic setValue:combine forKey:@"combine"];
  6489. }
  6490. // well,what under the row is the info for total
  6491. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6492. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6493. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6494. int carton=[bsubtotaljson[@"carton"] intValue];
  6495. TotalCuft += cuft;
  6496. TotalWeight += weight;
  6497. TotalCarton += carton;
  6498. payments += subTotal;
  6499. //---------------------------
  6500. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6501. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6502. }];
  6503. [dic setValue:@"Model Information" forKey:@"title"];
  6504. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6505. return dic;
  6506. }
  6507. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6508. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6509. [dic setValue:@"Remarks Content" forKey:@"title"];
  6510. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6511. // setting
  6512. NSDictionary *setting = params[@"setting"];
  6513. NSNumber *hide = setting[@"RemarksContentHide"];
  6514. [dic setValue:hide forKey:@"hide"];
  6515. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6516. orderCode = [self translateSingleQuote:orderCode];
  6517. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6518. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6519. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6520. int mustCall = sqlite3_column_int(stmt, 1);
  6521. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6522. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6523. NSDictionary *po_dic = @{
  6524. @"aname" : @"PO#",
  6525. @"control" : @"edit",
  6526. @"keyboard" : @"text",
  6527. @"name" : @"poNumber",
  6528. @"value" : poNumber
  6529. };
  6530. NSDictionary *must_call_dic = @{
  6531. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6532. @"control" : @"switch",
  6533. @"name" : @"must_call",
  6534. @"value" : mustCall ? @"true" : @"false"
  6535. };
  6536. NSDictionary *general_notes_dic = @{
  6537. @"aname" : @"General notes",
  6538. @"control" : @"text_view",
  6539. @"keyboard" : @"text",
  6540. @"name" : @"comments",
  6541. @"value" : generalNotes
  6542. };
  6543. NSDictionary *internal_notes_dic = @{
  6544. @"aname" : @"Internal notes",
  6545. @"control" : @"text_view",
  6546. @"keyboard" : @"text",
  6547. @"name" : @"internal_notes",
  6548. @"value" : internalNotes
  6549. };
  6550. [dic setValue:po_dic forKey:@"item_0"];
  6551. [dic setValue:must_call_dic forKey:@"item_1"];
  6552. [dic setValue:general_notes_dic forKey:@"item_2"];
  6553. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6554. }];
  6555. return dic;
  6556. }
  6557. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6558. // params
  6559. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6560. orderCode = [self translateSingleQuote:orderCode];
  6561. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6562. // setting
  6563. NSDictionary *setting = params[@"setting"];
  6564. NSNumber *hide = setting[@"OrderTotalHide"];
  6565. [dic setValue:hide forKey:@"hide"];
  6566. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6567. __block double lift_gate_value = 0;
  6568. __block double handling_fee = 0;
  6569. __block double shipping = 0;
  6570. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6571. int lift_gate = sqlite3_column_int(stmt, 0);
  6572. lift_gate_value = sqlite3_column_double(stmt, 1);
  6573. shipping = sqlite3_column_double(stmt, 2);
  6574. handling_fee = sqlite3_column_double(stmt, 3);
  6575. if (!lift_gate) {
  6576. lift_gate_value = 0;
  6577. }
  6578. }];
  6579. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6580. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6581. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6582. double payments = [[total valueForKey:@"payments"] doubleValue];
  6583. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6584. double totalPrice = payments;
  6585. [dic setValue:@"Order Total" forKey:@"title"];
  6586. NSDictionary *payments_dic = @{
  6587. @"align" : @"right",
  6588. @"aname" : @"Payments/Credits",
  6589. @"control" : @"text",
  6590. @"name" : @"paymentsAndCredits",
  6591. @"type" : @"price",
  6592. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6593. };
  6594. [dic setValue:payments_dic forKey:@"item_0"];
  6595. NSDictionary *handling_fee_dic = @{
  6596. @"align" : @"right",
  6597. @"aname" : @"Handling Fee",
  6598. @"control" : @"text",
  6599. @"name" : @"handling_fee_value",
  6600. @"required" : @"true",
  6601. @"type" : @"price",
  6602. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6603. };
  6604. NSDictionary *shipping_dic = @{
  6605. @"align" : @"right",
  6606. @"aname" : @"Shipping*",
  6607. @"control" : @"text",
  6608. @"name" : @"shipping",
  6609. @"required" : @"true",
  6610. @"type" : @"price",
  6611. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6612. };
  6613. NSDictionary *lift_gate_dic = @{
  6614. @"align" : @"right",
  6615. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6616. @"control" : @"text",
  6617. @"name" : @"lift_gate_value",
  6618. @"required" : @"true",
  6619. @"type" : @"price",
  6620. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6621. };
  6622. int item_count = 1;
  6623. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6624. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6625. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6626. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6627. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6628. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6629. } else {
  6630. }
  6631. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6632. NSDictionary *total_price_dic = @{
  6633. @"align" : @"right",
  6634. @"aname" : @"Total",
  6635. @"control" : @"text",
  6636. @"name" : @"totalPrice",
  6637. @"type" : @"price",
  6638. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6639. };
  6640. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6641. NSDictionary *total_cuft_dic = @{
  6642. @"align" : @"right",
  6643. @"aname" : @"Total Cuft",
  6644. @"control" : @"text",
  6645. @"name" : @"",
  6646. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6647. };
  6648. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6649. NSDictionary *total_weight_dic = @{
  6650. @"align" : @"right",
  6651. @"aname" : @"Total Weight",
  6652. @"control" : @"text",
  6653. @"name" : @"",
  6654. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6655. };
  6656. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6657. NSDictionary *total_carton_dic = @{
  6658. @"align" : @"right",
  6659. @"aname" : @"Total Carton",
  6660. @"control" : @"text",
  6661. @"name" : @"",
  6662. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6663. };
  6664. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6665. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6666. return dic;
  6667. }
  6668. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6669. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6670. orderCode = [self translateSingleQuote:orderCode];
  6671. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6672. // setting
  6673. NSDictionary *setting = params[@"setting"];
  6674. NSNumber *hide = setting[@"SignatureHide"];
  6675. [dic setValue:hide forKey:@"hide"];
  6676. [dic setValue:@"Signature" forKey:@"title"];
  6677. __block NSString *pic_path = @"";
  6678. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6679. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6680. pic_path = [self textAtColumn:0 statement:stmt];
  6681. }];
  6682. NSDictionary *pic_dic = @{
  6683. @"aname" : @"Signature",
  6684. @"avalue" :pic_path,
  6685. @"control" : @"signature",
  6686. @"name" : @"sign_picpath",
  6687. @"value" : pic_path
  6688. };
  6689. [dic setValue:pic_dic forKey:@"item_0"];
  6690. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6691. return dic;
  6692. }
  6693. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6694. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6695. // setting
  6696. NSDictionary *setting = params[@"setting"];
  6697. NSNumber *hide = setting[@"ShippingMethodHide"];
  6698. [dic setValue:hide forKey:@"hide"];
  6699. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6700. orderCode = [self translateSingleQuote:orderCode];
  6701. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6702. __block NSString *logist = @"";
  6703. __block NSString *lift_gate = @"";
  6704. __block int lift_gate_integer = 0;
  6705. __block NSString *logistic_note = @"";
  6706. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6707. logist = [self textAtColumn:0 statement:stmt];
  6708. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6709. logistic_note = [self textAtColumn:2 statement:stmt];
  6710. }];
  6711. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6712. [dic setValue:@"Shipping Method" forKey:@"title"];
  6713. // val_0
  6714. int PERSONAL_PICK_UP_check = 0;
  6715. int USE_MY_CARRIER_check = 0;
  6716. NSString *logistic_note_text = @"";
  6717. int will_call_check = 0;
  6718. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6719. will_call_check = 1;
  6720. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6721. PERSONAL_PICK_UP_check = 1;
  6722. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6723. USE_MY_CARRIER_check = 1;
  6724. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6725. logistic_note = [logistic_note_array firstObject];
  6726. if (logistic_note_array.count > 1) {
  6727. logistic_note_text = [logistic_note_array lastObject];
  6728. }
  6729. }
  6730. }
  6731. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6732. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6733. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6734. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6735. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6736. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6737. NSDictionary *val0_subItem_item0 = @{
  6738. @"aname" : @"Option",
  6739. @"cadedate" : @{
  6740. @"count" : [NSNumber numberWithInteger:2],
  6741. @"val_0" : @{
  6742. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6743. @"refresh" : [NSNumber numberWithInteger:0],
  6744. @"value" : @"PERSONAL PICK UP",
  6745. @"value_id" : @"PERSONAL PICK UP"
  6746. },
  6747. @"val_1" : @{
  6748. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6749. @"refresh" : [NSNumber numberWithInteger:0],
  6750. @"sub_item" : @{
  6751. @"count" : [NSNumber numberWithInteger:1],
  6752. @"item_0" : @{
  6753. @"aname" : @"BOL",
  6754. @"control" : @"edit",
  6755. @"keyboard" : @"text",
  6756. @"name" : @"logist_note_text",
  6757. @"refresh" : [NSNumber numberWithInteger:0],
  6758. @"required" : @"false",
  6759. @"value" : logistic_note_text
  6760. }
  6761. },
  6762. @"value" : @"USE MY CARRIER",
  6763. @"value_id" : @"USE MY CARRIER"
  6764. }
  6765. },
  6766. @"control" : @"enum",
  6767. @"name" : @"logistic_note",
  6768. @"required" : @"true",
  6769. @"single_select" : @"true"
  6770. };
  6771. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6772. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6773. // val_1
  6774. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6775. if([lift_gate isEqualToString:@""]) {
  6776. if (lift_gate_integer == 1) {
  6777. lift_gate = @"true";
  6778. } else {
  6779. lift_gate = @"false";
  6780. }
  6781. }
  6782. int common_carrier_check = 0;
  6783. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6784. common_carrier_check = 1;
  6785. [params setValue:lift_gate forKey:@"lift_gate"];
  6786. }
  6787. NSDictionary *val_1 = @{
  6788. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6789. @"sub_item" : @{
  6790. @"count" : [NSNumber numberWithInteger:1],
  6791. @"item_0" : @{
  6792. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6793. @"control" : @"switch",
  6794. @"name" : @"lift_gate",
  6795. @"refresh" : [NSNumber numberWithInteger:1],
  6796. @"required" : @"true",
  6797. @"value" : lift_gate
  6798. }
  6799. },
  6800. @"value" : @"COMMON CARRIER",
  6801. @"value_id" : @"COMMON CARRIER"
  6802. };
  6803. // cadedate
  6804. NSDictionary *cadedate = @{
  6805. @"count" : [NSNumber numberWithInteger:2],
  6806. @"val_0" : val_0,
  6807. @"val_1" : val_1
  6808. };
  6809. // item_0
  6810. NSMutableDictionary *item_0 = @{
  6811. @"aname" : @"Shipping",
  6812. @"cadedate" : cadedate,
  6813. @"control" : @"enum",
  6814. @"name" : @"logist",
  6815. @"refresh" : [NSNumber numberWithInteger:1],
  6816. @"single_select" : @"true",
  6817. }.mutableCopy;
  6818. NSNumber *required = setting[@"ShippingMethodRequire"];
  6819. if ([required integerValue]) {
  6820. [item_0 setValue:@"true" forKey:@"required"];
  6821. }
  6822. [dic setValue:item_0 forKey:@"item_0"];
  6823. return dic;
  6824. }
  6825. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6826. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6827. orderCode = [self translateSingleQuote:orderCode];
  6828. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6829. __block int submit_as_integer = 0;
  6830. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6831. submit_as_integer = sqlite3_column_int(stmt, 0);
  6832. }];
  6833. int check11 = 0;
  6834. int check10 = 0;
  6835. if (submit_as_integer == 11) {
  6836. check11 = 1;
  6837. }
  6838. if (submit_as_integer == 10) {
  6839. check10 = 1;
  6840. }
  6841. // section_0
  6842. NSMutableDictionary *dic = @{
  6843. @"count" : @(1),
  6844. @"item_0" : @{
  6845. @"aname" : @"Submit Order As",
  6846. @"cadedate" : @{
  6847. @"count" : @(2),
  6848. @"val_0" : @{
  6849. @"check" : [NSNumber numberWithInteger:check11],
  6850. @"value" : @"Sales Order",
  6851. @"value_id" : @(11)
  6852. },
  6853. @"val_1" : @{
  6854. @"check" : [NSNumber numberWithInteger:check10],
  6855. @"value" : @"Quote",
  6856. @"value_id" : @(10)
  6857. }
  6858. },
  6859. @"control" : @"enum",
  6860. @"name" : @"erpOrderStatus",
  6861. @"required" : @"true",
  6862. @"single_select" : @"true"
  6863. },
  6864. @"title" : @"Order Type"
  6865. }.mutableCopy;
  6866. // setting
  6867. NSDictionary *setting = params[@"setting"];
  6868. NSNumber *hide = setting[@"OrderTypeHide"];
  6869. [dic setValue:hide forKey:@"hide"];
  6870. return dic;
  6871. }
  6872. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6873. countryCode = [self translateSingleQuote:countryCode];
  6874. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6875. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6876. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6877. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6878. if (name == NULL) {
  6879. name = "";
  6880. }
  6881. if (code == NULL) {
  6882. code = "";
  6883. }
  6884. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6885. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6886. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6887. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6888. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6889. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6890. }
  6891. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6892. [container setValue:stateDic forKey:key];
  6893. }] mutableCopy];
  6894. // failure 可以不用了,一样的
  6895. if (ret.allKeys.count == 0) {
  6896. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6897. [stateDic setValue:@"Other" forKey:@"value"];
  6898. [stateDic setValue:@"" forKey:@"value_id"];
  6899. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6900. if (state_code && [@"" isEqualToString:state_code]) {
  6901. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6902. }
  6903. NSString *key = [NSString stringWithFormat:@"val_0"];
  6904. [ret setValue:stateDic forKey:key];
  6905. }
  6906. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6907. return ret;
  6908. }
  6909. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6910. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6911. orderCode = [self translateSingleQuote:orderCode];
  6912. 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];
  6913. __block NSString *payType = @"";
  6914. __block NSString *firstName = @"";
  6915. __block NSString *lastName = @"";
  6916. __block NSString *addr1 = @"";
  6917. __block NSString *addr2 = @"";
  6918. __block NSString *zipcode = @"";
  6919. __block NSString *cardType = @"";
  6920. __block NSString *cardNumber = @"";
  6921. __block NSString *securityCode = @"";
  6922. __block NSString *month = @"";
  6923. __block NSString *year = @"";
  6924. __block NSString *city = @"";
  6925. __block NSString *state = @"";
  6926. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6927. payType = [self textAtColumn:0 statement:stmt];
  6928. firstName = [self textAtColumn:1 statement:stmt];
  6929. lastName = [self textAtColumn:2 statement:stmt];
  6930. addr1 = [self textAtColumn:3 statement:stmt];
  6931. addr2 = [self textAtColumn:4 statement:stmt];
  6932. zipcode = [self textAtColumn:5 statement:stmt];
  6933. cardType = [self textAtColumn:6 statement:stmt];
  6934. cardNumber = [self textAtColumn:7 statement:stmt];
  6935. securityCode = [self textAtColumn:8 statement:stmt];
  6936. month = [self textAtColumn:9 statement:stmt];
  6937. year = [self textAtColumn:10 statement:stmt];
  6938. city = [self textAtColumn:11 statement:stmt];
  6939. state = [self textAtColumn:12 statement:stmt];
  6940. }];
  6941. NSString *required = @"true";
  6942. // setting
  6943. NSDictionary *setting = params[@"setting"];
  6944. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6945. if ([requiredNumber integerValue]) {
  6946. required = @"true";
  6947. } else {
  6948. required = @"false";
  6949. }
  6950. // "section_2"
  6951. NSMutableDictionary *dic = @{
  6952. @"count" : @(1),
  6953. @"item_0" : @{
  6954. @"aname" : @"Payment",
  6955. @"required" : required,
  6956. @"cadedate" : @{
  6957. @"count" : @(6),
  6958. @"val_0" : @{
  6959. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6960. @"value" : @"Check",
  6961. @"value_id" : @"Check"
  6962. },
  6963. @"val_1" : @{
  6964. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6965. @"value" : @"Cash",
  6966. @"value_id" : @"Cash"
  6967. },
  6968. @"val_2" : @{
  6969. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6970. @"value" : @"NET 30",
  6971. @"value_id" : @"NET 30"
  6972. },
  6973. @"val_3" : @{
  6974. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6975. @"value" : @"Wire Transfer",
  6976. @"value_id" : @"Wire Transfer"
  6977. },
  6978. @"val_4" : @{
  6979. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6980. @"sub_item" : @{
  6981. @"count" : @(3),
  6982. @"item_0" : @{
  6983. @"aname" : @"choose",
  6984. @"control" : @"multi_action",
  6985. @"count" : @(1),
  6986. @"item_0" : @{
  6987. @"aname" : @"Same as customer",
  6988. @"key_map" : @{
  6989. @"credit_card_address1" : @"customer_address1",
  6990. @"credit_card_address2" : @"customer_address2",
  6991. @"credit_card_city" : @"customer_city",
  6992. @"credit_card_first_name" : @"customer_first_name",
  6993. @"credit_card_last_name" : @"customer_last_name",
  6994. @"credit_card_state" : @"customer_state",
  6995. @"credit_card_zipcode" : @"customer_zipcode"
  6996. },
  6997. @"type" : @"pull"
  6998. }
  6999. },
  7000. @"item_1" : @{
  7001. @"aname" : @"",
  7002. @"color" : @"red",
  7003. @"control" : @"text",
  7004. @"name" : @"",
  7005. @"value" : @"USA Credit cards only"
  7006. },
  7007. @"item_2" : @{
  7008. @"aname" : @"Fill",
  7009. @"cadedate" : @{
  7010. @"count" : @(2),
  7011. @"val_0" : @{
  7012. @"check" : @(1),
  7013. @"sub_item" : @{
  7014. @"count" : @(11),
  7015. @"item_0" : @{
  7016. @"aname" : @"Type",
  7017. @"cadedate" : @{
  7018. @"count" : @(2),
  7019. @"val_0" : @{
  7020. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7021. @"value" : @"VISA",
  7022. @"value_id" : @(0)
  7023. },
  7024. @"val_1" : @{
  7025. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7026. @"value" : @"MASTER CARD",
  7027. @"value_id" : @(1)
  7028. }
  7029. },
  7030. @"control" : @"enum",
  7031. @"name" : @"credit_card_type",
  7032. @"required" : @"true",
  7033. @"single_select" : @"true"
  7034. },
  7035. @"item_1" : @{
  7036. @"aname" : @"Number",
  7037. @"control" : @"edit",
  7038. @"keyboard" : @"int",
  7039. @"length" : @"16",
  7040. @"name" : @"credit_card_number",
  7041. @"required" : @"true",
  7042. @"value" : cardNumber
  7043. },
  7044. @"item_10" : @{
  7045. @"aname" : @"State",
  7046. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7047. @"control" : @"enum",
  7048. @"enum" : @"true",
  7049. @"name" : @"credit_card_state",
  7050. @"required" : @"true",
  7051. @"single_select" : @"true"
  7052. },
  7053. @"item_2" : @{
  7054. @"aname" : @"Expiration Date",
  7055. @"control" : @"monthpicker",
  7056. @"name" : @"credit_card_expiration",
  7057. @"required" : @"true",
  7058. @"type" : @"date",
  7059. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7060. },
  7061. @"item_3" : @{
  7062. @"aname" : @"Security Code",
  7063. @"control" : @"edit",
  7064. @"keyboard" : @"int",
  7065. @"length" : @"3",
  7066. @"name" : @"credit_card_security_code",
  7067. @"required" : @"true",
  7068. @"value" : securityCode
  7069. },
  7070. @"item_4" : @{
  7071. @"aname" : @"First Name",
  7072. @"control" : @"edit",
  7073. @"keyboard" : @"text",
  7074. @"name" : @"credit_card_first_name",
  7075. @"required" : @"true",
  7076. @"value" : firstName
  7077. },
  7078. @"item_5" : @{
  7079. @"aname" : @"Last Name",
  7080. @"control" : @"edit",
  7081. @"keyboard" : @"text",
  7082. @"name" : @"credit_card_last_name",
  7083. @"required" : @"true",
  7084. @"value" : lastName
  7085. },
  7086. @"item_6" : @{
  7087. @"aname" : @"Address 1",
  7088. @"control" : @"edit",
  7089. @"keyboard" : @"text",
  7090. @"name" : @"credit_card_address1",
  7091. @"required" : @"true",
  7092. @"value" : addr1
  7093. },
  7094. @"item_7" : @{
  7095. @"aname" : @"Address 2",
  7096. @"control" : @"edit",
  7097. @"keyboard" : @"text",
  7098. @"name" : @"credit_card_address2",
  7099. @"value" : addr2
  7100. },
  7101. @"item_8" : @{
  7102. @"aname" : @"zip code",
  7103. @"control" : @"edit",
  7104. @"keyboard" : @"text",
  7105. @"name" : @"credit_card_zipcode",
  7106. @"required" : @"true",
  7107. @"value" : zipcode
  7108. },
  7109. @"item_9" : @{
  7110. @"aname" : @"City",
  7111. @"control" : @"edit",
  7112. @"keyboard" : @"text",
  7113. @"name" : @"credit_card_city",
  7114. @"required" : @"true",
  7115. @"value" : city
  7116. }
  7117. },
  7118. @"value" : @"Fill Now",
  7119. @"value_id" : @""
  7120. },
  7121. @"val_1" : @{
  7122. @"check" : @(0),
  7123. @"value" : @"Fill Later",
  7124. @"value_id" : @""
  7125. }
  7126. },
  7127. @"control" : @"enum",
  7128. @"name" : @"",
  7129. @"single_select" : @"true"
  7130. }
  7131. },
  7132. @"value" : @"Credit Card",
  7133. @"value_id" : @"Credit Card"
  7134. },
  7135. @"val_5" : @{
  7136. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7137. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7138. @"value_id" : @"FOLLOW EXISTING"
  7139. }
  7140. },
  7141. @"control" : @"enum",
  7142. @"name" : @"paymentType",
  7143. @"single_select" : @"true"
  7144. },
  7145. @"title" : @"Payment Information"
  7146. }.mutableCopy;
  7147. NSNumber *hide = setting[@"PaymentInformationHide"];
  7148. [dic setValue:hide forKey:@"hide"];
  7149. return dic;
  7150. }
  7151. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7152. {
  7153. sqlite3 *db = [iSalesDB get_db];
  7154. // UISetting
  7155. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7156. NSString *cachefolder = [paths objectAtIndex:0];
  7157. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  7158. NSData* json =nil;
  7159. json=[NSData dataWithContentsOfFile:img_cache];
  7160. NSError *error=nil;
  7161. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  7162. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  7163. [params setObject:setting forKey:@"setting"];
  7164. int section_count = 0;
  7165. // params
  7166. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7167. orderCode = [self translateSingleQuote:orderCode];
  7168. // 0 Order Type 1 Shipping Method 2 Payment Information
  7169. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  7170. // 0 Order Type
  7171. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  7172. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  7173. [ret setValue:order_type_dic forKey:key0];
  7174. // 1 Shipping Method
  7175. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  7176. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  7177. [ret setValue:shipping_method_dic forKey:key1];
  7178. // 2 Payment Information
  7179. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  7180. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  7181. [ret setValue:payment_info_dic forKey:key2];
  7182. // 3 Customer
  7183. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  7184. NSDictionary *customer_dic = [self customerDic:params db:db];
  7185. [ret setValue:customer_dic forKey:key3];
  7186. // 4 Ship To
  7187. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  7188. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  7189. [ret setValue:ship_to_dic forKey:key4];
  7190. // 5 Ship From
  7191. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  7192. if (![shipFromDisable integerValue]) {
  7193. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  7194. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  7195. [ret setValue:ship_from_dic forKey:key5];
  7196. }
  7197. // 6 Freight Bill To
  7198. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  7199. if (![freightBillToDisable integerValue]) {
  7200. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  7201. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  7202. [ret setValue:freight_bill_to forKey:key6];
  7203. }
  7204. // 7 Merchandise Bill To
  7205. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  7206. if (![merchandiseBillToDisable integerValue]) {
  7207. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  7208. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  7209. [ret setValue:merchandise_bill_to_dic forKey:key7];
  7210. }
  7211. // 8 Return To
  7212. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  7213. if (![returnToDisable integerValue]) {
  7214. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  7215. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  7216. [ret setValue:return_to_dic forKey:key8];
  7217. }
  7218. // 9 Model Information
  7219. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  7220. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  7221. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  7222. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  7223. [ret setValue:model_info_dic forKey:key9];
  7224. // 10 Remarks Content
  7225. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  7226. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  7227. [ret setValue:remarks_content_dic forKey:key10];
  7228. // 11 Order Total
  7229. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  7230. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  7231. [ret setValue:order_total_dic forKey:key11];
  7232. // 12 Signature
  7233. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  7234. NSDictionary *sign_dic = [self signatureDic:params db:db];
  7235. [ret setValue:sign_dic forKey:key12];
  7236. [ret setValue:@(section_count) forKey:@"section_count"];
  7237. [iSalesDB close_db:db];
  7238. return [RAUtils dict2data:ret];
  7239. // return nil;
  7240. }
  7241. #pragma mark addr editor
  7242. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  7243. NSMutableDictionary *new_item = [item mutableCopy];
  7244. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  7245. return new_item;
  7246. }
  7247. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  7248. {
  7249. // "is_subaction" = true;
  7250. // orderCode = MOB1608240002;
  7251. // password = 123456;
  7252. // "subaction_tag" = 1;
  7253. // user = EvanK;
  7254. // {
  7255. // "is_subaction" = true;
  7256. // orderCode = MOB1608240002;
  7257. // password = 123456;
  7258. // "refresh_trigger" = zipcode;
  7259. // "subaction_tag" = 1;
  7260. // user = EvanK;
  7261. // }
  7262. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  7263. [ret removeObjectForKey:@"up_params"];
  7264. [ret setObject:@"New Address" forKey:@"title"];
  7265. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  7266. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  7267. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  7268. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  7269. [country_dic removeObjectForKey:@"refresh"];
  7270. [country_dic removeObjectForKey:@"restore"];
  7271. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  7272. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  7273. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  7274. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  7275. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  7276. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  7277. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  7278. [zip_code_dic removeObjectForKey:@"refresh"];
  7279. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  7280. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  7281. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  7282. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  7283. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  7284. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  7285. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  7286. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  7287. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  7288. // country
  7289. NSString *countryCode = @"US";
  7290. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  7291. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  7292. // state
  7293. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  7294. NSDictionary *tmpDic = @{
  7295. @"value" : @"Other",
  7296. @"value_id" : @"",
  7297. @"check" : [NSNumber numberWithInteger:0]
  7298. };
  7299. for (int i = 0; i < allState.allKeys.count; i++) {
  7300. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  7301. NSDictionary *tmp = allState[key];
  7302. [allState setValue:tmpDic forKey:key];
  7303. tmpDic = tmp;
  7304. }
  7305. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  7306. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  7307. [ret setValue:new_section_0 forKey:@"section_0"];
  7308. return [RAUtils dict2data:ret];
  7309. }
  7310. #pragma mark save addr
  7311. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  7312. {
  7313. // NSString *companyName = [self valueInParams:params key:@"company"];
  7314. // NSString *addr1 = [self valueInParams:params key:@"address"];
  7315. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  7316. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  7317. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  7318. // NSString *countryId = [self valueInParams:params key:@"country"];
  7319. // NSString *stateCode = [self valueInParams:params key:@"state"];
  7320. // NSString *city = [self valueInParams:params key:@"city"];
  7321. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  7322. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  7323. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  7324. // NSString *phone = [self valueInParams:params key:@"phone"];
  7325. // NSString *fax = [self valueInParams:params key:@"fax"];
  7326. // NSString *email = [self valueInParams:params key:@"email"];
  7327. return [self offline_saveContact:params update:NO isCustomer:NO];
  7328. }
  7329. #pragma mark cancel order
  7330. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  7331. {
  7332. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7333. NSString *order_id = [self valueInParams:params key:@"orderId"];
  7334. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  7335. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  7336. int ret = [iSalesDB execSql:sql];
  7337. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7338. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7339. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7340. // [dic setValue:@"160409" forKey:@"min_ver"];
  7341. return [RAUtils dict2data:dic];
  7342. }
  7343. #pragma mark sign order
  7344. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  7345. {
  7346. //参考 offline_saveBusinesscard
  7347. NSLog(@"sign order params: %@",params);
  7348. // orderCode = MOB1608240002;
  7349. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  7350. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7351. orderCode = [self translateSingleQuote:orderCode];
  7352. NSString *picPath = [self valueInParams:params key:@"picpath"];
  7353. picPath = [self translateSingleQuote:picPath];
  7354. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  7355. int ret = [iSalesDB execSql:sql];
  7356. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7357. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7358. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7359. // [dic setValue:@"160409" forKey:@"min_ver"];
  7360. return [RAUtils dict2data:dic];
  7361. }
  7362. #pragma mark save order
  7363. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  7364. NSString *ret = [self valueInParams:params key:key];
  7365. if (translate) {
  7366. ret = [self translateSingleQuote:ret];
  7367. }
  7368. return ret;
  7369. }
  7370. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  7371. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  7372. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  7373. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  7374. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  7375. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  7376. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  7377. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  7378. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  7379. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  7380. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  7381. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  7382. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  7383. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  7384. if (![total_price isEqualToString:@""]) {
  7385. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  7386. } else {
  7387. total_price = @"";
  7388. }
  7389. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  7390. if ([paymentsAndCredits isEqualToString:@""]) {
  7391. paymentsAndCredits = @"";
  7392. } else {
  7393. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  7394. }
  7395. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  7396. if ([handling_fee_value isEqualToString:@""]) {
  7397. handling_fee_value = @"";
  7398. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  7399. handling_fee_value = @"";
  7400. } else {
  7401. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  7402. }
  7403. NSString *handling_fee_placeholder = handling_fee_value;
  7404. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  7405. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  7406. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  7407. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  7408. NSString *lift_gate_placeholder = @"";
  7409. if ([lift_gate_value isEqualToString:@""]) {
  7410. lift_gate_placeholder = @"";
  7411. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  7412. lift_gate_placeholder = @"";
  7413. } else {
  7414. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  7415. }
  7416. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  7417. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  7418. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  7419. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  7420. 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];
  7421. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  7422. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  7423. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  7424. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  7425. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  7426. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  7427. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  7428. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  7429. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  7430. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  7431. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  7432. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  7433. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  7434. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  7435. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  7436. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  7437. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  7438. BOOL number_nil = [credit_card_number isEqualToString:@""];
  7439. if (!number_nil) {
  7440. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  7441. }
  7442. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  7443. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  7444. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  7445. if (!security_code_nil) {
  7446. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  7447. }
  7448. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  7449. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  7450. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  7451. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  7452. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  7453. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  7454. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  7455. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  7456. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  7457. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  7458. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  7459. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  7460. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  7461. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  7462. NSString *contact_id = customer_cid;
  7463. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  7464. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  7465. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7466. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7467. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7468. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7469. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7470. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7471. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7472. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7473. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7474. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7475. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7476. //
  7477. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7478. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7479. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7480. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7481. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7482. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7483. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7484. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7485. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7486. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7487. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7488. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7489. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7490. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7491. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7492. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7493. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7494. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7495. 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];
  7496. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7497. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7498. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7499. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7500. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7501. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7502. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7503. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7504. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7505. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7506. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7507. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7508. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7509. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7510. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7511. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7512. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7513. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7514. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7515. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7516. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7517. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7518. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7519. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7520. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7521. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7522. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7523. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7524. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7525. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7526. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7527. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7528. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7529. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7530. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7531. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7532. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7533. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7534. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7535. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7536. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7537. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7538. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7539. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7540. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7541. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7542. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7543. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7544. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7545. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7546. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7547. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7548. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7549. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7550. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7551. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7552. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7553. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7554. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7555. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7556. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7557. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7558. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7559. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7560. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7561. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7562. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7563. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7564. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7565. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7566. NSString *order_status = @"status = 1,";
  7567. if (submit) {
  7568. order_status = @"status = -11,";
  7569. }
  7570. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7571. NSString *sync_sql = @"";
  7572. if (submit) {
  7573. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7574. [param setValue:sales_rep forKey:@"sales_rep"];
  7575. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7576. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7577. }
  7578. 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];
  7579. DebugLog(@"save order contactSql: %@",contactSql);
  7580. DebugLog(@"save order orderSql: %@",orderSql);
  7581. // int contact_ret = [iSalesDB execSql:contactSql];
  7582. int order_ret = [iSalesDB execSql:orderSql];
  7583. int ret = order_ret;
  7584. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7585. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7586. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7587. // [dic setValue:@"160409" forKey:@"min_ver"];
  7588. [dic setObject:so_id forKey:@"so#"];
  7589. return [RAUtils dict2data:dic];
  7590. }
  7591. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7592. {
  7593. // id foo = nil;
  7594. // NSMutableArray *a = @[].mutableCopy;
  7595. // [a addObject:foo];
  7596. return [self saveorder:param submit:NO];
  7597. }
  7598. #pragma mark add to cart by name
  7599. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7600. {
  7601. NSString *orderCode = [params objectForKey:@"orderCode"];
  7602. NSString *product_name = [params objectForKey:@"product_name"];
  7603. product_name = [self translateSingleQuote:product_name];
  7604. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7605. sqlite3 *db = [iSalesDB get_db];
  7606. __block NSMutableString *product_id_string = [NSMutableString string];
  7607. for (int i = 0; i < product_name_array.count; i++) {
  7608. NSString *name = [product_name_array objectAtIndex:i];
  7609. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7610. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7611. int product_id = sqlite3_column_int(stmt, 0);
  7612. if (i == product_name_array.count - 1) {
  7613. [product_id_string appendFormat:@"%d",product_id];
  7614. } else {
  7615. [product_id_string appendFormat:@"%d,",product_id];
  7616. }
  7617. }];
  7618. }
  7619. NSDictionary *newParams = @{
  7620. @"product_id" : product_id_string,
  7621. @"orderCode" : orderCode
  7622. };
  7623. [iSalesDB close_db:db];
  7624. return [self offline_add2cart:[newParams mutableCopy]];
  7625. }
  7626. #pragma mark reset order
  7627. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7628. {
  7629. UIApplication * app = [UIApplication sharedApplication];
  7630. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7631. appDelegate.disable_trigger=true;
  7632. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7633. appDelegate.disable_trigger=false;
  7634. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7635. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7636. return [RAUtils dict2data:dic];
  7637. }
  7638. #pragma mark submit order
  7639. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7640. {
  7641. return [self saveorder:params submit:YES];
  7642. }
  7643. #pragma mark copy order
  7644. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7645. {
  7646. NSMutableDictionary *ret = @{}.mutableCopy;
  7647. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7648. sqlite3 *db = [iSalesDB get_db];
  7649. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7650. // 首先查看联系人是否active
  7651. 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];
  7652. __block int customer_is_active = 1;
  7653. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7654. customer_is_active = sqlite3_column_int(stmt, 0);
  7655. }];
  7656. if (!customer_is_active) {
  7657. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7658. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7659. [iSalesDB close_db:db];
  7660. return [RAUtils dict2data:ret];
  7661. }
  7662. // new order
  7663. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7664. NSString *new_order_code = [self get_offline_soid:db];
  7665. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7666. user = [self translateSingleQuote:user];
  7667. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  7668. __block int result = 1;
  7669. result = [iSalesDB execSql:insert_order_sql db:db];
  7670. if (!result) {
  7671. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7672. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7673. [iSalesDB close_db:db];
  7674. return [RAUtils dict2data:ret];
  7675. }
  7676. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7677. // __block NSString *product_id = @"";
  7678. __weak typeof(self) weakSelf = self;
  7679. 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];
  7680. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7681. int is_active = sqlite3_column_int(stmt, 1);
  7682. if (is_active) {
  7683. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7684. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7685. 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];
  7686. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7687. }
  7688. }];
  7689. // product_id = [product_id substringFromIndex:1];
  7690. //
  7691. // [self offline_add2cart:@{}.mutableCopy];
  7692. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7693. [iSalesDB close_db:db];
  7694. if (result) {
  7695. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7696. } else {
  7697. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7698. }
  7699. [ret setObject:new_order_code forKey:@"so_id"];
  7700. return [RAUtils dict2data:ret];
  7701. }
  7702. #pragma mark move wish list to cart
  7703. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7704. NSString *collectId = params[@"collectId"];
  7705. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7706. __block NSString *product_id = @"";
  7707. __block NSString *qty = @"";
  7708. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7709. int productId = sqlite3_column_int(stmt, 0);
  7710. int item_qty = sqlite3_column_int(stmt, 1);
  7711. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7712. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7713. }];
  7714. product_id = [product_id substringFromIndex:1];
  7715. qty = [qty substringFromIndex:1];
  7716. NSDictionary *newParams = @{
  7717. @"product_id" : product_id,
  7718. @"orderCode" : params[@"orderCode"],
  7719. @"qty" : qty
  7720. };
  7721. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7722. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7723. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7724. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7725. //
  7726. // int ret = [iSalesDB execSql:deleteSQL];
  7727. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7728. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7729. }
  7730. return [RAUtils dict2data:retDic];
  7731. }
  7732. #pragma mark - portfolio
  7733. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7734. int sort = [[params valueForKey:@"sort"] integerValue];
  7735. int offset = [[params valueForKey:@"offset"] integerValue];
  7736. int limit = [[params valueForKey:@"limit"] integerValue];
  7737. NSString *orderBy = @"";
  7738. switch (sort) {
  7739. case 0:{
  7740. orderBy = @"modify_time desc";
  7741. }
  7742. break;
  7743. case 1:{
  7744. orderBy = @"modify_time asc";
  7745. }
  7746. break;
  7747. case 2:{
  7748. orderBy = @"name asc";
  7749. }
  7750. break;
  7751. case 3:{
  7752. orderBy = @"name desc";
  7753. }
  7754. break;
  7755. case 4:{
  7756. orderBy = @"description asc";
  7757. }
  7758. break;
  7759. default:
  7760. break;
  7761. }
  7762. 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];
  7763. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7764. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7765. sqlite3 *db = [iSalesDB get_db];
  7766. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7767. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7768. int product_id = sqlite3_column_int(stmt, 0);
  7769. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7770. NSString *name = [self textAtColumn:1 statement:stmt];
  7771. NSString *description = [self textAtColumn:2 statement:stmt];
  7772. double price = sqlite3_column_double(stmt, 3);
  7773. double discount = sqlite3_column_double(stmt,4);
  7774. int qty = sqlite3_column_int(stmt, 5);
  7775. int percentage = sqlite3_column_int(stmt, 6);
  7776. int item_id = sqlite3_column_int(stmt, 7);
  7777. // int fashion_id = sqlite3_column_int(stmt, 8);
  7778. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7779. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7780. double percent = sqlite3_column_double(stmt, 11);
  7781. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7782. if ([price_null isEqualToString:@"null"]) {
  7783. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7784. }
  7785. NSMutableDictionary *item = @{
  7786. @"linenotes": line_note,
  7787. @"check": @(1),
  7788. @"product_id": product_id_string,
  7789. @"available_qty": @(qty),
  7790. @"available_percent" : @(percent),
  7791. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7792. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7793. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7794. @"img": img_path,
  7795. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7796. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7797. }.mutableCopy;
  7798. if (percentage) {
  7799. [item removeObjectForKey:@"available_qty"];
  7800. } else {
  7801. [item removeObjectForKey:@"available_percent"];
  7802. }
  7803. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7804. if ([qty_null isEqualToString:@"null"]) {
  7805. [item removeObjectForKey:@"available_qty"];
  7806. }
  7807. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7808. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7809. }];
  7810. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7811. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7812. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7813. }
  7814. [iSalesDB close_db:db];
  7815. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7816. [dic setValue:@"" forKey:@"email_content"];
  7817. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7818. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7819. return [RAUtils dict2data:dic];
  7820. }
  7821. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  7822. __block int qty = 0;
  7823. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7824. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7825. qty = sqlite3_column_int(stmt, 0);
  7826. }];
  7827. return qty;
  7828. }
  7829. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7830. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7831. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7832. NSMutableDictionary * values = params[@"replaceValue"];
  7833. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  7834. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7835. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7836. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7837. NSString *pdf_path = @"";
  7838. if (direct) {
  7839. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7840. } else {
  7841. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7842. }
  7843. NSString *create_user = [self valueInParams:params key:@"user"];
  7844. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7845. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7846. // model info
  7847. 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];
  7848. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7849. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  7850. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7851. sqlite3 *db = [iSalesDB get_db];
  7852. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7853. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7854. int product_id = sqlite3_column_int(stmt, 0);
  7855. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7856. double price = sqlite3_column_double(stmt, 1);
  7857. double discount = sqlite3_column_double(stmt,2);
  7858. int qty = sqlite3_column_int(stmt, 3);
  7859. int percentage = sqlite3_column_int(stmt, 4);
  7860. int item_id = sqlite3_column_int(stmt, 5);
  7861. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7862. double percent = sqlite3_column_double(stmt, 7);
  7863. NSString *price_null = [self textAtColumn:1 statement:stmt];
  7864. /* if ([price_null isEqualToString:@"null"]) {
  7865. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7866. }
  7867. */
  7868. [product_ids_string appendFormat:@"%@,",product_id_string];
  7869. // Regular Price
  7870. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  7871. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  7872. // QTY
  7873. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7874. if ([qty_null isEqualToString:@"null"]) {
  7875. // 查available
  7876. qty = [self model_QTY:product_id_string db:db];
  7877. }
  7878. if (percentage) {
  7879. qty = qty * percent / 100;
  7880. }
  7881. // Special Price
  7882. if ([price_null isEqualToString:@"null"]) {
  7883. // price = regular price
  7884. price = [regular_price_str doubleValue];
  7885. }
  7886. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  7887. if (![discount_null isEqualToString:@"null"]) {
  7888. price = price * (1 - discount / 100.0);
  7889. }
  7890. NSMutableDictionary *item = @{
  7891. @"line_note": line_note,
  7892. @"product_id": product_id_string,
  7893. @"available_qty": @(qty),
  7894. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7895. @"regular_price" : regular_price_str,
  7896. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  7897. }.mutableCopy;
  7898. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7899. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7900. }];
  7901. [iSalesDB close_db:db];
  7902. if (product_ids_string.length > 0) {
  7903. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  7904. }
  7905. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7906. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7907. return [RAUtils dict2data:resultDictionary];
  7908. }
  7909. NSString *model_info = [RAUtils dict2string:dic];
  7910. // 创建PDF
  7911. // 在preview情况下保存,则不需要新建了
  7912. if (direct) {
  7913. NSMutableDictionary *tear_sheet_params = params;
  7914. // if (tear_sheet_id) {
  7915. // tear_sheet_params = values;
  7916. // }
  7917. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  7918. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7919. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7920. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7921. resultDictionary = pdfInfo.mutableCopy;
  7922. } else { // 创建PDF失败
  7923. return pdfData;
  7924. }
  7925. } else {
  7926. // pdf_path 就是本地路径
  7927. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7928. }
  7929. // 将文件移动到PDF Cache文件夹
  7930. NSString *newPath = [pdf_path lastPathComponent];
  7931. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7932. NSString *cachefolder = [paths objectAtIndex:0];
  7933. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7934. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7935. NSFileManager *fileManager = [NSFileManager defaultManager];
  7936. NSError *error = nil;
  7937. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7938. if (error) { // 移动文件失败
  7939. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7940. return [RAUtils dict2data:resultDictionary];
  7941. }
  7942. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7943. pdf_path = [newPath lastPathComponent];
  7944. // 保存信息
  7945. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  7946. NSString *off_params = [RAUtils dict2string:params];
  7947. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  7948. pdf_path = [self translateSingleQuote:pdf_path];
  7949. create_user = [self translateSingleQuote:create_user];
  7950. tear_note = [self translateSingleQuote:tear_note];
  7951. tear_name = [self translateSingleQuote:tear_name];
  7952. model_info = [self translateSingleQuote:model_info];
  7953. configureParams = [self translateSingleQuote:configureParams];
  7954. off_params = [self translateSingleQuote:off_params];
  7955. 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];
  7956. if (tear_sheet_id) {
  7957. int _id = [tear_sheet_id integerValue];
  7958. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  7959. }
  7960. int result = [iSalesDB execSql:save_pdf_sql];
  7961. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7962. //
  7963. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7964. if (remove_Item) {
  7965. // portfolioId
  7966. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7967. }
  7968. return [RAUtils dict2data:resultDictionary];
  7969. }
  7970. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7971. return [self offline_savePDF:params direct:YES];
  7972. }
  7973. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7974. return [self offline_savePDF:params direct:NO];
  7975. }
  7976. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7977. int offset = [[params valueForKey:@"offset"] integerValue];
  7978. int limit = [[params valueForKey:@"limit"] integerValue];
  7979. NSString *keyword = [params valueForKey:@"keyWord"];
  7980. NSString *where = @"where is_delete is null or is_delete = 0";
  7981. if (keyword.length) {
  7982. keyword = [self translateSingleQuote:keyword];
  7983. 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];
  7984. }
  7985. 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
  7986. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7987. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7988. NSString *cachefolder = [paths objectAtIndex:0];
  7989. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7990. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7991. NSString *name = [self textAtColumn:0 statement:stmt];
  7992. NSString *note = [self textAtColumn:1 statement:stmt];
  7993. NSString *time = [self textAtColumn:2 statement:stmt];
  7994. NSString *user = [self textAtColumn:3 statement:stmt];
  7995. NSString *path = [self textAtColumn:4 statement:stmt];
  7996. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7997. NSString *off_params = [self textAtColumn:7 statement:stmt];
  7998. path = [pdfFolder stringByAppendingPathComponent:path];
  7999. BOOL bdir=NO;
  8000. NSFileManager* fileManager = [NSFileManager defaultManager];
  8001. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  8002. {
  8003. //pdf文件不存在
  8004. path=nil;
  8005. }
  8006. time = [self changeDateTimeFormate:time];
  8007. time = [time stringByAppendingString:@" PST"];
  8008. int sheet_id = sqlite3_column_int(stmt, 5);
  8009. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  8010. item[@"tearsheetsId"]=@(sheet_id);
  8011. item[@"pdf_path"]=path;
  8012. item[@"create_time"]=time;
  8013. item[@"create_user"]=user;
  8014. item[@"tear_note"]=note;
  8015. item[@"tear_name"]=name;
  8016. item[@"isLocalFile"]=@"true";
  8017. item[@"model_info"]=model_info;
  8018. item[@"off_params"]=off_params;
  8019. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8020. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8021. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8022. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8023. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8024. }];
  8025. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8026. return [RAUtils dict2data:dic];
  8027. }
  8028. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  8029. NSString *prodct_ids = [params objectForKey:@"product_id"];
  8030. // NSString *user = [params objectForKey:@"user"];
  8031. // 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];
  8032. 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];
  8033. int result = [iSalesDB execSql:sql];
  8034. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8035. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  8036. if (result == RESULT_TRUE) {
  8037. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  8038. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  8039. sqlite3 *db = [iSalesDB get_db];
  8040. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  8041. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  8042. [iSalesDB close_db:db];
  8043. }
  8044. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8045. return [RAUtils dict2data:dic];
  8046. }
  8047. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  8048. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  8049. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  8050. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  8051. // Regurlar Price
  8052. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  8053. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  8054. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  8055. __block long val_count = 0;
  8056. NSString *sql = @"select name,type,order_by from price order by order_by";
  8057. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8058. NSString *name = [self textAtColumn:0 statement:stmt];
  8059. int type = sqlite3_column_int(stmt, 1);
  8060. int order_by = sqlite3_column_int(stmt, 2);
  8061. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8062. NSDictionary *price_dic = @{
  8063. @"value" : name,
  8064. @"value_id" : [NSNumber numberWithInteger:type],
  8065. @"check" : order_by == 0 ? @(1) : @(0)
  8066. };
  8067. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8068. val_count = ++(*count);
  8069. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8070. val_count = 0;
  8071. }];
  8072. [cadedate setObject:@{@"value" : @"None",
  8073. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  8074. val_count++;
  8075. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  8076. [price setObject:cadedate forKey:@"cadedate"];
  8077. [section1 setObject:price forKey:@"item_2"];
  8078. [dic setObject:section1 forKey:@"section_1"];
  8079. return [RAUtils dict2data:dic];
  8080. }
  8081. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  8082. NSString *product_id = [params objectForKey:@"fashionId"];
  8083. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8084. __block int result = RESULT_TRUE;
  8085. __block int qty = 0;
  8086. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8087. qty = sqlite3_column_int(stmt, 0);
  8088. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8089. result = RESULT_FALSE;
  8090. }];
  8091. NSMutableDictionary *dic = @{
  8092. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  8093. @"mode" : @"Regular Mode",
  8094. @"quantity_available" : @(qty),
  8095. @"result" : @(result),
  8096. }.mutableCopy;
  8097. return [RAUtils dict2data:dic];
  8098. }
  8099. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  8100. NSString *item_ids = [params objectForKey:@"item_id"];
  8101. NSString *line_notes = [params objectForKey:@"notes"];
  8102. NSString *price_str = [params objectForKey:@"price"];
  8103. NSString *discount_str = [params objectForKey:@"discount"];
  8104. NSString *percent = [params objectForKey:@"available_percent"];
  8105. NSString *qty = [params objectForKey:@"available_qty"];
  8106. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  8107. int dot = 0;
  8108. if (line_notes) {
  8109. line_notes = [self translateSingleQuote:line_notes];
  8110. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  8111. sql = [sql stringByAppendingString:line_notes];
  8112. dot = 1;
  8113. } else {
  8114. line_notes = @"";
  8115. }
  8116. if (price_str) {
  8117. if (dot) {
  8118. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  8119. } else {
  8120. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  8121. dot = 1;
  8122. }
  8123. sql = [sql stringByAppendingString:price_str];
  8124. } else {
  8125. price_str = @"";
  8126. }
  8127. if (discount_str) {
  8128. if (dot) {
  8129. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  8130. } else {
  8131. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  8132. dot = 1;
  8133. }
  8134. sql = [sql stringByAppendingString:discount_str];
  8135. } else {
  8136. discount_str = @"";
  8137. }
  8138. if (percent) {
  8139. if (dot) {
  8140. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  8141. } else {
  8142. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  8143. dot = 1;
  8144. }
  8145. sql = [sql stringByAppendingString:percent];
  8146. } else {
  8147. percent = @"";
  8148. }
  8149. if (qty) {
  8150. if (dot) {
  8151. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  8152. } else {
  8153. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  8154. dot = 1;
  8155. }
  8156. sql = [sql stringByAppendingString:qty];
  8157. } else {
  8158. qty = @"";
  8159. }
  8160. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  8161. sql = [sql stringByAppendingString:where];
  8162. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8163. int result = [iSalesDB execSql:sql];
  8164. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8165. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8166. return [RAUtils dict2data:dic];
  8167. }
  8168. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  8169. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  8170. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8171. sqlite3 *db = [iSalesDB get_db];
  8172. 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];
  8173. int result = [iSalesDB execSql:sql db:db];
  8174. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8175. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8176. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  8177. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  8178. [iSalesDB close_db:db];
  8179. return [RAUtils dict2data:dic];
  8180. }
  8181. + (void)offline_removePDFWithName:(NSString *)name {
  8182. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8183. NSString *cachefolder = [paths objectAtIndex:0];
  8184. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8185. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  8186. NSFileManager *fileManager = [NSFileManager defaultManager];
  8187. [fileManager removeItemAtPath:path error:nil];
  8188. }
  8189. + (void)offline_clear_PDFCache {
  8190. NSFileManager *fileManager = [NSFileManager defaultManager];
  8191. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8192. NSString *cachefolder = [paths objectAtIndex:0];
  8193. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8194. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  8195. for (NSString *path in pdf_files) {
  8196. [self offline_removePDFWithName:[path lastPathComponent]];
  8197. }
  8198. }
  8199. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  8200. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8201. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  8202. NSString *user = [params objectForKey:@"user"];
  8203. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  8204. if (![create_user isEqualToString:user]) {
  8205. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8206. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8207. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  8208. return [RAUtils dict2data:dic];
  8209. }
  8210. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8211. __block int is_local = 0;
  8212. __block NSString *path = @"";
  8213. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8214. is_local = sqlite3_column_int(stmt, 0);
  8215. path = [self textAtColumn:1 statement:stmt];
  8216. }];
  8217. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8218. if (is_local == 1) {
  8219. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  8220. // 删除文件
  8221. [self offline_removePDFWithName:path];
  8222. }
  8223. int result = [iSalesDB execSql:sql];
  8224. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8225. [dic setObject:@"Regular Mode" forKey:@"mode"];
  8226. return [RAUtils dict2data:dic];
  8227. }
  8228. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  8229. {
  8230. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8231. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8232. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8233. NSString* where=@"1=1";
  8234. if (ver!=nil) {
  8235. where=@"is_dirty=1";
  8236. }
  8237. 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];
  8238. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8239. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8240. sqlite3 *db = [iSalesDB get_db];
  8241. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8242. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8243. NSInteger _id = sqlite3_column_int(stmt, 0);
  8244. NSInteger product_id = sqlite3_column_int(stmt, 1);
  8245. NSString *name = [self textAtColumn:2 statement:stmt];
  8246. NSString *desc = [self textAtColumn:3 statement:stmt];
  8247. NSInteger item_id = sqlite3_column_int(stmt, 4);
  8248. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  8249. NSInteger qty = sqlite3_column_int(stmt, 6);
  8250. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  8251. double percent = sqlite3_column_double(stmt, 8);
  8252. double price = sqlite3_column_double(stmt, 9);
  8253. double discount = sqlite3_column_double(stmt, 10);
  8254. NSString *img = [self textAtColumn:11 statement:stmt];
  8255. NSString *line_note = [self textAtColumn:12 statement:stmt];
  8256. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  8257. NSString *create_time = [self textAtColumn:14 statement:stmt];
  8258. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  8259. NSString *price_null = [self textAtColumn:9 statement:stmt];
  8260. if ([price_null isEqualToString:@"null"]) {
  8261. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8262. }
  8263. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  8264. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  8265. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  8266. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8267. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  8268. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  8269. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  8270. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  8271. if ([qty_null isEqualToString:@"null"]) {
  8272. // [item setValue:@"null" forKey:@"available_qty"];
  8273. } else {
  8274. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  8275. }
  8276. if ([is_percent_null isEqualToString:@"null"]) {
  8277. // [item setValue:@"null" forKey:@"percentage"];
  8278. } else {
  8279. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  8280. }
  8281. if ([percent_null isEqualToString:@"null"]) {
  8282. // [item setValue:@"null" forKey:@"percent"];
  8283. } else {
  8284. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  8285. }
  8286. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  8287. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  8288. [item setValue:name forKey:@"name"];
  8289. [item setValue:desc forKey:@"description"];
  8290. [item setValue:img forKey:@"img"];
  8291. [item setValue:line_note forKey:@"line_note"];
  8292. [item setValue:create_time forKey:@"createtime"];
  8293. [item setValue:modify_time forKey:@"modifytime"];
  8294. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8295. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8296. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8297. }];
  8298. [iSalesDB close_db:db];
  8299. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8300. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8301. }
  8302. return ret;
  8303. }
  8304. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  8305. {
  8306. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  8307. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  8308. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  8309. NSString* where=@"1=1";
  8310. if (ver!=nil) {
  8311. where=@"is_dirty=1";
  8312. }
  8313. 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];
  8314. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  8315. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8316. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  8317. NSInteger _id = sqlite3_column_int(stmt, 0);
  8318. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  8319. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  8320. NSString *create_user = [self textAtColumn:3 statement:stmt];
  8321. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  8322. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  8323. NSString *model_info = [self textAtColumn:6 statement:stmt];
  8324. NSString *createtime = [self textAtColumn:7 statement:stmt];
  8325. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  8326. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  8327. NSString *off_params = [self textAtColumn:10 statement:stmt];
  8328. NSString *uuid = [NSUUID UUID].UUIDString;
  8329. int is_delete = sqlite3_column_int(stmt, 11);
  8330. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  8331. int is_local = sqlite3_column_int(stmt, 12);
  8332. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  8333. if (![tearsheet_id_null isEqualToString:@"null"]) {
  8334. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  8335. }
  8336. [item setObject:pdf_path forKey:@"pdf_path"];
  8337. [item setObject:create_user forKey:@"create_user"];
  8338. [item setObject:tear_note forKey:@"tear_note"];
  8339. [item setObject:tear_name forKey:@"tear_name"];
  8340. [item setObject:model_info forKey:@"model_info"];
  8341. [item setObject:createtime forKey:@"createtime"];
  8342. [item setObject:modifytime forKey:@"modifytime"];
  8343. [item setObject:urlParams forKey:@"urlParams"];
  8344. [item setObject:off_params forKey:@"off_params"];
  8345. [item setObject:uuid forKey:@"pdf_token"];
  8346. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  8347. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  8348. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  8349. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  8350. } else {
  8351. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  8352. }
  8353. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8354. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  8355. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  8356. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8357. }];
  8358. return ret;
  8359. }
  8360. @end