OLDataProvider.m 667 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433
  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 "RASingleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. @interface OLDataProvider ()
  24. @end
  25. @implementation OLDataProvider
  26. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  27. {
  28. 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];
  29. NSString* ret=@"No Price.";
  30. sqlite3_stmt * statement;
  31. // int count=0;
  32. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  33. {
  34. if (sqlite3_step(statement) == SQLITE_ROW)
  35. {
  36. char *price = (char*)sqlite3_column_text(statement, 0);
  37. if(price==nil)
  38. price="";
  39. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  40. double dp= [nsprice doubleValue];
  41. ret=[NSString stringWithFormat:@"%.2f",dp];
  42. }
  43. sqlite3_finalize(statement);
  44. }
  45. return ret;
  46. // // [iSalesDB close_db:db];
  47. //
  48. // return nil;
  49. //// if(dprice==DBL_MAX)
  50. //// ret= nil;
  51. //// else
  52. //// ret= [NSNumber numberWithDouble:dprice];
  53. //// return ret;
  54. }
  55. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  56. {
  57. NSString* ret= nil;
  58. NSString *sqlQuery = nil;
  59. // 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
  60. 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];
  61. sqlite3_stmt * statement;
  62. // int count=0;
  63. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  64. {
  65. if (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  68. if(imgurl==nil)
  69. imgurl="";
  70. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  71. ret=nsimgurl;
  72. }
  73. sqlite3_finalize(statement);
  74. }
  75. else
  76. {
  77. ret=@"";
  78. }
  79. // [iSalesDB close_db:db];
  80. // DebugLog(@"data string: %@",ret );
  81. return ret;
  82. }
  83. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  84. {
  85. // assert(params[@"contact_id"]!=nil);
  86. assert(params[@"user"]!=nil);
  87. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  88. //生成portfolio pdf需要的数据
  89. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  90. data[@"npd_url"]=COMPANY_WEB;
  91. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  92. [formatter setDateFormat:@"MMMM yyyy"];
  93. NSString* date = [formatter stringFromDate:[NSDate date]];
  94. data[@"create_month"]=date;
  95. data[@"company_name"]=COMPANY_FULL_NAME;
  96. data[@"catalog_name"]=params[@"catalog_name"];
  97. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  98. data[@"TOTAL_PAGE"]=0;
  99. data[@"CURRENT_PAGE"]=0;
  100. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  101. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  102. bool show_stockout=[params[@"show_stockout"] boolValue];
  103. NSString *sql = nil;
  104. // v1.90 more color
  105. NSString *group_by = @"";
  106. if(values==nil)
  107. {
  108. if (show_group_by) {
  109. group_by = @"group by b.product_group ";
  110. }
  111. 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,b.more_color 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"],group_by];
  112. }
  113. else
  114. {
  115. if (show_group_by) {
  116. group_by = @"group by product_group ";
  117. }
  118. 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,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  119. }
  120. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  121. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  122. sqlite3 *db = [iSalesDB get_db];
  123. // NSDictionary *resultDic =
  124. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  125. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  126. int product_id = sqlite3_column_int(stmt, 0);
  127. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  128. NSString *name = [self textAtColumn:1 statement:stmt];
  129. NSString *description = [self textAtColumn:2 statement:stmt];
  130. double price = sqlite3_column_double(stmt, 3);
  131. NSString *s_price = [self textAtColumn:3 statement:stmt];
  132. double discount = sqlite3_column_double(stmt,4);
  133. int qty = sqlite3_column_int(stmt, 5);
  134. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  135. int is_percentage = sqlite3_column_int(stmt, 6);
  136. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  137. int item_id = sqlite3_column_int(stmt, 7);
  138. // int fashion_id = sqlite3_column_int(stmt, 8);
  139. NSString *line_note = [self textAtColumn:9 statement:stmt];
  140. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  141. double percent = sqlite3_column_double(stmt, 10);
  142. // int portfolio_id = sqlite3_column_int(stmt, 11);
  143. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  144. int availability = sqlite3_column_int(stmt, 12);
  145. NSString *color = [self textAtColumn:13 statement:stmt];
  146. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  147. NSString *demension = [self textAtColumn:15 statement:stmt];
  148. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  149. NSString *material = [self textAtColumn:17 statement:stmt];
  150. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  151. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  152. double volume = sqlite3_column_double(stmt, 19);
  153. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  154. double weight = sqlite3_column_double(stmt, 20);
  155. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  156. int model_set = sqlite3_column_int(stmt, 21);
  157. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  158. double load_ability = sqlite3_column_double(stmt, 22);
  159. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  160. NSString *assembling = [self textAtColumn:24 statement:stmt];
  161. NSString *made_in = [self textAtColumn:25 statement:stmt];
  162. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  163. NSString* gprice = [self get_portfolio_price :params[@"contact_id"] item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  164. float bprice=0;
  165. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  166. for(int b=0;b< [bundle[@"count"] intValue];b++)
  167. {
  168. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  169. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  170. }
  171. if(gprice!=nil)
  172. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  173. if(s_percent.length==0||is_percentage==0)
  174. {
  175. percent=100.0;
  176. if([s_qty isEqualToString:@"null"])
  177. qty=availability;
  178. }
  179. else
  180. {
  181. qty=availability;
  182. }
  183. qty=qty*percent/100+0.5;
  184. for(int i=0;i<[values[@"count"] intValue];i++)
  185. {
  186. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  187. if([valueitem[@"product_id"] intValue]==product_id)
  188. {
  189. line_note=valueitem[@"line_note"];
  190. qty=[valueitem[@"available_qty"] intValue];
  191. if([[valueitem allKeys] containsObject:@"available_qty"])
  192. s_qty=[NSString stringWithFormat:@"%d",qty];
  193. else
  194. s_qty=@"null";
  195. if(valueitem[@"regular_price"]!=nil)
  196. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  197. s_price=valueitem[@"special_price"];
  198. price=[valueitem[@"special_price"] floatValue];
  199. discount=0;
  200. // v1.90
  201. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  202. }
  203. }
  204. if(is_percentage==0&&qty==0&&!show_stockout)
  205. {
  206. return;
  207. }
  208. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  209. {
  210. return;
  211. }
  212. NSString* set_price=@"";
  213. if([params[@"entered_price"] boolValue])
  214. {
  215. if (s_price==nil ) {
  216. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  217. }
  218. else
  219. {
  220. if(price*(1-discount/100.0)!=gprice.floatValue)
  221. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  222. }
  223. }
  224. NSString* get_price=@"";
  225. {
  226. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  227. // DebugLog(@"price time interval");
  228. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  229. if(gprice==nil)
  230. get_price=@"Price:No Price.";
  231. else
  232. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  233. }
  234. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  235. // if ([qty_null isEqualToString:@"null"]) {
  236. // qty=availability;
  237. // }
  238. //
  239. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  240. // if ([qty_percent_null isEqualToString:@"null"]) {
  241. // percentage=1;
  242. // }
  243. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  244. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  245. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  246. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  247. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  248. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  249. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  250. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  251. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  252. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  253. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  254. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  255. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  256. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  257. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  258. if([params[@"availability"] boolValue]==false)
  259. str_availability=@"";
  260. else
  261. {
  262. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  263. str_availability=@"<b>Availability:</b> In Production";
  264. }
  265. if([params[@"color"] boolValue]==false || color.length==0 )
  266. str_color=@"";
  267. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  268. str_model_set=@"";
  269. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  270. str_legcolor=@"";
  271. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  272. str_demension=@"";
  273. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  274. str_seat_height=@"";
  275. if([params[@"material"] boolValue]==false || material.length==0 )
  276. str_meterial=@"";
  277. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  278. str_box_dim=@"";
  279. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  280. str_volume=@"";
  281. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  282. str_weight=@"";
  283. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  284. str_load_ability=@"";
  285. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  286. str_fabric_content=@"";
  287. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  288. str_assembling=@"";
  289. if([params[@"made"] boolValue]==false || made_in.length==0 )
  290. str_made_in=@"";
  291. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  292. str_line_note=@"";
  293. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  294. [arr_detail addObject:str_availability];
  295. [arr_detail addObject:str_color];
  296. [arr_detail addObject:str_model_set];
  297. [arr_detail addObject:str_legcolor];
  298. [arr_detail addObject:str_demension];
  299. [arr_detail addObject:str_seat_height];
  300. [arr_detail addObject:str_meterial];
  301. [arr_detail addObject:str_box_dim];
  302. [arr_detail addObject:str_volume];
  303. [arr_detail addObject:str_weight];
  304. [arr_detail addObject:str_load_ability];
  305. [arr_detail addObject:str_fabric_content];
  306. [arr_detail addObject:str_assembling];
  307. [arr_detail addObject:str_made_in];
  308. [arr_detail addObject:str_line_note];
  309. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  310. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  311. //model image;
  312. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  313. // qr image
  314. NSString* qrpath=nil;
  315. if([params[@"show_barcode"] boolValue])
  316. {
  317. NSString* temp = NSTemporaryDirectory();
  318. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  319. qrpath=[temp stringByAppendingPathComponent:filename];
  320. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  321. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  322. }
  323. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  324. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  325. item[@"img"]=img_path;
  326. item[@"img_qr"]=qrpath;
  327. item[@"special_price"]=set_price;
  328. item[@"price"]=get_price;
  329. item[@"name"]=name;
  330. item[@"description"]=description;
  331. item[@"detail"]=detail;
  332. item[@"img"]=img_path;
  333. if (show_group_by && more_color_int != 0) {
  334. item[@"more_color"] = more_color_path;
  335. }
  336. // @{
  337. // //@"linenotes": line_note,
  338. //// @"product_id": product_id_string,
  339. //// @"available_qty": @(qty),
  340. //// @"available_percent" : @(percent),
  341. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  342. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  343. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  344. // @"": ,
  345. // @"": ,
  346. // @"": ,
  347. // @"": ,
  348. // @"": ,
  349. // @"": ,
  350. // @"":
  351. // }.mutableCopy;
  352. // if (percentage) {
  353. // [item removeObjectForKey:@"available_qty"];
  354. // } else {
  355. // [item removeObjectForKey:@"available_percent"];
  356. // }
  357. //
  358. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  359. // if ([qty_null isEqualToString:@"null"]) {
  360. // [item removeObjectForKey:@"available_qty"];
  361. // }
  362. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  363. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  364. }];
  365. [iSalesDB close_db:db];
  366. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  367. // [dic setValue:@"" forKey:@"email_content"];
  368. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  369. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  370. //
  371. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  372. // cell[@"count"]=[NSNumber numberWithInt:10];
  373. grid[@"cell0"]=dic;
  374. data[@"grid0"]=grid;
  375. return data;
  376. }
  377. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  378. {
  379. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  380. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  381. NSString *cache_folder=[paths objectAtIndex:0];
  382. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  383. BOOL bdir=NO;
  384. NSFileManager* fileManager = [NSFileManager defaultManager];
  385. if(PDF_DEBUG)
  386. {
  387. NSData *data = [NSData dataWithContentsOfFile:default_path];
  388. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  389. return ret;
  390. }
  391. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  392. {
  393. NSError * error=nil;
  394. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  395. {
  396. return nil;
  397. }
  398. }
  399. NSData *data = [NSData dataWithContentsOfFile:template_path];
  400. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  401. return ret;
  402. }
  403. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  404. {
  405. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  406. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  407. DebugLog(@"offline_login sql:%@",sqlQuery);
  408. sqlite3_stmt * statement;
  409. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  410. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  411. {
  412. if (sqlite3_step(statement) == SQLITE_ROW)
  413. {
  414. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  415. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  416. // int can_show_price = sqlite3_column_int(statement, 0);
  417. // int can_see_price = sqlite3_column_int(statement, 1);
  418. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  419. // if(contact_id==nil)
  420. // contact_id="";
  421. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  422. // int user_type = sqlite3_column_int(statement, 3);
  423. // int can_cancel_order = sqlite3_column_int(statement, 4);
  424. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  425. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  426. // int can_delete_order = sqlite3_column_int(statement, 7);
  427. // int can_submit_order = sqlite3_column_int(statement, 8);
  428. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  429. // int can_create_order = sqlite3_column_int(statement, 10);
  430. // char *mode = (char*)sqlite3_column_text(statement, 11);
  431. // if(mode==nil)
  432. // mode="";
  433. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  434. // char *username = (char*)sqlite3_column_text(statement, 12);
  435. // if(username==nil)
  436. // username="";
  437. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  438. // int can_update_contact_info = sqlite3_column_int(statement, 13);
  439. char *first_name = (char*)sqlite3_column_text(statement, 14);
  440. if(first_name==nil)
  441. first_name="";
  442. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  443. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  444. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  445. // [header setValue:nscontact_id forKey:@"contact_id"];
  446. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  447. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  448. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  449. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  450. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  451. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  452. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  453. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  454. //
  455. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  456. //
  457. // [header setValue:nsusername forKey:@"username"];
  458. //
  459. // NSError* error=nil;
  460. //
  461. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  462. // [header setValue:statusFilter forKey:@"statusFilter"];
  463. //
  464. //
  465. //
  466. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  467. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  468. //
  469. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  470. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  471. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  472. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  473. //
  474. // [ret setObject:header forKey:@"header"];
  475. [ret setValue:nsfirst_name forKey:@"first_name"];
  476. }
  477. sqlite3_finalize(statement);
  478. }
  479. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  480. return ret;
  481. }
  482. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code user:(NSString*) user type:(int) type
  483. {
  484. assert(user!=nil);
  485. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  486. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  487. data[@"npd_url"]=COMPANY_WEB;
  488. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  489. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  490. NSString* date = [formatter stringFromDate:[NSDate date]];
  491. data[@"print_date"]=date;
  492. data[@"company_name"]=COMPANY_FULL_NAME;
  493. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  494. data[@"TOTAL_PAGE"]=0;
  495. data[@"CURRENT_PAGE"]=0;
  496. // [formatter setDateFormat:@"MM/dd/yyyy"];
  497. // date = [formatter stringFromDate:[NSDate date]];
  498. // data[@"creat_date"]=date;
  499. NSString* temp = NSTemporaryDirectory();
  500. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  501. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  502. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  503. // bool bwrite=
  504. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  505. data[@"img_barcode"]=qrpath;
  506. data[@"so_id"]=order_code;
  507. data[@"company"]=COMPANY_FULL_NAME;
  508. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  509. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  510. sqlite3 *db = [iSalesDB get_db];
  511. data[@"print_by"]= [self query_login:user db:db][@"first_name"];
  512. sqlite3_stmt * statement;
  513. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  514. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  515. row0[@"count"]=[NSNumber numberWithInt:1];
  516. datasource[@"row0"]=row0;
  517. NSString* print_status=@"";
  518. NSString* order_type=@"Offline Quote";
  519. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  520. {
  521. if (sqlite3_step(statement) == SQLITE_ROW)
  522. {
  523. int erpOrderStatus = sqlite3_column_int(statement, 0);
  524. int status = sqlite3_column_int(statement, 26);
  525. if(status==2)
  526. {
  527. switch (erpOrderStatus) {
  528. case 10:
  529. // order_type=@"Quoted Order";
  530. print_status=@"Quote";
  531. break;
  532. case 11:
  533. print_status=@"Booked";
  534. break;
  535. case 12:
  536. print_status=@"Picked";
  537. break;
  538. case 13:
  539. print_status=@"Shipped";
  540. break;
  541. case 14:
  542. print_status=@"Closed";
  543. break;
  544. case 15:
  545. print_status=@"Canceled";
  546. break;
  547. case 16:
  548. print_status=@"All";
  549. break;
  550. case 18:
  551. print_status=@"Processing";
  552. break;
  553. default:
  554. break;
  555. }
  556. }
  557. else{
  558. switch (status) {
  559. case 0:
  560. {
  561. print_status=@"Quote";
  562. break;
  563. }
  564. case 1:
  565. {
  566. print_status=@"Saved";
  567. break;
  568. }
  569. case 3:
  570. {
  571. //canceled
  572. print_status=@"Canceled";
  573. break;
  574. }
  575. default:
  576. break;
  577. }
  578. }
  579. NSString* sender_name = [self textAtColumn:1 statement:statement];
  580. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  581. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  582. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  583. NSString* sender_email = [self textAtColumn:5 statement:statement];
  584. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  585. NSString* billing_name = [self textAtColumn:7 statement:statement];
  586. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  587. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  588. if(billing_phone.length>0)
  589. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  590. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  591. if(billing_fax.length>0)
  592. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  593. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  594. [billto_arr0 addObject:billing_phone];
  595. [billto_arr0 addObject:billing_fax];
  596. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  597. NSString* billing_email = [self textAtColumn:11 statement:statement];
  598. if(billing_email.length>0)
  599. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  600. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  601. if(billing_contact.length>0)
  602. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  603. NSString* receive_name = [self textAtColumn:12 statement:statement];
  604. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  605. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  606. if(receive_phone.length>0)
  607. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  608. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  609. if(receive_fax.length>0)
  610. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  611. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  612. [receive_arr0 addObject:receive_phone];
  613. [receive_arr0 addObject:receive_fax];
  614. NSString* receive_email = [self textAtColumn:16 statement:statement];
  615. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  616. if(receive_email.length>0)
  617. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  618. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  619. if(receive_contact.length>0)
  620. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  621. NSString* poNumber = [self textAtColumn:17 statement:statement];
  622. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  623. NSString* carrier = [self textAtColumn:19 statement:statement];
  624. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  625. NSString* terms = [self textAtColumn:21 statement:statement];
  626. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  627. NSString* port_destination = [self textAtColumn:23 statement:statement];
  628. NSString* etd = [self textAtColumn:24 statement:statement];
  629. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  630. if(sign_picpath.length==0)
  631. sign_picpath=nil;
  632. NSString* create_date = [self textAtColumn:27 statement:statement];
  633. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  634. NSString* general_notes=[self textAtColumn:33 statement:statement];
  635. NSString *customerID = [self textAtColumn:34 statement:statement];
  636. if (customerID.length == 0) {
  637. customerID = nil;
  638. }
  639. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  640. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  641. if(general_notes.length>0)
  642. {
  643. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  644. row2[@"item_0"]=@{@"g_notes":general_notes };
  645. row2[@"count"]=[NSNumber numberWithInt:1];
  646. datasource[@"row2"]=row2;
  647. }
  648. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  649. [billto_arr addObject:billing_ext];
  650. [billto_arr addObject:billing_pf];
  651. [billto_arr addObject:billing_contact];
  652. [billto_arr addObject:billing_email];
  653. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  654. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  655. [receive_arr addObject:receive_ext];
  656. [receive_arr addObject:receive_pf];
  657. [receive_arr addObject:receive_contact];
  658. [receive_arr addObject:receive_email];
  659. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  660. float shipping = sqlite3_column_double(statement, 28);
  661. float lift_gate_value = sqlite3_column_double(statement, 29);
  662. float handling_fee_value = sqlite3_column_double(statement, 30);
  663. data[@"company"]=sender_name;
  664. data[@"shipfrom_addr"]=sender_ext;
  665. data[@"shipfrom_phone"]=sender_phone;
  666. data[@"shipfrom_fax"]=sender_fax;
  667. data[@"shipfrom_email"]=sender_email;
  668. data[@"warehouse_name"]=warehouse_name;
  669. data[@"bill_to_name"]=billing_name;
  670. data[@"bill_to_ext"]=billto;
  671. data[@"ship_to_name"]=receive_name;
  672. data[@"ship_to_ext"]=receive;
  673. data[@"po_no"]=poNumber;
  674. data[@"rep"]=sales_rep;
  675. data[@"e_schdule"]=etd;
  676. data[@"sales_terms"]=sales_terms;
  677. data[@"port_destination"]=port_destination;
  678. data[@"terms"]=terms;
  679. data[@"carrier"]=carrier;
  680. data[@"vendor_no"]=vendor_no;
  681. data[@"customer_sign"]=sign_picpath;
  682. data[@"print_status"]=print_status;
  683. data[@"create_date"]=create_date;
  684. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  685. sqlite3_stmt * statement_cart;
  686. // NSDate *date1 = [NSDate date];
  687. int count=0;
  688. // int cart_count=0;
  689. float credits=0;
  690. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  691. if ( dbresult== SQLITE_OK)
  692. {
  693. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  694. int row1count=0;
  695. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  696. {
  697. // NSDate *row_date = [NSDate date];
  698. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  699. // int product_id = sqlite3_column_int(statement_cart, 0);
  700. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  701. int item_id = sqlite3_column_int(statement_cart, 7);
  702. double discount = sqlite3_column_double(statement_cart, 2);
  703. NSString* Price=nil;
  704. if(str_price==nil)
  705. {
  706. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  707. // NSDate *price_date = [NSDate date];
  708. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db];
  709. // DebugLog(@"price time interval");
  710. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  711. if(price==nil)
  712. Price=@"No Price.";
  713. else
  714. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  715. }
  716. else
  717. {
  718. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  719. }
  720. float dunitprice=0;
  721. int item_count = sqlite3_column_int(statement_cart, 3);
  722. NSString* Amount=@"";
  723. if(![Price isEqualToString:@"No Price."])
  724. {
  725. dunitprice=[Price floatValue];
  726. dunitprice=dunitprice* (1-discount/100.0);
  727. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  728. credits+=dunitprice*item_count;
  729. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  730. }
  731. else
  732. {
  733. Price=@"";
  734. }
  735. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  736. // NSString *nsline_note=nil;
  737. // if(line_note!=nil)
  738. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  739. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  740. NSString *nsname=nil;
  741. if(name!=nil)
  742. nsname= [[NSString alloc]initWithUTF8String:name];
  743. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  744. NSString *nsdescription=nil;
  745. if(description!=nil)
  746. nsdescription= [[NSString alloc]initWithUTF8String:description];
  747. // int stockUom = sqlite3_column_int(statement_cart, 8);
  748. // int _id = sqlite3_column_int(statement_cart, 9);
  749. // NSDate *subtotal_date = [NSDate date];
  750. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  751. // DebugLog(@"subtotal_date time interval");
  752. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  753. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  754. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  755. // int carton=[bsubtotaljson[@"carton"] intValue];
  756. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  757. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  758. // NSDate *img_date = [NSDate date];
  759. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  760. // DebugLog(@"img_date time interval");
  761. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  762. itemjson[@"model"]=nsname;
  763. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  764. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  765. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  766. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  767. // itemjson[@"check"]=@"true";
  768. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  769. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  770. itemjson[@"unit_price"]=Price;
  771. itemjson[@"amount"]=Amount;
  772. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  773. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  774. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  775. // itemjson[@"note"]=nsline_note;
  776. // NSDate *date2 = [NSDate date];
  777. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  778. // DebugLog(@"model_bundle time interval");
  779. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  780. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  781. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  782. row1count++;
  783. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID user:user];
  784. for(int c=0;c<[combine[@"count"] intValue];c++)
  785. {
  786. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  787. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  788. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  789. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  790. credits+=combine_amount;
  791. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  792. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  793. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  794. row1count++;
  795. }
  796. count++;
  797. // DebugLog(@"row time interval");
  798. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  799. }
  800. row1[@"count"]=[NSNumber numberWithInt:row1count];
  801. datasource[@"row1"]=row1;
  802. data[@"table_model"]=datasource;
  803. sqlite3_finalize(statement_cart);
  804. }
  805. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  806. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  807. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  808. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  809. // if(credits>0)
  810. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  811. // else
  812. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  813. if(shipping>0)
  814. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  815. else
  816. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  817. if(lift_gate_value>0)
  818. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  819. else
  820. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  821. if(handling_fee_value>0)
  822. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  823. else
  824. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  825. float total = credits+shipping+lift_gate_value+handling_fee_value;
  826. if(total>0)
  827. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  828. else
  829. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  830. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  831. fee[@"row0"]=fee_row0;
  832. data[@"table_fee"]=fee;
  833. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  834. // ret[@"sign_url"] = sign_url;
  835. //
  836. // customer_contact = [self textAtColumn:52 statement:statement];
  837. // customer_email = [self textAtColumn:53 statement:statement];
  838. // customer_phone = [self textAtColumn:54 statement:statement];
  839. // customer_fax = [self textAtColumn:55 statement:statement];
  840. //
  841. //
  842. // int offline_edit=sqlite3_column_int(statement, 56);
  843. //
  844. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  845. //
  846. // char *soid = (char*)sqlite3_column_text(statement, 1);
  847. // if(soid==nil)
  848. // soid= "";
  849. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  850. // // so#
  851. // ret[@"so#"] = nssoid;
  852. //
  853. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  854. // if(poNumber==nil)
  855. // poNumber= "";
  856. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  857. //
  858. //
  859. //
  860. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  861. // if(create_time==nil)
  862. // create_time= "";
  863. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  864. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  865. //
  866. // int status = sqlite3_column_int(statement, 4);
  867. // int erpStatus = sqlite3_column_int(statement, 49);
  868. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  869. //
  870. // // status
  871. // if (status > 1 && status != 3) {
  872. // status = erpStatus;
  873. // } else if (status == 3) {
  874. // status = 15;
  875. // }
  876. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  877. // ret[@"order_status"] = nsstatus;
  878. //
  879. //
  880. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  881. // if(company_name==nil)
  882. // company_name= "";
  883. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  884. // // company name
  885. // ret[@"company_name"] = nscompany_name;
  886. //
  887. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  888. // if(customer_contact==nil)
  889. // customer_contact= "";
  890. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  891. //
  892. //
  893. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  894. // if(addr_1==nil)
  895. // addr_1="";
  896. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  897. //
  898. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  899. // if(addr_2==nil)
  900. // addr_2="";
  901. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  902. //
  903. //
  904. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  905. // if(addr_3==nil)
  906. // addr_3="";
  907. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  908. //
  909. //
  910. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  911. // if(addr_4==nil)
  912. // addr_4="";
  913. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  914. //
  915. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  916. // [arr_addr addObject:nsaddr_1];
  917. // [arr_addr addObject:nsaddr_2];
  918. // [arr_addr addObject:nsaddr_3];
  919. // [arr_addr addObject:nsaddr_4];
  920. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  921. //
  922. //
  923. // char *logist = (char*)sqlite3_column_text(statement, 11);
  924. // if(logist==nil)
  925. // logist= "";
  926. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  927. // if (status == -11 || status == 10 || status == 11) {
  928. // nslogist = [self textAtColumn:59 statement:statement];
  929. // };
  930. //
  931. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  932. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  933. // shipping = @"Shipping To Be Quoted";
  934. // } else {
  935. // shippingFee = sqlite3_column_double(statement, 12);
  936. // }
  937. //
  938. // // Shipping
  939. // ret[@"Shipping"] = shipping;
  940. //
  941. // int have_lift_gate = sqlite3_column_int(statement, 17);
  942. // lift_gate = sqlite3_column_double(statement, 13);
  943. // // Liftgate Fee(No loading dock)
  944. // if (!have_lift_gate) {
  945. // lift_gate = 0;
  946. // }
  947. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  948. // if (sqlite3_column_int(statement, 57)) {
  949. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  950. // }
  951. //
  952. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  953. // if(general_notes==nil)
  954. // general_notes= "";
  955. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  956. //
  957. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  958. // if(internal_notes==nil)
  959. // internal_notes= "";
  960. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  961. //
  962. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  963. // if(payment_type==nil)
  964. // payment_type= "";
  965. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  966. //
  967. //
  968. //
  969. // // order info
  970. // orderinfo = [self textFileName:@"order_info.html"];
  971. //
  972. //
  973. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  974. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  975. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  977. //
  978. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  979. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  981. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  982. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  983. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  984. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  985. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  986. //
  987. // NSString *payment = nil;
  988. // if([nspayment_type isEqualToString:@"Credit Card"])
  989. // {
  990. // payment = [self textFileName:@"creditcardpayment.html"];
  991. //
  992. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  993. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  994. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  995. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  996. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  997. // NSString *card_type = [self textAtColumn:42 statement:statement];
  998. // if (card_type.length > 0) { // 显示星号
  999. // card_type = @"****";
  1000. // }
  1001. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  1002. // if (card_number.length > 0 && card_number.length > 4) {
  1003. // for (int i = 0; i < card_number.length - 4; i++) {
  1004. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1005. // }
  1006. // } else {
  1007. // card_number = @"";
  1008. // }
  1009. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1010. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1011. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1012. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1013. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1014. // card_expiration = @"****";
  1015. // }
  1016. //
  1017. //
  1018. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1019. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1020. //
  1021. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1022. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1023. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1024. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1025. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1026. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1027. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1028. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1029. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1030. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1031. //
  1032. //
  1033. // }
  1034. // else
  1035. // {
  1036. // payment=[self textFileName:@"normalpayment.html"];
  1037. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1038. // }
  1039. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1040. //
  1041. // ret[@"result"]= [NSNumber numberWithInt:2];
  1042. //
  1043. // // more info
  1044. // moreInfo = [self textFileName:@"more_info.html"];
  1045. //
  1046. //
  1047. // // ShipToCompany_or_&nbsp
  1048. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1049. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1050. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1051. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1052. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1054. //
  1055. //
  1056. // // ShipFromCompany_or_&nbsp
  1057. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1058. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1059. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1060. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1061. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1063. //
  1064. //
  1065. // // FreightBillToCompany_or_&nbsp
  1066. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1067. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1068. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1069. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1070. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1072. //
  1073. //
  1074. // // MerchandiseBillToCompany_or_&nbsp
  1075. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1076. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1077. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1078. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1079. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1080. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1081. //
  1082. //
  1083. // // ReturnToCompany_or_&nbsp
  1084. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1085. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1086. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1087. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1088. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1089. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1090. // //
  1091. //
  1092. // // DebugLog(@"more info : %@",moreInfo);
  1093. //
  1094. // // handling fee
  1095. // handlingFee = sqlite3_column_double(statement, 33);
  1096. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1097. // if (sqlite3_column_int(statement, 58)) {
  1098. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1099. // }
  1100. // //
  1101. //
  1102. // // customer info
  1103. // customerID = [self textAtColumn:36 statement:statement];
  1104. //
  1105. // // mode
  1106. // ret[@"mode"] = appDelegate.mode;
  1107. //
  1108. // // model_count
  1109. // ret[@"model_count"] = @(0);
  1110. }
  1111. sqlite3_finalize(statement);
  1112. }
  1113. [iSalesDB close_db:db];
  1114. data[@"order_type"]=order_type;
  1115. /*
  1116. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1117. // "customer_email" = "Shui Hu";
  1118. // "customer_fax" = "";
  1119. // "customer_first_name" = F;
  1120. // "customer_last_name" = L;
  1121. // "customer_name" = ",da He Xiang Dong Liu A";
  1122. // "customer_phone" = "Hey Xuan Feng";
  1123. contactInfo[@"customer_phone"] = customer_phone;
  1124. contactInfo[@"customer_fax"] = customer_fax;
  1125. contactInfo[@"customer_email"] = customer_email;
  1126. NSString *first_name = @"";
  1127. NSString *last_name = @"";
  1128. if ([customer_contact isEqualToString:@""]) {
  1129. } else if ([customer_contact containsString:@" "]) {
  1130. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1131. first_name = [customer_contact substringToIndex:first_space_index];
  1132. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1133. }
  1134. contactInfo[@"customer_first_name"] = first_name;
  1135. contactInfo[@"customer_last_name"] = last_name;
  1136. ret[@"customerInfo"] = contactInfo;
  1137. // models
  1138. if (nssoid) {
  1139. __block double TotalCuft = 0;
  1140. __block double TotalWeight = 0;
  1141. __block int TotalCarton = 0;
  1142. __block double allItemPrice = 0;
  1143. 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];
  1144. sqlite3 *db1 = [iSalesDB get_db];
  1145. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1146. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1147. int product_id = sqlite3_column_int(stmt, 0);
  1148. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1149. int item_id = sqlite3_column_int(stmt, 7);
  1150. NSString* Price=nil;
  1151. if(str_price==nil)
  1152. {
  1153. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1154. if(price==nil)
  1155. Price=@"No Price.";
  1156. else
  1157. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1158. }
  1159. else
  1160. {
  1161. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1162. }
  1163. double discount = sqlite3_column_double(stmt, 2);
  1164. int item_count = sqlite3_column_int(stmt, 3);
  1165. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1166. NSString *nsline_note=nil;
  1167. if(line_note!=nil)
  1168. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1169. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1170. // NSString *nsname = nil;
  1171. // if(name!=nil)
  1172. // nsname= [[NSString alloc]initWithUTF8String:name];
  1173. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1174. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1175. // NSString *nsdescription=nil;
  1176. // if(description!=nil)
  1177. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1178. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1179. // int stockUom = sqlite3_column_int(stmt, 8);
  1180. // int _id = sqlite3_column_int(stmt, 9);
  1181. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1182. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1183. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1184. int carton=[bsubtotaljson[@"carton"] intValue];
  1185. TotalCuft += cuft;
  1186. TotalWeight += weight;
  1187. TotalCarton += carton;
  1188. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1189. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1190. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1191. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1192. itemjson[@"note"]=nsline_note;
  1193. itemjson[@"origin_price"] = Price;
  1194. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1195. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1196. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1197. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1198. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1199. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1200. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1201. if(itemjson[@"combine"] != nil)
  1202. {
  1203. // int citem=0;
  1204. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1205. for(int bc=0;bc<bcount;bc++)
  1206. {
  1207. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1208. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1209. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1210. subTotal += uprice * modulus * item_count;
  1211. }
  1212. }
  1213. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1214. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1215. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1216. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1217. allItemPrice += subTotal;
  1218. }];
  1219. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1220. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1221. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1222. // payments/Credits
  1223. // payments_and_credist = sqlite3_column_double(statement, 34);
  1224. payments_and_credist = allItemPrice;
  1225. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1226. // // total
  1227. // totalPrice = sqlite3_column_double(statement, 35);
  1228. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1229. } else {
  1230. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1231. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1232. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1233. // payments/Credits
  1234. payments_and_credist = 0;
  1235. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1236. }
  1237. // total
  1238. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1239. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1240. ret[@"order_info"]= orderinfo;
  1241. ret[@"more_order_info"] = moreInfo;
  1242. return [RAConvertor dict2data:ret];
  1243. */
  1244. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1245. return data;
  1246. }
  1247. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1248. {
  1249. assert(params[@"user"]!=nil);
  1250. NSString* template_file=nil;
  1251. switch([params[@"thumb"] intValue])
  1252. {
  1253. case 0:
  1254. template_file= @"so.json";
  1255. break;
  1256. case 1:
  1257. template_file= @"so_thumb.json";
  1258. break;
  1259. default:
  1260. template_file= @"so.json";
  1261. }
  1262. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] user:params[@"user"] type:[params[@"thumb"] intValue]];
  1263. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1264. if(data[@"table_model"][@"row2"]==nil)
  1265. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1266. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1267. DebugLog(@"%@",file);
  1268. // return nil;
  1269. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1270. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1271. if (file) {
  1272. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1273. [dic setObject:file forKey:@"pdf_path"];
  1274. dic[@"isLocalFile"]=@"true";
  1275. return [RAConvertor dict2data:dic];
  1276. }
  1277. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1278. dic[@"isLocalFile"]=@"true";
  1279. return [RAConvertor dict2data:dic];
  1280. }
  1281. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1282. {
  1283. // assert(params[@"contact_id"]!=nil);
  1284. assert(params[@"user"]!=nil);
  1285. NSString* template_file=nil;
  1286. switch([params[@"pdf_style"] intValue])
  1287. {
  1288. case 0:
  1289. template_file= @"portfolio_2x3.json";
  1290. break;
  1291. case 1:
  1292. template_file= @"portfolio_3x2.json";
  1293. break;
  1294. default:
  1295. template_file= @"portfolio_3x2.json";
  1296. }
  1297. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1298. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1299. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1300. DebugLog(@"%@",file);
  1301. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1302. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1303. if (file) {
  1304. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1305. [dic setObject:file forKey:@"pdf_path"];
  1306. dic[@"isLocalFile"]=@"true";
  1307. return [RAConvertor dict2data:dic];
  1308. }
  1309. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1310. dic[@"isLocalFile"]=@"true";
  1311. return [RAConvertor dict2data:dic];
  1312. }
  1313. +(NSString*) get_offline_soid:(sqlite3*)db
  1314. {
  1315. NSString* soid=nil;
  1316. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1317. [formatter setDateFormat:@"yyMMdd"];
  1318. NSString* date = [formatter stringFromDate:[NSDate date]];
  1319. for(int i=1;i<999;i++)
  1320. {
  1321. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1322. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1323. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1324. if(count==0)
  1325. return soid;
  1326. }
  1327. return nil;
  1328. }
  1329. +(NSArray*) enumOfflineOrder
  1330. {
  1331. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1332. sqlite3 *db = [iSalesDB get_db];
  1333. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1334. sqlite3_stmt * statement;
  1335. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1336. if ( dbresult== SQLITE_OK)
  1337. {
  1338. while (sqlite3_step(statement) == SQLITE_ROW)
  1339. {
  1340. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1341. NSString* nsso_id=nil;
  1342. if(so_id!=nil)
  1343. {
  1344. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1345. }
  1346. [ret addObject:nsso_id];
  1347. }
  1348. sqlite3_finalize(statement);
  1349. }
  1350. [iSalesDB close_db:db];
  1351. return ret;
  1352. }
  1353. //+(void) uploadFile:(NSString*) file
  1354. //{
  1355. // NSData* data = [NSData dataWithContentsOfFile: file];
  1356. // UIApplication * app = [UIApplication sharedApplication];
  1357. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1358. //
  1359. //
  1360. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1361. //
  1362. //
  1363. //
  1364. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1365. //
  1366. // if(appDelegate.user!=nil)
  1367. // [params setValue:appDelegate.user forKey:@"user"];
  1368. // // if(appDelegate.contact_id!=nil)
  1369. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1370. // if(appDelegate.password!=nil)
  1371. // [params setValue:appDelegate.password forKey:@"password"];
  1372. //
  1373. //
  1374. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1375. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1376. // } error:nil];
  1377. //
  1378. //
  1379. //
  1380. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1381. //
  1382. // NSProgress *progress = nil;
  1383. //
  1384. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1385. //
  1386. //
  1387. //
  1388. // if (error) {
  1389. //
  1390. // NSString* err_msg = [error localizedDescription];
  1391. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1392. //
  1393. //
  1394. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1395. //
  1396. // DebugLog(@"data string: %@",str);
  1397. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1398. //
  1399. // } else {
  1400. // DebugLog(@"response ");
  1401. //
  1402. //
  1403. //
  1404. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1405. //
  1406. // // 再将NSData转为字符串
  1407. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1408. //
  1409. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1410. //
  1411. // DebugLog(@"data string: %@",jsonStr);
  1412. //
  1413. // NSDictionary* json = responseObject;
  1414. //
  1415. //
  1416. // if([[json valueForKey:@"result"] intValue]==2)
  1417. // {
  1418. //// NSString* img_url_down = json[@"img_url_aname"];
  1419. //// NSString* img_url_up = json[@"img_url"];
  1420. //
  1421. //
  1422. // }
  1423. // else
  1424. // {
  1425. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1426. // }
  1427. //
  1428. //
  1429. // }
  1430. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1431. // }];
  1432. //
  1433. //
  1434. //
  1435. //
  1436. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1437. // //
  1438. //
  1439. //
  1440. //
  1441. //
  1442. //
  1443. //
  1444. // [uploadTask resume];
  1445. //
  1446. //}
  1447. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  1448. {
  1449. assert(add_params[@"user"]!=nil);
  1450. // assert(add_params[@"contact_id"]!=nil);
  1451. // assert(add_params[@"password"]!=nil);
  1452. NSString* serial= [[NSUUID UUID] UUIDString];
  1453. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1454. NSString *cachefolder = [paths objectAtIndex:0];
  1455. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1456. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1457. NSFileManager* fileManager = [NSFileManager defaultManager];
  1458. BOOL bdir=YES;
  1459. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1460. {
  1461. NSError *error = nil;
  1462. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1463. if(!bsuccess)
  1464. DebugLog(@"Create UPLOAD folder failed");
  1465. }
  1466. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1467. ret[@"contact"]=[self prepareContact:serial];
  1468. ret[@"wishlist"]=[self prepareWishlist:serial];
  1469. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  1470. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  1471. ret[@"view_portfolio"] = [self preparePDF:serial];
  1472. NSString* str= [RAConvertor dict2string:ret];
  1473. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1474. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1475. NSError *error=nil;
  1476. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1477. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1478. ZipArchive* zip = [[ZipArchive alloc] init];
  1479. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1480. for(int i=0;i<arr_files.count;i++)
  1481. {
  1482. NSString* file=arr_files[i];
  1483. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1484. }
  1485. if( ![zip CloseZipFile2] )
  1486. {
  1487. zippath = @"";
  1488. }
  1489. ret[@"file"]=zippath;
  1490. return ret;
  1491. }
  1492. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1493. {
  1494. if(filename.length==0)
  1495. return false;
  1496. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1497. bool ret=false;
  1498. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1499. NSString *cachefolder = [paths objectAtIndex:0];
  1500. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1501. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1502. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1503. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1504. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1505. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1506. NSFileManager* fileManager = [NSFileManager defaultManager];
  1507. BOOL bdir=NO;
  1508. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1509. {
  1510. NSError *error = nil;
  1511. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1512. {
  1513. ret=false;
  1514. }
  1515. else
  1516. {
  1517. ret=true;
  1518. }
  1519. }
  1520. return ret;
  1521. }
  1522. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1523. {
  1524. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1525. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1526. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1527. NSString* where=@" 1=1";
  1528. if (ver!=nil) {
  1529. where=@"is_dirty=1";
  1530. }
  1531. sqlite3 *db = [iSalesDB get_db];
  1532. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1533. sqlite3_stmt * statement;
  1534. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1535. int count=0;
  1536. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1537. if ( dbresult== SQLITE_OK)
  1538. {
  1539. while (sqlite3_step(statement) == SQLITE_ROW)
  1540. {
  1541. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1542. int _id = sqlite3_column_int(statement, 0);
  1543. int product_id = sqlite3_column_int(statement, 1);
  1544. int item_id = sqlite3_column_int(statement, 2);
  1545. int qty = sqlite3_column_int(statement, 3);
  1546. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1547. NSString* nscreate_time=nil;
  1548. if(create_time!=nil)
  1549. {
  1550. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1551. }
  1552. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1553. NSString* nsmodify_time=nil;
  1554. if(modify_time!=nil)
  1555. {
  1556. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1557. }
  1558. int is_delete = sqlite3_column_int(statement, 6);
  1559. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1560. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1561. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1562. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1563. itemjson[@"create_time"]=nscreate_time;
  1564. itemjson[@"modify_time"]=nsmodify_time;
  1565. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1566. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1567. count++;
  1568. }
  1569. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1570. sqlite3_finalize(statement);
  1571. }
  1572. ret[@"count"]=[NSNumber numberWithInt:count ];
  1573. [iSalesDB close_db:db];
  1574. return ret;
  1575. }
  1576. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  1577. {
  1578. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1579. // UIApplication * app = [UIApplication sharedApplication];
  1580. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1581. assert(add_params[@"user"]!=nil);
  1582. // assert(add_params[@"password"]!=nil);
  1583. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1584. // 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 ];
  1585. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1586. DebugLog(@"offline_login sql:%@",sqlQuery);
  1587. sqlite3_stmt * statement;
  1588. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1589. int count=0;
  1590. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1591. if ( dbresult== SQLITE_OK)
  1592. {
  1593. while (sqlite3_step(statement) == SQLITE_ROW)
  1594. {
  1595. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1596. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1597. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1598. int product_id = sqlite3_column_int(statement, 0);
  1599. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1600. int item_id = sqlite3_column_int(statement, 7);
  1601. NSString* Price=nil;
  1602. if(str_price==nil)
  1603. {
  1604. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1605. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  1606. if(price==nil)
  1607. Price=@"No Price.";
  1608. else
  1609. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1610. }
  1611. else
  1612. {
  1613. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1614. }
  1615. double discount = sqlite3_column_double(statement, 2);
  1616. int item_count = sqlite3_column_int(statement, 3);
  1617. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1618. NSString *nsline_note=nil;
  1619. if(line_note!=nil)
  1620. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1621. char *name = (char*)sqlite3_column_text(statement, 5);
  1622. NSString *nsname=nil;
  1623. if(name!=nil)
  1624. nsname= [[NSString alloc]initWithUTF8String:name];
  1625. char *description = (char*)sqlite3_column_text(statement, 6);
  1626. NSString *nsdescription=nil;
  1627. if(description!=nil)
  1628. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1629. // int stockUom = sqlite3_column_int(statement, 8);
  1630. // int _id = sqlite3_column_int(statement, 9);
  1631. //
  1632. //
  1633. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1634. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1635. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1636. // int carton=[bsubtotaljson[@"carton"] intValue];
  1637. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1638. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1639. itemjson[@"model"]=nsname;
  1640. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1641. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1642. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1643. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1644. // itemjson[@"check"]=@"true";
  1645. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1646. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1647. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1648. itemjson[@"unit_price"]=Price;
  1649. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1650. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1651. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1652. itemjson[@"note"]=nsline_note;
  1653. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  1654. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1655. count++;
  1656. }
  1657. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1658. sqlite3_finalize(statement);
  1659. }
  1660. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1661. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1662. return ret;
  1663. }
  1664. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  1665. {
  1666. assert(add_params[@"user"]!=nil);
  1667. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1668. sqlite3 *db = [iSalesDB get_db];
  1669. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1670. // for(int i=0;i<arr_soid.count;i++)
  1671. sqlite3_stmt * statement;
  1672. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1673. //int count=0;
  1674. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1675. if ( dbresult== SQLITE_OK)
  1676. {
  1677. int count=0;
  1678. while (sqlite3_step(statement) == SQLITE_ROW)
  1679. {
  1680. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1681. int _id = sqlite3_column_int(statement, 0);
  1682. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1683. NSString* nssync_data=nil;
  1684. if(sync_data!=nil)
  1685. {
  1686. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1687. }
  1688. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1689. NSString* nsimg_1=nil;
  1690. if(img_1!=nil)
  1691. {
  1692. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1693. [self copy_upImg:serial file:nsimg_1];
  1694. }
  1695. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1696. NSString* nsso_no=nil;
  1697. if(so_no!=nil)
  1698. {
  1699. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1700. }
  1701. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1702. itemjson[@"order_type"]=@"submit order";
  1703. else
  1704. itemjson[@"order_type"]=@"archive order";
  1705. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1706. itemjson[@"json_data"]= nssync_data;
  1707. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  1708. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1709. count++;
  1710. }
  1711. ret[@"count"]=[NSNumber numberWithInt:count ];
  1712. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1713. }
  1714. sqlite3_finalize(statement);
  1715. [iSalesDB close_db:db];
  1716. return ret;
  1717. }
  1718. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1719. {
  1720. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1721. sqlite3 *db = [iSalesDB get_db];
  1722. // UIApplication * app = [UIApplication sharedApplication];
  1723. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1724. 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";
  1725. sqlite3_stmt * statement;
  1726. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1727. int count=0;
  1728. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1729. if ( dbresult== SQLITE_OK)
  1730. {
  1731. while (sqlite3_step(statement) == SQLITE_ROW)
  1732. {
  1733. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1734. int _id = sqlite3_column_int(statement, 0);
  1735. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1736. NSString* nsimg_0=nil;
  1737. if(img_0!=nil)
  1738. {
  1739. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1740. [self copy_upImg:serial file:nsimg_0];
  1741. }
  1742. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1743. NSString* nsimg_1=nil;
  1744. if(img_1!=nil)
  1745. {
  1746. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1747. [self copy_upImg:serial file:nsimg_1];
  1748. }
  1749. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1750. NSString* nsimg_2=nil;
  1751. if(img_2!=nil)
  1752. {
  1753. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1754. [self copy_upImg:serial file:nsimg_2];
  1755. }
  1756. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1757. NSString* nssync_data=nil;
  1758. if(sync_data!=nil)
  1759. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1760. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1761. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1762. itemjson[@"json_data"]= nssync_data;
  1763. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1764. count++;
  1765. }
  1766. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1767. sqlite3_finalize(statement);
  1768. }
  1769. ret[@"count"]=[NSNumber numberWithInt:count ];
  1770. [iSalesDB close_db:db];
  1771. return ret;
  1772. }
  1773. +(bool) check_offlinedata
  1774. {
  1775. // UIApplication * app = [UIApplication sharedApplication];
  1776. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1777. sqlite3 *db = [iSalesDB get_db];
  1778. NSString * where=@"1=1";
  1779. // if(appDelegate.user!=nil)
  1780. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1781. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1782. [iSalesDB close_db:db];
  1783. if(count==0)
  1784. {
  1785. return false;
  1786. }
  1787. return true;
  1788. //
  1789. //[iSalesDB close_db:db];
  1790. }
  1791. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1792. {
  1793. // UIApplication * app = [UIApplication sharedApplication];
  1794. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1795. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1796. sqlite3 *db = [iSalesDB get_db];
  1797. NSString* collectId=params[@"collectId"];
  1798. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1799. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1800. [iSalesDB execSql:sqlQuery db:db];
  1801. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1802. [iSalesDB close_db:db];
  1803. // appDelegate.wish_count =count;
  1804. //
  1805. // [appDelegate update_count_mark];
  1806. ret[@"result"]= [NSNumber numberWithInt:2];
  1807. ret[@"wish_count"]=@(count);
  1808. return ret;
  1809. }
  1810. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1811. //{
  1812. //
  1813. // UIApplication * app = [UIApplication sharedApplication];
  1814. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1815. //
  1816. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1817. // sqlite3 *db = [iSalesDB get_db];
  1818. // NSString* product_id=params[@"product_id"];
  1819. //
  1820. //
  1821. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1822. //
  1823. //
  1824. // for(int i=0;i<arr.count;i++)
  1825. // {
  1826. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1827. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1828. // if(count==0)
  1829. // {
  1830. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1831. // [iSalesDB execSql:sqlQuery db:db];
  1832. // }
  1833. // }
  1834. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1835. // [iSalesDB close_db:db];
  1836. //
  1837. // appDelegate.wish_count =count;
  1838. //
  1839. // [appDelegate update_count_mark];
  1840. // ret[@"result"]= [NSNumber numberWithInt:2];
  1841. // return ret;
  1842. // //
  1843. // //return ret;
  1844. //}
  1845. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1846. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1847. DebugLog(@"time interval: %lf",interval);
  1848. }
  1849. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1850. //{
  1851. // UIApplication * app = [UIApplication sharedApplication];
  1852. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1853. //
  1854. // int sort = [[params objectForKey:@"sort"] intValue];
  1855. // NSString *sort_str = @"";
  1856. // switch (sort) {
  1857. // case 0:{
  1858. // sort_str = @"order by w.modify_time desc";
  1859. // }
  1860. // break;
  1861. // case 1:{
  1862. // sort_str = @"order by w.modify_time asc";
  1863. // }
  1864. // break;
  1865. // case 2:{
  1866. // sort_str = @"order by m.name asc";
  1867. // }
  1868. // break;
  1869. // case 3:{
  1870. // sort_str = @"order by m.name desc";
  1871. // }
  1872. // break;
  1873. // case 4:{
  1874. // sort_str = @"order by m.description asc";
  1875. // }
  1876. // break;
  1877. //
  1878. // default:
  1879. // break;
  1880. //
  1881. // }
  1882. //
  1883. //
  1884. //// NSString* user = appDelegate.user;
  1885. //
  1886. // sqlite3 *db = [iSalesDB get_db];
  1887. //
  1888. // // order by w.create_time
  1889. // 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];
  1890. //
  1891. //// 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];
  1892. //
  1893. //
  1894. // sqlite3_stmt * statement;
  1895. //
  1896. // NSDate *date1 = [NSDate date];
  1897. //// NSDate *date2 = nil;
  1898. //
  1899. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1900. // int count=0;
  1901. //
  1902. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1903. // {
  1904. //
  1905. //
  1906. // while (sqlite3_step(statement) == SQLITE_ROW)
  1907. // {
  1908. //
  1909. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1910. //
  1911. // int product_id = sqlite3_column_double(statement, 0);
  1912. //
  1913. //
  1914. //
  1915. //
  1916. //
  1917. // char *description = (char*)sqlite3_column_text(statement, 1);
  1918. // if(description==nil)
  1919. // description= "";
  1920. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1921. //
  1922. // int item_id = sqlite3_column_double(statement, 2);
  1923. //
  1924. // NSDate *date_image = [NSDate date];
  1925. //
  1926. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1927. //
  1928. // printf("image : ");
  1929. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1930. //
  1931. // // char *url = (char*)sqlite3_column_text(statement, 3);
  1932. // // if(url==nil)
  1933. // // url="";
  1934. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1935. //
  1936. // int qty = sqlite3_column_int(statement, 3);
  1937. //
  1938. //
  1939. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1940. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1941. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1942. // item[@"description"]= nsdescription;
  1943. // item[@"img"]= nsurl;
  1944. //
  1945. //
  1946. //
  1947. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1948. // count++;
  1949. //
  1950. // }
  1951. // printf("total time:");
  1952. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1953. //
  1954. // ret[@"count"]= [NSNumber numberWithInt:count];
  1955. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  1956. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1957. // ret[@"result"]= [NSNumber numberWithInt:2];
  1958. //
  1959. //
  1960. // appDelegate.wish_count =count;
  1961. //
  1962. // [appDelegate update_count_mark];
  1963. // sqlite3_finalize(statement);
  1964. //
  1965. //
  1966. //
  1967. //
  1968. // }
  1969. //
  1970. // [iSalesDB close_db:db];
  1971. //
  1972. // return ret;
  1973. //}
  1974. +(NSDictionary*) offline_notimpl
  1975. {
  1976. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1977. ret[@"result"]=@"8";
  1978. ret[@"err_msg"]=@"offline mode does not support this function.";
  1979. return ret;
  1980. }
  1981. +(NSDictionary*) offline_home
  1982. {
  1983. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1984. NSString *cachefolder = [paths objectAtIndex:0];
  1985. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1986. NSData* json =nil;
  1987. json=[NSData dataWithContentsOfFile:img_cache];
  1988. if(json==nil)
  1989. return nil;
  1990. NSError *error=nil;
  1991. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1992. return menu;
  1993. }
  1994. +(NSDictionary*) offline_category_menu
  1995. {
  1996. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1997. NSString *cachefolder = [paths objectAtIndex:0];
  1998. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1999. NSData* json =nil;
  2000. json=[NSData dataWithContentsOfFile:img_cache];
  2001. if(json==nil)
  2002. return nil;
  2003. NSError *error=nil;
  2004. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2005. return menu;
  2006. }
  2007. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2008. {
  2009. NSString* offline_command=params[@"offline_Command"];
  2010. NSDictionary* ret=nil;
  2011. if([offline_command isEqualToString:@"model_NIYMAL"])
  2012. {
  2013. NSString* category = params[@"category"];
  2014. ret = [self refresh_model_NIYMAL:category];
  2015. }
  2016. return ret;
  2017. }
  2018. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2019. {
  2020. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2021. [ret setValue:@"2" forKey:@"result"];
  2022. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2023. sqlite3* db= [iSalesDB get_db];
  2024. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2025. [iSalesDB close_db:db];
  2026. [ret setObject:detail1_section forKey:@"detail_1"];
  2027. return ret;
  2028. }
  2029. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2030. {
  2031. //assert(user!=nil);
  2032. // UIApplication * app = [UIApplication sharedApplication];
  2033. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2034. // NSArray* arr1 = [self get_user_all_price_type];
  2035. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2036. // NSSet *set1 = [NSSet setWithArray:arr1];
  2037. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2038. // if(appDelegate.contact_id==nil)
  2039. // set2=[set1 mutableCopy];
  2040. // else
  2041. // [set2 intersectsSet:set1];
  2042. // NSArray *retarr = [set2 allObjects];
  2043. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2044. // sqlite3 *db = [iSalesDB get_db];
  2045. NSString* sqlQuery = nil;
  2046. if(contact_id==nil)
  2047. {
  2048. if(!blogin)
  2049. return nil;
  2050. 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];
  2051. }
  2052. else
  2053. 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];
  2054. sqlite3_stmt * statement;
  2055. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2056. int count=0;
  2057. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2058. {
  2059. while (sqlite3_step(statement) == SQLITE_ROW)
  2060. {
  2061. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2062. char *name = (char*)sqlite3_column_text(statement, 0);
  2063. if(name==nil)
  2064. name="";
  2065. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2066. // double price = sqlite3_column_double(statement, 1);
  2067. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2068. // if(isnull==nil)
  2069. // item[nsname]= @"No Price";
  2070. // else
  2071. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2072. char *price = (char*)sqlite3_column_text(statement, 1);
  2073. if(price!=nil)
  2074. {
  2075. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2076. nsprice=[AESCrypt fastdecrypt:nsprice];
  2077. if(nsprice.length>0)
  2078. {
  2079. double dp= [nsprice doubleValue];
  2080. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2081. }
  2082. }
  2083. else
  2084. {
  2085. item[nsname]= @"No Price";
  2086. }
  2087. // int type= sqlite3_column_int(statement, 2);
  2088. //item[@"type"]=@"price";
  2089. // item[nsname]= nsprice;
  2090. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2091. count++;
  2092. }
  2093. ret[@"count"]= [NSNumber numberWithInt:count];
  2094. sqlite3_finalize(statement);
  2095. }
  2096. // [iSalesDB close_db:db];
  2097. return ret;
  2098. }
  2099. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2100. //{
  2101. // NSArray* arr1 = [self get_user_all_price_type:db];
  2102. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2103. //
  2104. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2105. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2106. // // [set2 intersectsSet:set1];
  2107. // //
  2108. // //
  2109. // // NSArray *retarr = [set2 allObjects];
  2110. //
  2111. // NSString* whereprice=nil;
  2112. // if(contact_id==nil)
  2113. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2114. // else
  2115. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2116. //
  2117. //
  2118. // // sqlite3 *db = [iSalesDB get_db];
  2119. //
  2120. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2121. // sqlite3_stmt * statement;
  2122. //
  2123. //
  2124. // NSNumber* ret = nil;
  2125. // double dprice=DBL_MAX;
  2126. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2127. // {
  2128. //
  2129. //
  2130. // while (sqlite3_step(statement) == SQLITE_ROW)
  2131. // {
  2132. //
  2133. // // double val = sqlite3_column_double(statement, 0);
  2134. // char *price = (char*)sqlite3_column_text(statement, 0);
  2135. // if(price!=nil)
  2136. // {
  2137. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2138. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2139. // if(nsprice.length>0)
  2140. // {
  2141. // double dp= [nsprice doubleValue];
  2142. // if(dp<dprice)
  2143. // dprice=dp;
  2144. // }
  2145. // }
  2146. // }
  2147. //
  2148. //
  2149. //
  2150. //
  2151. // sqlite3_finalize(statement);
  2152. //
  2153. //
  2154. //
  2155. //
  2156. // }
  2157. //
  2158. // // [iSalesDB close_db:db];
  2159. //
  2160. // if(dprice==DBL_MAX)
  2161. // ret= nil;
  2162. // else
  2163. // ret= [NSNumber numberWithDouble:dprice];
  2164. // return ret;
  2165. //}
  2166. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2167. {
  2168. assert(user!=nil);
  2169. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2170. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2171. NSString* whereprice=nil;
  2172. if(contact_id==nil)
  2173. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2174. else
  2175. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2176. // sqlite3 *db = [iSalesDB get_db];
  2177. NSString *productIdCondition = @"1 = 1";
  2178. if (product_id) {
  2179. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2180. }
  2181. NSString *itemIdCondition = @"";
  2182. if (item_id) {
  2183. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2184. }
  2185. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2186. sqlite3_stmt * statement;
  2187. NSNumber* ret = nil;
  2188. double dprice=DBL_MAX;
  2189. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2190. {
  2191. while (sqlite3_step(statement) == SQLITE_ROW)
  2192. {
  2193. // double val = sqlite3_column_double(statement, 0);
  2194. char *price = (char*)sqlite3_column_text(statement, 0);
  2195. if(price!=nil)
  2196. {
  2197. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2198. nsprice=[AESCrypt fastdecrypt:nsprice];
  2199. if(nsprice.length>0)
  2200. {
  2201. double dp= [nsprice doubleValue];
  2202. if(dp<dprice)
  2203. dprice=dp;
  2204. }
  2205. }
  2206. }
  2207. sqlite3_finalize(statement);
  2208. }
  2209. // [iSalesDB close_db:db];
  2210. if(dprice==DBL_MAX)
  2211. ret= nil;
  2212. else
  2213. ret= [NSNumber numberWithDouble:dprice];
  2214. return ret;
  2215. }
  2216. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2217. {
  2218. assert(user!=nil);
  2219. NSArray* ret=nil;
  2220. // sqlite3 *db = [iSalesDB get_db];
  2221. // no customer assigned , use login user contact_id
  2222. // UIApplication * app = [UIApplication sharedApplication];
  2223. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2224. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2225. sqlite3_stmt * statement;
  2226. // int count=0;
  2227. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2228. {
  2229. if (sqlite3_step(statement) == SQLITE_ROW)
  2230. {
  2231. char *val = (char*)sqlite3_column_text(statement, 0);
  2232. if(val==nil)
  2233. val="";
  2234. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2235. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2236. }
  2237. sqlite3_finalize(statement);
  2238. }
  2239. // [iSalesDB close_db:db];
  2240. return ret;
  2241. }
  2242. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2243. {
  2244. // sqlite3 *db = [iSalesDB get_db];
  2245. if(contact_id==nil)
  2246. {
  2247. // no customer assigned , use login user contact_id
  2248. // UIApplication * app = [UIApplication sharedApplication];
  2249. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2250. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2251. sqlite3_stmt * statement;
  2252. // int count=0;
  2253. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2254. {
  2255. if (sqlite3_step(statement) == SQLITE_ROW)
  2256. {
  2257. char *val = (char*)sqlite3_column_text(statement, 0);
  2258. if(val==nil)
  2259. val="";
  2260. contact_id = [[NSString alloc]initWithUTF8String:val];
  2261. }
  2262. sqlite3_finalize(statement);
  2263. }
  2264. if(contact_id.length<=0)
  2265. {
  2266. // [iSalesDB close_db:db];
  2267. return nil;
  2268. }
  2269. }
  2270. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2271. sqlite3_stmt * statement;
  2272. NSArray* ret=nil;
  2273. // int count=0;
  2274. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2275. {
  2276. if (sqlite3_step(statement) == SQLITE_ROW)
  2277. {
  2278. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2279. if(price_type==nil)
  2280. price_type="";
  2281. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2282. if(nsprice_type.length>0)
  2283. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2284. else
  2285. ret=nil;
  2286. }
  2287. sqlite3_finalize(statement);
  2288. }
  2289. // [iSalesDB close_db:db];
  2290. return ret;
  2291. }
  2292. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2293. {
  2294. NSString* ret= nil;
  2295. // sqlite3 *db = [iSalesDB get_db];
  2296. NSString *sqlQuery = nil;
  2297. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2298. // 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;
  2299. // 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
  2300. if(product_id==nil && model_name)
  2301. 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;
  2302. else if (product_id)
  2303. 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
  2304. sqlite3_stmt * statement;
  2305. // int count=0;
  2306. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2307. {
  2308. if (sqlite3_step(statement) == SQLITE_ROW)
  2309. {
  2310. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2311. if(imgurl==nil)
  2312. imgurl="";
  2313. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2314. ret=nsimgurl;
  2315. }
  2316. sqlite3_finalize(statement);
  2317. }
  2318. else
  2319. {
  2320. [ret setValue:@"8" forKey:@"result"];
  2321. }
  2322. // [iSalesDB close_db:db];
  2323. DebugLog(@"data string: %@",ret );
  2324. return ret;
  2325. }
  2326. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2327. {
  2328. NSString* ret= nil;
  2329. sqlite3 *db = [iSalesDB get_db];
  2330. NSString *sqlQuery = nil;
  2331. if(product_id==nil)
  2332. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2333. else
  2334. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2335. sqlite3_stmt * statement;
  2336. // int count=0;
  2337. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2338. {
  2339. if (sqlite3_step(statement) == SQLITE_ROW)
  2340. {
  2341. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2342. if(default_category==nil)
  2343. default_category="";
  2344. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2345. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2346. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2347. ret=nsdefault_category;
  2348. }
  2349. sqlite3_finalize(statement);
  2350. }
  2351. else
  2352. {
  2353. [ret setValue:@"8" forKey:@"result"];
  2354. }
  2355. [iSalesDB close_db:db];
  2356. DebugLog(@"data string: %@",ret );
  2357. return ret;
  2358. }
  2359. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2360. NSString *sql = nil;
  2361. if (product_id != nil) {
  2362. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2363. } else {
  2364. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2365. }
  2366. NSString *result_set = [iSalesDB jk_queryText:sql];
  2367. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2368. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2369. return result_set;
  2370. }
  2371. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2372. //{
  2373. //
  2374. //
  2375. // assert(params[@"user"]!=nil);
  2376. //
  2377. //
  2378. //
  2379. // NSString* model_name = [params valueForKey:@"product_name"];
  2380. //
  2381. // NSString* product_id = [params valueForKey:@"product_id"];
  2382. //
  2383. // NSString* category = [params valueForKey:@"category"];
  2384. //
  2385. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2386. // if(category==nil) {
  2387. // category = default_category_id;
  2388. // } else {
  2389. //
  2390. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2391. // // 参数重有多个category id
  2392. // if (arr_0.count > 1) {
  2393. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2394. //
  2395. // // 取交集
  2396. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2397. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2398. // [set_0 intersectSet:set_1];
  2399. //
  2400. // if (set_0.count == 1) {
  2401. // category = (NSString *)[set_0 anyObject];
  2402. // } else {
  2403. // if ([set_0 containsObject:default_category_id]) {
  2404. // category = default_category_id;
  2405. // } else {
  2406. // category = (NSString *)[set_0 anyObject];
  2407. // }
  2408. // }
  2409. // }
  2410. // }
  2411. //
  2412. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2413. //
  2414. //
  2415. //
  2416. //
  2417. //
  2418. //
  2419. // sqlite3 *db = [iSalesDB get_db];
  2420. //
  2421. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2422. //
  2423. //
  2424. // NSString *sqlQuery = nil;
  2425. //
  2426. // if(product_id==nil)
  2427. // 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,seat_dimension,product_content_writing 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='%@';
  2428. // else
  2429. //
  2430. // 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,seat_dimension,product_content_writing 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=%@;
  2431. //
  2432. //
  2433. // sqlite3_stmt * statement;
  2434. // [ret setValue:@"2" forKey:@"result"];
  2435. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2436. //
  2437. // // int count=0;
  2438. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2439. // {
  2440. //
  2441. //
  2442. // if (sqlite3_step(statement) == SQLITE_ROW)
  2443. // {
  2444. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2445. //
  2446. //
  2447. // char *name = (char*)sqlite3_column_text(statement, 0);
  2448. // if(name==nil)
  2449. // name="";
  2450. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2451. //
  2452. // char *description = (char*)sqlite3_column_text(statement, 1);
  2453. // if(description==nil)
  2454. // description="";
  2455. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2456. //
  2457. //
  2458. // int product_id = sqlite3_column_int(statement, 2);
  2459. //
  2460. //
  2461. // char *color = (char*)sqlite3_column_text(statement, 3);
  2462. // if(color==nil)
  2463. // color="";
  2464. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2465. // //
  2466. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2467. // // if(legcolor==nil)
  2468. // // legcolor="";
  2469. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2470. // //
  2471. // //
  2472. // int availability = sqlite3_column_int(statement, 5);
  2473. // //
  2474. // int incoming_stock = sqlite3_column_int(statement, 6);
  2475. //
  2476. //
  2477. // char *demension = (char*)sqlite3_column_text(statement, 7);
  2478. // if(demension==nil)
  2479. // demension="";
  2480. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2481. //
  2482. //
  2483. //
  2484. // // ,,,,,,,,,
  2485. //
  2486. //
  2487. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2488. // if(seat_height==nil)
  2489. // seat_height="";
  2490. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2491. //
  2492. //
  2493. // char *material = (char*)sqlite3_column_text(statement, 9);
  2494. // if(material==nil)
  2495. // material="";
  2496. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2497. //
  2498. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2499. // if(box_dim==nil)
  2500. // box_dim="";
  2501. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2502. //
  2503. // char *volume = (char*)sqlite3_column_text(statement, 11);
  2504. // if(volume==nil)
  2505. // volume="";
  2506. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2507. //
  2508. // double weight = sqlite3_column_double(statement, 12);
  2509. //
  2510. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  2511. // if(model_set==nil)
  2512. // model_set="";
  2513. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2514. //
  2515. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2516. // if(load_ability==nil)
  2517. // load_ability="";
  2518. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2519. //
  2520. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  2521. //// if(default_category==nil)
  2522. //// default_category="";
  2523. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2524. //
  2525. //
  2526. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2527. // if(fabric_content==nil)
  2528. // fabric_content="";
  2529. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2530. //
  2531. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  2532. // if(assembling==nil)
  2533. // assembling="";
  2534. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2535. //
  2536. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  2537. // if(made_in==nil)
  2538. // made_in="";
  2539. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2540. //
  2541. //
  2542. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2543. // if(special_remarks==nil)
  2544. // special_remarks="";
  2545. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2546. //
  2547. //
  2548. // int stockUcom = sqlite3_column_double(statement, 20);
  2549. //
  2550. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  2551. // if(product_group==nil)
  2552. // product_group="";
  2553. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2554. //
  2555. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2556. // // if(fashion_selector==nil)
  2557. // // fashion_selector="";
  2558. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2559. //
  2560. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2561. // if(selector_field==nil)
  2562. // selector_field="";
  2563. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2564. //
  2565. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  2566. // if(property_field==nil)
  2567. // property_field="";
  2568. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2569. //
  2570. //
  2571. //
  2572. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  2573. // if(packaging==nil)
  2574. // packaging="";
  2575. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2576. //
  2577. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2578. //
  2579. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2580. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2581. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2582. //
  2583. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  2584. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2585. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2586. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2587. //
  2588. //
  2589. //
  2590. //
  2591. //
  2592. // NSString* Availability=nil;
  2593. // if(availability>0)
  2594. // Availability=[NSString stringWithFormat:@"%d",availability];
  2595. // else
  2596. // Availability = @"Out of Stock";
  2597. //
  2598. // [img_section setValue:Availability forKey:@"Availability"];
  2599. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2600. //
  2601. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2602. //
  2603. //
  2604. // char *eta = (char*)sqlite3_column_text(statement, 25);
  2605. // if(eta==nil)
  2606. // eta="";
  2607. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2608. // if ([nseta isEqualToString:@"null"]) {
  2609. // nseta = @"";
  2610. // }
  2611. // if (availability <= 0) {
  2612. // [img_section setValue:nseta forKey:@"ETA"];
  2613. // }
  2614. //
  2615. //
  2616. // int item_id = sqlite3_column_int(statement, 26);
  2617. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2618. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  2619. //
  2620. // NSString* Price=nil;
  2621. // if(appDelegate.bLogin==false)
  2622. // Price=@"Must Sign in.";
  2623. // else
  2624. // {
  2625. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2626. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2627. // if(price==nil)
  2628. // Price=@"No Price.";
  2629. // else
  2630. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2631. // }
  2632. //
  2633. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  2634. // [img_section setValue:Price forKey:@"price"];
  2635. // [img_section setValue:nsname forKey:@"model_name"];
  2636. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  2637. //
  2638. // if (appDelegate.order_code) { // 离线order code即so#
  2639. //
  2640. // NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2641. // __block int cartQTY = 0;
  2642. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2643. //
  2644. // cartQTY = sqlite3_column_int(stmt, 0);
  2645. //
  2646. // }];
  2647. //
  2648. // if (cartQTY > 0) {
  2649. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2650. // }
  2651. // }
  2652. //
  2653. // [ret setObject:img_section forKey:@"img_section"];
  2654. //
  2655. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2656. // int detail0_item_count=0;
  2657. //
  2658. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2659. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2660. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2661. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2662. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2663. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2664. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2665. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2666. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2667. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2668. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2669. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2670. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2671. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2672. //
  2673. //
  2674. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2675. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2676. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2677. // {
  2678. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2679. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2680. // }
  2681. //
  2682. //
  2683. //
  2684. //
  2685. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2686. // [detail0_section setValue:@"kv" forKey:@"type"];
  2687. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  2688. //
  2689. // [ret setObject:detail0_section forKey:@"detail_0"];
  2690. //
  2691. //
  2692. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2693. // // [detail1_section setValue:@"detail" forKey:@"target"];
  2694. // // [detail1_section setValue:@"popup" forKey:@"action"];
  2695. // // [detail1_section setValue:@"content" forKey:@"type"];
  2696. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2697. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2698. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  2699. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2700. // [ret setObject:detail1_section forKey:@"detail_1"];
  2701. //
  2702. //
  2703. //
  2704. //
  2705. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2706. // [detail2_section setValue:@"detail" forKey:@"target"];
  2707. // [detail2_section setValue:@"popup" forKey:@"action"];
  2708. // [detail2_section setValue:@"content" forKey:@"type"];
  2709. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2710. // [detail2_section setValue:@"true" forKey:@"single_row"];
  2711. // [detail2_section setValue:@"local" forKey:@"data"];
  2712. // [ret setObject:detail2_section forKey:@"detail_2"];
  2713. // }
  2714. //
  2715. //
  2716. //
  2717. //
  2718. // sqlite3_finalize(statement);
  2719. // }
  2720. // else
  2721. // {
  2722. // [ret setValue:@"8" forKey:@"result"];
  2723. // }
  2724. //// DebugLog(@"count:%d",count);
  2725. //
  2726. //
  2727. // [iSalesDB close_db:db];
  2728. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2729. //
  2730. // return ret;
  2731. //}
  2732. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2733. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2734. NSString* keyword = [params valueForKey:@"keyword"];
  2735. keyword=keyword.lowercaseString;
  2736. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2737. int limit = [[params valueForKey:@"limit"] intValue];
  2738. int offset = [[params valueForKey:@"offset"] intValue];
  2739. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2740. NSString *limit_str = @"";
  2741. if (limited) {
  2742. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2743. }
  2744. sqlite3 *db = [iSalesDB get_db];
  2745. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2746. // UIApplication * app = [UIApplication sharedApplication];
  2747. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2748. NSString *sqlQuery = nil;
  2749. if(exactMatch )
  2750. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 %@;",params[@"orderCode"], 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 ;
  2751. else
  2752. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",params[@"orderCode"], keyword,keyword,keyword,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
  2753. DebugLog(@"offline_search sql:%@",sqlQuery);
  2754. sqlite3_stmt * statement;
  2755. [ret setValue:@"2" forKey:@"result"];
  2756. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2757. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2758. // int count=0;
  2759. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2760. {
  2761. int i=0;
  2762. while (sqlite3_step(statement) == SQLITE_ROW)
  2763. {
  2764. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2765. // char *name = (char*)sqlite3_column_text(statement, 1);
  2766. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2767. char *name = (char*)sqlite3_column_text(statement, 0);
  2768. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2769. char *description = (char*)sqlite3_column_text(statement, 1);
  2770. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2771. int product_id = sqlite3_column_int(statement, 2);
  2772. // char *url = (char*)sqlite3_column_text(statement, 3);
  2773. // if(url==nil)
  2774. // url="";
  2775. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2776. int wid = sqlite3_column_int(statement, 3);
  2777. int closeout = sqlite3_column_int(statement, 4);
  2778. int cid = sqlite3_column_int(statement, 5);
  2779. int wisdelete = sqlite3_column_int(statement, 6);
  2780. int more_color = sqlite3_column_int(statement, 7);
  2781. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2782. if(wid !=0 && wisdelete != 1)
  2783. [item setValue:@"true" forKey:@"wish_exists"];
  2784. else
  2785. [item setValue:@"false" forKey:@"wish_exists"];
  2786. if(closeout==0)
  2787. [item setValue:@"false" forKey:@"is_closeout"];
  2788. else
  2789. [item setValue:@"true" forKey:@"is_closeout"];
  2790. if(cid==0)
  2791. [item setValue:@"false" forKey:@"cart_exists"];
  2792. else
  2793. [item setValue:@"true" forKey:@"cart_exists"];
  2794. if (more_color == 0) {
  2795. [item setObject:@(false) forKey:@"more_color"];
  2796. } else if (more_color == 1) {
  2797. [item setObject:@(true) forKey:@"more_color"];
  2798. }
  2799. [item addEntriesFromDictionary:imgjson];
  2800. // [item setValue:nsurl forKey:@"img"];
  2801. [item setValue:nsname forKey:@"fash_name"];
  2802. [item setValue:nsdescription forKey:@"description"];
  2803. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2804. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2805. i++;
  2806. }
  2807. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2808. [ret setObject:items forKey:@"items"];
  2809. sqlite3_finalize(statement);
  2810. }
  2811. DebugLog(@"count:%d",count);
  2812. [iSalesDB close_db:db];
  2813. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2814. return ret;
  2815. }
  2816. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2817. {
  2818. return [self search:params limited:YES];
  2819. }
  2820. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2821. //{
  2822. // UIApplication * app = [UIApplication sharedApplication];
  2823. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2824. //
  2825. //// [iSalesDB disable_trigger]
  2826. // [iSalesDB disable_trigger];
  2827. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2828. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2829. //
  2830. // [iSalesDB enable_trigger];
  2831. //// [iSalesDB enable_trigger]
  2832. // //
  2833. // // NSString* user = [params valueForKey:@"user"];
  2834. // //
  2835. // // NSString* password = [params valueForKey:@"password"];
  2836. //
  2837. //
  2838. //
  2839. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2840. //
  2841. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  2842. //
  2843. //
  2844. //
  2845. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2846. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2847. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2848. //
  2849. // [appDelegate update_count_mark];
  2850. //
  2851. //
  2852. // appDelegate.can_show_price =false;
  2853. // appDelegate.can_see_price =false;
  2854. // appDelegate.can_create_portfolio =false;
  2855. // appDelegate.can_create_order =false;
  2856. //
  2857. //
  2858. // appDelegate.can_cancel_order =false;
  2859. // appDelegate.can_set_cart_price =false;
  2860. // appDelegate.can_delete_order =false;
  2861. // appDelegate.can_submit_order =false;
  2862. // appDelegate.can_set_tearsheet_price =false;
  2863. // appDelegate.can_update_contact_info = false;
  2864. //
  2865. // appDelegate.save_order_logout = false;
  2866. // appDelegate.submit_order_logout = false;
  2867. // appDelegate.alert_sold_in_quantities = false;
  2868. //
  2869. // appDelegate.ipad_perm =nil ;
  2870. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  2871. // appDelegate.OrderFilter= nil;
  2872. // [appDelegate SetSo:nil];
  2873. // [appDelegate set_main_button_panel];
  2874. //
  2875. //
  2876. // // sqlite3 *db = [iSalesDB get_db];
  2877. // //
  2878. // //
  2879. // //
  2880. // //
  2881. // //
  2882. // // 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"]];
  2883. // //
  2884. // //
  2885. // //
  2886. // //
  2887. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  2888. // // sqlite3_stmt * statement;
  2889. // //
  2890. // //
  2891. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2892. // //
  2893. // //
  2894. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2895. // // {
  2896. // //
  2897. // //
  2898. // // if (sqlite3_step(statement) == SQLITE_ROW)
  2899. // // {
  2900. // //
  2901. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2902. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2903. // //
  2904. // //
  2905. // //
  2906. // // int can_show_price = sqlite3_column_int(statement, 0);
  2907. // // int can_see_price = sqlite3_column_int(statement, 1);
  2908. // //
  2909. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2910. // // if(contact_id==nil)
  2911. // // contact_id="";
  2912. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2913. // //
  2914. // // int user_type = sqlite3_column_int(statement, 3);
  2915. // //
  2916. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  2917. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2918. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2919. // // int can_delete_order = sqlite3_column_int(statement, 7);
  2920. // // int can_submit_order = sqlite3_column_int(statement, 8);
  2921. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2922. // // int can_create_order = sqlite3_column_int(statement, 10);
  2923. // //
  2924. // //
  2925. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  2926. // // if(mode==nil)
  2927. // // mode="";
  2928. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2929. // //
  2930. // //
  2931. // // char *username = (char*)sqlite3_column_text(statement, 12);
  2932. // // if(username==nil)
  2933. // // username="";
  2934. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2935. // //
  2936. // //
  2937. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2938. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2939. // // [header setValue:nscontact_id forKey:@"contact_id"];
  2940. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2941. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2942. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2943. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2944. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2945. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2946. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2947. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2948. // //
  2949. // // [header setValue:nsusername forKey:@"username"];
  2950. // //
  2951. // //
  2952. // // [ret setObject:header forKey:@"header"];
  2953. // // [ret setValue:nsmode forKey:@"mode"];
  2954. // //
  2955. // //
  2956. // // }
  2957. // //
  2958. // //
  2959. // //
  2960. // // sqlite3_finalize(statement);
  2961. // // }
  2962. // //
  2963. // //
  2964. // //
  2965. // // [iSalesDB close_db:db];
  2966. // //
  2967. // //
  2968. // //
  2969. // //
  2970. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2971. //
  2972. // return ret;
  2973. //}
  2974. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  2975. //{
  2976. //
  2977. // UIApplication * app = [UIApplication sharedApplication];
  2978. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2979. //
  2980. // [iSalesDB disable_trigger];
  2981. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2982. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2983. // [iSalesDB enable_trigger];
  2984. //
  2985. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2986. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2987. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2988. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2989. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2990. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2991. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2992. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2993. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2994. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2995. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2996. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2997. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2998. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2999. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3000. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3001. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3002. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3003. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3004. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3005. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3006. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3007. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3008. //
  3009. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3010. //
  3011. // [arr_name addObject:customer_first_name];
  3012. // [arr_name addObject:customer_last_name];
  3013. //
  3014. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3015. //
  3016. // // default ship from
  3017. // 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';";
  3018. //
  3019. // __block NSString *cid = @"";
  3020. // __block NSString *name = @"";
  3021. // __block NSString *ext = @"";
  3022. // __block NSString *contact = @"";
  3023. // __block NSString *email = @"";
  3024. // __block NSString *fax = @"";
  3025. // __block NSString *phone = @"";
  3026. //
  3027. // sqlite3 *db = [iSalesDB get_db];
  3028. //
  3029. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3030. //
  3031. // cid = [self textAtColumn:0 statement:statment];
  3032. // name = [self textAtColumn:1 statement:statment];
  3033. // ext = [self textAtColumn:2 statement:statment];
  3034. // contact = [self textAtColumn:3 statement:statment];
  3035. // email = [self textAtColumn:4 statement:statment];
  3036. // fax = [self textAtColumn:5 statement:statment];
  3037. // phone = [self textAtColumn:6 statement:statment];
  3038. //
  3039. // }];
  3040. //
  3041. // NSString* so_id = [self get_offline_soid:db];
  3042. // if(so_id==nil)
  3043. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3044. //
  3045. // NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  3046. //
  3047. //
  3048. //
  3049. // int result =[iSalesDB execSql:sql_neworder db:db];
  3050. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3051. //
  3052. //
  3053. //
  3054. // //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'
  3055. // //soId
  3056. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3057. //
  3058. //
  3059. //
  3060. //
  3061. //
  3062. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3063. // sqlite3_stmt * statement;
  3064. //
  3065. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3066. // {
  3067. // if (sqlite3_step(statement) == SQLITE_ROW)
  3068. // {
  3069. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3070. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3071. //
  3072. // //ret = sqlite3_column_int(statement, 0);
  3073. //
  3074. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3075. // if(soId==nil)
  3076. // soId="";
  3077. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3078. // [ret setValue:nssoId forKey:@"soId"];
  3079. // [ret setValue:nssoId forKey:@"orderCode"];
  3080. //
  3081. // }
  3082. // sqlite3_finalize(statement);
  3083. // }
  3084. //
  3085. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3086. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3087. //
  3088. //
  3089. // [iSalesDB close_db:db];
  3090. //
  3091. // return [RAConvertor dict2data:ret];
  3092. //
  3093. //}
  3094. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3095. {
  3096. //assert(params[@"order_code"]);
  3097. // assert(params[@"order_code"]);
  3098. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3099. NSString* orderCode = [params valueForKey:@"orderCode"];
  3100. NSString* app_order_code= params[@"appDelegate.order_code"];
  3101. // UIApplication * app = [UIApplication sharedApplication];
  3102. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3103. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3104. {
  3105. [iSalesDB disable_trigger];
  3106. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3107. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3108. [iSalesDB enable_trigger];
  3109. }
  3110. sqlite3 *db = [iSalesDB get_db];
  3111. int cart_count=[self query_ordercartcount:orderCode db:db];
  3112. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3113. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3114. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3115. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3116. [iSalesDB close_db:db];
  3117. return [RAConvertor dict2data:ret];
  3118. }
  3119. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3120. {
  3121. assert(params[@"can_create_backorder"]!=nil);
  3122. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3123. sqlite3 *db = [iSalesDB get_db];
  3124. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3125. int count =0;
  3126. if(params[@"count"]!=nil)
  3127. {
  3128. count = [params[@"count"] intValue];
  3129. }
  3130. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3131. NSString* product_id=params[@"product_id"];
  3132. NSString* orderCode=params[@"orderCode"];
  3133. NSString *qty = params[@"qty"];
  3134. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3135. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3136. __block int number_of_outOfStock = 0;
  3137. for(int i=0;i<arr_id.count;i++)
  3138. {
  3139. NSInteger item_qty= count;
  3140. if (qty) {
  3141. item_qty = [qty_arr[i] integerValue];
  3142. }
  3143. if(item_qty==0)
  3144. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3145. // 检查新加Model数量是否大于库存
  3146. if (![params[@"can_create_backorder"] boolValue]) {
  3147. __block BOOL needContinue = NO;
  3148. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3149. int availability = sqlite3_column_int(stmt, 0);
  3150. // 库存小于购买量为缺货
  3151. if (availability < item_qty || availability <= 0) {
  3152. number_of_outOfStock++;
  3153. needContinue = YES;
  3154. }
  3155. }];
  3156. if (needContinue) {
  3157. continue;
  3158. }
  3159. }
  3160. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3161. NSString* sql=nil;
  3162. sqlite3_stmt *stmt = nil;
  3163. BOOL shouldStep = NO;
  3164. if(_id<0)
  3165. {
  3166. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3167. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3168. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3169. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3170. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3171. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3172. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3173. shouldStep = YES;
  3174. }
  3175. else
  3176. {
  3177. if (qty) { // wish list move to cart
  3178. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3179. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3180. sqlite3_bind_int(stmt, 1, _id);
  3181. shouldStep = YES;
  3182. } else {
  3183. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  3184. __block BOOL update = YES;
  3185. if (![params[@"can_create_backorder"] boolValue]) {
  3186. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3187. int newQTY = sqlite3_column_int(stmt, 0);
  3188. int availability = sqlite3_column_int(stmt, 1);
  3189. if (newQTY > availability) { // 库存不够
  3190. update = NO;
  3191. number_of_outOfStock++;
  3192. }
  3193. }];
  3194. }
  3195. if (update) {
  3196. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3197. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3198. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3199. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3200. shouldStep = YES;
  3201. }
  3202. }
  3203. }
  3204. if (shouldStep) {
  3205. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3206. [iSalesDB execSql:@"ROLLBACK" db:db];
  3207. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3208. [iSalesDB close_db:db];
  3209. DebugLog(@"add to cart error");
  3210. return [RAConvertor dict2data:ret];
  3211. }
  3212. }
  3213. }
  3214. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3215. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3216. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3217. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3218. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3219. if (![params[@"can_create_backorder"] boolValue]) {
  3220. if (number_of_outOfStock > 0) {
  3221. ret[@"result"]=[NSNumber numberWithInt:8];
  3222. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3223. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3224. }
  3225. }
  3226. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3227. [iSalesDB close_db:db];
  3228. return [RAConvertor dict2data:ret];
  3229. }
  3230. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3231. {
  3232. // UIApplication * app = [UIApplication sharedApplication];
  3233. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3234. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3235. 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];
  3236. sqlite3_stmt * statement;
  3237. int count=0;
  3238. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3239. {
  3240. while (sqlite3_step(statement) == SQLITE_ROW)
  3241. {
  3242. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3243. int bitem_id = sqlite3_column_int(statement, 0);
  3244. int bitem_qty = sqlite3_column_int(statement, 1);
  3245. char *name = (char*)sqlite3_column_text(statement, 2);
  3246. if(name==nil)
  3247. name="";
  3248. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3249. char *description = (char*)sqlite3_column_text(statement, 3);
  3250. if(description==nil)
  3251. description="";
  3252. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3253. double unit_price = sqlite3_column_double(statement, 4);
  3254. int use_unitprice = sqlite3_column_int(statement, 5);
  3255. if(use_unitprice!=1)
  3256. {
  3257. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3258. }
  3259. itemjson[@"model"]=nsname;
  3260. itemjson[@"description"]=nsdescription;
  3261. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3262. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3263. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3264. if(compute)
  3265. {
  3266. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3267. }
  3268. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3269. count++;
  3270. }
  3271. sqlite3_finalize(statement);
  3272. }
  3273. ret[@"count"]=@(count);
  3274. if(count==0)
  3275. return nil;
  3276. else
  3277. return ret;
  3278. }
  3279. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3280. {
  3281. //compute: add part to subtotal;
  3282. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3283. dict_item[@(item_id)]=@"1";
  3284. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3285. double cuft=0;
  3286. double weight=0;
  3287. int carton=0;
  3288. 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];
  3289. sqlite3_stmt * statement;
  3290. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3291. {
  3292. if (sqlite3_step(statement) == SQLITE_ROW)
  3293. {
  3294. double ulength = sqlite3_column_double(statement, 0);
  3295. double uwidth = sqlite3_column_double(statement, 1);
  3296. double uheight = sqlite3_column_double(statement, 2);
  3297. double uweight = sqlite3_column_double(statement, 3);
  3298. double mlength = sqlite3_column_double(statement, 4);
  3299. double mwidth = sqlite3_column_double(statement, 5);
  3300. double mheight = sqlite3_column_double(statement, 6);
  3301. double mweight = sqlite3_column_double(statement, 7);
  3302. double ilength = sqlite3_column_double(statement, 8);
  3303. double iwidth = sqlite3_column_double(statement, 9);
  3304. double iheight = sqlite3_column_double(statement, 10);
  3305. double iweight = sqlite3_column_double(statement, 11);
  3306. // int pcs = sqlite3_column_int(statement,12);
  3307. int mpack = sqlite3_column_int(statement, 13);
  3308. int ipack = sqlite3_column_int(statement, 14);
  3309. double ucbf = sqlite3_column_double(statement, 15);
  3310. // double icbf = sqlite3_column_double(statement, 16);
  3311. // double mcbf = sqlite3_column_double(statement, 17);
  3312. if(ipack==0)
  3313. {
  3314. carton= count/mpack ;
  3315. weight = mweight*carton;
  3316. cuft= carton*(mlength*mwidth*mheight);
  3317. int remain=count%mpack;
  3318. if(remain==0)
  3319. {
  3320. //do nothing;
  3321. }
  3322. else
  3323. {
  3324. carton++;
  3325. weight += uweight*remain;
  3326. cuft += (ulength*uwidth*uheight)*remain;
  3327. }
  3328. }
  3329. else
  3330. {
  3331. carton = count/(mpack*ipack);
  3332. weight = mweight*carton;
  3333. cuft= carton*(mlength*mwidth*mheight);
  3334. int remain=count%(mpack*ipack);
  3335. if(remain==0)
  3336. {
  3337. // do nothing;
  3338. }
  3339. else
  3340. {
  3341. carton++;
  3342. int icarton =remain/ipack;
  3343. int iremain=remain%ipack;
  3344. weight += iweight*icarton;
  3345. cuft += (ilength*iwidth*iheight)*icarton;
  3346. if(iremain==0)
  3347. {
  3348. //do nothing;
  3349. }
  3350. else
  3351. {
  3352. weight += uweight*iremain;
  3353. cuft += (ulength*uwidth*uheight)*iremain;
  3354. }
  3355. }
  3356. }
  3357. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3358. cuft=ucbf*count;
  3359. weight= uweight*count;
  3360. #endif
  3361. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3362. cuft=ucbf*count;
  3363. weight= uweight*count;
  3364. #endif
  3365. }
  3366. sqlite3_finalize(statement);
  3367. }
  3368. if(compute)
  3369. {
  3370. NSArray * arr_count=nil;
  3371. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3372. for(int i=0;i<arr_bundle.count;i++)
  3373. {
  3374. dict_item[arr_bundle[i]]=@"1";
  3375. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3376. cuft+=[bundlejson[@"cuft"] doubleValue];
  3377. weight+=[bundlejson[@"weight"] doubleValue];
  3378. carton+=[bundlejson[@"carton"] intValue];
  3379. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3380. }
  3381. }
  3382. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3383. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3384. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3385. ret[@"items"]=dict_item;
  3386. return ret;
  3387. }
  3388. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3389. {
  3390. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3391. // 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 ];
  3392. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3393. DebugLog(@"offline_login sql:%@",sqlQuery);
  3394. sqlite3_stmt * statement;
  3395. int cart_count=0;
  3396. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3397. if ( dbresult== SQLITE_OK)
  3398. {
  3399. while (sqlite3_step(statement) == SQLITE_ROW)
  3400. {
  3401. int item_id = sqlite3_column_int(statement, 0);
  3402. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3403. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3404. }
  3405. sqlite3_finalize(statement);
  3406. }
  3407. return cart_count;
  3408. }
  3409. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3410. //{
  3411. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3412. // sqlite3 *db = [iSalesDB get_db];
  3413. // UIApplication * app = [UIApplication sharedApplication];
  3414. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3415. //
  3416. // NSString* orderCode=params[@"orderCode"];
  3417. //
  3418. // int sort = [[params objectForKey:@"sort"] intValue];
  3419. // NSString *sort_str = @"";
  3420. // switch (sort) {
  3421. // case 0:{
  3422. // sort_str = @"order by c.modify_time desc";
  3423. // }
  3424. // break;
  3425. // case 1:{
  3426. // sort_str = @"order by c.modify_time asc";
  3427. // }
  3428. // break;
  3429. // case 2:{
  3430. // sort_str = @"order by m.name asc";
  3431. // }
  3432. // break;
  3433. // case 3:{
  3434. // sort_str = @"order by m.name desc";
  3435. // }
  3436. // break;
  3437. // case 4:{
  3438. // sort_str = @"order by m.description asc";
  3439. // }
  3440. // break;
  3441. //
  3442. // default:
  3443. // break;
  3444. //
  3445. // }
  3446. //
  3447. //
  3448. //
  3449. // 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,m.availability 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 ];
  3450. //
  3451. //
  3452. //// 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 ];
  3453. //
  3454. //
  3455. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3456. //
  3457. // DebugLog(@"offline_login sql:%@",sqlQuery);
  3458. // sqlite3_stmt * statement;
  3459. //
  3460. //
  3461. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3462. //
  3463. // NSDate *date1 = [NSDate date];
  3464. //
  3465. // int count=0;
  3466. // int cart_count=0;
  3467. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3468. // if ( dbresult== SQLITE_OK)
  3469. // {
  3470. //
  3471. //
  3472. // while (sqlite3_step(statement) == SQLITE_ROW)
  3473. // {
  3474. //// NSDate *row_date = [NSDate date];
  3475. //
  3476. //
  3477. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3478. //
  3479. //
  3480. //
  3481. //
  3482. // int product_id = sqlite3_column_int(statement, 0);
  3483. //
  3484. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  3485. //
  3486. // int item_id = sqlite3_column_int(statement, 7);
  3487. //
  3488. // NSString* Price=nil;
  3489. // if(str_price==nil)
  3490. // {
  3491. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3492. //// NSDate *price_date = [NSDate date];
  3493. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3494. //// DebugLog(@"price time interval");
  3495. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3496. //
  3497. // if(price==nil)
  3498. // Price=@"No Price.";
  3499. // else
  3500. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3501. // }
  3502. // else
  3503. // {
  3504. //
  3505. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3506. // }
  3507. //
  3508. //
  3509. // double discount = sqlite3_column_double(statement, 2);
  3510. // int item_count = sqlite3_column_int(statement, 3);
  3511. //
  3512. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  3513. // NSString *nsline_note=nil;
  3514. // if(line_note!=nil)
  3515. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3516. //
  3517. //
  3518. // char *name = (char*)sqlite3_column_text(statement, 5);
  3519. // NSString *nsname=nil;
  3520. // if(name!=nil)
  3521. // nsname= [[NSString alloc]initWithUTF8String:name];
  3522. //
  3523. // char *description = (char*)sqlite3_column_text(statement, 6);
  3524. // NSString *nsdescription=nil;
  3525. // if(description!=nil)
  3526. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  3527. //
  3528. //
  3529. //
  3530. // int stockUom = sqlite3_column_int(statement, 8);
  3531. // int _id = sqlite3_column_int(statement, 9);
  3532. // int availability = sqlite3_column_int(statement, 10);
  3533. //
  3534. //// NSDate *subtotal_date = [NSDate date];
  3535. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3536. //// DebugLog(@"subtotal_date time interval");
  3537. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3538. //
  3539. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3540. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  3541. // int carton=[bsubtotaljson[@"carton"] intValue];
  3542. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3543. //
  3544. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3545. //// NSDate *img_date = [NSDate date];
  3546. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3547. //// DebugLog(@"img_date time interval");
  3548. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3549. //
  3550. // itemjson[@"model"]=nsname;
  3551. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3552. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3553. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3554. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3555. // itemjson[@"check"]=@"true";
  3556. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3557. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3558. // itemjson[@"unit_price"]=Price;
  3559. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3560. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3561. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3562. // itemjson[@"note"]=nsline_note;
  3563. // if (!appDelegate.can_create_backorder) {
  3564. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3565. // }
  3566. //// NSDate *date2 = [NSDate date];
  3567. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3568. //// DebugLog(@"model_bundle time interval");
  3569. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3570. //
  3571. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3572. // count++;
  3573. //
  3574. //// DebugLog(@"row time interval");
  3575. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3576. // }
  3577. //
  3578. //
  3579. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3580. // sqlite3_finalize(statement);
  3581. // }
  3582. //
  3583. //
  3584. // DebugLog(@"request cart total time interval");
  3585. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3586. //
  3587. //
  3588. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3589. //
  3590. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3591. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3592. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3593. // ret[@"count"]=[NSNumber numberWithInt:count ];
  3594. //
  3595. // ret[@"mode"]=@"Regular Mode";
  3596. //
  3597. // [iSalesDB close_db:db];
  3598. //
  3599. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3600. //
  3601. // DebugLog(@"general notes :%@",general_note);
  3602. //
  3603. // ret[@"general_note"]= general_note;
  3604. //
  3605. // return [RAConvertor dict2data:ret];
  3606. //}
  3607. +(NSData*) offline_login :(NSMutableDictionary *) params
  3608. {
  3609. NSString* user = [params valueForKey:@"user"];
  3610. NSString* password = [params valueForKey:@"password"];
  3611. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3612. sqlite3 *db = [iSalesDB get_db];
  3613. 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"]];
  3614. DebugLog(@"offline_login sql:%@",sqlQuery);
  3615. sqlite3_stmt * statement;
  3616. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3617. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3618. {
  3619. if (sqlite3_step(statement) == SQLITE_ROW)
  3620. {
  3621. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3622. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3623. int can_show_price = sqlite3_column_int(statement, 0);
  3624. int can_see_price = sqlite3_column_int(statement, 1);
  3625. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3626. if(contact_id==nil)
  3627. contact_id="";
  3628. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3629. int user_type = sqlite3_column_int(statement, 3);
  3630. int can_cancel_order = sqlite3_column_int(statement, 4);
  3631. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3632. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3633. int can_delete_order = sqlite3_column_int(statement, 7);
  3634. int can_submit_order = sqlite3_column_int(statement, 8);
  3635. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3636. int can_create_order = sqlite3_column_int(statement, 10);
  3637. char *mode = (char*)sqlite3_column_text(statement, 11);
  3638. if(mode==nil)
  3639. mode="";
  3640. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3641. char *username = (char*)sqlite3_column_text(statement, 12);
  3642. if(username==nil)
  3643. username="";
  3644. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3645. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3646. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3647. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3648. [header setValue:nscontact_id forKey:@"contact_id"];
  3649. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3650. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3651. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3652. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3653. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3654. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3655. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3656. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3657. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3658. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3659. [header setValue:nsusername forKey:@"username"];
  3660. NSError* error=nil;
  3661. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3662. [header setValue:statusFilter forKey:@"statusFilter"];
  3663. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3664. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3665. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3666. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3667. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3668. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3669. [ret setObject:header forKey:@"header"];
  3670. [ret setValue:nsmode forKey:@"mode"];
  3671. }
  3672. sqlite3_finalize(statement);
  3673. }
  3674. [iSalesDB close_db:db];
  3675. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3676. return [RAConvertor dict2data:ret];
  3677. }
  3678. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3679. {
  3680. assert(params[@"mode"]!=nil);
  3681. NSString* contactId = [params valueForKey:@"contactId"];
  3682. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3683. sqlite3 *db = [iSalesDB get_db];
  3684. NSString *sqlQuery = nil;
  3685. {
  3686. 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];
  3687. }
  3688. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3689. sqlite3_stmt * statement;
  3690. [ret setValue:@"2" forKey:@"result"];
  3691. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3692. {
  3693. //int i = 0;
  3694. if (sqlite3_step(statement) == SQLITE_ROW)
  3695. {
  3696. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3697. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3698. // int editable = sqlite3_column_int(statement, 0);
  3699. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3700. NSString *nscompany_name =nil;
  3701. if(company_name==nil)
  3702. nscompany_name=@"";
  3703. else
  3704. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3705. char *country = (char*)sqlite3_column_text(statement, 2);
  3706. if(country==nil)
  3707. country="";
  3708. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3709. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3710. // if(addr==nil)
  3711. // addr="";
  3712. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3713. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3714. if(zipcode==nil)
  3715. zipcode="";
  3716. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3717. char *state = (char*)sqlite3_column_text(statement, 5);
  3718. if(state==nil)
  3719. state="";
  3720. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3721. char *city = (char*)sqlite3_column_text(statement, 6);
  3722. if(city==nil)
  3723. city="";
  3724. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3725. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3726. // NSString *nscontact_name = nil;
  3727. // if(contact_name==nil)
  3728. // nscontact_name=@"";
  3729. // else
  3730. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3731. char *phone = (char*)sqlite3_column_text(statement, 8);
  3732. NSString *nsphone = nil;
  3733. if(phone==nil)
  3734. nsphone=@"";
  3735. else
  3736. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3737. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3738. // if(contact_id==nil)
  3739. // contact_id="";
  3740. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3741. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3742. if(addr_1==nil)
  3743. addr_1="";
  3744. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3745. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3746. if(addr_2==nil)
  3747. addr_2="";
  3748. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3749. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3750. if(addr_3==nil)
  3751. addr_3="";
  3752. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3753. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3754. if(addr_4==nil)
  3755. addr_4="";
  3756. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3757. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3758. if(first_name==nil)
  3759. first_name="";
  3760. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3761. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3762. if(last_name==nil)
  3763. last_name="";
  3764. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3765. char *fax = (char*)sqlite3_column_text(statement, 16);
  3766. NSString *nsfax = nil;
  3767. if(fax==nil)
  3768. nsfax=@"";
  3769. else
  3770. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3771. char *email = (char*)sqlite3_column_text(statement, 17);
  3772. NSString *nsemail = nil;
  3773. if(email==nil)
  3774. nsemail=@"";
  3775. else
  3776. nsemail= [[NSString alloc]initWithUTF8String:email];
  3777. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3778. NSString *nsimg_0 = nil;
  3779. if(img_0==nil)
  3780. nsimg_0=@"";
  3781. else
  3782. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3783. [self copy_bcardImg:nsimg_0];
  3784. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3785. NSString *nsimg_1 = nil;
  3786. if(img_1==nil)
  3787. nsimg_1=@"";
  3788. else
  3789. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3790. [self copy_bcardImg:nsimg_1];
  3791. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3792. NSString *nsimg_2 = nil;
  3793. if(img_2==nil)
  3794. nsimg_2=@"";
  3795. else
  3796. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3797. [self copy_bcardImg:nsimg_2];
  3798. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3799. NSString *nsprice_type = nil;
  3800. if(price_type==nil)
  3801. nsprice_type=@"";
  3802. else
  3803. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3804. char *notes = (char*)sqlite3_column_text(statement, 22);
  3805. NSString *nsnotes = nil;
  3806. if(notes==nil)
  3807. nsnotes=@"";
  3808. else
  3809. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3810. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3811. NSString *nssalesrep = nil;
  3812. if(salesrep==nil)
  3813. nssalesrep=@"";
  3814. else
  3815. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3816. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3817. {
  3818. // decrypt
  3819. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3820. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3821. nsphone=[AESCrypt fastdecrypt:nsphone];
  3822. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3823. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3824. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3825. }
  3826. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3827. [arr_name addObject:nsfirst_name];
  3828. [arr_name addObject:nslast_name];
  3829. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3830. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3831. [arr_ext addObject:nsaddr_1];
  3832. [arr_ext addObject:nsaddr_2];
  3833. [arr_ext addObject:nsaddr_3];
  3834. [arr_ext addObject:nsaddr_4];
  3835. [arr_ext addObject:@"\r\n"];
  3836. [arr_ext addObject:nscity];
  3837. [arr_ext addObject:nsstate];
  3838. [arr_ext addObject:nszipcode];
  3839. [arr_ext addObject:nscountry];
  3840. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3841. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3842. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3843. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3844. [item setValue:nscountry forKey:@"customer_country"];
  3845. [item setValue:nsphone forKey:@"customer_phone"];
  3846. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3847. [item setValue:nscompany_name forKey:@"customer_name"];
  3848. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3849. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3850. [item setValue:nsext forKey:@"customer_contact_ext"];
  3851. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3852. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3853. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3854. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3855. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3856. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3857. [item setValue:nslast_name forKey:@"customer_last_name"];
  3858. [item setValue:nscity forKey:@"customer_city"];
  3859. [item setValue:nsstate forKey:@"customer_state"];
  3860. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3861. [item setValue:contactId forKey:@"customer_cid"];
  3862. [item setValue:nscontact_name forKey:@"customer_contact"];
  3863. [item setValue:nsfax forKey:@"customer_fax"];
  3864. [item setValue:nsemail forKey:@"customer_email"];
  3865. [item setValue:contact_type forKey:@"customer_contact_type"];
  3866. [ret setObject:item forKey:@"customerInfo"];
  3867. // i++;
  3868. }
  3869. // UIApplication * app = [UIApplication sharedApplication];
  3870. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3871. [ret setValue:params[@"mode"] forKey:@"mode"];
  3872. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3873. sqlite3_finalize(statement);
  3874. }
  3875. [iSalesDB close_db:db];
  3876. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3877. return ret;
  3878. }
  3879. + (bool) copy_bcardImg:(NSString*) filename
  3880. {
  3881. if(filename.length==0)
  3882. return false;
  3883. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3884. bool ret=false;
  3885. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3886. NSString *cachefolder = [paths objectAtIndex:0];
  3887. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3888. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3889. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3890. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3891. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3892. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3893. //
  3894. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3895. NSFileManager* fileManager = [NSFileManager defaultManager];
  3896. BOOL bdir=NO;
  3897. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3898. {
  3899. NSError *error = nil;
  3900. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3901. {
  3902. ret=false;
  3903. }
  3904. else
  3905. {
  3906. ret=true;
  3907. }
  3908. // NSError *error = nil;
  3909. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3910. //
  3911. // if(!bsuccess)
  3912. // {
  3913. // DebugLog(@"Create offline_createimg folder failed");
  3914. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3915. // return [RAConvertor dict2data:ret];
  3916. // }
  3917. // if(bsuccess)
  3918. // {
  3919. // sqlite3 *db = [self get_db];
  3920. //
  3921. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3922. // [iSalesDB close_db:db];
  3923. // }
  3924. }
  3925. return ret;
  3926. //
  3927. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3928. // if(bsuccess)
  3929. // {
  3930. // NSError *error = nil;
  3931. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3932. // {
  3933. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3934. // }
  3935. // else
  3936. // {
  3937. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3938. //
  3939. // ret[@"img_url_aname"]=filename;
  3940. // ret[@"img_url"]=savedImagePath;
  3941. // }
  3942. // }
  3943. }
  3944. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3945. {
  3946. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3947. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3948. NSString *cachefolder = [paths objectAtIndex:0];
  3949. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3950. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3951. NSFileManager* fileManager = [NSFileManager defaultManager];
  3952. BOOL bdir=YES;
  3953. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3954. {
  3955. NSError *error = nil;
  3956. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3957. if(!bsuccess)
  3958. {
  3959. DebugLog(@"Create offline_createimg folder failed");
  3960. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3961. return [RAConvertor dict2data:ret];
  3962. }
  3963. // if(bsuccess)
  3964. // {
  3965. // sqlite3 *db = [self get_db];
  3966. //
  3967. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3968. // [iSalesDB close_db:db];
  3969. // }
  3970. }
  3971. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3972. //JEPG格式
  3973. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3974. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3975. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3976. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3977. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3978. if(bsuccess)
  3979. {
  3980. NSError *error = nil;
  3981. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3982. {
  3983. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3984. }
  3985. else
  3986. {
  3987. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3988. ret[@"img_url_aname"]=filename;
  3989. ret[@"img_url"]=filename;
  3990. }
  3991. }
  3992. else
  3993. {
  3994. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3995. }
  3996. return [RAConvertor dict2data:ret];
  3997. }
  3998. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3999. {
  4000. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4001. category = [category substringToIndex:3];
  4002. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4003. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4004. params[@"category"]= category;
  4005. ret[@"params"]= params;
  4006. [ret setValue:@"detail" forKey:@"target"];
  4007. [ret setValue:@"popup" forKey:@"action"];
  4008. [ret setValue:@"content" forKey:@"type"];
  4009. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4010. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4011. [ret setValue:@"true" forKey:@"single_row"];
  4012. [ret setValue:@"true" forKey:@"partial_refresh"];
  4013. // sqlite3 *db = [iSalesDB get_db];
  4014. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4015. 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 ;
  4016. sqlite3_stmt * statement;
  4017. int count = 0;
  4018. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4019. // int count=0;
  4020. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4021. {
  4022. int i=0;
  4023. while (sqlite3_step(statement) == SQLITE_ROW)
  4024. {
  4025. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4026. // char *name = (char*)sqlite3_column_text(statement, 1);
  4027. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4028. char *name = (char*)sqlite3_column_text(statement, 0);
  4029. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4030. int product_id = sqlite3_column_int(statement, 1);
  4031. char *url = (char*)sqlite3_column_text(statement, 2);
  4032. if(url==nil)
  4033. url="";
  4034. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4035. [item setValue:nsurl forKey:@"picture_path"];
  4036. [item setValue:nsname forKey:@"fash_name"];
  4037. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4038. [item setValue:category forKey:@"category"];
  4039. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4040. i++;
  4041. }
  4042. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4043. sqlite3_finalize(statement);
  4044. }
  4045. DebugLog(@"count:%d",count);
  4046. // [iSalesDB close_db:db];
  4047. return ret;
  4048. }
  4049. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4050. {
  4051. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4052. [ret setValue:key forKey:@"key"];
  4053. [ret setValue:value forKey:@"val"];
  4054. [ret setValue:@"price" forKey:@"type"];
  4055. return ret;
  4056. }
  4057. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4058. {
  4059. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4060. [ret setValue:key forKey:@"key"];
  4061. [ret setValue:value forKey:@"val"];
  4062. return ret;
  4063. }
  4064. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4065. {
  4066. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4067. [ret setValue:@"0" forKey:@"img_count"];
  4068. // sqlite3 *db = [iSalesDB get_db];
  4069. 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 ;
  4070. sqlite3_stmt * statement;
  4071. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4072. {
  4073. int i=0;
  4074. if (sqlite3_step(statement) == SQLITE_ROW)
  4075. {
  4076. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4077. // char *name = (char*)sqlite3_column_text(statement, 1);
  4078. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4079. char *value = (char*)sqlite3_column_text(statement, 0);
  4080. if(value==nil)
  4081. value="";
  4082. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4083. char *key = (char*)sqlite3_column_text(statement, 1);
  4084. if(key==nil)
  4085. key="";
  4086. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4087. [item setValue:nsvalue forKey:@"val"];
  4088. [item setValue:nskey forKey:@"key"];
  4089. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4090. [ret setValue:@"1" forKey:@"count"];
  4091. i++;
  4092. }
  4093. sqlite3_finalize(statement);
  4094. }
  4095. // [iSalesDB close_db:db];
  4096. return ret;
  4097. }
  4098. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4099. {
  4100. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4101. [ret setValue:@"0" forKey:@"count"];
  4102. // sqlite3 *db = [iSalesDB get_db];
  4103. 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;
  4104. sqlite3_stmt * statement;
  4105. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4106. {
  4107. int i=0;
  4108. while (sqlite3_step(statement) == SQLITE_ROW)
  4109. {
  4110. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4111. // char *name = (char*)sqlite3_column_text(statement, 1);
  4112. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4113. char *value = (char*)sqlite3_column_text(statement, 0);
  4114. if(value==nil)
  4115. value="";
  4116. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4117. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4118. if(selector_display==nil)
  4119. selector_display="";
  4120. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4121. int product_id = sqlite3_column_int(statement, 2);
  4122. char *category = (char*)sqlite3_column_text(statement, 3);
  4123. if(category==nil)
  4124. category="";
  4125. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4126. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4127. [item setValue:nsvalue forKey:@"title"];
  4128. [item setValue:url forKey:@"pic_url"];
  4129. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4130. [params setValue:@"2" forKey:@"count"];
  4131. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4132. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4133. [param0 setValue:@"product_id" forKey:@"name"];
  4134. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4135. [param1 setValue:nscategory forKey:@"val"];
  4136. [param1 setValue:@"category" forKey:@"name"];
  4137. [params setObject:param0 forKey:@"param_0"];
  4138. [params setObject:param1 forKey:@"param_1"];
  4139. [item setObject:params forKey:@"params"];
  4140. [ret setValue:nsselector_display forKey:@"name"];
  4141. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4142. i++;
  4143. }
  4144. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4145. [ret setValue:@"switch" forKey:@"action"];
  4146. sqlite3_finalize(statement);
  4147. }
  4148. // [iSalesDB close_db:db];
  4149. return ret;
  4150. }
  4151. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4152. {
  4153. // model 在 category search 显示的图片。
  4154. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4155. // sqlite3 *db = [iSalesDB get_db];
  4156. 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];
  4157. sqlite3_stmt * statement;
  4158. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4159. {
  4160. while (sqlite3_step(statement) == SQLITE_ROW)
  4161. {
  4162. char *url = (char*)sqlite3_column_text(statement, 0);
  4163. if(url==nil)
  4164. url="";
  4165. int type = sqlite3_column_int(statement, 1);
  4166. if(type==0)
  4167. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4168. else
  4169. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4170. }
  4171. sqlite3_finalize(statement);
  4172. }
  4173. // [iSalesDB close_db:db];
  4174. return ret;
  4175. }
  4176. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4177. {
  4178. int item_id=-1;
  4179. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4180. sqlite3_stmt * statement;
  4181. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4182. {
  4183. if (sqlite3_step(statement) == SQLITE_ROW)
  4184. {
  4185. item_id = sqlite3_column_int(statement, 0);
  4186. }
  4187. sqlite3_finalize(statement);
  4188. }
  4189. return item_id;
  4190. }
  4191. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4192. {
  4193. // NSString* ret = @"";
  4194. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4195. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4196. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4197. sqlite3_stmt * statement;
  4198. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4199. {
  4200. while (sqlite3_step(statement) == SQLITE_ROW)
  4201. {
  4202. int bitem_id = sqlite3_column_int(statement, 0);
  4203. int bitem_qty = sqlite3_column_int(statement, 1);
  4204. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4205. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4206. }
  4207. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4208. sqlite3_finalize(statement);
  4209. }
  4210. // if(ret==nil)
  4211. // ret=@"";
  4212. *count=arr_count;
  4213. return arr_bundle;
  4214. }
  4215. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4216. {
  4217. // get default sold qty, return -1 if model not found;
  4218. int ret = -1;
  4219. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4220. sqlite3_stmt * statement;
  4221. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4222. {
  4223. if (sqlite3_step(statement) == SQLITE_ROW)
  4224. {
  4225. ret = sqlite3_column_int(statement, 0);
  4226. }
  4227. sqlite3_finalize(statement);
  4228. }
  4229. return ret;
  4230. }
  4231. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4232. {
  4233. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4234. [ret setValue:@"0" forKey:@"img_count"];
  4235. // sqlite3 *db = [iSalesDB get_db];
  4236. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4237. sqlite3_stmt * statement;
  4238. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4239. {
  4240. int i=0;
  4241. while (sqlite3_step(statement) == SQLITE_ROW)
  4242. {
  4243. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4244. // char *name = (char*)sqlite3_column_text(statement, 1);
  4245. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4246. char *url = (char*)sqlite3_column_text(statement, 0);
  4247. if(url==nil)
  4248. url="";
  4249. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4250. [item setValue:nsurl forKey:@"s"];
  4251. [item setValue:nsurl forKey:@"l"];
  4252. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4253. i++;
  4254. }
  4255. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4256. sqlite3_finalize(statement);
  4257. }
  4258. // [iSalesDB close_db:db];
  4259. return ret;
  4260. }
  4261. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4262. // UIApplication * app = [UIApplication sharedApplication];
  4263. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4264. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4265. sqlite3 *db = [iSalesDB get_db];
  4266. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4267. NSString* product_id=params[@"product_id"];
  4268. NSString *item_count_str = params[@"item_count"];
  4269. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4270. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4271. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4272. // NSString *sql = @"";
  4273. for(int i=0;i<arr.count;i++)
  4274. {
  4275. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4276. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4277. __block int cart_count = 0;
  4278. if (!item_count_str) {
  4279. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4280. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4281. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4282. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4283. cart_count = [[model_set_components lastObject] intValue];
  4284. }];
  4285. }
  4286. if(count==0)
  4287. {
  4288. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4289. sqlite3_stmt *stmt;
  4290. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4291. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4292. if (item_count_arr) {
  4293. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4294. } else {
  4295. sqlite3_bind_int(stmt,2,cart_count);
  4296. }
  4297. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4298. [iSalesDB execSql:@"ROLLBACK" db:db];
  4299. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4300. [iSalesDB close_db:db];
  4301. DebugLog(@"add to wishlist error");
  4302. return ret;
  4303. }
  4304. } else {
  4305. int qty = 0;
  4306. if (item_count_arr) {
  4307. qty = [item_count_arr[i] intValue];
  4308. } else {
  4309. qty = cart_count;
  4310. }
  4311. 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]];
  4312. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4313. [iSalesDB execSql:@"ROLLBACK" db:db];
  4314. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4315. [iSalesDB close_db:db];
  4316. DebugLog(@"add to wishlist error");
  4317. return ret;
  4318. }
  4319. }
  4320. }
  4321. // [iSalesDB execSql:sql db:db];
  4322. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4323. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4324. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4325. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4326. [iSalesDB close_db:db];
  4327. ret[@"wish_count"]=@(count);
  4328. ret[@"result"]= [NSNumber numberWithInt:2];
  4329. return ret;
  4330. }
  4331. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4332. // 0 category
  4333. // 1 search
  4334. // 2 itemsearch
  4335. NSData *ret = nil;
  4336. NSDictionary *items = nil;
  4337. switch (from) {
  4338. case 0:{
  4339. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4340. }
  4341. break;
  4342. case 1:{
  4343. items = [[self search:params limited:NO] objectForKey:@"items"];
  4344. }
  4345. break;
  4346. case 2:{
  4347. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4348. }
  4349. break;
  4350. default:
  4351. break;
  4352. }
  4353. if (!items) {
  4354. return ret;
  4355. }
  4356. int count = [[items objectForKey:@"count"] intValue];
  4357. NSMutableString *product_id_str = [@"" mutableCopy];
  4358. for (int i = 0; i < count; i++) {
  4359. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4360. NSDictionary *item = [items objectForKey:key];
  4361. NSString *product_id = [item objectForKey:@"product_id"];
  4362. if (i == 0) {
  4363. [product_id_str appendString:product_id];
  4364. } else {
  4365. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4366. }
  4367. }
  4368. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4369. if ([add_to isEqualToString:@"cart"]) {
  4370. NSString *order_code = [params objectForKey:@"orderCode"];
  4371. if (order_code.length) {
  4372. NSDictionary *newParams = @{
  4373. @"product_id" : product_id_str,
  4374. @"orderCode" : order_code,
  4375. @"can_create_backorder":params[@"can_create_backorder"]
  4376. };
  4377. ret = [self offline_add2cart:newParams.mutableCopy];
  4378. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4379. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4380. ret = [RAConvertor dict2data:retDic];
  4381. }
  4382. } else if([add_to isEqualToString:@"wishlist"]) {
  4383. NSDictionary *newParams = @{
  4384. @"product_id" : product_id_str
  4385. };
  4386. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4387. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4388. ret = [RAConvertor dict2data:retDic];
  4389. } else if([add_to isEqualToString:@"portfolio"]) {
  4390. NSDictionary *newParams = @{
  4391. @"product_id" : product_id_str
  4392. };
  4393. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4394. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4395. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4396. ret = [RAConvertor dict2data:retDic];
  4397. }
  4398. return ret;
  4399. }
  4400. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4401. {
  4402. return [self addAll:params from:0];
  4403. }
  4404. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4405. {
  4406. return [self addAll:params from:1];
  4407. }
  4408. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4409. {
  4410. return [self addAll:params from:2];
  4411. }
  4412. #pragma mark - Jack
  4413. #warning 做SQL操作时转义!!
  4414. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4415. // "val_227" : {
  4416. // "check" : 1,
  4417. // "value" : "US United States",
  4418. // "value_id" : "228"
  4419. // },
  4420. if (!countryCode) {
  4421. countryCode = @"US";
  4422. }
  4423. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4424. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4425. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4426. int code_id = sqlite3_column_int(stmt, 3); // id
  4427. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4428. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4429. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4430. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4431. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4432. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4433. }
  4434. long n = *count;
  4435. *count = n + 1;
  4436. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4437. [container setValue:countryDic forKey:key];
  4438. }] mutableCopy];
  4439. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4440. return ret;
  4441. }
  4442. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4443. countryCode = [self translateSingleQuote:countryCode];
  4444. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4445. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4446. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4447. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4448. if (name == NULL) {
  4449. name = "";
  4450. }
  4451. if (code == NULL) {
  4452. code = "";
  4453. }
  4454. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4455. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4456. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4457. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4458. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4459. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4460. }
  4461. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4462. [container setValue:stateDic forKey:key];
  4463. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4464. DebugLog(@"query all state error: %@",err_msg);
  4465. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4466. // [stateDic setValue:@"Other" forKey:@"value"];
  4467. // [stateDic setValue:@"" forKey:@"value_id"];
  4468. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4469. //
  4470. // if (state_code && [@"" isEqualToString:state_code]) {
  4471. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4472. // }
  4473. //
  4474. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4475. // [container setValue:stateDic forKey:key];
  4476. }] mutableCopy];
  4477. [ret removeObjectForKey:@"result"];
  4478. // failure 可以不用了,一样的
  4479. if (ret.allKeys.count == 0) {
  4480. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4481. [stateDic setValue:@"Other" forKey:@"value"];
  4482. [stateDic setValue:@"" forKey:@"value_id"];
  4483. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4484. if (state_code && [@"" isEqualToString:state_code]) {
  4485. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4486. }
  4487. NSString *key = [NSString stringWithFormat:@"val_0"];
  4488. [ret setValue:stateDic forKey:key];
  4489. }
  4490. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4491. return ret;
  4492. }
  4493. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4494. codeId = [self translateSingleQuote:codeId];
  4495. 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];
  4496. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4497. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4498. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4499. if (name == NULL) {
  4500. name = "";
  4501. }
  4502. if (code == NULL) {
  4503. code = "";
  4504. }
  4505. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4506. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4507. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4508. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4509. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4510. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4511. }
  4512. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4513. [container setValue:stateDic forKey:key];
  4514. }] mutableCopy];
  4515. [ret removeObjectForKey:@"result"];
  4516. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4517. return ret;
  4518. }
  4519. + (NSDictionary *)offline_getPrice {
  4520. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4521. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4522. char *name = (char *) sqlite3_column_text(stmt, 1);
  4523. int type = sqlite3_column_int(stmt, 2);
  4524. int orderBy = sqlite3_column_int(stmt, 3);
  4525. if (name == NULL) {
  4526. name = "";
  4527. }
  4528. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4529. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4530. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4531. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4532. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4533. if (orderBy == 0) {
  4534. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4535. }
  4536. [container setValue:priceDic forKey:key];
  4537. }] mutableCopy];
  4538. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4539. return ret;
  4540. }
  4541. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4542. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4543. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4544. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4545. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4546. // int _id = sqlite3_column_int(stmt, 0);
  4547. NSString *name = [self textAtColumn:1 statement:stmt];
  4548. NSDictionary *typeDic = @{
  4549. @"value_id" : name,
  4550. @"value" : name,
  4551. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4552. };
  4553. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4554. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  4555. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4556. }];
  4557. return ret;
  4558. }
  4559. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  4560. // 首先从offline_login表中取出sales_code
  4561. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4562. // NSString *user = app.user;
  4563. user = [self translateSingleQuote:user];
  4564. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4565. __block NSString *user_code = @"";
  4566. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4567. char *code = (char *)sqlite3_column_text(stmt, 0);
  4568. if (code == NULL) {
  4569. code = "";
  4570. }
  4571. user_code = [NSString stringWithUTF8String:code];
  4572. }];
  4573. // 再取所有salesRep
  4574. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4575. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4576. // 1 name 2 code 3 salesrep_id
  4577. char *name = (char *)sqlite3_column_text(stmt, 1);
  4578. char *code = (char *)sqlite3_column_text(stmt, 2);
  4579. int salesrep_id = sqlite3_column_int(stmt, 3);
  4580. if (name == NULL) {
  4581. name = "";
  4582. }
  4583. if (code == NULL) {
  4584. code = "";
  4585. }
  4586. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4587. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4588. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4589. // 比较code 相等则check
  4590. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4591. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4592. }
  4593. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4594. }] mutableCopy];
  4595. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4596. return ret;
  4597. }
  4598. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4599. zipcode = [self translateSingleQuote:zipcode];
  4600. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4601. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4602. char *country = (char *)sqlite3_column_text(stmt, 0);
  4603. char *state = (char *)sqlite3_column_text(stmt, 1);
  4604. char *city = (char *)sqlite3_column_text(stmt, 2);
  4605. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4606. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4607. if (country == NULL) {
  4608. country = "";
  4609. }
  4610. if (state == NULL) {
  4611. state = "";
  4612. }
  4613. if (city == NULL) {
  4614. city = "";
  4615. }
  4616. if (country_code == NULL) {
  4617. country_code = "";
  4618. }
  4619. if (state_code == NULL) {
  4620. state_code = "";
  4621. }
  4622. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4623. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4624. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4625. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4626. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4627. }] mutableCopy];
  4628. return ret;
  4629. }
  4630. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4631. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4632. [item setValue:value forKey:valueKey];
  4633. [dic setValue:item forKey:itemKey];
  4634. }
  4635. + (NSString *)countryCodeByid:(NSString *)code_id {
  4636. NSString *ret = nil;
  4637. code_id = [self translateSingleQuote:code_id];
  4638. sqlite3 *db = [iSalesDB get_db];
  4639. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4640. sqlite3_stmt * statement;
  4641. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4642. while (sqlite3_step(statement) == SQLITE_ROW) {
  4643. char *code = (char *)sqlite3_column_text(statement, 0);
  4644. if (code == NULL) {
  4645. code = "";
  4646. }
  4647. ret = [NSString stringWithUTF8String:code];
  4648. }
  4649. sqlite3_finalize(statement);
  4650. }
  4651. [iSalesDB close_db:db];
  4652. return ret;
  4653. }
  4654. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4655. NSString *ret = nil;
  4656. code = [self translateSingleQuote:code];
  4657. sqlite3 *db = [iSalesDB get_db];
  4658. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4659. sqlite3_stmt * statement;
  4660. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4661. while (sqlite3_step(statement) == SQLITE_ROW) {
  4662. char *_id = (char *)sqlite3_column_text(statement, 0);
  4663. if (_id == NULL) {
  4664. _id = "";
  4665. }
  4666. ret = [NSString stringWithFormat:@"%s",_id];
  4667. }
  4668. sqlite3_finalize(statement);
  4669. }
  4670. [iSalesDB close_db:db];
  4671. return ret;
  4672. }
  4673. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4674. NSString *name = nil;
  4675. codeId = [self translateSingleQuote:codeId];
  4676. sqlite3 *db = [iSalesDB get_db];
  4677. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4678. sqlite3_stmt * statement;
  4679. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4680. while (sqlite3_step(statement) == SQLITE_ROW) {
  4681. char *value = (char *)sqlite3_column_text(statement, 0);
  4682. if (value == NULL) {
  4683. value = "";
  4684. }
  4685. name = [NSString stringWithUTF8String:value];
  4686. }
  4687. sqlite3_finalize(statement);
  4688. }
  4689. [iSalesDB close_db:db];
  4690. return name;
  4691. }
  4692. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4693. NSString *ret = nil;
  4694. sqlite3 *db = [iSalesDB get_db];
  4695. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4696. sqlite3_stmt * statement;
  4697. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4698. while (sqlite3_step(statement) == SQLITE_ROW) {
  4699. char *name = (char *)sqlite3_column_text(statement, 0);
  4700. if (name == NULL) {
  4701. name = "";
  4702. }
  4703. ret = [NSString stringWithUTF8String:name];
  4704. }
  4705. sqlite3_finalize(statement);
  4706. }
  4707. [iSalesDB close_db:db];
  4708. return ret;
  4709. }
  4710. + (NSString *)salesRepCodeById:(NSString *)_id {
  4711. NSString *ret = nil;
  4712. _id = [self translateSingleQuote:_id];
  4713. sqlite3 *db = [iSalesDB get_db];
  4714. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4715. sqlite3_stmt * statement;
  4716. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4717. while (sqlite3_step(statement) == SQLITE_ROW) {
  4718. char *rep = (char *)sqlite3_column_text(statement, 0);
  4719. if (rep == NULL) {
  4720. rep = "";
  4721. }
  4722. ret = [NSString stringWithUTF8String:rep];
  4723. }
  4724. sqlite3_finalize(statement);
  4725. }
  4726. [iSalesDB close_db:db];
  4727. return ret;
  4728. }
  4729. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4730. char *tx = (char *)sqlite3_column_text(stmt, col);
  4731. if (tx == NULL) {
  4732. tx = "";
  4733. }
  4734. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4735. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4736. if (!text) {
  4737. text = @"";
  4738. }
  4739. // 将字符全部为' '的字符串干掉
  4740. int spaceCount = 0;
  4741. for (int i = 0; i < text.length; i++) {
  4742. if ([text characterAtIndex:i] == ' ') {
  4743. spaceCount++;
  4744. }
  4745. }
  4746. if (spaceCount == text.length) {
  4747. text = @"";
  4748. }
  4749. return text;
  4750. }
  4751. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4752. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4753. NSData *data = [NSData dataWithContentsOfFile:path];
  4754. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4755. return ret;
  4756. }
  4757. + (NSString *)textFileName:(NSString *)name {
  4758. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4759. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4760. if (!text) {
  4761. text = @"";
  4762. }
  4763. return text;
  4764. }
  4765. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4766. return [[dic objectForKey:key] mutableCopy];
  4767. }
  4768. + (id)translateSingleQuote:(NSString *)string {
  4769. if ([string isKindOfClass:[NSString class]])
  4770. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4771. return string;
  4772. }
  4773. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4774. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4775. }
  4776. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4777. NSString* ret= nil;
  4778. NSString *sqlQuery = nil;
  4779. // 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
  4780. 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];
  4781. sqlite3_stmt * statement;
  4782. // int count=0;
  4783. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4784. {
  4785. if (sqlite3_step(statement) == SQLITE_ROW)
  4786. {
  4787. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4788. if(imgurl==nil)
  4789. imgurl="";
  4790. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4791. ret=nsimgurl;
  4792. }
  4793. sqlite3_finalize(statement);
  4794. }
  4795. else
  4796. {
  4797. [ret setValue:@"8" forKey:@"result"];
  4798. }
  4799. // [iSalesDB close_db:db];
  4800. // DebugLog(@"data string: %@",ret );
  4801. return ret;
  4802. }
  4803. #pragma mark contact Advanced search
  4804. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4805. {
  4806. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4807. return [RAConvertor dict2data:contactAdvanceDic];
  4808. }
  4809. #pragma mark create new contact
  4810. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4811. assert(params[@"user"]!=nil);
  4812. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4813. NSData *data = [NSData dataWithContentsOfFile:path];
  4814. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4815. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4816. NSString *countryCode = nil;
  4817. NSString *countryCode_id = nil;
  4818. NSString *stateCode = nil;
  4819. NSString *city = nil;
  4820. NSString *zipCode = nil;
  4821. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4822. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4823. NSString *code_id = params[@"country"];
  4824. countryCode_id = code_id;
  4825. countryCode = [self countryCodeByid:code_id];
  4826. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4827. NSString *zip_code = params[@"zipcode"];
  4828. // 剔除全部为空格
  4829. int spaceCount = 0;
  4830. for (int i = 0; i < zip_code.length; i++) {
  4831. if ([zip_code characterAtIndex:i] == ' ') {
  4832. spaceCount++;
  4833. }
  4834. }
  4835. if (spaceCount == zip_code.length) {
  4836. zip_code = @"";
  4837. }
  4838. zipCode = zip_code;
  4839. if (zipCode.length > 0) {
  4840. countryCode_id = params[@"country"];
  4841. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4842. countryCode = [dic valueForKey:@"country_code"];
  4843. if (!countryCode) {
  4844. // countryCode = @"US";
  4845. NSString *code_id = params[@"country"];
  4846. countryCode = [self countryCodeByid:code_id];
  4847. }
  4848. stateCode = [dic valueForKey:@"state_code"];
  4849. if (!stateCode.length) {
  4850. stateCode = params[@"state"];
  4851. }
  4852. city = [dic valueForKey:@"city"];
  4853. if (!city.length) {
  4854. city = params[@"city"];
  4855. }
  4856. // zip code
  4857. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4858. [zipDic setValue:zipCode forKey:@"value"];
  4859. [section_0 setValue:zipDic forKey:@"item_11"];
  4860. } else {
  4861. NSString *code_id = params[@"country"];
  4862. countryCode = [self countryCodeByid:code_id];
  4863. stateCode = params[@"state"];
  4864. city = params[@"city"];
  4865. }
  4866. }
  4867. } else {
  4868. // default: US United States
  4869. countryCode = @"US";
  4870. countryCode_id = @"228";
  4871. }
  4872. // country
  4873. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4874. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4875. // state
  4876. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4877. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4878. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4879. // city
  4880. if (city) {
  4881. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4882. [cityDic setValue:city forKey:@"value"];
  4883. [section_0 setValue:cityDic forKey:@"item_13"];
  4884. }
  4885. // price type
  4886. NSDictionary *priceDic = [self offline_getPrice];
  4887. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4888. // contact type
  4889. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4890. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4891. // Sales Rep
  4892. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  4893. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4894. [ret setValue:section_0 forKey:@"section_0"];
  4895. return [RAConvertor dict2data:ret];
  4896. }
  4897. #pragma mark save
  4898. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4899. NSString *addr = nil;
  4900. NSString *contact_name = nil;
  4901. NSString *companyName = [params objectForKey:@"company"];
  4902. if (companyName) {
  4903. companyName = [AESCrypt fastencrypt:companyName];
  4904. } else {
  4905. companyName = @"";
  4906. }
  4907. DebugLog(@"company");
  4908. companyName = [self translateSingleQuote:companyName];
  4909. NSString *addr1 = [params objectForKey:@"address"];
  4910. NSString *addr2 = [params objectForKey:@"address2"];
  4911. NSString *addr3 = [params objectForKey:@"address_3"];
  4912. NSString *addr4 = [params objectForKey:@"address_4"];
  4913. if (!addr2) {
  4914. addr2 = @"";
  4915. }
  4916. if (!addr3) {
  4917. addr3 = @"";
  4918. }
  4919. if (!addr4) {
  4920. addr4 = @"";
  4921. }
  4922. if (!addr1) {
  4923. addr1 = @"";
  4924. }
  4925. DebugLog(@"addr");
  4926. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4927. addr = [AESCrypt fastencrypt:addr];
  4928. addr = [self translateSingleQuote:addr];
  4929. if (addr1 && ![addr1 isEqualToString:@""]) {
  4930. addr1 = [AESCrypt fastencrypt:addr1];
  4931. }
  4932. addr1 = [self translateSingleQuote:addr1];
  4933. addr2 = [self translateSingleQuote:addr2];
  4934. addr3 = [self translateSingleQuote:addr3];
  4935. addr4 = [self translateSingleQuote:addr4];
  4936. NSString *country = [params objectForKey:@"country"];
  4937. if (country) {
  4938. country = [self countryNameByCountryCodeId:country];
  4939. } else {
  4940. country = @"";
  4941. }
  4942. DebugLog(@"country");
  4943. country = [self translateSingleQuote:country];
  4944. NSString *state = [params objectForKey:@"state"];
  4945. if (!state) {
  4946. state = @"";
  4947. }
  4948. DebugLog(@"state");
  4949. state = [self translateSingleQuote:state];
  4950. NSString *city = [params objectForKey:@"city"];
  4951. if (!city) {
  4952. city = @"";
  4953. }
  4954. city = [self translateSingleQuote:city];
  4955. NSString *zipcode = [params objectForKey:@"zipcode"];
  4956. if (!zipcode) {
  4957. zipcode = @"";
  4958. }
  4959. DebugLog(@"zip");
  4960. zipcode = [self translateSingleQuote:zipcode];
  4961. NSString *fistName = [params objectForKey:@"firstname"];
  4962. if (!fistName) {
  4963. fistName = @"";
  4964. }
  4965. NSString *lastName = [params objectForKey:@"lastname"];
  4966. if (!lastName) {
  4967. lastName = @"";
  4968. }
  4969. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  4970. DebugLog(@"contact_name");
  4971. contact_name = [self translateSingleQuote:contact_name];
  4972. fistName = [self translateSingleQuote:fistName];
  4973. lastName = [self translateSingleQuote:lastName];
  4974. NSString *phone = [params objectForKey:@"phone"];
  4975. if (phone) {
  4976. phone = [AESCrypt fastencrypt:phone];
  4977. } else {
  4978. phone = @"";
  4979. }
  4980. DebugLog(@"PHONE");
  4981. phone = [self translateSingleQuote:phone];
  4982. NSString *fax = [params objectForKey:@"fax"];
  4983. if (!fax) {
  4984. fax = @"";
  4985. }
  4986. DebugLog(@"FAX");
  4987. fax = [self translateSingleQuote:fax];
  4988. NSString *email = [params objectForKey:@"email"];
  4989. if (!email) {
  4990. email = @"";
  4991. }
  4992. DebugLog(@"EMAIL:%@",email);
  4993. email = [self translateSingleQuote:email];
  4994. NSString *notes = [params objectForKey:@"contact_notes"];
  4995. if (!notes) {
  4996. notes = @"";
  4997. }
  4998. DebugLog(@"NOTE:%@",notes);
  4999. notes = [self translateSingleQuote:notes];
  5000. NSString *price = [params objectForKey:@"price_name"];
  5001. if (price) {
  5002. price = [self priceNameByPriceId:price];
  5003. } else {
  5004. price = @"";
  5005. }
  5006. DebugLog(@"PRICE");
  5007. price = [self translateSingleQuote:price];
  5008. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5009. if (salesRep) {
  5010. salesRep = [self salesRepCodeById:salesRep];
  5011. } else {
  5012. salesRep = @"";
  5013. }
  5014. salesRep = [self translateSingleQuote:salesRep];
  5015. NSString *img = [params objectForKey:@"business_card"];
  5016. NSArray *array = [img componentsSeparatedByString:@","];
  5017. NSString *img_0 = array[0];
  5018. if (!img_0) {
  5019. img_0 = @"";
  5020. }
  5021. img_0 = [self translateSingleQuote:img_0];
  5022. NSString *img_1 = array[1];
  5023. if (!img_1) {
  5024. img_1 = @"";
  5025. }
  5026. img_1 = [self translateSingleQuote:img_1];
  5027. NSString *img_2 = array[2];
  5028. if (!img_2) {
  5029. img_2 = @"";
  5030. }
  5031. img_2 = [self translateSingleQuote:img_2];
  5032. NSString *contact_id = [NSUUID UUID].UUIDString;
  5033. NSString *contact_type = [params objectForKey:@"type_name"];
  5034. if (!contact_type) {
  5035. contact_type = @"";
  5036. }
  5037. contact_type = [self translateSingleQuote:contact_type];
  5038. // 判断更新时是否为customer
  5039. if (update) {
  5040. contact_id = [params objectForKey:@"contact_id"];
  5041. if (!contact_id) {
  5042. contact_id = @"";
  5043. }
  5044. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5045. __block int customer = 0;
  5046. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5047. customer = sqlite3_column_int(stmt, 0);
  5048. }];
  5049. isCustomer = customer ? YES : NO;
  5050. }
  5051. NSMutableDictionary *sync_dic = [params mutableCopy];
  5052. if (isCustomer) {
  5053. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5054. } else {
  5055. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5056. }
  5057. NSString *sync_data = nil;
  5058. NSString *sql = nil;
  5059. if (update){
  5060. contact_id = [params objectForKey:@"contact_id"];
  5061. if (!contact_id) {
  5062. contact_id = @"";
  5063. }
  5064. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5065. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5066. sync_data = [RAConvertor dict2string:sync_dic];
  5067. sync_data = [self translateSingleQuote:sync_data];
  5068. 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];
  5069. } else {
  5070. contact_id = [self translateSingleQuote:contact_id];
  5071. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5072. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5073. sync_data = [RAConvertor dict2string:sync_dic];
  5074. sync_data = [self translateSingleQuote:sync_data];
  5075. 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];
  5076. }
  5077. int result = [iSalesDB execSql:sql];
  5078. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5079. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5080. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5081. }
  5082. #pragma mark save new contact
  5083. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5084. {
  5085. return [self offline_saveContact:params update:NO isCustomer:YES];
  5086. }
  5087. #pragma mark edit contact
  5088. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5089. {
  5090. assert(params[@"user"]!=nil);
  5091. // {
  5092. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5093. // password = 123456;
  5094. // user = EvanK;
  5095. // }
  5096. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5097. NSData *data = [NSData dataWithContentsOfFile:path];
  5098. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5099. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5100. NSString *countryCode = nil;
  5101. NSString *countryCode_id = nil;
  5102. NSString *stateCode = nil;
  5103. /*------contact infor------*/
  5104. __block NSString *country = nil;
  5105. __block NSString *company_name = nil;
  5106. __block NSString *contact_id = params[@"contact_id"];
  5107. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5108. __block NSString *zipcode = nil;
  5109. __block NSString *state = nil; // state_code
  5110. __block NSString *city = nil; //
  5111. __block NSString *firt_name,*last_name;
  5112. __block NSString *phone,*fax,*email;
  5113. __block NSString *notes,*price_type,*sales_rep;
  5114. __block NSString *img_0,*img_1,*img_2;
  5115. __block NSString *contact_type;
  5116. contact_id = [self translateSingleQuote:contact_id];
  5117. 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];
  5118. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5119. country = [self textAtColumn:0 statement:stmt]; // country name
  5120. company_name = [self textAtColumn:1 statement:stmt];
  5121. addr_1 = [self textAtColumn:2 statement:stmt];
  5122. addr_2 = [self textAtColumn:3 statement:stmt];
  5123. addr_3 = [self textAtColumn:4 statement:stmt];
  5124. addr_4 = [self textAtColumn:5 statement:stmt];
  5125. zipcode = [self textAtColumn:6 statement:stmt];
  5126. state = [self textAtColumn:7 statement:stmt]; // state code
  5127. city = [self textAtColumn:8 statement:stmt];
  5128. firt_name = [self textAtColumn:9 statement:stmt];
  5129. last_name = [self textAtColumn:10 statement:stmt];
  5130. phone = [self textAtColumn:11 statement:stmt];
  5131. fax = [self textAtColumn:12 statement:stmt];
  5132. email = [self textAtColumn:13 statement:stmt];
  5133. notes = [self textAtColumn:14 statement:stmt];
  5134. price_type = [self textAtColumn:15 statement:stmt]; // name
  5135. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5136. img_0 = [self textAtColumn:17 statement:stmt];
  5137. img_1 = [self textAtColumn:18 statement:stmt];
  5138. img_2 = [self textAtColumn:19 statement:stmt];
  5139. contact_type = [self textAtColumn:20 statement:stmt];
  5140. }];
  5141. // decrypt
  5142. if (company_name) {
  5143. company_name = [AESCrypt fastdecrypt:company_name];
  5144. }
  5145. if (addr_1) {
  5146. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5147. }
  5148. if (phone) {
  5149. phone = [AESCrypt fastdecrypt:phone];
  5150. }
  5151. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5152. countryCode = [iSalesDB jk_queryText:countrySql];
  5153. stateCode = state;
  5154. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5155. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5156. NSString *code_id = params[@"country"];
  5157. countryCode_id = code_id;
  5158. countryCode = [self countryCodeByid:code_id];
  5159. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5160. NSString *zip_code = params[@"zipcode"];
  5161. // 剔除全部为空格
  5162. int spaceCount = 0;
  5163. for (int i = 0; i < zip_code.length; i++) {
  5164. if ([zip_code characterAtIndex:i] == ' ') {
  5165. spaceCount++;
  5166. }
  5167. }
  5168. if (spaceCount == zip_code.length) {
  5169. zip_code = @"";
  5170. }
  5171. if (zipcode.length > 0) {
  5172. zipcode = zip_code;
  5173. countryCode_id = params[@"country"];
  5174. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5175. countryCode = [dic valueForKey:@"country_code"];
  5176. if (!countryCode) {
  5177. NSString *code_id = params[@"country"];
  5178. countryCode = [self countryCodeByid:code_id];
  5179. }
  5180. stateCode = [dic valueForKey:@"state_code"];
  5181. if (!stateCode.length) {
  5182. stateCode = params[@"state"];
  5183. }
  5184. city = [dic valueForKey:@"city"];
  5185. if (!city.length) {
  5186. city = params[@"city"];
  5187. }
  5188. // zip code
  5189. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5190. [zipDic setValue:zipcode forKey:@"value"];
  5191. [section_0 setValue:zipDic forKey:@"item_8"];
  5192. } else {
  5193. NSString *code_id = params[@"country"];
  5194. countryCode = [self countryCodeByid:code_id];
  5195. stateCode = params[@"state"];
  5196. city = params[@"city"];
  5197. }
  5198. }
  5199. }
  5200. // 0 Country
  5201. // 1 Company Name
  5202. // 2 Contact ID
  5203. // 3 Picture
  5204. // 4 Address 1
  5205. // 5 Address 2
  5206. // 6 Address 3
  5207. // 7 Address 4
  5208. // 8 Zip Code
  5209. // 9 State/Province
  5210. // 10 City
  5211. // 11 Contact First Name
  5212. // 12 Contact Last Name
  5213. // 13 Phone
  5214. // 14 Fax
  5215. // 15 Email
  5216. // 16 Contact Notes
  5217. // 17 Price Type
  5218. // 18 Contact Type
  5219. // 19 Sales Rep
  5220. // country
  5221. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5222. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5223. // company
  5224. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5225. // contact_id
  5226. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5227. // picture
  5228. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5229. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5230. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5231. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5232. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5233. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5234. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5235. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5236. // addr 1 2 3 4
  5237. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5238. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5239. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5240. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5241. // zip code
  5242. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5243. // state
  5244. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5245. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5246. // city
  5247. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5248. // first last
  5249. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5250. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5251. // phone fax email
  5252. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5253. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5254. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5255. // notes
  5256. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5257. // price
  5258. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5259. for (NSString *key in priceDic.allKeys) {
  5260. if ([key containsString:@"val_"]) {
  5261. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5262. if ([dic[@"value"] isEqualToString:price_type]) {
  5263. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5264. [priceDic setValue:dic forKey:key];
  5265. }
  5266. }
  5267. }
  5268. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5269. // Contact Rep
  5270. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5271. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5272. // Sales Rep
  5273. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5274. for (NSString *key in repDic.allKeys) {
  5275. if ([key containsString:@"val_"]) {
  5276. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5277. NSString *value = dic[@"value"];
  5278. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5279. if (code && [code isEqualToString:sales_rep]) {
  5280. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5281. [repDic setValue:dic forKey:key];
  5282. }
  5283. }
  5284. }
  5285. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5286. [ret setValue:section_0 forKey:@"section_0"];
  5287. return [RAConvertor dict2data:ret];
  5288. }
  5289. #pragma mark save contact
  5290. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5291. {
  5292. return [self offline_saveContact:params update:YES isCustomer:YES];
  5293. }
  5294. #pragma mark category
  5295. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5296. if (ck) {
  5297. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5298. for (NSString *key in res.allKeys) {
  5299. if (![key isEqualToString:@"count"]) {
  5300. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5301. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5302. if ([val[@"value"] isEqualToString:ck]) {
  5303. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5304. }
  5305. [res setValue:val forKey:key];
  5306. }
  5307. }
  5308. [dic setValue:res forKey:valueKey];
  5309. }
  5310. }
  5311. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5312. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5313. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5314. NSString* category = [params valueForKey:@"category"];
  5315. if (!category || [category isEqualToString:@""]) {
  5316. category = @"%";
  5317. }
  5318. category = [self translateSingleQuote:category];
  5319. int limit = [[params valueForKey:@"limit"] intValue];
  5320. int offset = [[params valueForKey:@"offset"] intValue];
  5321. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5322. NSString *limit_str = @"";
  5323. if (limited) {
  5324. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5325. }
  5326. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5327. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5328. sqlite3 *db = [iSalesDB get_db];
  5329. // [iSalesDB AddExFunction:db];
  5330. int count;
  5331. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5332. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color 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 %@ ;",params[@"orderCode"], category,limit_str];
  5333. double price_min = 0;
  5334. double price_max = 0;
  5335. if ([params.allKeys containsObject:@"alert"]) {
  5336. // alert
  5337. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5338. NSString *alert = params[@"alert"];
  5339. if ([alert isEqualToString:@"Display All"]) {
  5340. alert = [NSString stringWithFormat:@""];
  5341. } else {
  5342. alert = [self translateSingleQuote:alert];
  5343. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5344. }
  5345. // available
  5346. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5347. NSString *available = params[@"available"];
  5348. NSString *available_condition;
  5349. if ([available isEqualToString:@"Display All"]) {
  5350. available_condition = @"";
  5351. } else if ([available isEqualToString:@"Available Now"]) {
  5352. available_condition = @"and availability > 0";
  5353. } else {
  5354. available_condition = @"and availability == 0";
  5355. }
  5356. // best seller
  5357. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5358. NSString *best_seller = @"";
  5359. NSString *order_best_seller = @"m.name asc";
  5360. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5361. best_seller = @"and best_seller > 0";
  5362. order_best_seller = @"m.best_seller desc,m.name asc";
  5363. }
  5364. // price
  5365. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5366. NSString *price = params[@"price"];
  5367. price_min = 0;
  5368. price_max = MAXFLOAT;
  5369. if (params[@"user"] && price != nil) {
  5370. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5371. NSMutableString *priceName = [NSMutableString string];
  5372. for (int i = 0; i < priceTypeArray.count; i++) {
  5373. NSString *pricetype = priceTypeArray[i];
  5374. pricetype = [self translateSingleQuote:pricetype];
  5375. if (i == 0) {
  5376. [priceName appendFormat:@"'%@'",pricetype];
  5377. } else {
  5378. [priceName appendFormat:@",'%@'",pricetype];
  5379. }
  5380. }
  5381. if ([price isEqualToString:@"Display All"]) {
  5382. price = [NSString stringWithFormat:@""];
  5383. } else if([price containsString:@"+"]){
  5384. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5385. price_min = [price doubleValue];
  5386. 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];
  5387. } else {
  5388. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5389. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5390. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5391. 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];
  5392. }
  5393. } else {
  5394. price = @"";
  5395. }
  5396. // sold_by_qty : Sold in quantities of %@
  5397. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5398. NSString *qty = params[@"sold_by_qty"];
  5399. if ([qty isEqualToString:@"Display All"]) {
  5400. qty = @"";
  5401. } else {
  5402. qty = [self translateSingleQuote:qty];
  5403. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5404. }
  5405. // model name;
  5406. NSString* modelname =params[@"modelName"];
  5407. if(modelname.length==0)
  5408. {
  5409. modelname=@"";
  5410. }
  5411. else
  5412. {
  5413. modelname = modelname.lowercaseString;
  5414. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5415. }
  5416. //modelDescription
  5417. NSString* modelDescription =params[@"modelDescription"];
  5418. if(modelDescription.length==0)
  5419. {
  5420. modelDescription=@"";
  5421. }
  5422. else
  5423. {
  5424. modelDescription = modelDescription.lowercaseString;
  5425. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5426. }
  5427. // cate
  5428. // category = [self translateSingleQuote:category];
  5429. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5430. // cate mutiple selection
  5431. NSString *category_id = params[@"category"];
  5432. NSMutableArray *cate_id_array = nil;
  5433. NSMutableString *cateWhere = [NSMutableString string];
  5434. if ([category_id isEqualToString:@""] || !category_id) {
  5435. [cateWhere appendString:@"1 = 1"];
  5436. } else {
  5437. if ([category_id containsString:@","]) {
  5438. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5439. } else {
  5440. cate_id_array = [@[category_id] mutableCopy];
  5441. }
  5442. [cateWhere appendString:@"("];
  5443. for (int i = 0; i < cate_id_array.count; i++) {
  5444. if (i == 0) {
  5445. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5446. } else {
  5447. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5448. }
  5449. }
  5450. [cateWhere appendString:@")"];
  5451. }
  5452. // where bestseller > 0 order by bestseller desc
  5453. // sql query: alert availability(int) best_seller(int) price qty
  5454. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color 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,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  5455. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5456. }
  5457. DebugLog(@"offline category where: %@",where);
  5458. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5459. if (!params[@"user"]) {
  5460. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5461. }
  5462. [ret setValue:filter forKey:@"filter"];
  5463. DebugLog(@"offline_category sql:%@",sqlQuery);
  5464. sqlite3_stmt * statement;
  5465. [ret setValue:@"2" forKey:@"result"];
  5466. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5467. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5468. // int count=0;
  5469. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5470. {
  5471. int i=0;
  5472. while (sqlite3_step(statement) == SQLITE_ROW)
  5473. {
  5474. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5475. char *name = (char*)sqlite3_column_text(statement, 0);
  5476. if(name==nil)
  5477. name="";
  5478. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5479. char *description = (char*)sqlite3_column_text(statement, 1);
  5480. if(description==nil)
  5481. description="";
  5482. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5483. int product_id = sqlite3_column_int(statement, 2);
  5484. int wid = sqlite3_column_int(statement, 3);
  5485. int closeout = sqlite3_column_int(statement, 4);
  5486. int cid = sqlite3_column_int(statement, 5);
  5487. int wisdelete = sqlite3_column_int(statement, 6);
  5488. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5489. int more_color = sqlite3_column_int(statement, 8);
  5490. // Defaul Category ID
  5491. __block NSString *categoryID = nil;
  5492. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5493. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5494. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5495. if(default_category==nil)
  5496. default_category="";
  5497. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5498. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5499. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5500. categoryID = nsdefault_category;
  5501. }];
  5502. if (!categoryID.length) {
  5503. NSString *cateIDs = params[@"category"];
  5504. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5505. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5506. for (NSString *cateID in requestCategoryArr) {
  5507. BOOL needBreak = NO;
  5508. for (NSString *itemCateIDBox in itemCategoryArr) {
  5509. if (itemCateIDBox.length > 4) {
  5510. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5511. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5512. if ([itemCategoryID isEqualToString:cateID]) {
  5513. needBreak = YES;
  5514. categoryID = itemCategoryID;
  5515. break;
  5516. }
  5517. }
  5518. }
  5519. if (needBreak) {
  5520. break;
  5521. }
  5522. }
  5523. }
  5524. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5525. if(wid !=0 && wisdelete != 1)
  5526. [item setValue:@"true" forKey:@"wish_exists"];
  5527. else
  5528. [item setValue:@"false" forKey:@"wish_exists"];
  5529. if(closeout==0)
  5530. [item setValue:@"false" forKey:@"is_closeout"];
  5531. else
  5532. [item setValue:@"true" forKey:@"is_closeout"];
  5533. if(cid==0)
  5534. [item setValue:@"false" forKey:@"cart_exists"];
  5535. else
  5536. [item setValue:@"true" forKey:@"cart_exists"];
  5537. if (more_color == 0) {
  5538. [item setObject:@(false) forKey:@"more_color"];
  5539. } else if (more_color == 1) {
  5540. [item setObject:@(true) forKey:@"more_color"];
  5541. }
  5542. [item addEntriesFromDictionary:imgjson];
  5543. // [item setValue:nsurl forKey:@"img"];
  5544. [item setValue:nsname forKey:@"name"];
  5545. [item setValue:nsdescription forKey:@"description"];
  5546. if (categoryID) {
  5547. [item setValue:categoryID forKey:@"item_category_id"];
  5548. }
  5549. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5550. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5551. i++;
  5552. }
  5553. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5554. [ret setObject:items forKey:@"items"];
  5555. sqlite3_finalize(statement);
  5556. } else {
  5557. DebugLog(@"nothing...");
  5558. }
  5559. DebugLog(@"count:%d",count);
  5560. [iSalesDB close_db:db];
  5561. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5562. return ret;
  5563. }
  5564. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5565. {
  5566. return [self categoryList:params limited:YES];
  5567. }
  5568. # pragma mark item search
  5569. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5570. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5571. // assert(params[@"order_code"]);
  5572. // params[@"user"]
  5573. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5574. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5575. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5576. // category
  5577. NSDictionary *category_menu = [self offline_category_menu];
  5578. [filter setValue:category_menu forKey:@"category"];
  5579. NSString* where= nil;
  5580. NSString* orderby= @"m.name";
  5581. if (!filterSearch) {
  5582. int covertype = [[params valueForKey:@"covertype"] intValue];
  5583. switch (covertype) {
  5584. case 0:
  5585. {
  5586. where=@"m.category like'%%#005#%%'";
  5587. break;
  5588. }
  5589. case 1:
  5590. {
  5591. where=@"m.alert like '%QS%'";
  5592. break;
  5593. }
  5594. case 2:
  5595. {
  5596. where=@"m.availability>0";
  5597. break;
  5598. }
  5599. case 3:
  5600. {
  5601. where=@"m.best_seller>0";
  5602. orderby=@"m.best_seller desc,m.name asc";
  5603. break;
  5604. }
  5605. default:
  5606. where=@"1=1";
  5607. break;
  5608. }
  5609. }
  5610. int limit = [[params valueForKey:@"limit"] intValue];
  5611. int offset = [[params valueForKey:@"offset"] intValue];
  5612. NSString *limit_str = @"";
  5613. if (limited) {
  5614. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5615. }
  5616. sqlite3 *db = [iSalesDB get_db];
  5617. // [iSalesDB AddExFunction:db];
  5618. int count;
  5619. NSString *sqlQuery = nil;
  5620. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5621. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 %@ %@;",params[@"orderCode"], where,orderby,limit_str];
  5622. double price_min = 0;
  5623. double price_max = 0;
  5624. if (filterSearch) {
  5625. // alert
  5626. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5627. NSString *alert = params[@"alert"];
  5628. if ([alert isEqualToString:@"Display All"]) {
  5629. alert = [NSString stringWithFormat:@""];
  5630. } else {
  5631. alert = [self translateSingleQuote:alert];
  5632. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5633. }
  5634. // available
  5635. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5636. NSString *available = params[@"available"];
  5637. NSString *available_condition;
  5638. if ([available isEqualToString:@"Display All"]) {
  5639. available_condition = @"";
  5640. } else if ([available isEqualToString:@"Available Now"]) {
  5641. available_condition = @"and availability > 0";
  5642. } else {
  5643. available_condition = @"and availability == 0";
  5644. }
  5645. // best seller
  5646. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5647. NSString *best_seller = @"";
  5648. NSString *order_best_seller = @"m.name asc";
  5649. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5650. best_seller = @"and best_seller > 0";
  5651. order_best_seller = @"m.best_seller desc,m.name asc";
  5652. }
  5653. // price
  5654. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5655. NSString *price = params[@"price"];
  5656. price_min = 0;
  5657. price_max = MAXFLOAT;
  5658. if (params[@"user"]) {
  5659. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5660. NSMutableString *priceName = [NSMutableString string];
  5661. for (int i = 0; i < priceTypeArray.count; i++) {
  5662. NSString *pricetype = priceTypeArray[i];
  5663. pricetype = [self translateSingleQuote:pricetype];
  5664. if (i == 0) {
  5665. [priceName appendFormat:@"'%@'",pricetype];
  5666. } else {
  5667. [priceName appendFormat:@",'%@'",pricetype];
  5668. }
  5669. }
  5670. if ([price isEqualToString:@"Display All"]) {
  5671. price = [NSString stringWithFormat:@""];
  5672. } else if([price containsString:@"+"]){
  5673. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5674. price_min = [price doubleValue];
  5675. 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];
  5676. } else {
  5677. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5678. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5679. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5680. 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];
  5681. }
  5682. } else {
  5683. price = @"";
  5684. }
  5685. // sold_by_qty : Sold in quantities of %@
  5686. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5687. NSString *qty = params[@"sold_by_qty"];
  5688. if ([qty isEqualToString:@"Display All"]) {
  5689. qty = @"";
  5690. } else {
  5691. qty = [self translateSingleQuote:qty];
  5692. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5693. }
  5694. // model name;
  5695. NSString* modelname =params[@"modelName"];
  5696. if(modelname.length==0)
  5697. {
  5698. modelname=@"";
  5699. }
  5700. else
  5701. {
  5702. modelname = modelname.lowercaseString;
  5703. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5704. }
  5705. //modelDescription
  5706. NSString* modelDescription =params[@"modelDescription"];
  5707. if(modelDescription.length==0)
  5708. {
  5709. modelDescription=@"";
  5710. }
  5711. else
  5712. {
  5713. modelDescription = modelDescription.lowercaseString;
  5714. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5715. }
  5716. //lower(description) like'%%%@%%'
  5717. // category
  5718. NSString *category_id = params[@"ctgId"];
  5719. NSMutableArray *cate_id_array = nil;
  5720. NSMutableString *cateWhere = [NSMutableString string];
  5721. if ([category_id isEqualToString:@""] || !category_id) {
  5722. [cateWhere appendString:@"1 = 1"];
  5723. } else {
  5724. if ([category_id containsString:@","]) {
  5725. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5726. } else {
  5727. cate_id_array = [@[category_id] mutableCopy];
  5728. }
  5729. /*-----------*/
  5730. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5731. [cateWhere appendString:@"("];
  5732. for (int i = 0; i < cate_id_array.count; i++) {
  5733. for (NSString *key0 in cateDic.allKeys) {
  5734. if ([key0 containsString:@"category_"]) {
  5735. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5736. for (NSString *key1 in category0.allKeys) {
  5737. if ([key1 containsString:@"category_"]) {
  5738. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5739. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5740. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5741. cate_id_array[i] = [category1 objectForKey:@"id"];
  5742. if (i == 0) {
  5743. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5744. } else {
  5745. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5746. }
  5747. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5748. [category0 setValue:category1 forKey:key1];
  5749. [cateDic setValue:category0 forKey:key0];
  5750. }
  5751. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5752. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5753. cate_id_array[i] = [category0 objectForKey:@"id"];
  5754. if (i == 0) {
  5755. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5756. } else {
  5757. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5758. }
  5759. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5760. [cateDic setValue:category0 forKey:key0];
  5761. }
  5762. }
  5763. }
  5764. }
  5765. }
  5766. }
  5767. [cateWhere appendString:@")"];
  5768. [filter setValue:cateDic forKey:@"category"];
  5769. }
  5770. // where bestseller > 0 order by bestseller desc
  5771. // sql query: alert availability(int) best_seller(int) price qty
  5772. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color 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,modelname,modelDescription,price,params[@"orderCode"],order_best_seller,limit_str];
  5773. // count
  5774. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  5775. }
  5776. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5777. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5778. if (!params[@"user"]) {
  5779. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5780. }
  5781. [ret setValue:filter forKey:@"filter"];
  5782. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5783. sqlite3_stmt * statement;
  5784. [ret setValue:@"2" forKey:@"result"];
  5785. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5786. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5787. // int count=0;
  5788. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5789. {
  5790. int i=0;
  5791. while (sqlite3_step(statement) == SQLITE_ROW)
  5792. {
  5793. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5794. char *name = (char*)sqlite3_column_text(statement, 0);
  5795. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5796. char *description = (char*)sqlite3_column_text(statement, 1);
  5797. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5798. int product_id = sqlite3_column_int(statement, 2);
  5799. int wid = sqlite3_column_int(statement, 3);
  5800. int closeout = sqlite3_column_int(statement, 4);
  5801. int cid = sqlite3_column_int(statement, 5);
  5802. int wisdelete = sqlite3_column_int(statement, 6);
  5803. int more_color = sqlite3_column_int(statement, 7);
  5804. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5805. if(wid !=0 && wisdelete != 1)
  5806. [item setValue:@"true" forKey:@"wish_exists"];
  5807. else
  5808. [item setValue:@"false" forKey:@"wish_exists"];
  5809. if(closeout==0)
  5810. [item setValue:@"false" forKey:@"is_closeout"];
  5811. else
  5812. [item setValue:@"true" forKey:@"is_closeout"];
  5813. if(cid==0)
  5814. [item setValue:@"false" forKey:@"cart_exists"];
  5815. else
  5816. [item setValue:@"true" forKey:@"cart_exists"];
  5817. if (more_color == 0) {
  5818. [item setObject:@(false) forKey:@"more_color"];
  5819. } else if (more_color == 1) {
  5820. [item setObject:@(true) forKey:@"more_color"];
  5821. }
  5822. [item addEntriesFromDictionary:imgjson];
  5823. // [item setValue:nsurl forKey:@"img"];
  5824. [item setValue:nsname forKey:@"fash_name"];
  5825. [item setValue:nsdescription forKey:@"description"];
  5826. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5827. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5828. i++;
  5829. }
  5830. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5831. [ret setObject:items forKey:@"items"];
  5832. sqlite3_finalize(statement);
  5833. }
  5834. [iSalesDB close_db:db];
  5835. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5836. return ret;
  5837. }
  5838. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5839. {
  5840. return [self itemsearch:params limited:YES];
  5841. }
  5842. #pragma mark order detail
  5843. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5844. if (str1.length == 0) {
  5845. str1 = @"&nbsp";
  5846. }
  5847. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5848. return str;
  5849. }
  5850. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5851. {
  5852. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5853. [fromformatter setDateFormat:from];
  5854. NSDate *date = [fromformatter dateFromString:datetime];
  5855. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5856. [toformatter setDateFormat:to];
  5857. NSString * ret = [toformatter stringFromDate:date];
  5858. return ret;
  5859. }
  5860. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5861. // 把毫秒去掉
  5862. if ([dateTime containsString:@"."]) {
  5863. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5864. }
  5865. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5866. formatter.dateFormat = formate;
  5867. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5868. NSDate *date = [formatter dateFromString:dateTime];
  5869. formatter.dateFormat = newFormate;
  5870. NSString *result = [formatter stringFromDate:date];
  5871. return result ? result : @"";
  5872. }
  5873. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5874. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5875. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5876. }
  5877. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5878. {
  5879. assert(params[@"mode"]!=nil);
  5880. assert(params[@"user"]!=nil);
  5881. DebugLog(@"offline oderdetail params: %@",params);
  5882. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5883. int orderId = [params[@"orderId"] intValue];
  5884. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5885. // decrypt card number and card security code
  5886. // 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 ];
  5887. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country,o.must_call 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,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5888. sqlite3 *db = [iSalesDB get_db];
  5889. sqlite3_stmt * statement;
  5890. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5891. NSString *nssoid = @"";
  5892. NSString* orderinfo = @"";
  5893. NSString *moreInfo = @"";
  5894. double handlingFee = 0;
  5895. double payments_and_credist = 0;
  5896. double totalPrice = 0;
  5897. double shippingFee = 0;
  5898. double lift_gate = 0;
  5899. NSString *customer_contact = @"";
  5900. NSString *customer_email = @"";
  5901. NSString *customer_fax = @"";
  5902. NSString *customer_phone = @"";
  5903. NSString *customer_city = @"";
  5904. NSString *customer_state = @"";
  5905. NSString *customer_zipcode = @"";
  5906. NSString *customer_country = @"";
  5907. BOOL must_call = NO;
  5908. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5909. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5910. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5911. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5912. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5913. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5914. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5915. {
  5916. if (sqlite3_step(statement) == SQLITE_ROW)
  5917. {
  5918. // int order_id = sqlite3_column_int(statement, 0);
  5919. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5920. // ret[@"sign_url"] = sign_url;
  5921. section_1 = @{
  5922. @"data":sign_url,
  5923. @"title":@"Signature",
  5924. @"type":@"sign_url"
  5925. }.mutableCopy;
  5926. [ret setObject:section_1 forKey:@"section_1"];
  5927. customer_contact = [self textAtColumn:52 statement:statement];
  5928. customer_email = [self textAtColumn:53 statement:statement];
  5929. customer_phone = [self textAtColumn:54 statement:statement];
  5930. customer_fax = [self textAtColumn:55 statement:statement];
  5931. customer_city = [self textAtColumn:60 statement:statement];
  5932. customer_state = [self textAtColumn:61 statement:statement];
  5933. customer_zipcode = [self textAtColumn:62 statement:statement];
  5934. customer_country = [self textAtColumn:63 statement:statement];
  5935. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  5936. int offline_edit=sqlite3_column_int(statement, 56);
  5937. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5938. char *soid = (char*)sqlite3_column_text(statement, 1);
  5939. if(soid==nil)
  5940. soid= "";
  5941. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5942. // so#
  5943. ret[@"so#"] = nssoid;
  5944. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5945. if(poNumber==nil)
  5946. poNumber= "";
  5947. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5948. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5949. if(create_time==nil)
  5950. create_time= "";
  5951. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5952. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5953. int status = sqlite3_column_int(statement, 4);
  5954. int erpStatus = sqlite3_column_int(statement, 49);
  5955. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5956. // status
  5957. if (status > 1 && status != 3) {
  5958. status = erpStatus;
  5959. } else if (status == 3) {
  5960. status = 15;
  5961. }
  5962. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5963. ret[@"order_status"] = nsstatus;
  5964. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5965. if(company_name==nil)
  5966. company_name= "";
  5967. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5968. // company name
  5969. ret[@"company_name"] = nscompany_name;
  5970. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5971. if(customer_contact==nil)
  5972. customer_contact= "";
  5973. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5974. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5975. if(addr_1==nil)
  5976. addr_1="";
  5977. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5978. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5979. if(addr_2==nil)
  5980. addr_2="";
  5981. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5982. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5983. if(addr_3==nil)
  5984. addr_3="";
  5985. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5986. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5987. if(addr_4==nil)
  5988. addr_4="";
  5989. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5990. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5991. [arr_addr addObject:nsaddr_1];
  5992. [arr_addr addObject:nsaddr_2];
  5993. [arr_addr addObject:nsaddr_3];
  5994. [arr_addr addObject:nsaddr_4];
  5995. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5996. [arr_addr addObject:customer_state];
  5997. [arr_addr addObject:customer_zipcode];
  5998. [arr_addr addObject:customer_country];
  5999. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6000. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6001. char *logist = (char*)sqlite3_column_text(statement, 11);
  6002. if(logist==nil)
  6003. logist= "";
  6004. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6005. if (/*status == -11 || */status == 10 || status == 11) {
  6006. nslogist = [self textAtColumn:59 statement:statement];
  6007. };
  6008. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6009. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6010. shipping = @"Shipping To Be Quoted";
  6011. } else {
  6012. shippingFee = sqlite3_column_double(statement, 12);
  6013. }
  6014. // Shipping
  6015. // ret[@"Shipping"] = shipping;
  6016. NSDictionary *shipping_item = @{
  6017. @"title":@"Shipping",
  6018. @"value":shipping
  6019. };
  6020. [price_data setObject:shipping_item forKey:@"item_1"];
  6021. int have_lift_gate = sqlite3_column_int(statement, 17);
  6022. lift_gate = sqlite3_column_double(statement, 13);
  6023. // Liftgate Fee(No loading dock)
  6024. if (!have_lift_gate) {
  6025. lift_gate = 0;
  6026. }
  6027. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6028. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6029. if (sqlite3_column_int(statement, 57)) {
  6030. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6031. liftgate_value = @"Shipping To Be Quoted";
  6032. }
  6033. NSDictionary *liftgate_item = @{
  6034. @"title":@"Liftgate Fee(No loading dock)",
  6035. @"value":liftgate_value
  6036. };
  6037. [price_data setObject:liftgate_item forKey:@"item_2"];
  6038. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6039. // [ret removeObjectForKey:@"Shipping"];
  6040. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6041. // }
  6042. //
  6043. // if (have_lift_gate) {
  6044. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6045. // }
  6046. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6047. if(general_notes==nil)
  6048. general_notes= "";
  6049. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6050. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6051. if(internal_notes==nil)
  6052. internal_notes= "";
  6053. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6054. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6055. if(payment_type==nil)
  6056. payment_type= "";
  6057. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6058. // order info
  6059. orderinfo = [self textFileName:@"order_info.html"];
  6060. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6061. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6062. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6063. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6064. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6065. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6066. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6067. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6068. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6069. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6070. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6071. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6072. NSString *payment = nil;
  6073. // id -> show
  6074. __block NSString *show_pay_type = nspayment_type;
  6075. [iSalesDB jk_query:[NSString stringWithFormat:@"select pay_type from payment_type where type_id = '%@';",nspayment_type] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6076. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6077. if (show_typ_ch != NULL) {
  6078. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6079. }
  6080. }];
  6081. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6082. {
  6083. payment = [self textFileName:@"creditcardpayment.html"];
  6084. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6085. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6086. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6087. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6088. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6089. NSString *card_type = [self textAtColumn:42 statement:statement];
  6090. if (card_type.length > 0) { // 显示星号
  6091. card_type = @"****";
  6092. }
  6093. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6094. if (card_number.length > 0 && card_number.length > 4) {
  6095. for (int i = 0; i < card_number.length - 4; i++) {
  6096. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6097. }
  6098. } else {
  6099. card_number = @"";
  6100. }
  6101. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6102. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6103. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6104. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6105. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6106. card_expiration = @"****";
  6107. }
  6108. NSString *card_city = [self textAtColumn:46 statement:statement];
  6109. NSString *card_state = [self textAtColumn:47 statement:statement];
  6110. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6111. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6112. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6113. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6114. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6115. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6116. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6117. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6118. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6119. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6120. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6121. }
  6122. else
  6123. {
  6124. payment=[self textFileName:@"normalpayment.html"];
  6125. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6126. }
  6127. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6128. ret[@"result"]= [NSNumber numberWithInt:2];
  6129. // more info
  6130. moreInfo = [self textFileName:@"more_info.html"];
  6131. /*****ship to******/
  6132. // ShipToCompany_or_&nbsp
  6133. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6134. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6135. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6136. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6137. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6138. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6139. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6140. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6141. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6142. /*****ship from******/
  6143. // ShipFromCompany_or_&nbsp
  6144. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6145. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6146. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6147. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6148. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6149. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6150. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6151. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6152. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6153. /*****freight to******/
  6154. // FreightBillToCompany_or_&nbsp
  6155. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6156. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6157. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6158. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6159. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6160. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6161. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6162. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6163. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6164. /*****merchandise to******/
  6165. // MerchandiseBillToCompany_or_&nbsp
  6166. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6167. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6168. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6169. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6170. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6171. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6172. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6173. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6174. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6175. /*****return to******/
  6176. // ReturnToCompany_or_&nbsp
  6177. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6178. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6179. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6180. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6181. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6182. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6183. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6184. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6185. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6186. //
  6187. // DebugLog(@"more info : %@",moreInfo);
  6188. // handling fee
  6189. handlingFee = sqlite3_column_double(statement, 33);
  6190. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6191. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6192. if (sqlite3_column_int(statement, 58)) {
  6193. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6194. handling_fee_value = @"Shipping To Be Quoted";
  6195. }
  6196. NSDictionary *handling_fee_item = @{
  6197. @"title":@"Handling Fee",
  6198. @"value":handling_fee_value
  6199. };
  6200. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6201. //
  6202. // customer info
  6203. customerID = [self textAtColumn:36 statement:statement];
  6204. // mode
  6205. ret[@"mode"] = params[@"mode"];
  6206. // model_count
  6207. ret[@"model_count"] = @(0);
  6208. }
  6209. sqlite3_finalize(statement);
  6210. }
  6211. [iSalesDB close_db:db];
  6212. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6213. // "customer_email" = "Shui Hu";
  6214. // "customer_fax" = "";
  6215. // "customer_first_name" = F;
  6216. // "customer_last_name" = L;
  6217. // "customer_name" = ",da He Xiang Dong Liu A";
  6218. // "customer_phone" = "Hey Xuan Feng";
  6219. contactInfo[@"customer_phone"] = customer_phone;
  6220. contactInfo[@"customer_fax"] = customer_fax;
  6221. contactInfo[@"customer_email"] = customer_email;
  6222. NSString *first_name = @"";
  6223. NSString *last_name = @"";
  6224. if ([customer_contact isEqualToString:@""]) {
  6225. } else if ([customer_contact containsString:@" "]) {
  6226. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6227. first_name = [customer_contact substringToIndex:first_space_index];
  6228. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6229. }
  6230. contactInfo[@"customer_first_name"] = first_name;
  6231. contactInfo[@"customer_last_name"] = last_name;
  6232. ret[@"customerInfo"] = contactInfo;
  6233. // models
  6234. if (nssoid) {
  6235. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6236. __block double TotalCuft = 0;
  6237. __block double TotalWeight = 0;
  6238. __block int TotalCarton = 0;
  6239. __block double allItemPrice = 0;
  6240. 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];
  6241. sqlite3 *db1 = [iSalesDB get_db];
  6242. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6243. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6244. int product_id = sqlite3_column_int(stmt, 0);
  6245. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6246. int item_id = sqlite3_column_int(stmt, 7);
  6247. NSString* Price=nil;
  6248. if(str_price==nil)
  6249. {
  6250. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6251. if(price==nil)
  6252. Price=@"No Price.";
  6253. else
  6254. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6255. }
  6256. else
  6257. {
  6258. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6259. }
  6260. double discount = sqlite3_column_double(stmt, 2);
  6261. int item_count = sqlite3_column_int(stmt, 3);
  6262. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6263. NSString *nsline_note=nil;
  6264. if(line_note!=nil)
  6265. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6266. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6267. // NSString *nsname = nil;
  6268. // if(name!=nil)
  6269. // nsname= [[NSString alloc]initWithUTF8String:name];
  6270. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6271. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6272. // NSString *nsdescription=nil;
  6273. // if(description!=nil)
  6274. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6275. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6276. // int stockUom = sqlite3_column_int(stmt, 8);
  6277. // int _id = sqlite3_column_int(stmt, 9);
  6278. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6279. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6280. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6281. int carton=[bsubtotaljson[@"carton"] intValue];
  6282. TotalCuft += cuft;
  6283. TotalWeight += weight;
  6284. TotalCarton += carton;
  6285. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6286. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6287. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6288. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6289. itemjson[@"note"]=nsline_note;
  6290. itemjson[@"origin_price"] = Price;
  6291. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6292. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6293. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6294. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6295. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6296. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6297. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6298. if(itemjson[@"combine"] != nil)
  6299. {
  6300. // int citem=0;
  6301. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6302. for(int bc=0;bc<bcount;bc++)
  6303. {
  6304. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6305. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6306. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6307. subTotal += uprice * modulus * item_count;
  6308. }
  6309. }
  6310. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6311. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6312. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6313. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6314. itemjson[@"type"] = @"order_item";
  6315. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6316. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6317. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6318. allItemPrice += subTotal;
  6319. }];
  6320. section_3[@"data"] = model_data;
  6321. section_3[@"type"] = @"sub_order";
  6322. section_3[@"title"] = @"Models";
  6323. section_3[@"switch"] = @(false);
  6324. ret[@"section_3"] = section_3;
  6325. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6326. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6327. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6328. // payments/Credits
  6329. // payments_and_credist = sqlite3_column_double(statement, 34);
  6330. payments_and_credist = allItemPrice;
  6331. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6332. NSDictionary *sub_total_item = @{
  6333. @"title":@"Sub-Total",
  6334. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6335. };
  6336. [price_data setObject:sub_total_item forKey:@"item_0"];
  6337. // // total
  6338. // totalPrice = sqlite3_column_double(statement, 35);
  6339. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6340. } else {
  6341. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6342. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6343. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6344. // payments/Credits
  6345. payments_and_credist = 0;
  6346. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6347. NSDictionary *sub_total_item = @{
  6348. @"title":@"Sub-Total",
  6349. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6350. };
  6351. [price_data setObject:sub_total_item forKey:@"item_0"];
  6352. }
  6353. // total
  6354. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6355. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6356. NSDictionary *total_item = @{
  6357. @"title":@"Total",
  6358. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6359. };
  6360. [price_data setObject:total_item forKey:@"item_4"];
  6361. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6362. section_4 = @{
  6363. @"data":price_data,
  6364. @"title":@"Price Info",
  6365. @"type":@"price_info"
  6366. }.mutableCopy;
  6367. ret[@"section_4"] = section_4;
  6368. // ret[@"order_info"]= orderinfo;
  6369. section_0 = @{
  6370. @"data":orderinfo,
  6371. @"title":@"Order Info",
  6372. @"type":@"order_info"
  6373. }.mutableCopy;
  6374. ret[@"section_0"] = section_0;
  6375. // ret[@"more_order_info"] = moreInfo;
  6376. section_2 = @{
  6377. @"data":moreInfo,
  6378. @"title":@"More Info",
  6379. @"type":@"more_order_info"
  6380. }.mutableCopy;
  6381. ret[@"section_2"] = section_2;
  6382. ret[@"count"] = @(5);
  6383. return [RAConvertor dict2data:ret];
  6384. }
  6385. #pragma mark order list
  6386. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6387. NSString *nsstatus = @"";
  6388. switch (status) {
  6389. case 0:
  6390. {
  6391. nsstatus=@"Temp Order";
  6392. break;
  6393. }
  6394. case 1:
  6395. {
  6396. nsstatus=@"Saved Order";
  6397. break;
  6398. }
  6399. case 2: {
  6400. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6401. break;
  6402. }
  6403. case 3:
  6404. case 15:
  6405. {
  6406. nsstatus=@"Cancelled";
  6407. break;
  6408. }
  6409. case 10:
  6410. {
  6411. nsstatus=@"Quote Submitted";
  6412. break;
  6413. }
  6414. case 11:
  6415. {
  6416. nsstatus=@"Sales Order Submitted";
  6417. break;
  6418. }
  6419. case 12:
  6420. {
  6421. nsstatus=@"Processing";
  6422. break;
  6423. }
  6424. case 13:
  6425. {
  6426. nsstatus=@"Shipped";
  6427. break;
  6428. }
  6429. case 14:
  6430. {
  6431. nsstatus=@"Closed";
  6432. break;
  6433. }
  6434. case -11:
  6435. {
  6436. nsstatus = @"Ready For Submit";
  6437. break;
  6438. }
  6439. default:
  6440. break;
  6441. }
  6442. return nsstatus;
  6443. }
  6444. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  6445. {
  6446. double total = 0;
  6447. __block double payments_and_credist = 0;
  6448. __block double allItemPrice = 0;
  6449. // sqlite3 *db1 = [iSalesDB get_db];
  6450. if (so_id) {
  6451. // 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];
  6452. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6453. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6454. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6455. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6456. int product_id = sqlite3_column_int(stmt, 0);
  6457. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6458. int discount = sqlite3_column_int(stmt, 2);
  6459. int item_count = sqlite3_column_int(stmt, 3);
  6460. // int item_id = sqlite3_column_int(stmt, 7);
  6461. int item_id = sqlite3_column_int(stmt, 4);
  6462. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6463. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6464. NSString* Price=nil;
  6465. if(str_price==nil)
  6466. {
  6467. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  6468. if(price==nil)
  6469. Price=@"No Price.";
  6470. else
  6471. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6472. }
  6473. else
  6474. {
  6475. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6476. }
  6477. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  6478. itemjson[@"The unit price"]=Price;
  6479. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6480. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6481. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6482. if(itemjson[@"combine"] != nil)
  6483. {
  6484. // int citem=0;
  6485. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6486. for(int bc=0;bc<bcount;bc++)
  6487. {
  6488. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6489. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6490. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6491. subTotal += uprice * modulus * item_count;
  6492. }
  6493. }
  6494. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6495. allItemPrice += subTotal;
  6496. }];
  6497. payments_and_credist = allItemPrice;
  6498. } else {
  6499. // payments/Credits
  6500. payments_and_credist = 0;
  6501. }
  6502. // lift_gate handlingFee shippingFee
  6503. __block double lift_gate = 0;
  6504. __block double handlingFee = 0;
  6505. __block double shippingFee = 0;
  6506. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6507. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6508. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6509. if (have_lift_gate) {
  6510. lift_gate = sqlite3_column_double(stmt, 1);
  6511. }
  6512. handlingFee = sqlite3_column_double(stmt, 2);
  6513. shippingFee = sqlite3_column_double(stmt, 3);
  6514. }];
  6515. // total
  6516. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6517. if (close) {
  6518. [iSalesDB close_db:db1];
  6519. }
  6520. return total;
  6521. }
  6522. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6523. {
  6524. assert(params[@"user"]!=nil);
  6525. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6526. int limit = [[params valueForKey:@"limit"] intValue];
  6527. int offset = [[params valueForKey:@"offset"] intValue];
  6528. NSString* keyword = [params valueForKey:@"keyWord"];
  6529. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6530. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6531. NSString* where=@"1 = 1";
  6532. if(keyword.length>0)
  6533. 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]];
  6534. if (orderStatus.length > 0) {
  6535. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6536. if (order_status_array.count == 1) {
  6537. int status_value = [[order_status_array firstObject] intValue];
  6538. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6539. if (status_value == 15 || status_value == 3) {
  6540. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6541. } else {
  6542. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6543. }
  6544. } else {
  6545. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6546. }
  6547. } else if (order_status_array.count > 1) {
  6548. for (int i = 0; i < order_status_array.count;i++) {
  6549. NSString *status = order_status_array[i];
  6550. NSString *condition = @" or";
  6551. if (i == 0) {
  6552. condition = @" and (";
  6553. }
  6554. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6555. int status_value = [status intValue];
  6556. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6557. if (status_value == 15 || status_value == 3) {
  6558. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6559. } else {
  6560. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6561. }
  6562. } else {
  6563. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6564. }
  6565. }
  6566. where = [where stringByAppendingString:@" )"];
  6567. }
  6568. }
  6569. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6570. // DebugLog(@"order list sql: %@",sqlQuery);
  6571. sqlite3 *db = [iSalesDB get_db];
  6572. sqlite3_stmt * statement;
  6573. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6574. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6575. {
  6576. int count=0;
  6577. while (sqlite3_step(statement) == SQLITE_ROW)
  6578. {
  6579. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6580. int order_id = sqlite3_column_double(statement, 0);
  6581. char *soid = (char*)sqlite3_column_text(statement, 1);
  6582. if(soid==nil)
  6583. soid= "";
  6584. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6585. int status = sqlite3_column_double(statement, 2);
  6586. int erpStatus = sqlite3_column_double(statement, 9);
  6587. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6588. if(sales_rep==nil)
  6589. sales_rep= "";
  6590. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6591. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6592. if(create_by==nil)
  6593. create_by= "";
  6594. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6595. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6596. if(company_name==nil)
  6597. company_name= "";
  6598. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6599. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6600. if(create_time==nil)
  6601. create_time= "";
  6602. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6603. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6604. // double total_price = sqlite3_column_double(statement, 7);
  6605. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  6606. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6607. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6608. int offline_edit = sqlite3_column_int(statement, 10);
  6609. // ": "JH",
  6610. // "": "$8307.00",
  6611. // "": "MOB1608050001",
  6612. // "": "ArpithaT",
  6613. // "": "1st Stage Property Transformations",
  6614. // "": "JANICE SUTTON",
  6615. // "": 2255,
  6616. // "": "08/02/2016 09:49:18",
  6617. // "": 1,
  6618. // "": "Saved Order"
  6619. // "": "1470384050483",
  6620. // "model_count": "6 / 28"
  6621. item[@"sales_rep"]= nssales_rep;
  6622. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6623. item[@"so#"]= nssoid;
  6624. item[@"create_by"]= nscreate_by;
  6625. item[@"customer_name"]= nscompany_name;
  6626. item[@"customer_contact"] = customer_contact;
  6627. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6628. item[@"purchase_time"]= nscreate_time;
  6629. int statusCode = status;
  6630. if (statusCode == 2) {
  6631. statusCode = erpStatus;
  6632. } else if (statusCode == 3) {
  6633. statusCode = 15;
  6634. }
  6635. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6636. item[@"order_status"]= nsstatus;
  6637. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6638. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6639. // item[@"model_count"]
  6640. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6641. count++;
  6642. }
  6643. ret[@"count"]= [NSNumber numberWithInt:count];
  6644. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6645. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6646. ret[@"result"]= [NSNumber numberWithInt:2];
  6647. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6648. ret[@"time_zone"] = @"PST";
  6649. sqlite3_finalize(statement);
  6650. }
  6651. 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];
  6652. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6653. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6654. [iSalesDB close_db:db];
  6655. return [RAConvertor dict2data:ret];
  6656. }
  6657. #pragma mark update gnotes
  6658. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6659. {
  6660. DebugLog(@"params: %@",params);
  6661. // comments = Meyoyoyoyoyoyoy;
  6662. // orderCode = MOB1608110001;
  6663. // password = 123456;
  6664. // user = EvanK;
  6665. 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]];
  6666. int ret = [iSalesDB execSql:sql];
  6667. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6668. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6669. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6670. // [dic setValue:@"160409" forKey:@"min_ver"];
  6671. return [RAConvertor dict2data:dic];
  6672. }
  6673. #pragma mark move to wishlist
  6674. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6675. {
  6676. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6677. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6678. _id = [NSString stringWithFormat:@"(%@)",_id];
  6679. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6680. sqlite3 *db = [iSalesDB get_db];
  6681. __block NSString *product_id = @"";
  6682. __block NSString *item_count_str = @"";
  6683. // __block NSString *item_id = nil;
  6684. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6685. // product_id = [self textAtColumn:0 statement:stmt];
  6686. int item_count = sqlite3_column_int(stmt, 1);
  6687. // item_id = [self textAtColumn:2 statement:stmt];
  6688. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6689. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6690. if (p_id.length) {
  6691. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6692. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6693. }
  6694. }];
  6695. [iSalesDB close_db:db];
  6696. // 去除第一个,
  6697. if (product_id.length > 1) {
  6698. product_id = [product_id substringFromIndex:1];
  6699. }
  6700. if (item_count_str.length > 1) {
  6701. item_count_str = [item_count_str substringFromIndex:1];
  6702. }
  6703. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6704. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6705. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6706. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6707. sqlite3 *db1 = [iSalesDB get_db];
  6708. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6709. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6710. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6711. // 删除
  6712. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6713. int ret = [iSalesDB execSql:deleteSql db:db1];
  6714. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6715. [iSalesDB close_db:db1];
  6716. return [RAConvertor dict2data:dic];
  6717. }
  6718. #pragma mark cart delete
  6719. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6720. {
  6721. // cartItemId = 548;
  6722. // orderCode = MOB1608110001;
  6723. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6724. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6725. _id = [NSString stringWithFormat:@"(%@)",_id];
  6726. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6727. int ret = [iSalesDB execSql:sql];
  6728. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6729. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6730. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6731. // [dic setValue:@"160409" forKey:@"min_ver"];
  6732. return [RAConvertor dict2data:dic];
  6733. }
  6734. #pragma mark set price
  6735. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6736. {
  6737. DebugLog(@"cart set price params: %@",params);
  6738. // "cartitem_id" = 1;
  6739. // discount = "0.000000";
  6740. // "item_note" = "";
  6741. // price = "269.000000";
  6742. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6743. NSString *notes = [self valueInParams:params key:@"item_note"];
  6744. NSString *discount = [self valueInParams:params key:@"discount"];
  6745. NSString *price = [self valueInParams:params key:@"price"];
  6746. // bool badd_price_changed=false;
  6747. // sqlite3* db=[iSalesDB get_db];
  6748. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6749. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6750. // NSRange range;
  6751. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6752. //
  6753. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6754. // badd_price_changed=true;
  6755. // [iSalesDB close_db:db];
  6756. //
  6757. // if(badd_price_changed)
  6758. // {
  6759. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6760. // }
  6761. //
  6762. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6763. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6764. int ret = [iSalesDB execSql:sql];
  6765. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6766. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6767. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6768. // [dic setValue:@"160409" forKey:@"min_ver"];
  6769. return [RAConvertor dict2data:dic];
  6770. }
  6771. #pragma mark set line notes
  6772. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6773. {
  6774. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6775. NSString *notes = [self valueInParams:params key:@"notes"];
  6776. notes = [self translateSingleQuote:notes];
  6777. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6778. int ret = [iSalesDB execSql:sql];
  6779. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6780. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6781. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6782. // [dic setValue:@"160409" forKey:@"min_ver"];
  6783. return [RAConvertor dict2data:dic];
  6784. }
  6785. #pragma mark set qty
  6786. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6787. {
  6788. assert(params[@"can_create_backorder"]!=nil);
  6789. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6790. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6791. int item_count = [params[@"inputInt"] intValue];
  6792. // 购买检查数量大于库存
  6793. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6794. if (!params[@"can_create_backorder"]) {
  6795. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6796. __block BOOL out_of_stock = NO;
  6797. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6798. int availability = sqlite3_column_int(stmt, 0);
  6799. if (availability < item_count) {
  6800. out_of_stock = YES;
  6801. }
  6802. }];
  6803. if (out_of_stock) { // 缺货
  6804. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6805. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6806. return [RAConvertor dict2data:dic];
  6807. }
  6808. }
  6809. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6810. int ret = [iSalesDB execSql:sql];
  6811. __block int item_id = 0;
  6812. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6813. item_id = sqlite3_column_int(stmt, 0);
  6814. }];
  6815. sqlite3 *db = [iSalesDB get_db];
  6816. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6817. [iSalesDB close_db:db];
  6818. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6819. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6820. // [dic setValue:@"160409" forKey:@"min_ver"];
  6821. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6822. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6823. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6824. return [RAConvertor dict2data:dic];
  6825. }
  6826. #pragma mark place order
  6827. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6828. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6829. if (aname) {
  6830. [dic setValue:aname forKey:@"aname"];
  6831. }
  6832. if (control) {
  6833. [dic setValue:control forKey:@"control"];
  6834. }
  6835. if (keyboard) {
  6836. [dic setValue:keyboard forKey:@"keyboard"];
  6837. }
  6838. if (name) {
  6839. [dic setValue:name forKey:@"name"];
  6840. }
  6841. if (value) {
  6842. [dic setValue:value forKey:@"value"];
  6843. }
  6844. return dic;
  6845. }
  6846. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6847. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6848. orderCode = [self translateSingleQuote:orderCode];
  6849. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6850. 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];
  6851. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6852. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6853. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6854. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6855. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6856. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6857. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6858. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6859. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6860. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6861. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6862. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6863. NSString *email = [self textAtColumn:11 statement:stmt];
  6864. NSString *phone = [self textAtColumn:12 statement:stmt];
  6865. NSString *fax = [self textAtColumn:13 statement:stmt];
  6866. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6867. NSString *city = [self textAtColumn:15 statement:stmt];
  6868. NSString *state = [self textAtColumn:16 statement:stmt];
  6869. NSString *country = [self textAtColumn:17 statement:stmt];
  6870. NSString *name = [self textAtColumn:18 statement:stmt];
  6871. // contact id
  6872. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6873. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6874. [contact_id_dic setValue:@"text" forKey:@"control"];
  6875. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6876. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6877. [contact_id_dic setValue:@"true" forKey:@"required"];
  6878. [contact_id_dic setValue:contact_id forKey:@"value"];
  6879. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6880. // business card
  6881. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6882. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6883. [business_card_dic setValue:@"img" forKey:@"control"];
  6884. [business_card_dic setValue:@"1" forKey:@"disable"];
  6885. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6886. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6887. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6888. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6889. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6890. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6891. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6892. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6893. // fax
  6894. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6895. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6896. // zipcode
  6897. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6898. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6899. // city
  6900. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6901. [customer_dic setValue:city_dic forKey:@"item_12"];
  6902. // state
  6903. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6904. [customer_dic setValue:state_dic forKey:@"item_13"];
  6905. // country
  6906. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6907. [customer_dic setValue:country_dic forKey:@"item_14"];
  6908. // company name
  6909. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6910. [customer_dic setValue:company_dic forKey:@"item_2"];
  6911. // addr_1
  6912. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6913. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6914. // addr_2
  6915. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6916. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6917. // addr_3
  6918. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6919. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6920. // addr_4
  6921. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6922. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6923. // Contact
  6924. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6925. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6926. // email
  6927. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6928. [customer_dic setValue:email_dic forKey:@"item_8"];
  6929. // phone
  6930. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6931. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6932. // title
  6933. [customer_dic setValue:@"Customer" forKey:@"title"];
  6934. // count
  6935. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6936. }];
  6937. // setting
  6938. NSDictionary *setting = params[@"setting"];
  6939. NSNumber *hide = setting[@"CustomerHide"];
  6940. [customer_dic setValue:hide forKey:@"hide"];
  6941. return customer_dic;
  6942. }
  6943. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6944. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6945. // setting
  6946. NSDictionary *setting = params[@"setting"];
  6947. NSNumber *hide = setting[@"ShipToHide"];
  6948. [dic setValue:hide forKey:@"hide"];
  6949. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6950. orderCode = [self translateSingleQuote:orderCode];
  6951. 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];
  6952. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6953. NSString *cid = [self textAtColumn:0 statement:stmt];
  6954. NSString *name = [self textAtColumn:1 statement:stmt];
  6955. NSString *ext = [self textAtColumn:2 statement:stmt];
  6956. NSString *contact = [self textAtColumn:3 statement:stmt];
  6957. NSString *email = [self textAtColumn:4 statement:stmt];
  6958. NSString *fax = [self textAtColumn:5 statement:stmt];
  6959. NSString *phone = [self textAtColumn:6 statement:stmt];
  6960. // count
  6961. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6962. // title
  6963. [dic setValue:@"Ship To" forKey:@"title"];
  6964. // choose
  6965. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6966. [choose_dic setValue:@"choose" forKey:@"aname"];
  6967. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6968. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6969. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6970. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6971. @"receive_contact" : @"customer_contact",
  6972. @"receive_email" : @"customer_email",
  6973. @"receive_ext" : @"customer_contact_ext",
  6974. @"receive_fax" : @"customer_fax",
  6975. @"receive_name" : @"customer_name",
  6976. @"receive_phone" : @"customer_phone"},
  6977. @"refresh" : [NSNumber numberWithInteger:1],
  6978. @"type" : @"pull"};
  6979. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6980. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6981. @"name" : @"Add new address",
  6982. @"refresh" : [NSNumber numberWithInteger:1],
  6983. @"value" : @"new_addr"
  6984. };
  6985. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6986. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6987. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6988. @"receive_contact" : @"customer_contact",
  6989. @"receive_email" : @"customer_email",
  6990. @"receive_ext" : @"customer_contact_ext",
  6991. @"receive_fax" : @"customer_fax",
  6992. @"receive_name" : @"customer_name",
  6993. @"receive_phone" : @"customer_phone"},
  6994. @"name" : @"select_ship_to",
  6995. @"refresh" : [NSNumber numberWithInteger:1],
  6996. @"value" : @"Sales_Order_Ship_To"};
  6997. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6998. [dic setValue:choose_dic forKey:@"item_0"];
  6999. // contact id
  7000. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7001. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7002. [contact_id_dic setValue:@"true" forKey:@"required"];
  7003. [dic setValue:contact_id_dic forKey:@"item_1"];
  7004. // company name
  7005. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7006. [dic setValue:company_name_dic forKey:@"item_2"];
  7007. // address
  7008. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7009. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7010. if ([required integerValue]) {
  7011. [ext_dic setValue:@"true" forKey:@"required"];
  7012. }
  7013. [dic setValue:ext_dic forKey:@"item_3"];
  7014. // contact
  7015. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7016. [dic setValue:contact_dic forKey:@"item_4"];
  7017. // phone
  7018. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7019. [dic setValue:phone_dic forKey:@"item_5"];
  7020. // fax
  7021. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7022. [dic setValue:fax_dic forKey:@"item_6"];
  7023. // email
  7024. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7025. [dic setValue:email_dic forKey:@"item_7"];
  7026. }];
  7027. return dic;
  7028. }
  7029. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7030. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7031. // setting
  7032. NSDictionary *setting = params[@"setting"];
  7033. NSNumber *hide = setting[@"ShipFromHide"];
  7034. [dic setValue:hide forKey:@"hide"];
  7035. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7036. orderCode = [self translateSingleQuote:orderCode];
  7037. 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];
  7038. 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';";
  7039. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7040. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7041. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7042. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7043. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7044. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7045. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7046. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7047. if (!cid.length) {
  7048. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7049. cid = [self textAtColumn:0 statement:statment];
  7050. name = [self textAtColumn:1 statement:statment];
  7051. ext = [self textAtColumn:2 statement:statment];
  7052. contact = [self textAtColumn:3 statement:statment];
  7053. email = [self textAtColumn:4 statement:statment];
  7054. fax = [self textAtColumn:5 statement:statment];
  7055. phone = [self textAtColumn:6 statement:statment];
  7056. }];
  7057. }
  7058. // count
  7059. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7060. // title
  7061. [dic setValue:@"Ship From" forKey:@"title"];
  7062. // hide
  7063. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7064. // choose
  7065. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7066. [choose_dic setValue:@"choose" forKey:@"aname"];
  7067. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7068. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7069. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7070. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7071. @"sender_contact" : @"customer_contact",
  7072. @"sender_email" : @"customer_email",
  7073. @"sender_ext" : @"customer_contact_ext",
  7074. @"sender_fax" : @"customer_fax",
  7075. @"sender_name" : @"customer_name",
  7076. @"sender_phone" : @"customer_phone"},
  7077. @"name" : @"select_cid",
  7078. @"refresh" : [NSNumber numberWithInteger:0],
  7079. @"value" : @"Sales_Order_Ship_From"};
  7080. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7081. [dic setValue:choose_dic forKey:@"item_0"];
  7082. // contact id
  7083. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7084. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7085. [contact_id_dic setValue:@"true" forKey:@"required"];
  7086. [dic setValue:contact_id_dic forKey:@"item_1"];
  7087. // company name
  7088. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7089. [dic setValue:company_name_dic forKey:@"item_2"];
  7090. // address
  7091. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7092. [dic setValue:ext_dic forKey:@"item_3"];
  7093. // contact
  7094. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7095. [dic setValue:contact_dic forKey:@"item_4"];
  7096. // phone
  7097. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7098. [dic setValue:phone_dic forKey:@"item_5"];
  7099. // fax
  7100. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7101. [dic setValue:fax_dic forKey:@"item_6"];
  7102. // email
  7103. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7104. [dic setValue:email_dic forKey:@"item_7"];
  7105. }];
  7106. return dic;
  7107. }
  7108. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7109. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7110. // setting
  7111. NSDictionary *setting = params[@"setting"];
  7112. NSNumber *hide = setting[@"FreightBillToHide"];
  7113. [dic setValue:hide forKey:@"hide"];
  7114. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7115. orderCode = [self translateSingleQuote:orderCode];
  7116. 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];
  7117. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7118. NSString *cid = [self textAtColumn:0 statement:stmt];
  7119. NSString *name = [self textAtColumn:1 statement:stmt];
  7120. NSString *ext = [self textAtColumn:2 statement:stmt];
  7121. NSString *contact = [self textAtColumn:3 statement:stmt];
  7122. NSString *email = [self textAtColumn:4 statement:stmt];
  7123. NSString *fax = [self textAtColumn:5 statement:stmt];
  7124. NSString *phone = [self textAtColumn:6 statement:stmt];
  7125. // count
  7126. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7127. // title
  7128. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7129. // hide
  7130. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7131. // choose
  7132. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7133. [choose_dic setValue:@"choose" forKey:@"aname"];
  7134. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7135. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7136. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7137. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7138. @"shipping_billto_contact" : @"receive_contact",
  7139. @"shipping_billto_email" : @"receive_email",
  7140. @"shipping_billto_ext" : @"receive_ext",
  7141. @"shipping_billto_fax" : @"receive_fax",
  7142. @"shipping_billto_name" : @"receive_name",
  7143. @"shipping_billto_phone" : @"receive_phone"},
  7144. @"type" : @"pull"};
  7145. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7146. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7147. @"type" : @"pull",
  7148. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7149. @"shipping_billto_contact" : @"customer_contact",
  7150. @"shipping_billto_email" : @"customer_email",
  7151. @"shipping_billto_ext" : @"customer_contact_ext",
  7152. @"shipping_billto_fax" : @"customer_fax",
  7153. @"shipping_billto_name" : @"customer_name",
  7154. @"shipping_billto_phone" : @"customer_phone"}
  7155. };
  7156. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7157. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7158. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7159. @"shipping_billto_contact" : @"sender_contact",
  7160. @"shipping_billto_email" : @"sender_email",
  7161. @"shipping_billto_ext" : @"sender_ext",
  7162. @"shipping_billto_fax" : @"sender_fax",
  7163. @"shipping_billto_name" : @"sender_name",
  7164. @"shipping_billto_phone" : @"sender_phone"},
  7165. @"type" : @"pull"
  7166. };
  7167. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7168. NSDictionary *select_freight_bill_to_dic = @{
  7169. @"aname" : @"Select freight bill to",
  7170. @"name" : @"select_cid",
  7171. @"refresh" : [NSNumber numberWithInteger:0],
  7172. @"value" : @"Sales_Order_Freight_Bill_To",
  7173. @"key_map" : @{
  7174. @"shipping_billto_cid" : @"customer_cid",
  7175. @"shipping_billto_contact" : @"customer_contact",
  7176. @"shipping_billto_email" : @"customer_email",
  7177. @"shipping_billto_ext" : @"customer_contact_ext",
  7178. @"shipping_billto_fax" : @"customer_fax",
  7179. @"shipping_billto_name" : @"customer_name",
  7180. @"shipping_billto_phone" : @"customer_phone"
  7181. }
  7182. };
  7183. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7184. [dic setValue:choose_dic forKey:@"item_0"];
  7185. // contact id
  7186. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7187. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7188. [contact_id_dic setValue:@"true" forKey:@"required"];
  7189. [dic setValue:contact_id_dic forKey:@"item_1"];
  7190. // company name
  7191. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7192. [dic setValue:company_name_dic forKey:@"item_2"];
  7193. // address
  7194. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7195. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7196. if ([ext_required integerValue]) {
  7197. [ext_dic setValue:@"true" forKey:@"required"];
  7198. }
  7199. [dic setValue:ext_dic forKey:@"item_3"];
  7200. // contact
  7201. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7202. [dic setValue:contact_dic forKey:@"item_4"];
  7203. // phone
  7204. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7205. [dic setValue:phone_dic forKey:@"item_5"];
  7206. // fax
  7207. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7208. [dic setValue:fax_dic forKey:@"item_6"];
  7209. // email
  7210. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7211. [dic setValue:email_dic forKey:@"item_7"];
  7212. }];
  7213. return dic;
  7214. }
  7215. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7216. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7217. // setting
  7218. NSDictionary *setting = params[@"setting"];
  7219. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7220. [dic setValue:hide forKey:@"hide"];
  7221. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7222. orderCode = [self translateSingleQuote:orderCode];
  7223. 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];
  7224. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7225. NSString *cid = [self textAtColumn:0 statement:stmt];
  7226. NSString *name = [self textAtColumn:1 statement:stmt];
  7227. NSString *ext = [self textAtColumn:2 statement:stmt];
  7228. NSString *contact = [self textAtColumn:3 statement:stmt];
  7229. NSString *email = [self textAtColumn:4 statement:stmt];
  7230. NSString *fax = [self textAtColumn:5 statement:stmt];
  7231. NSString *phone = [self textAtColumn:6 statement:stmt];
  7232. // count
  7233. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7234. // title
  7235. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7236. // hide
  7237. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7238. // choose
  7239. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7240. [choose_dic setValue:@"choose" forKey:@"aname"];
  7241. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7242. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7243. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7244. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7245. @"billing_contact" : @"receive_contact",
  7246. @"billing_email" : @"receive_email",
  7247. @"billing_ext" : @"receive_ext",
  7248. @"billing_fax" : @"receive_fax",
  7249. @"billing_name" : @"receive_name",
  7250. @"billing_phone" : @"receive_phone"},
  7251. @"type" : @"pull"};
  7252. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7253. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7254. @"type" : @"pull",
  7255. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7256. @"billing_contact" : @"customer_contact",
  7257. @"billing_email" : @"customer_email",
  7258. @"billing_ext" : @"customer_contact_ext",
  7259. @"billing_fax" : @"customer_fax",
  7260. @"billing_name" : @"customer_name",
  7261. @"billing_phone" : @"customer_phone"}
  7262. };
  7263. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7264. NSDictionary *select_bill_to_dic = @{
  7265. @"aname" : @"Select bill to",
  7266. @"name" : @"select_cid",
  7267. @"refresh" : [NSNumber numberWithInteger:0],
  7268. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7269. @"key_map" : @{
  7270. @"billing_cid" : @"customer_cid",
  7271. @"billing_contact" : @"customer_contact",
  7272. @"billing_email" : @"customer_email",
  7273. @"billing_ext" : @"customer_contact_ext",
  7274. @"billing_fax" : @"customer_fax",
  7275. @"billing_name" : @"customer_name",
  7276. @"billing_phone" : @"customer_phone"
  7277. }
  7278. };
  7279. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7280. [dic setValue:choose_dic forKey:@"item_0"];
  7281. // contact id
  7282. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7283. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7284. [contact_id_dic setValue:@"true" forKey:@"required"];
  7285. [dic setValue:contact_id_dic forKey:@"item_1"];
  7286. // company name
  7287. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7288. [dic setValue:company_name_dic forKey:@"item_2"];
  7289. // address
  7290. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7291. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7292. if ([ext_required integerValue]) {
  7293. [ext_dic setValue:@"true" forKey:@"required"];
  7294. }
  7295. [dic setValue:ext_dic forKey:@"item_3"];
  7296. // contact
  7297. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7298. [dic setValue:contact_dic forKey:@"item_4"];
  7299. // phone
  7300. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7301. [dic setValue:phone_dic forKey:@"item_5"];
  7302. // fax
  7303. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7304. [dic setValue:fax_dic forKey:@"item_6"];
  7305. // email
  7306. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7307. [dic setValue:email_dic forKey:@"item_7"];
  7308. }];
  7309. return dic;
  7310. }
  7311. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7312. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7313. // setting
  7314. NSDictionary *setting = params[@"setting"];
  7315. NSNumber *hide = setting[@"ReturnToHide"];
  7316. [dic setValue:hide forKey:@"hide"];
  7317. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7318. orderCode = [self translateSingleQuote:orderCode];
  7319. 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];
  7320. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7321. NSString *cid = [self textAtColumn:0 statement:stmt];
  7322. NSString *name = [self textAtColumn:1 statement:stmt];
  7323. NSString *ext = [self textAtColumn:2 statement:stmt];
  7324. NSString *contact = [self textAtColumn:3 statement:stmt];
  7325. NSString *email = [self textAtColumn:4 statement:stmt];
  7326. NSString *fax = [self textAtColumn:5 statement:stmt];
  7327. NSString *phone = [self textAtColumn:6 statement:stmt];
  7328. // count
  7329. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7330. // title
  7331. [dic setValue:@"Return To" forKey:@"title"];
  7332. // hide
  7333. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7334. // choose
  7335. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7336. [choose_dic setValue:@"choose" forKey:@"aname"];
  7337. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7338. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7339. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7340. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7341. @"returnto_contact" : @"sender_contact",
  7342. @"returnto_email" : @"sender_email",
  7343. @"returnto_ext" : @"sender_ext",
  7344. @"returnto_fax" : @"sender_fax",
  7345. @"returnto_name" : @"sender_name",
  7346. @"returnto_phone" : @"sender_phone"},
  7347. @"type" : @"pull"};
  7348. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7349. NSDictionary *select_return_to_dic = @{
  7350. @"aname" : @"Select return to",
  7351. @"name" : @"select_cid",
  7352. @"refresh" : [NSNumber numberWithInteger:0],
  7353. @"value" : @"Contact_Return_To",
  7354. @"key_map" : @{
  7355. @"returnto_cid" : @"customer_cid",
  7356. @"returnto_contact" : @"customer_contact",
  7357. @"returnto_email" : @"customer_email",
  7358. @"returnto_ext" : @"customer_contact_ext",
  7359. @"returnto_fax" : @"customer_fax",
  7360. @"returnto_name" : @"customer_name",
  7361. @"returnto_phone" : @"customer_phone"
  7362. }
  7363. };
  7364. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7365. [dic setValue:choose_dic forKey:@"item_0"];
  7366. // contact id
  7367. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7368. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7369. [contact_id_dic setValue:@"true" forKey:@"required"];
  7370. [dic setValue:contact_id_dic forKey:@"item_1"];
  7371. // company name
  7372. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7373. [dic setValue:company_name_dic forKey:@"item_2"];
  7374. // address
  7375. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7376. [dic setValue:ext_dic forKey:@"item_3"];
  7377. // contact
  7378. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7379. [dic setValue:contact_dic forKey:@"item_4"];
  7380. // phone
  7381. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7382. [dic setValue:phone_dic forKey:@"item_5"];
  7383. // fax
  7384. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7385. [dic setValue:fax_dic forKey:@"item_6"];
  7386. // email
  7387. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7388. [dic setValue:email_dic forKey:@"item_7"];
  7389. }];
  7390. return dic;
  7391. }
  7392. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7393. {
  7394. assert(params[@"user"]!=nil);
  7395. assert(params[@"contact_id"]!=nil);
  7396. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7397. orderCode = [self translateSingleQuote:orderCode];
  7398. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7399. __block double TotalCuft = 0;
  7400. __block double TotalWeight = 0;
  7401. __block int TotalCarton = 0;
  7402. __block double payments = 0;
  7403. // setting
  7404. NSDictionary *setting = params[@"setting"];
  7405. NSNumber *hide = setting[@"ModelInformationHide"];
  7406. [dic setValue:hide forKey:@"hide"];
  7407. 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];
  7408. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7409. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7410. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7411. // item id
  7412. int item_id = sqlite3_column_int(stmt, 0);
  7413. // count
  7414. int item_count = sqlite3_column_int(stmt, 1);
  7415. // stockUom
  7416. int stockUom = sqlite3_column_int(stmt, 2);
  7417. // unit price
  7418. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7419. NSString* Price=nil;
  7420. if([str_price isEqualToString:@""])
  7421. {
  7422. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  7423. if(price==nil)
  7424. Price=@"No Price.";
  7425. else
  7426. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7427. }
  7428. else
  7429. {
  7430. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7431. }
  7432. // discount
  7433. double discount = sqlite3_column_double(stmt, 4);
  7434. // name
  7435. NSString *name = [self textAtColumn:5 statement:stmt];
  7436. // description
  7437. NSString *description = [self textAtColumn:6 statement:stmt];
  7438. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7439. // line note
  7440. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7441. int avaulability = sqlite3_column_int(stmt, 8);
  7442. // img
  7443. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7444. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  7445. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7446. if(combine != nil)
  7447. {
  7448. // int citem=0;
  7449. int bcount=[[combine valueForKey:@"count"] intValue];
  7450. for(int bc=0;bc<bcount;bc++)
  7451. {
  7452. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7453. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7454. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7455. subTotal += uprice * modulus * item_count;
  7456. }
  7457. }
  7458. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7459. [model_dic setValue:@"model" forKey:@"control"];
  7460. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7461. [model_dic setValue:description forKey:@"description"];
  7462. [model_dic setValue:line_note forKey:@"note"];
  7463. [model_dic setValue:img forKey:@"img_url"];
  7464. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7465. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7466. [model_dic setValue:Price forKey:@"unit_price"];
  7467. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7468. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7469. if (combine) {
  7470. [model_dic setValue:combine forKey:@"combine"];
  7471. }
  7472. // well,what under the row is the info for total
  7473. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7474. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7475. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7476. int carton=[bsubtotaljson[@"carton"] intValue];
  7477. TotalCuft += cuft;
  7478. TotalWeight += weight;
  7479. TotalCarton += carton;
  7480. payments += subTotal;
  7481. //---------------------------
  7482. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7483. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7484. }];
  7485. [dic setValue:@"Model Information" forKey:@"title"];
  7486. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7487. return dic;
  7488. }
  7489. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7490. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7491. [dic setValue:@"Remarks Content" forKey:@"title"];
  7492. // setting
  7493. NSDictionary *setting = params[@"setting"];
  7494. NSNumber *hide = setting[@"RemarksContentHide"];
  7495. [dic setValue:hide forKey:@"hide"];
  7496. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7497. orderCode = [self translateSingleQuote:orderCode];
  7498. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7499. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7500. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7501. int mustCall = sqlite3_column_int(stmt, 1);
  7502. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7503. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7504. NSDictionary *po_dic = @{
  7505. @"aname" : @"PO#",
  7506. @"control" : @"edit",
  7507. @"keyboard" : @"text",
  7508. @"name" : @"poNumber",
  7509. @"value" : poNumber
  7510. };
  7511. NSDictionary *must_call_dic = @{
  7512. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7513. @"control" : @"switch",
  7514. @"name" : @"must_call",
  7515. @"value" : mustCall ? @"true" : @"false"
  7516. };
  7517. NSDictionary *general_notes_dic = @{
  7518. @"aname" : @"General notes",
  7519. @"control" : @"text_view",
  7520. @"keyboard" : @"text",
  7521. @"name" : @"comments",
  7522. @"value" : generalNotes
  7523. };
  7524. // NSDictionary *internal_notes_dic = @{
  7525. // @"aname" : @"Internal notes",
  7526. // @"control" : @"text_view",
  7527. // @"keyboard" : @"text",
  7528. // @"name" : @"internal_notes",
  7529. // @"value" : internalNotes
  7530. // };
  7531. [dic setValue:po_dic forKey:@"item_0"];
  7532. [dic setValue:must_call_dic forKey:@"item_1"];
  7533. [dic setValue:general_notes_dic forKey:@"item_2"];
  7534. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7535. }];
  7536. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7537. return dic;
  7538. }
  7539. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7540. // params
  7541. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7542. orderCode = [self translateSingleQuote:orderCode];
  7543. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7544. // setting
  7545. NSDictionary *setting = params[@"setting"];
  7546. NSNumber *hide = setting[@"OrderTotalHide"];
  7547. [dic setValue:hide forKey:@"hide"];
  7548. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7549. __block double lift_gate_value = 0;
  7550. __block double handling_fee = 0;
  7551. __block double shipping = 0;
  7552. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7553. int lift_gate = sqlite3_column_int(stmt, 0);
  7554. lift_gate_value = sqlite3_column_double(stmt, 1);
  7555. shipping = sqlite3_column_double(stmt, 2);
  7556. handling_fee = sqlite3_column_double(stmt, 3);
  7557. if (!lift_gate) {
  7558. lift_gate_value = 0;
  7559. }
  7560. }];
  7561. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7562. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7563. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7564. double payments = [[total valueForKey:@"payments"] doubleValue];
  7565. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7566. double totalPrice = payments;
  7567. [dic setValue:@"Order Total" forKey:@"title"];
  7568. NSDictionary *payments_dic = @{
  7569. @"align" : @"right",
  7570. @"aname" : @"Payments/Credits",
  7571. @"control" : @"text",
  7572. @"name" : @"paymentsAndCredits",
  7573. @"type" : @"price",
  7574. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7575. };
  7576. [dic setValue:payments_dic forKey:@"item_0"];
  7577. // version 1.71 remove
  7578. // NSDictionary *handling_fee_dic = @{
  7579. // @"align" : @"right",
  7580. // @"aname" : @"Handling Fee",
  7581. // @"control" : @"text",
  7582. // @"name" : @"handling_fee_value",
  7583. // @"required" : @"true",
  7584. // @"type" : @"price",
  7585. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7586. // };
  7587. NSDictionary *shipping_dic = @{
  7588. @"align" : @"right",
  7589. @"aname" : @"Shipping*",
  7590. @"control" : @"text",
  7591. @"name" : @"shipping",
  7592. @"required" : @"true",
  7593. @"type" : @"price",
  7594. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7595. };
  7596. NSDictionary *lift_gate_dic = @{
  7597. @"align" : @"right",
  7598. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7599. @"control" : @"text",
  7600. @"name" : @"lift_gate_value",
  7601. @"required" : @"true",
  7602. @"type" : @"price",
  7603. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7604. };
  7605. int item_count = 1;
  7606. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7607. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7608. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7609. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7610. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7611. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7612. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7613. } else {
  7614. }
  7615. }
  7616. // version 1.71 remove
  7617. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7618. NSDictionary *total_price_dic = @{
  7619. @"align" : @"right",
  7620. @"aname" : @"Total",
  7621. @"control" : @"text",
  7622. @"name" : @"totalPrice",
  7623. @"type" : @"price",
  7624. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7625. };
  7626. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7627. NSDictionary *total_cuft_dic = @{
  7628. @"align" : @"right",
  7629. @"aname" : @"Total Cuft",
  7630. @"control" : @"text",
  7631. @"name" : @"",
  7632. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7633. };
  7634. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7635. NSDictionary *total_weight_dic = @{
  7636. @"align" : @"right",
  7637. @"aname" : @"Total Weight",
  7638. @"control" : @"text",
  7639. @"name" : @"",
  7640. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7641. };
  7642. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7643. NSDictionary *total_carton_dic = @{
  7644. @"align" : @"right",
  7645. @"aname" : @"Total Carton",
  7646. @"control" : @"text",
  7647. @"name" : @"",
  7648. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7649. };
  7650. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7651. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7652. return dic;
  7653. }
  7654. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7655. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7656. orderCode = [self translateSingleQuote:orderCode];
  7657. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7658. // setting
  7659. NSDictionary *setting = params[@"setting"];
  7660. NSNumber *hide = setting[@"SignatureHide"];
  7661. [dic setValue:hide forKey:@"hide"];
  7662. [dic setValue:@"Signature" forKey:@"title"];
  7663. __block NSString *pic_path = @"";
  7664. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7665. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7666. pic_path = [self textAtColumn:0 statement:stmt];
  7667. }];
  7668. NSDictionary *pic_dic = @{
  7669. @"aname" : @"Signature",
  7670. @"avalue" :pic_path,
  7671. @"control" : @"signature",
  7672. @"name" : @"sign_picpath",
  7673. @"value" : pic_path
  7674. };
  7675. [dic setValue:pic_dic forKey:@"item_0"];
  7676. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7677. return dic;
  7678. }
  7679. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7680. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7681. // setting
  7682. NSDictionary *setting = params[@"setting"];
  7683. NSNumber *hide = setting[@"ShippingMethodHide"];
  7684. [dic setValue:hide forKey:@"hide"];
  7685. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7686. orderCode = [self translateSingleQuote:orderCode];
  7687. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7688. __block NSString *logist = @"";
  7689. __block NSString *lift_gate = @"";
  7690. __block int lift_gate_integer = 0;
  7691. __block NSString *logistic_note = @"";
  7692. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7693. logist = [self textAtColumn:0 statement:stmt];
  7694. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7695. logistic_note = [self textAtColumn:2 statement:stmt];
  7696. }];
  7697. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7698. [dic setValue:@"Shipping Method" forKey:@"title"];
  7699. // val_0
  7700. int PERSONAL_PICK_UP_check = 0;
  7701. int USE_MY_CARRIER_check = 0;
  7702. NSString *logistic_note_text = @"";
  7703. int will_call_check = 0;
  7704. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7705. will_call_check = 1;
  7706. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7707. PERSONAL_PICK_UP_check = 1;
  7708. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7709. USE_MY_CARRIER_check = 1;
  7710. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7711. logistic_note = [logistic_note_array firstObject];
  7712. if (logistic_note_array.count > 1) {
  7713. logistic_note_text = [logistic_note_array lastObject];
  7714. }
  7715. }
  7716. }
  7717. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7718. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7719. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7720. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7721. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7722. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7723. NSDictionary *val0_subItem_item0 = @{
  7724. @"aname" : @"Option",
  7725. @"cadedate" : @{
  7726. @"count" : [NSNumber numberWithInteger:2],
  7727. @"val_0" : @{
  7728. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7729. @"refresh" : [NSNumber numberWithInteger:0],
  7730. @"value" : @"PERSONAL PICK UP",
  7731. @"value_id" : @"PERSONAL PICK UP"
  7732. },
  7733. @"val_1" : @{
  7734. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7735. @"refresh" : [NSNumber numberWithInteger:0],
  7736. @"sub_item" : @{
  7737. @"count" : [NSNumber numberWithInteger:1],
  7738. @"item_0" : @{
  7739. @"aname" : @"BOL",
  7740. @"control" : @"edit",
  7741. @"keyboard" : @"text",
  7742. @"name" : @"logist_note_text",
  7743. @"refresh" : [NSNumber numberWithInteger:0],
  7744. @"required" : @"false",
  7745. @"value" : logistic_note_text
  7746. }
  7747. },
  7748. @"value" : @"USE MY CARRIER",
  7749. @"value_id" : @"USE MY CARRIER"
  7750. }
  7751. },
  7752. @"control" : @"enum",
  7753. @"name" : @"logistic_note",
  7754. @"required" : @"true",
  7755. @"single_select" : @"true"
  7756. };
  7757. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7758. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7759. // val_1
  7760. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7761. if([lift_gate isEqualToString:@""]) {
  7762. if (lift_gate_integer == 1) {
  7763. lift_gate = @"true";
  7764. } else {
  7765. lift_gate = @"false";
  7766. }
  7767. }
  7768. int common_carrier_check = 0;
  7769. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7770. common_carrier_check = 1;
  7771. [params setValue:lift_gate forKey:@"lift_gate"];
  7772. }
  7773. NSDictionary *val_1 = @{
  7774. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7775. @"sub_item" : @{
  7776. @"count" : [NSNumber numberWithInteger:1],
  7777. @"item_0" : @{
  7778. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7779. @"control" : @"switch",
  7780. @"name" : @"lift_gate",
  7781. @"refresh" : [NSNumber numberWithInteger:1],
  7782. @"required" : @"true",
  7783. @"value" : lift_gate
  7784. }
  7785. },
  7786. @"value" : @"COMMON CARRIER",
  7787. @"value_id" : @"COMMON CARRIER"
  7788. };
  7789. // cadedate
  7790. NSDictionary *cadedate = @{
  7791. @"count" : [NSNumber numberWithInteger:2],
  7792. @"val_0" : val_0,
  7793. @"val_1" : val_1
  7794. };
  7795. // item_0
  7796. NSMutableDictionary *item_0 = @{
  7797. @"aname" : @"Shipping",
  7798. @"cadedate" : cadedate,
  7799. @"control" : @"enum",
  7800. @"name" : @"logist",
  7801. @"refresh" : [NSNumber numberWithInteger:1],
  7802. @"single_select" : @"true",
  7803. }.mutableCopy;
  7804. NSNumber *required = setting[@"ShippingMethodRequire"];
  7805. if ([required integerValue]) {
  7806. [item_0 setValue:@"true" forKey:@"required"];
  7807. }
  7808. [dic setValue:item_0 forKey:@"item_0"];
  7809. if ([logist isEqualToString:@"WILL CALL"]) {
  7810. [dic removeObjectForKey:@"lift_gate"];
  7811. }
  7812. return dic;
  7813. }
  7814. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7815. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7816. orderCode = [self translateSingleQuote:orderCode];
  7817. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7818. __block int submit_as_integer = 0;
  7819. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7820. submit_as_integer = sqlite3_column_int(stmt, 0);
  7821. }];
  7822. int check11 = 0;
  7823. int check10 = 0;
  7824. if (submit_as_integer == 11) {
  7825. check11 = 1;
  7826. }
  7827. if (submit_as_integer == 10) {
  7828. check10 = 1;
  7829. }
  7830. // section_0
  7831. NSMutableDictionary *dic = @{
  7832. @"count" : @(1),
  7833. @"item_0" : @{
  7834. @"aname" : @"Submit Order As",
  7835. @"cadedate" : @{
  7836. @"count" : @(2),
  7837. @"val_0" : @{
  7838. @"check" : [NSNumber numberWithInteger:check11],
  7839. @"value" : @"Sales Order",
  7840. @"value_id" : @(11)
  7841. },
  7842. @"val_1" : @{
  7843. @"check" : [NSNumber numberWithInteger:check10],
  7844. @"value" : @"Quote",
  7845. @"value_id" : @(10)
  7846. }
  7847. },
  7848. @"control" : @"enum",
  7849. @"name" : @"erpOrderStatus",
  7850. @"required" : @"true",
  7851. @"single_select" : @"true"
  7852. },
  7853. @"title" : @"Order Type"
  7854. }.mutableCopy;
  7855. // setting
  7856. NSDictionary *setting = params[@"setting"];
  7857. NSNumber *hide = setting[@"OrderTypeHide"];
  7858. [dic setValue:hide forKey:@"hide"];
  7859. return dic;
  7860. }
  7861. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7862. countryCode = [self translateSingleQuote:countryCode];
  7863. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7864. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7865. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7866. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7867. if (name == NULL) {
  7868. name = "";
  7869. }
  7870. if (code == NULL) {
  7871. code = "";
  7872. }
  7873. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7874. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7875. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7876. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7877. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7878. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7879. }
  7880. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7881. [container setValue:stateDic forKey:key];
  7882. }] mutableCopy];
  7883. [ret removeObjectForKey:@"result"];
  7884. // failure 可以不用了,一样的
  7885. if (ret.allKeys.count == 0) {
  7886. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7887. [stateDic setValue:@"Other" forKey:@"value"];
  7888. [stateDic setValue:@"" forKey:@"value_id"];
  7889. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7890. if (state_code && [@"" isEqualToString:state_code]) {
  7891. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7892. }
  7893. NSString *key = [NSString stringWithFormat:@"val_0"];
  7894. [ret setValue:stateDic forKey:key];
  7895. }
  7896. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  7897. return ret;
  7898. }
  7899. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7900. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7901. orderCode = [self translateSingleQuote:orderCode];
  7902. 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];
  7903. __block NSString *payType = @"";
  7904. __block NSString *firstName = @"";
  7905. __block NSString *lastName = @"";
  7906. __block NSString *addr1 = @"";
  7907. __block NSString *addr2 = @"";
  7908. __block NSString *zipcode = @"";
  7909. __block NSString *cardType = @"";
  7910. __block NSString *cardNumber = @"";
  7911. __block NSString *securityCode = @"";
  7912. __block NSString *month = @"";
  7913. __block NSString *year = @"";
  7914. __block NSString *city = @"";
  7915. __block NSString *state = @"";
  7916. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7917. payType = [self textAtColumn:0 statement:stmt];
  7918. firstName = [self textAtColumn:1 statement:stmt];
  7919. lastName = [self textAtColumn:2 statement:stmt];
  7920. addr1 = [self textAtColumn:3 statement:stmt];
  7921. addr2 = [self textAtColumn:4 statement:stmt];
  7922. zipcode = [self textAtColumn:5 statement:stmt];
  7923. cardType = [self textAtColumn:6 statement:stmt];
  7924. cardNumber = [self textAtColumn:7 statement:stmt];
  7925. securityCode = [self textAtColumn:8 statement:stmt];
  7926. month = [self textAtColumn:9 statement:stmt];
  7927. year = [self textAtColumn:10 statement:stmt];
  7928. city = [self textAtColumn:11 statement:stmt];
  7929. state = [self textAtColumn:12 statement:stmt];
  7930. }];
  7931. NSString *required = @"true";
  7932. // setting
  7933. NSDictionary *setting = params[@"setting"];
  7934. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7935. if ([requiredNumber integerValue]) {
  7936. required = @"true";
  7937. } else {
  7938. required = @"false";
  7939. }
  7940. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7941. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7942. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7943. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7944. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7945. NSString *type = [self textAtColumn:1 statement:stmt];
  7946. NSMutableDictionary *val = @{
  7947. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7948. @"value" : type,
  7949. @"value_id" : type_id
  7950. }.mutableCopy;
  7951. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7952. NSDictionary *sub_item = @{
  7953. @"count" : @(3),
  7954. @"item_0" : @{
  7955. @"aname" : @"choose",
  7956. @"control" : @"multi_action",
  7957. @"count" : @(1),
  7958. @"item_0" : @{
  7959. @"aname" : @"Same as customer",
  7960. @"key_map" : @{
  7961. @"credit_card_address1" : @"customer_address1",
  7962. @"credit_card_address2" : @"customer_address2",
  7963. @"credit_card_city" : @"customer_city",
  7964. @"credit_card_first_name" : @"customer_first_name",
  7965. @"credit_card_last_name" : @"customer_last_name",
  7966. @"credit_card_state" : @"customer_state",
  7967. @"credit_card_zipcode" : @"customer_zipcode"
  7968. },
  7969. @"type" : @"pull"
  7970. }
  7971. },
  7972. @"item_1" : @{
  7973. @"aname" : @"",
  7974. @"color" : @"red",
  7975. @"control" : @"text",
  7976. @"name" : @"",
  7977. @"value" : @"USA Credit cards only"
  7978. },
  7979. @"item_2" : @{
  7980. @"aname" : @"Fill",
  7981. @"cadedate" : @{
  7982. @"count" : @(2),
  7983. @"val_0" : @{
  7984. @"check" : @(1),
  7985. @"sub_item" : @{
  7986. @"count" : @(11),
  7987. @"item_0" : @{
  7988. @"aname" : @"Type",
  7989. @"cadedate" : @{
  7990. @"count" : @(2),
  7991. @"val_0" : @{
  7992. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7993. @"value" : @"VISA",
  7994. @"value_id" : @"VISA"/*@(0)*/
  7995. },
  7996. @"val_1" : @{
  7997. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7998. @"value" : @"MASTER CARD",
  7999. @"value_id" : @"MASTER CARD"/*@(1)*/
  8000. }
  8001. },
  8002. @"control" : @"enum",
  8003. @"name" : @"credit_card_type",
  8004. @"required" : @"true",
  8005. @"single_select" : @"true"
  8006. },
  8007. @"item_1" : @{
  8008. @"aname" : @"Number",
  8009. @"control" : @"edit",
  8010. @"keyboard" : @"int",
  8011. @"length" : @"16",
  8012. @"name" : @"credit_card_number",
  8013. @"required" : @"true",
  8014. @"value" : cardNumber
  8015. },
  8016. @"item_10" : @{
  8017. @"aname" : @"State",
  8018. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8019. @"control" : @"enum",
  8020. @"enum" : @"true",
  8021. @"name" : @"credit_card_state",
  8022. @"required" : @"true",
  8023. @"single_select" : @"true"
  8024. },
  8025. @"item_2" : @{
  8026. @"aname" : @"Expiration Date",
  8027. @"control" : @"monthpicker",
  8028. @"name" : @"credit_card_expiration",
  8029. @"required" : @"true",
  8030. @"type" : @"date",
  8031. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8032. },
  8033. @"item_3" : @{
  8034. @"aname" : @"Security Code",
  8035. @"control" : @"edit",
  8036. @"keyboard" : @"int",
  8037. @"length" : @"3",
  8038. @"name" : @"credit_card_security_code",
  8039. @"required" : @"true",
  8040. @"value" : securityCode
  8041. },
  8042. @"item_4" : @{
  8043. @"aname" : @"First Name",
  8044. @"control" : @"edit",
  8045. @"keyboard" : @"text",
  8046. @"name" : @"credit_card_first_name",
  8047. @"required" : @"true",
  8048. @"value" : firstName
  8049. },
  8050. @"item_5" : @{
  8051. @"aname" : @"Last Name",
  8052. @"control" : @"edit",
  8053. @"keyboard" : @"text",
  8054. @"name" : @"credit_card_last_name",
  8055. @"required" : @"true",
  8056. @"value" : lastName
  8057. },
  8058. @"item_6" : @{
  8059. @"aname" : @"Address 1",
  8060. @"control" : @"edit",
  8061. @"keyboard" : @"text",
  8062. @"name" : @"credit_card_address1",
  8063. @"required" : @"true",
  8064. @"value" : addr1
  8065. },
  8066. @"item_7" : @{
  8067. @"aname" : @"Address 2",
  8068. @"control" : @"edit",
  8069. @"keyboard" : @"text",
  8070. @"name" : @"credit_card_address2",
  8071. @"value" : addr2
  8072. },
  8073. @"item_8" : @{
  8074. @"aname" : @"zip code",
  8075. @"control" : @"edit",
  8076. @"keyboard" : @"text",
  8077. @"name" : @"credit_card_zipcode",
  8078. @"required" : @"true",
  8079. @"value" : zipcode
  8080. },
  8081. @"item_9" : @{
  8082. @"aname" : @"City",
  8083. @"control" : @"edit",
  8084. @"keyboard" : @"text",
  8085. @"name" : @"credit_card_city",
  8086. @"required" : @"true",
  8087. @"value" : city
  8088. }
  8089. },
  8090. @"value" : @"Fill Now",
  8091. @"value_id" : @""
  8092. },
  8093. @"val_1" : @{
  8094. @"check" : @(0),
  8095. @"value" : @"Fill Later",
  8096. @"value_id" : @""
  8097. }
  8098. },
  8099. @"control" : @"enum",
  8100. @"name" : @"",
  8101. @"single_select" : @"true"
  8102. }
  8103. };
  8104. [val setObject:sub_item forKey:@"sub_item"];
  8105. }
  8106. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8107. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8108. }];
  8109. // "section_2"
  8110. NSMutableDictionary *dic = @{
  8111. @"count" : @(1),
  8112. @"item_0" : @{
  8113. @"aname" : @"Payment",
  8114. @"required" : required,
  8115. @"cadedate" : cadedate,
  8116. // @{
  8117. // @"count" : @(6),
  8118. // @"val_3" : @{
  8119. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8120. // @"value" : @"Check",
  8121. // @"value_id" : @"Check"
  8122. // },
  8123. // @"val_2" : @{
  8124. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8125. // @"value" : @"Cash",
  8126. // @"value_id" : @"Cash"
  8127. // },
  8128. // @"val_1" : @{
  8129. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8130. // @"value" : @"NET 60",
  8131. // @"value_id" : @"NET 30"
  8132. // },
  8133. // @"val_4" : @{
  8134. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8135. // @"value" : @"Wire Transfer",
  8136. // @"value_id" : @"Wire Transfer"
  8137. // },
  8138. // @"val_0" : @{
  8139. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8140. // @"sub_item" : @{
  8141. // @"count" : @(3),
  8142. // @"item_0" : @{
  8143. // @"aname" : @"choose",
  8144. // @"control" : @"multi_action",
  8145. // @"count" : @(1),
  8146. // @"item_0" : @{
  8147. // @"aname" : @"Same as customer",
  8148. // @"key_map" : @{
  8149. // @"credit_card_address1" : @"customer_address1",
  8150. // @"credit_card_address2" : @"customer_address2",
  8151. // @"credit_card_city" : @"customer_city",
  8152. // @"credit_card_first_name" : @"customer_first_name",
  8153. // @"credit_card_last_name" : @"customer_last_name",
  8154. // @"credit_card_state" : @"customer_state",
  8155. // @"credit_card_zipcode" : @"customer_zipcode"
  8156. // },
  8157. // @"type" : @"pull"
  8158. // }
  8159. // },
  8160. // @"item_1" : @{
  8161. // @"aname" : @"",
  8162. // @"color" : @"red",
  8163. // @"control" : @"text",
  8164. // @"name" : @"",
  8165. // @"value" : @"USA Credit cards only"
  8166. // },
  8167. // @"item_2" : @{
  8168. // @"aname" : @"Fill",
  8169. // @"cadedate" : @{
  8170. // @"count" : @(2),
  8171. // @"val_0" : @{
  8172. // @"check" : @(1),
  8173. // @"sub_item" : @{
  8174. // @"count" : @(11),
  8175. // @"item_0" : @{
  8176. // @"aname" : @"Type",
  8177. // @"cadedate" : @{
  8178. // @"count" : @(2),
  8179. // @"val_0" : @{
  8180. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8181. // @"value" : @"VISA",
  8182. // @"value_id" : @(0)
  8183. // },
  8184. // @"val_1" : @{
  8185. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8186. // @"value" : @"MASTER CARD",
  8187. // @"value_id" : @(1)
  8188. // }
  8189. // },
  8190. // @"control" : @"enum",
  8191. // @"name" : @"credit_card_type",
  8192. // @"required" : @"true",
  8193. // @"single_select" : @"true"
  8194. // },
  8195. // @"item_1" : @{
  8196. // @"aname" : @"Number",
  8197. // @"control" : @"edit",
  8198. // @"keyboard" : @"int",
  8199. // @"length" : @"16",
  8200. // @"name" : @"credit_card_number",
  8201. // @"required" : @"true",
  8202. // @"value" : cardNumber
  8203. // },
  8204. // @"item_10" : @{
  8205. // @"aname" : @"State",
  8206. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8207. // @"control" : @"enum",
  8208. // @"enum" : @"true",
  8209. // @"name" : @"credit_card_state",
  8210. // @"required" : @"true",
  8211. // @"single_select" : @"true"
  8212. // },
  8213. // @"item_2" : @{
  8214. // @"aname" : @"Expiration Date",
  8215. // @"control" : @"monthpicker",
  8216. // @"name" : @"credit_card_expiration",
  8217. // @"required" : @"true",
  8218. // @"type" : @"date",
  8219. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8220. // },
  8221. // @"item_3" : @{
  8222. // @"aname" : @"Security Code",
  8223. // @"control" : @"edit",
  8224. // @"keyboard" : @"int",
  8225. // @"length" : @"3",
  8226. // @"name" : @"credit_card_security_code",
  8227. // @"required" : @"true",
  8228. // @"value" : securityCode
  8229. // },
  8230. // @"item_4" : @{
  8231. // @"aname" : @"First Name",
  8232. // @"control" : @"edit",
  8233. // @"keyboard" : @"text",
  8234. // @"name" : @"credit_card_first_name",
  8235. // @"required" : @"true",
  8236. // @"value" : firstName
  8237. // },
  8238. // @"item_5" : @{
  8239. // @"aname" : @"Last Name",
  8240. // @"control" : @"edit",
  8241. // @"keyboard" : @"text",
  8242. // @"name" : @"credit_card_last_name",
  8243. // @"required" : @"true",
  8244. // @"value" : lastName
  8245. // },
  8246. // @"item_6" : @{
  8247. // @"aname" : @"Address 1",
  8248. // @"control" : @"edit",
  8249. // @"keyboard" : @"text",
  8250. // @"name" : @"credit_card_address1",
  8251. // @"required" : @"true",
  8252. // @"value" : addr1
  8253. // },
  8254. // @"item_7" : @{
  8255. // @"aname" : @"Address 2",
  8256. // @"control" : @"edit",
  8257. // @"keyboard" : @"text",
  8258. // @"name" : @"credit_card_address2",
  8259. // @"value" : addr2
  8260. // },
  8261. // @"item_8" : @{
  8262. // @"aname" : @"zip code",
  8263. // @"control" : @"edit",
  8264. // @"keyboard" : @"text",
  8265. // @"name" : @"credit_card_zipcode",
  8266. // @"required" : @"true",
  8267. // @"value" : zipcode
  8268. // },
  8269. // @"item_9" : @{
  8270. // @"aname" : @"City",
  8271. // @"control" : @"edit",
  8272. // @"keyboard" : @"text",
  8273. // @"name" : @"credit_card_city",
  8274. // @"required" : @"true",
  8275. // @"value" : city
  8276. // }
  8277. // },
  8278. // @"value" : @"Fill Now",
  8279. // @"value_id" : @""
  8280. // },
  8281. // @"val_1" : @{
  8282. // @"check" : @(0),
  8283. // @"value" : @"Fill Later",
  8284. // @"value_id" : @""
  8285. // }
  8286. // },
  8287. // @"control" : @"enum",
  8288. // @"name" : @"",
  8289. // @"single_select" : @"true"
  8290. // }
  8291. // },
  8292. // @"value" : @"Visa/Master",
  8293. // @"value_id" : @"Credit Card"
  8294. // },
  8295. // @"val_5" : @{
  8296. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8297. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8298. // @"value_id" : @"FOLLOW EXISTING"
  8299. // }
  8300. // },
  8301. @"control" : @"enum",
  8302. @"name" : @"paymentType",
  8303. @"single_select" : @"true"
  8304. },
  8305. @"title" : @"Payment Information"
  8306. }.mutableCopy;
  8307. NSNumber *hide = setting[@"PaymentInformationHide"];
  8308. [dic setValue:hide forKey:@"hide"];
  8309. return dic;
  8310. }
  8311. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8312. // params
  8313. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8314. orderCode = [self translateSingleQuote:orderCode];
  8315. // 缺货检查
  8316. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  8317. __block BOOL outOfStock = NO;
  8318. sqlite3 *database = db;
  8319. if (!db) {
  8320. database = [iSalesDB get_db];
  8321. }
  8322. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8323. int availability = sqlite3_column_int(stmt, 0);
  8324. int item_qty = sqlite3_column_int(stmt, 1);
  8325. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8326. }];
  8327. if (!db) {
  8328. [iSalesDB close_db:database];
  8329. }
  8330. return outOfStock;
  8331. }
  8332. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8333. {
  8334. assert(params[@"user"]!=nil);
  8335. assert(params[@"contact_id"]!=nil);
  8336. assert(params[@"can_create_backorder"]!=nil);
  8337. sqlite3 *db = [iSalesDB get_db];
  8338. // params
  8339. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8340. orderCode = [self translateSingleQuote:orderCode];
  8341. // 缺货检查
  8342. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8343. if (![params[@"can_create_backorder"] boolValue]) {
  8344. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8345. if (out_of_stock) {
  8346. [iSalesDB close_db:db];
  8347. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8348. [resultDic setObject:[NSString stringWithFormat:@"some item(s) are out of stock,please remove them from cart before placing order with %@ or try again after sync",COMPANY_SHORT_NAME] forKey:@"err_msg"];
  8349. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8350. return [RAConvertor dict2data:resultDic];
  8351. }
  8352. }
  8353. // UISetting
  8354. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8355. NSString *cachefolder = [paths objectAtIndex:0];
  8356. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8357. NSData* json =nil;
  8358. json=[NSData dataWithContentsOfFile:img_cache];
  8359. NSError *error=nil;
  8360. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8361. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8362. [params setObject:setting forKey:@"setting"];
  8363. int section_count = 0;
  8364. // 0 Order Type 1 Shipping Method 2 Payment Information
  8365. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8366. // 0 Order Type
  8367. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8368. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8369. [ret setValue:order_type_dic forKey:key0];
  8370. // 1 Shipping Method
  8371. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8372. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8373. [ret setValue:shipping_method_dic forKey:key1];
  8374. // 2 Payment Information
  8375. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8376. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8377. [ret setValue:payment_info_dic forKey:key2];
  8378. // 3 Customer
  8379. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8380. NSDictionary *customer_dic = [self customerDic:params db:db];
  8381. [ret setValue:customer_dic forKey:key3];
  8382. // 4 Ship To
  8383. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8384. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8385. [ret setValue:ship_to_dic forKey:key4];
  8386. // 5 Ship From
  8387. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8388. if (![shipFromDisable integerValue]) {
  8389. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8390. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8391. [ret setValue:ship_from_dic forKey:key5];
  8392. }
  8393. // 6 Freight Bill To
  8394. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8395. if (![freightBillToDisable integerValue]) {
  8396. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8397. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8398. [ret setValue:freight_bill_to forKey:key6];
  8399. }
  8400. // 7 Merchandise Bill To
  8401. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8402. if (![merchandiseBillToDisable integerValue]) {
  8403. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8404. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8405. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8406. }
  8407. // 8 Return To
  8408. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8409. if (![returnToDisable integerValue]) {
  8410. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8411. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8412. [ret setValue:return_to_dic forKey:key8];
  8413. }
  8414. // 9 Model Information
  8415. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8416. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8417. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8418. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8419. [ret setValue:model_info_dic forKey:key9];
  8420. // 10 Remarks Content
  8421. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8422. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8423. [ret setValue:remarks_content_dic forKey:key10];
  8424. // 11 Order Total
  8425. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8426. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8427. [ret setValue:order_total_dic forKey:key11];
  8428. // 12 Signature
  8429. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8430. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8431. [ret setValue:sign_dic forKey:key12];
  8432. [ret setValue:@(section_count) forKey:@"section_count"];
  8433. [iSalesDB close_db:db];
  8434. return [RAConvertor dict2data:ret];
  8435. // return nil;
  8436. }
  8437. #pragma mark addr editor
  8438. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8439. NSMutableDictionary *new_item = [item mutableCopy];
  8440. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8441. return new_item;
  8442. }
  8443. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8444. {
  8445. // "is_subaction" = true;
  8446. // orderCode = MOB1608240002;
  8447. // password = 123456;
  8448. // "subaction_tag" = 1;
  8449. // user = EvanK;
  8450. // {
  8451. // "is_subaction" = true;
  8452. // orderCode = MOB1608240002;
  8453. // password = 123456;
  8454. // "refresh_trigger" = zipcode;
  8455. // "subaction_tag" = 1;
  8456. // user = EvanK;
  8457. // }
  8458. NSString *country_code = nil;
  8459. NSString *zipCode = nil;
  8460. NSString *stateCode = nil;
  8461. NSString *city = nil;
  8462. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8463. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8464. NSString *code_id = params[@"country"];
  8465. country_code = [self countryCodeByid:code_id];
  8466. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8467. NSString *zip_code = params[@"zipcode"];
  8468. // 剔除全部为空格
  8469. int spaceCount = 0;
  8470. for (int i = 0; i < zip_code.length; i++) {
  8471. if ([zip_code characterAtIndex:i] == ' ') {
  8472. spaceCount++;
  8473. }
  8474. }
  8475. if (spaceCount == zip_code.length) {
  8476. zip_code = @"";
  8477. }
  8478. zipCode = zip_code;
  8479. if (zipCode.length > 0) {
  8480. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8481. country_code = [dic valueForKey:@"country_code"];
  8482. if (!country_code) {
  8483. // country_code = @"US";
  8484. NSString *code_id = params[@"country"];
  8485. country_code = [self countryCodeByid:code_id];
  8486. }
  8487. stateCode = [dic valueForKey:@"state_code"];
  8488. if(!stateCode.length) {
  8489. stateCode = params[@"state"];
  8490. }
  8491. city = [dic valueForKey:@"city"];
  8492. if (!city.length) {
  8493. city = params[@"city"];
  8494. }
  8495. // zip code
  8496. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8497. // [zipDic setValue:zipCode forKey:@"value"];
  8498. // [section_0 setValue:zipDic forKey:@"item_11"];
  8499. } else {
  8500. NSString *code_id = params[@"country"];
  8501. country_code = [self countryCodeByid:code_id];
  8502. stateCode = params[@"state"];
  8503. city = params[@"city"];
  8504. }
  8505. }
  8506. }
  8507. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8508. // [ret removeObjectForKey:@"up_params"];
  8509. [ret setObject:@"New Address" forKey:@"title"];
  8510. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8511. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8512. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8513. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8514. // [country_dic removeObjectForKey:@"refresh"];
  8515. // [country_dic removeObjectForKey:@"restore"];
  8516. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8517. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8518. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8519. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8520. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8521. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8522. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8523. // [zip_code_dic removeObjectForKey:@"refresh"];
  8524. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8525. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8526. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8527. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8528. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8529. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8530. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8531. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8532. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8533. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8534. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8535. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8536. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8537. // country
  8538. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8539. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8540. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8541. // state
  8542. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8543. // NSDictionary *tmpDic = @{
  8544. // @"value" : @"Other",
  8545. // @"value_id" : @"",
  8546. // @"check" : [NSNumber numberWithInteger:0]
  8547. // };
  8548. //
  8549. // for (int i = 0; i < allState.allKeys.count; i++) {
  8550. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8551. //
  8552. // NSDictionary *tmp = allState[key];
  8553. // [allState setValue:tmpDic forKey:key];
  8554. // tmpDic = tmp;
  8555. // }
  8556. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8557. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8558. [ret setValue:new_section_0 forKey:@"section_0"];
  8559. return [RAConvertor dict2data:ret];
  8560. }
  8561. #pragma mark save addr
  8562. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8563. {
  8564. // NSString *companyName = [self valueInParams:params key:@"company"];
  8565. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8566. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8567. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8568. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8569. // NSString *countryId = [self valueInParams:params key:@"country"];
  8570. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8571. // NSString *city = [self valueInParams:params key:@"city"];
  8572. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8573. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8574. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8575. // NSString *phone = [self valueInParams:params key:@"phone"];
  8576. // NSString *fax = [self valueInParams:params key:@"fax"];
  8577. // NSString *email = [self valueInParams:params key:@"email"];
  8578. return [self offline_saveContact:params update:NO isCustomer:NO];
  8579. }
  8580. #pragma mark cancel order
  8581. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8582. {
  8583. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8584. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8585. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8586. if (order_id.length) {
  8587. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8588. }
  8589. int ret = [iSalesDB execSql:sql];
  8590. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8591. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8592. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8593. // [dic setValue:@"160409" forKey:@"min_ver"];
  8594. return [RAConvertor dict2data:dic];
  8595. }
  8596. #pragma mark sign order
  8597. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8598. {
  8599. //参考 offline_saveBusinesscard
  8600. DebugLog(@"sign order params: %@",params);
  8601. // orderCode = MOB1608240002;
  8602. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8603. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8604. orderCode = [self translateSingleQuote:orderCode];
  8605. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8606. picPath = [self translateSingleQuote:picPath];
  8607. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8608. int ret = [iSalesDB execSql:sql];
  8609. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8610. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8611. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8612. // [dic setValue:@"160409" forKey:@"min_ver"];
  8613. return [RAConvertor dict2data:dic];
  8614. }
  8615. #pragma mark save order
  8616. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8617. NSString *ret = [self valueInParams:params key:key];
  8618. if (translate) {
  8619. ret = [self translateSingleQuote:ret];
  8620. }
  8621. return ret;
  8622. }
  8623. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8624. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8625. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8626. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8627. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8628. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8629. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8630. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8631. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8632. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8633. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8634. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8635. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8636. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8637. if (![total_price isEqualToString:@""]) {
  8638. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8639. } else {
  8640. total_price = @"";
  8641. }
  8642. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8643. if ([paymentsAndCredits isEqualToString:@""]) {
  8644. paymentsAndCredits = @"";
  8645. } else {
  8646. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8647. }
  8648. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8649. if ([handling_fee_value isEqualToString:@""]) {
  8650. handling_fee_value = @"";
  8651. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8652. handling_fee_value = @"";
  8653. } else {
  8654. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8655. }
  8656. NSString *handling_fee_placeholder = handling_fee_value;
  8657. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8658. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8659. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8660. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8661. NSString *lift_gate_placeholder = @"";
  8662. if ([lift_gate_value isEqualToString:@""]) {
  8663. lift_gate_placeholder = @"";
  8664. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8665. lift_gate_placeholder = @"";
  8666. } else {
  8667. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8668. }
  8669. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8670. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8671. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8672. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8673. 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];
  8674. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8675. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8676. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8677. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8678. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8679. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8680. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8681. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8682. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8683. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8684. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8685. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8686. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8687. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8688. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8689. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8690. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8691. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8692. if (!number_nil) {
  8693. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8694. }
  8695. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8696. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8697. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8698. if (!security_code_nil) {
  8699. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8700. }
  8701. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8702. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8703. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8704. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8705. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8706. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8707. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8708. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8709. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8710. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8711. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8712. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8713. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8714. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8715. NSString *contact_id = customer_cid;
  8716. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8717. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8718. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8719. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8720. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8721. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8722. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8723. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8724. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8725. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8726. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8727. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8728. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8729. //
  8730. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8731. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8732. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8733. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8734. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8735. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8736. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8737. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8738. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8739. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8740. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8741. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8742. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8743. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8744. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8745. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8746. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8747. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8748. 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];
  8749. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8750. if (receive_cid.length) {
  8751. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8752. }
  8753. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8754. if (receive_name.length) {
  8755. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8756. }
  8757. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8758. if (receive_ext.length) {
  8759. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8760. }
  8761. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8762. if (receive_contact.length) {
  8763. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8764. }
  8765. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8766. if (receive_phone.length) {
  8767. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8768. }
  8769. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8770. if (receive_email.length) {
  8771. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8772. }
  8773. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8774. if (receive_fax.length) {
  8775. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8776. }
  8777. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8778. if (sender_cid.length) {
  8779. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8780. }
  8781. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8782. if (sender_name.length) {
  8783. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8784. }
  8785. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8786. if (sender_ext.length) {
  8787. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8788. }
  8789. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8790. if(sender_contact.length) {
  8791. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8792. }
  8793. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8794. if (sender_phone.length) {
  8795. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8796. }
  8797. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8798. if (sender_fax.length) {
  8799. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8800. }
  8801. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8802. if (sender_email.length) {
  8803. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8804. }
  8805. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8806. if (shipping_billto_cid.length) {
  8807. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8808. }
  8809. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8810. if (shipping_billto_name.length) {
  8811. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8812. }
  8813. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8814. if (shipping_billto_ext.length) {
  8815. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8816. }
  8817. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8818. if (shipping_billto_contact.length) {
  8819. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8820. }
  8821. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8822. if (shipping_billto_phone.length) {
  8823. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8824. }
  8825. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8826. if (shipping_billto_fax.length) {
  8827. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8828. }
  8829. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8830. if (shipping_billto_email.length) {
  8831. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8832. }
  8833. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8834. if (billing_cid.length) {
  8835. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8836. }
  8837. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8838. if (billing_name.length) {
  8839. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8840. }
  8841. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8842. if (billing_ext.length) {
  8843. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8844. }
  8845. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8846. if (billing_contact.length) {
  8847. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8848. }
  8849. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8850. if (billing_phone.length) {
  8851. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8852. }
  8853. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8854. if (billing_fax.length) {
  8855. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8856. }
  8857. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8858. if (billing_email.length) {
  8859. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8860. }
  8861. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8862. if (returnto_cid.length) {
  8863. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8864. }
  8865. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8866. if (returnto_name.length) {
  8867. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8868. }
  8869. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8870. if (returnto_ext.length) {
  8871. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8872. }
  8873. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8874. if (returnto_contact.length) {
  8875. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8876. }
  8877. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8878. if (returnto_phone.length) {
  8879. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8880. }
  8881. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8882. if (returnto_fax.length) {
  8883. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8884. }
  8885. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8886. if (returnto_email.length) {
  8887. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8888. }
  8889. NSString *order_status = @"status = 1,";
  8890. if (submit) {
  8891. order_status = @"status = -11,";
  8892. }
  8893. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8894. NSString *sync_sql = @"";
  8895. if (submit) {
  8896. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8897. [param setValue:sales_rep forKey:@"sales_rep"];
  8898. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  8899. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8900. }
  8901. 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];
  8902. DebugLog(@"save order contactSql: %@",contactSql);
  8903. DebugLog(@"save order orderSql: %@",orderSql);
  8904. // int contact_ret = [iSalesDB execSql:contactSql];
  8905. int order_ret = [iSalesDB execSql:orderSql];
  8906. int ret = order_ret;
  8907. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8908. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8909. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8910. // [dic setValue:@"160409" forKey:@"min_ver"];
  8911. [dic setObject:so_id forKey:@"so#"];
  8912. return [RAConvertor dict2data:dic];
  8913. }
  8914. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8915. {
  8916. // id foo = nil;
  8917. // NSMutableArray *a = @[].mutableCopy;
  8918. // [a addObject:foo];
  8919. return [self saveorder:param submit:NO];
  8920. }
  8921. #pragma mark add to cart by name
  8922. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8923. {
  8924. NSString *orderCode = [params objectForKey:@"orderCode"];
  8925. NSString *product_name = [params objectForKey:@"product_name"];
  8926. product_name = [self translateSingleQuote:product_name];
  8927. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8928. bool search_upc = [params[@"search_upc"] boolValue];
  8929. sqlite3 *db = [iSalesDB get_db];
  8930. __block NSMutableString *product_id_string = [NSMutableString string];
  8931. for (int i = 0; i < product_name_array.count; i++) {
  8932. NSString *name = [product_name_array objectAtIndex:i];
  8933. NSString *sql =nil;
  8934. if(search_upc)
  8935. sql= [NSString stringWithFormat:@"select product_id from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",name,name,name,name];
  8936. else
  8937. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8938. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8939. int product_id = sqlite3_column_int(stmt, 0);
  8940. if (i == product_name_array.count - 1) {
  8941. [product_id_string appendFormat:@"%d",product_id];
  8942. } else {
  8943. [product_id_string appendFormat:@"%d,",product_id];
  8944. }
  8945. }];
  8946. }
  8947. if (!orderCode) {
  8948. orderCode = @"";
  8949. }
  8950. NSDictionary *newParams = @{
  8951. @"product_id" : product_id_string,
  8952. @"orderCode" : orderCode,
  8953. @"can_create_backorder":params[@"can_create_backorder"]
  8954. };
  8955. [iSalesDB close_db:db];
  8956. return [self offline_add2cart:[newParams mutableCopy]];
  8957. }
  8958. #pragma mark reset order
  8959. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8960. {
  8961. // UIApplication * app = [UIApplication sharedApplication];
  8962. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8963. [iSalesDB disable_trigger];
  8964. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8965. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8966. [iSalesDB enable_trigger];
  8967. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8968. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8969. return [RAConvertor dict2data:dic];
  8970. }
  8971. #pragma mark submit order
  8972. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8973. {
  8974. return [self saveorder:params submit:YES];
  8975. }
  8976. #pragma mark copy order
  8977. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8978. {
  8979. NSMutableDictionary *ret = @{}.mutableCopy;
  8980. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8981. sqlite3 *db = [iSalesDB get_db];
  8982. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8983. // 首先查看联系人是否active
  8984. 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];
  8985. __block int customer_is_active = 1;
  8986. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8987. customer_is_active = sqlite3_column_int(stmt, 0);
  8988. }];
  8989. if (!customer_is_active) {
  8990. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8991. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8992. [iSalesDB close_db:db];
  8993. return [RAConvertor dict2data:ret];
  8994. }
  8995. // new order
  8996. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8997. NSString *new_order_code = [self get_offline_soid:db];
  8998. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8999. user = [self translateSingleQuote:user];
  9000. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  9001. __block int result = 1;
  9002. result = [iSalesDB execSql:insert_order_sql db:db];
  9003. if (!result) {
  9004. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9005. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9006. [iSalesDB close_db:db];
  9007. return [RAConvertor dict2data:ret];
  9008. }
  9009. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9010. // __block NSString *product_id = @"";
  9011. __weak typeof(self) weakSelf = self;
  9012. 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];
  9013. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9014. int is_active = sqlite3_column_int(stmt, 1);
  9015. if (is_active) {
  9016. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9017. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9018. 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];
  9019. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9020. }
  9021. }];
  9022. // product_id = [product_id substringFromIndex:1];
  9023. //
  9024. // [self offline_add2cart:@{}.mutableCopy];
  9025. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9026. [iSalesDB close_db:db];
  9027. if (result) {
  9028. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9029. } else {
  9030. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9031. }
  9032. [ret setObject:new_order_code forKey:@"so_id"];
  9033. return [RAConvertor dict2data:ret];
  9034. }
  9035. #pragma mark move wish list to cart
  9036. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9037. assert(params[@"can_create_backorder"]!=nil);
  9038. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9039. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9040. NSString *collectId = params[@"collectId"];
  9041. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  9042. __block NSString *product_id = @"";
  9043. __block NSString *qty = @"";
  9044. __block int number_of_outOfStock = 0;
  9045. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9046. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9047. int productId = sqlite3_column_int(stmt, 0);
  9048. int item_qty = sqlite3_column_int(stmt, 1);
  9049. int availability = sqlite3_column_int(stmt, 2);
  9050. int _id = sqlite3_column_int(stmt, 3);
  9051. if (![params[@"can_create_backorder"] boolValue]) {
  9052. // 库存小于购买量为缺货
  9053. if (availability >= item_qty) {
  9054. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9055. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9056. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9057. } else {
  9058. number_of_outOfStock++;
  9059. }
  9060. } else {
  9061. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9062. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9063. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9064. }
  9065. }];
  9066. NSMutableDictionary *retDic = nil;
  9067. if (![params[@"can_create_backorder"] boolValue]) {
  9068. if (delete_collectId.count == 0) {
  9069. retDic = [NSMutableDictionary dictionary];
  9070. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9071. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9072. return [RAConvertor dict2data:retDic];
  9073. }
  9074. }
  9075. if (product_id.length > 1) {
  9076. product_id = [product_id substringFromIndex:1];
  9077. }
  9078. if (qty.length > 1) {
  9079. qty = [qty substringFromIndex:1];
  9080. }
  9081. NSString *orderCode = params[@"orderCode"];
  9082. if (!orderCode) {
  9083. orderCode = @"";
  9084. }
  9085. NSDictionary *newParams = @{
  9086. @"product_id" : product_id,
  9087. @"orderCode" : orderCode,
  9088. @"qty" : qty,
  9089. @"can_create_backorder":params[@"can_create_backorder"]
  9090. };
  9091. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9092. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9093. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9094. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9095. NSInteger ret = [wish_return[@"result"] intValue];
  9096. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9097. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9098. }
  9099. if (![params[@"can_create_backorder"] boolValue]) {
  9100. if (number_of_outOfStock > 0) {
  9101. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9102. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9103. }
  9104. }
  9105. return [RAConvertor dict2data:retDic];
  9106. }
  9107. #pragma mark - portfolio
  9108. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9109. // assert(params[@"contact_id"]!=nil);
  9110. assert(params[@"user"]!=nil);
  9111. assert(params[@"can_see_price"]!=nil);
  9112. int sort = [[params valueForKey:@"sort"] intValue];
  9113. int offset = [[params valueForKey:@"offset"] intValue];
  9114. int limit = [[params valueForKey:@"limit"] intValue];
  9115. NSString *orderBy = @"";
  9116. switch (sort) {
  9117. case 0:{
  9118. orderBy = @"p.modify_time desc";
  9119. }
  9120. break;
  9121. case 1:{
  9122. orderBy = @"modify_time asc";
  9123. }
  9124. break;
  9125. case 2:{
  9126. orderBy = @"p.name asc";
  9127. }
  9128. break;
  9129. case 3:{
  9130. orderBy = @"p.name desc";
  9131. }
  9132. break;
  9133. case 4:{
  9134. orderBy = @"p.description asc";
  9135. }
  9136. break;
  9137. case 5: {
  9138. orderBy = @"m.default_category asc";
  9139. }
  9140. default:
  9141. break;
  9142. }
  9143. // 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];
  9144. NSString *sql = [NSString stringWithFormat:@"select * from (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,p.modify_time 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 limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  9145. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9146. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9147. sqlite3 *db = [iSalesDB get_db];
  9148. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9149. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9150. int product_id = sqlite3_column_int(stmt, 0);
  9151. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9152. NSString *name = [self textAtColumn:1 statement:stmt];
  9153. NSString *description = [self textAtColumn:2 statement:stmt];
  9154. double price = sqlite3_column_double(stmt, 3);
  9155. double discount = sqlite3_column_double(stmt,4);
  9156. int qty = sqlite3_column_int(stmt, 5);
  9157. int percentage = sqlite3_column_int(stmt, 6);
  9158. int item_id = sqlite3_column_int(stmt, 7);
  9159. // int fashion_id = sqlite3_column_int(stmt, 8);
  9160. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9161. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9162. double percent = sqlite3_column_double(stmt, 11);
  9163. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9164. if ([price_null isEqualToString:@"null"]) {
  9165. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9166. }
  9167. NSMutableDictionary *item = @{
  9168. @"linenotes": line_note,
  9169. @"check": @(1),
  9170. @"product_id": product_id_string,
  9171. @"available_qty": @(qty),
  9172. @"available_percent" : @(percent),
  9173. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9174. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9175. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9176. @"img": img_path,
  9177. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9178. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9179. }.mutableCopy;
  9180. if (percentage) {
  9181. [item removeObjectForKey:@"available_qty"];
  9182. } else {
  9183. [item removeObjectForKey:@"available_percent"];
  9184. }
  9185. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9186. if ([qty_null isEqualToString:@"null"]) {
  9187. [item removeObjectForKey:@"available_qty"];
  9188. }
  9189. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9190. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9191. }];
  9192. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9193. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9194. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9195. }
  9196. [iSalesDB close_db:db];
  9197. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9198. [dic setValue:@"" forKey:@"email_content"];
  9199. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9200. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9201. return [RAConvertor dict2data:dic];
  9202. }
  9203. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9204. __block int qty = 0;
  9205. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9206. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9207. qty = sqlite3_column_int(stmt, 0);
  9208. }];
  9209. return qty;
  9210. }
  9211. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9212. {
  9213. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9214. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9215. // NSMutableDictionary * values = params[@"replaceValue"];
  9216. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9217. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9218. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9219. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9220. NSString *pdf_path = @"";
  9221. if (direct) {
  9222. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9223. } else {
  9224. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9225. }
  9226. NSString *create_user = [self valueInParams:params key:@"user"];
  9227. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9228. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9229. // model info
  9230. // 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];
  9231. // V1.90 more color
  9232. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  9233. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9234. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9235. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9236. sqlite3 *db = [iSalesDB get_db];
  9237. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9238. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9239. int product_id = sqlite3_column_int(stmt, 0);
  9240. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9241. double price = sqlite3_column_double(stmt, 1);
  9242. double discount = sqlite3_column_double(stmt,2);
  9243. int qty = sqlite3_column_int(stmt, 3);
  9244. int percentage = sqlite3_column_int(stmt, 4);
  9245. int item_id = sqlite3_column_int(stmt, 5);
  9246. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9247. double percent = sqlite3_column_double(stmt, 7);
  9248. int more_color = sqlite3_column_int(stmt, 8);
  9249. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9250. /* if ([price_null isEqualToString:@"null"]) {
  9251. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9252. }
  9253. */
  9254. [product_ids_string appendFormat:@"%@,",product_id_string];
  9255. // Regular Price
  9256. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9257. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9258. // QTY
  9259. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9260. if ([qty_null isEqualToString:@"null"]) {
  9261. // 查available
  9262. qty = [self model_QTY:product_id_string db:db];
  9263. }
  9264. if (percentage) {
  9265. qty = qty * percent / 100;
  9266. }
  9267. // Special Price
  9268. if ([price_null isEqualToString:@"null"]) {
  9269. // price = regular price
  9270. price = [regular_price_str doubleValue];
  9271. }
  9272. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9273. if (![discount_null isEqualToString:@"null"]) {
  9274. price = price * (1 - discount / 100.0);
  9275. }
  9276. NSMutableDictionary *item = @{
  9277. @"line_note": line_note,
  9278. @"product_id": product_id_string,
  9279. @"available_qty": @(qty),
  9280. @"more_color":@(more_color),
  9281. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9282. @"regular_price" : regular_price_str,
  9283. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9284. }.mutableCopy;
  9285. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9286. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9287. }];
  9288. [iSalesDB close_db:db];
  9289. if (product_ids_string.length > 0) {
  9290. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9291. }
  9292. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9293. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9294. return [RAConvertor dict2data:resultDictionary];
  9295. }
  9296. NSString *model_info = [RAConvertor dict2string:dic];
  9297. // 创建PDF
  9298. // 在preview情况下保存,则不需要新建了
  9299. if (direct) {
  9300. NSMutableDictionary *tear_sheet_params = params;
  9301. // if (tear_sheet_id) {
  9302. // tear_sheet_params = values;
  9303. // }
  9304. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9305. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9306. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9307. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9308. resultDictionary = pdfInfo.mutableCopy;
  9309. } else { // 创建PDF失败
  9310. return pdfData;
  9311. }
  9312. } else {
  9313. // pdf_path 就是本地路径
  9314. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9315. }
  9316. // 将文件移动到PDF Cache文件夹
  9317. NSString *newPath = [pdf_path lastPathComponent];
  9318. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9319. NSString *cachefolder = [paths objectAtIndex:0];
  9320. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9321. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9322. NSFileManager *fileManager = [NSFileManager defaultManager];
  9323. NSError *error = nil;
  9324. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9325. if (error) { // 移动文件失败
  9326. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9327. return [RAConvertor dict2data:resultDictionary];
  9328. }
  9329. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9330. pdf_path = [newPath lastPathComponent];
  9331. // 保存信息
  9332. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9333. NSString *off_params = [RAConvertor dict2string:params];
  9334. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9335. pdf_path = [self translateSingleQuote:pdf_path];
  9336. create_user = [self translateSingleQuote:create_user];
  9337. tear_note = [self translateSingleQuote:tear_note];
  9338. tear_name = [self translateSingleQuote:tear_name];
  9339. model_info = [self translateSingleQuote:model_info];
  9340. configureParams = [self translateSingleQuote:configureParams];
  9341. off_params = [self translateSingleQuote:off_params];
  9342. 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];
  9343. if (tear_sheet_id) {
  9344. int _id = [tear_sheet_id intValue];
  9345. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9346. }
  9347. int result = [iSalesDB execSql:save_pdf_sql];
  9348. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9349. //
  9350. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9351. if (remove_Item) {
  9352. // portfolioId
  9353. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9354. }
  9355. return [RAConvertor dict2data:resultDictionary];
  9356. }
  9357. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9358. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9359. }
  9360. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9361. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9362. }
  9363. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9364. int offset = [[params valueForKey:@"offset"] intValue];
  9365. int limit = [[params valueForKey:@"limit"] intValue];
  9366. NSString *keyword = [params valueForKey:@"keyWord"];
  9367. NSString *where = @"where is_delete is null or is_delete = 0";
  9368. if (keyword.length) {
  9369. keyword = [self translateSingleQuote:keyword];
  9370. 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];
  9371. }
  9372. 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
  9373. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9374. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9375. NSString *cachefolder = [paths objectAtIndex:0];
  9376. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9377. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9378. NSString *name = [self textAtColumn:0 statement:stmt];
  9379. NSString *note = [self textAtColumn:1 statement:stmt];
  9380. NSString *time = [self textAtColumn:2 statement:stmt];
  9381. NSString *user = [self textAtColumn:3 statement:stmt];
  9382. NSString *path = [self textAtColumn:4 statement:stmt];
  9383. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9384. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9385. path = [pdfFolder stringByAppendingPathComponent:path];
  9386. BOOL bdir=NO;
  9387. NSFileManager* fileManager = [NSFileManager defaultManager];
  9388. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9389. {
  9390. //pdf文件不存在
  9391. path=nil;
  9392. }
  9393. time = [self changeDateTimeFormate:time];
  9394. time = [time stringByAppendingString:@" PST"];
  9395. int sheet_id = sqlite3_column_int(stmt, 5);
  9396. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9397. item[@"tearsheetsId"]=@(sheet_id);
  9398. item[@"pdf_path"]=path;
  9399. item[@"create_time"]=time;
  9400. item[@"create_user"]=user;
  9401. item[@"tear_note"]=note;
  9402. item[@"tear_name"]=name;
  9403. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9404. item[@"model_info"]=model_info;
  9405. item[@"off_params"]=off_params;
  9406. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9407. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9408. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9409. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9410. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9411. }];
  9412. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9413. return [RAConvertor dict2data:dic];
  9414. }
  9415. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9416. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9417. // NSString *user = [params objectForKey:@"user"];
  9418. // 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];
  9419. 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];
  9420. int result = [iSalesDB execSql:sql];
  9421. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9422. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9423. if (result == RESULT_TRUE) {
  9424. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9425. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9426. sqlite3 *db = [iSalesDB get_db];
  9427. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9428. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9429. [iSalesDB close_db:db];
  9430. }
  9431. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9432. return [RAConvertor dict2data:dic];
  9433. }
  9434. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9435. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9436. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9437. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9438. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9439. NSDictionary *company_item = @{
  9440. @"control": @"text",
  9441. @"keyboard": @"text",
  9442. @"name": @"company_name",
  9443. @"value": COMPANY_FULL_NAME,
  9444. @"aname": @"Company Name"
  9445. };
  9446. [section_0 setObject:company_item forKey:@"item_0"];
  9447. [dic setObject:section_0 forKey:@"section_0"];
  9448. // Regurlar Price
  9449. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9450. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9451. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9452. __block long val_count = 0;
  9453. NSString *sql = @"select name,type,order_by from price order by order_by";
  9454. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9455. NSString *name = [self textAtColumn:0 statement:stmt];
  9456. int type = sqlite3_column_int(stmt, 1);
  9457. int order_by = sqlite3_column_int(stmt, 2);
  9458. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9459. NSDictionary *price_dic = @{
  9460. @"value" : name,
  9461. @"value_id" : [NSNumber numberWithInteger:type],
  9462. @"check" : order_by == 0 ? @(1) : @(0)
  9463. };
  9464. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9465. val_count = ++(*count);
  9466. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9467. val_count = 0;
  9468. }];
  9469. [cadedate setObject:@{@"value" : @"None",
  9470. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9471. val_count++;
  9472. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9473. [price setObject:cadedate forKey:@"cadedate"];
  9474. [section1 setObject:price forKey:@"item_2"];
  9475. [dic setObject:section1 forKey:@"section_1"];
  9476. return [RAConvertor dict2data:dic];
  9477. }
  9478. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9479. NSString *product_id = [params objectForKey:@"fashionId"];
  9480. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9481. __block int result = RESULT_TRUE;
  9482. __block int qty = 0;
  9483. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9484. qty = sqlite3_column_int(stmt, 0);
  9485. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9486. result = RESULT_FALSE;
  9487. }];
  9488. NSMutableDictionary *dic = @{
  9489. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9490. @"mode" : @"Regular Mode",
  9491. @"quantity_available" : @(qty),
  9492. @"result" : @(result),
  9493. }.mutableCopy;
  9494. return [RAConvertor dict2data:dic];
  9495. }
  9496. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9497. NSString *item_ids = [params objectForKey:@"item_id"];
  9498. NSString *line_notes = [params objectForKey:@"notes"];
  9499. NSString *price_str = [params objectForKey:@"price"];
  9500. NSString *discount_str = [params objectForKey:@"discount"];
  9501. NSString *percent = [params objectForKey:@"available_percent"];
  9502. NSString *qty = [params objectForKey:@"available_qty"];
  9503. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9504. int dot = 0;
  9505. if (line_notes) {
  9506. line_notes = [self translateSingleQuote:line_notes];
  9507. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9508. sql = [sql stringByAppendingString:line_notes];
  9509. dot = 1;
  9510. } else {
  9511. line_notes = @"";
  9512. }
  9513. if (price_str) {
  9514. if (dot) {
  9515. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9516. } else {
  9517. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9518. dot = 1;
  9519. }
  9520. sql = [sql stringByAppendingString:price_str];
  9521. } else {
  9522. price_str = @"";
  9523. }
  9524. if (discount_str) {
  9525. if (dot) {
  9526. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9527. } else {
  9528. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9529. dot = 1;
  9530. }
  9531. sql = [sql stringByAppendingString:discount_str];
  9532. } else {
  9533. discount_str = @"";
  9534. }
  9535. if (percent) {
  9536. if (dot) {
  9537. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9538. } else {
  9539. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9540. dot = 1;
  9541. }
  9542. sql = [sql stringByAppendingString:percent];
  9543. } else {
  9544. percent = @"";
  9545. }
  9546. if (qty) {
  9547. if (dot) {
  9548. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9549. } else {
  9550. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9551. dot = 1;
  9552. }
  9553. sql = [sql stringByAppendingString:qty];
  9554. } else {
  9555. qty = @"";
  9556. }
  9557. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9558. sql = [sql stringByAppendingString:where];
  9559. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9560. int result = [iSalesDB execSql:sql];
  9561. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9562. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9563. return [RAConvertor dict2data:dic];
  9564. }
  9565. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9566. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9567. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9568. sqlite3 *db = [iSalesDB get_db];
  9569. 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];
  9570. int result = [iSalesDB execSql:sql db:db];
  9571. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9572. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9573. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9574. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9575. [iSalesDB close_db:db];
  9576. return [RAConvertor dict2data:dic];
  9577. }
  9578. + (void)offline_removePDFWithName:(NSString *)name {
  9579. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9580. NSString *cachefolder = [paths objectAtIndex:0];
  9581. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9582. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9583. NSFileManager *fileManager = [NSFileManager defaultManager];
  9584. [fileManager removeItemAtPath:path error:nil];
  9585. }
  9586. + (void)offline_clear_PDFCache {
  9587. NSFileManager *fileManager = [NSFileManager defaultManager];
  9588. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9589. NSString *cachefolder = [paths objectAtIndex:0];
  9590. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9591. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9592. for (NSString *path in pdf_files) {
  9593. [self offline_removePDFWithName:[path lastPathComponent]];
  9594. }
  9595. }
  9596. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9597. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9598. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9599. NSString *user = [params objectForKey:@"user"];
  9600. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  9601. if (![create_user isEqualToString:user]) {
  9602. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9603. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9604. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9605. return [RAConvertor dict2data:dic];
  9606. }
  9607. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9608. __block int is_local = 0;
  9609. __block NSString *path = @"";
  9610. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9611. is_local = sqlite3_column_int(stmt, 0);
  9612. path = [self textAtColumn:1 statement:stmt];
  9613. }];
  9614. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9615. if (is_local == 1) {
  9616. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9617. // 删除文件
  9618. [self offline_removePDFWithName:path];
  9619. }
  9620. int result = [iSalesDB execSql:sql];
  9621. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9622. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9623. return [RAConvertor dict2data:dic];
  9624. }
  9625. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  9626. {
  9627. // assert(add_params[@"contact_id"]!=nil);
  9628. assert(add_params[@"user"]!=nil);
  9629. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9630. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9631. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9632. NSString* where=@"1=1";
  9633. if (ver!=nil) {
  9634. where=@"is_dirty=1";
  9635. }
  9636. 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];
  9637. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9638. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9639. sqlite3 *db = [iSalesDB get_db];
  9640. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9641. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9642. NSInteger _id = sqlite3_column_int(stmt, 0);
  9643. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9644. NSString *name = [self textAtColumn:2 statement:stmt];
  9645. NSString *desc = [self textAtColumn:3 statement:stmt];
  9646. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9647. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9648. NSInteger qty = sqlite3_column_int(stmt, 6);
  9649. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9650. double percent = sqlite3_column_double(stmt, 8);
  9651. double price = sqlite3_column_double(stmt, 9);
  9652. double discount = sqlite3_column_double(stmt, 10);
  9653. NSString *img = [self textAtColumn:11 statement:stmt];
  9654. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9655. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9656. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9657. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9658. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9659. if ([price_null isEqualToString:@"null"]) {
  9660. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9661. }
  9662. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9663. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9664. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9665. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9666. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9667. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9668. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9669. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9670. if ([qty_null isEqualToString:@"null"]) {
  9671. // [item setValue:@"null" forKey:@"available_qty"];
  9672. } else {
  9673. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9674. }
  9675. if ([is_percent_null isEqualToString:@"null"]) {
  9676. // [item setValue:@"null" forKey:@"percentage"];
  9677. } else {
  9678. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9679. }
  9680. if ([percent_null isEqualToString:@"null"]) {
  9681. // [item setValue:@"null" forKey:@"percent"];
  9682. } else {
  9683. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9684. }
  9685. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9686. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9687. [item setValue:name forKey:@"name"];
  9688. [item setValue:desc forKey:@"description"];
  9689. [item setValue:img forKey:@"img"];
  9690. [item setValue:line_note forKey:@"line_note"];
  9691. [item setValue:create_time forKey:@"createtime"];
  9692. [item setValue:modify_time forKey:@"modifytime"];
  9693. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9694. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9695. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9696. }];
  9697. [iSalesDB close_db:db];
  9698. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9699. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9700. }
  9701. return ret;
  9702. }
  9703. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9704. {
  9705. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9706. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9707. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9708. NSString* where=@"1=1";
  9709. if (ver!=nil) {
  9710. where=@"is_dirty=1";
  9711. }
  9712. 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];
  9713. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9714. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9715. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9716. NSInteger _id = sqlite3_column_int(stmt, 0);
  9717. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9718. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9719. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9720. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9721. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9722. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9723. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9724. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9725. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9726. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9727. NSString *uuid = [NSUUID UUID].UUIDString;
  9728. int is_delete = sqlite3_column_int(stmt, 11);
  9729. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9730. int is_local = sqlite3_column_int(stmt, 12);
  9731. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9732. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9733. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9734. }
  9735. [item setObject:pdf_path forKey:@"pdf_path"];
  9736. [item setObject:create_user forKey:@"create_user"];
  9737. [item setObject:tear_note forKey:@"tear_note"];
  9738. [item setObject:tear_name forKey:@"tear_name"];
  9739. [item setObject:model_info forKey:@"model_info"];
  9740. [item setObject:createtime forKey:@"createtime"];
  9741. [item setObject:modifytime forKey:@"modifytime"];
  9742. [item setObject:urlParams forKey:@"urlParams"];
  9743. [item setObject:off_params forKey:@"off_params"];
  9744. [item setObject:uuid forKey:@"pdf_token"];
  9745. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9746. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  9747. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9748. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9749. } else {
  9750. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9751. }
  9752. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9753. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9754. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9755. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9756. }];
  9757. return ret;
  9758. }
  9759. #pragma mark 2020
  9760. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  9761. {
  9762. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  9763. sqlite3 *db = [iSalesDB get_db];
  9764. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  9765. if (contactType) {
  9766. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  9767. } else {
  9768. contactType = @"1 = 1";
  9769. }
  9770. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  9771. DebugLog(@"offline contact list keyword: %@",keyword);
  9772. // advanced search
  9773. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  9774. if (contact_name) {
  9775. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9776. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  9777. } else {
  9778. contact_name = @"";
  9779. }
  9780. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  9781. if (customer_phone) {
  9782. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9783. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  9784. } else {
  9785. customer_phone = @"";
  9786. }
  9787. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  9788. if (customer_fax) {
  9789. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9790. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  9791. } else {
  9792. customer_fax = @"";
  9793. }
  9794. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  9795. if (customer_zipcode) {
  9796. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9797. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  9798. } else {
  9799. customer_zipcode = @"";
  9800. }
  9801. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  9802. if (customer_sales_rep) {
  9803. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9804. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  9805. } else {
  9806. customer_sales_rep = @"";
  9807. }
  9808. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  9809. if (customer_state) {
  9810. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9811. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  9812. } else {
  9813. customer_state = @"";
  9814. }
  9815. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  9816. if (customer_name) {
  9817. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9818. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  9819. } else {
  9820. customer_name = @"";
  9821. }
  9822. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  9823. if (customer_country) {
  9824. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9825. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  9826. } else {
  9827. customer_country = @"";
  9828. }
  9829. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  9830. if (customer_cid) {
  9831. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9832. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  9833. } else {
  9834. customer_cid = @"";
  9835. }
  9836. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  9837. if (customer_city) {
  9838. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9839. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  9840. } else {
  9841. customer_city = @"";
  9842. }
  9843. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  9844. if (customer_address) {
  9845. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9846. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  9847. } else {
  9848. customer_address = @"";
  9849. }
  9850. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  9851. if (customer_email) {
  9852. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9853. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  9854. } else {
  9855. customer_email = @"";
  9856. }
  9857. NSString *price_name = [params valueForKey:@"price_name"];
  9858. if (price_name) {
  9859. if ([price_name containsString:@","]) {
  9860. // 首先从 price表中查处name
  9861. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  9862. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  9863. for (int i = 1;i < pArray.count;i++) {
  9864. NSString *p = pArray[i];
  9865. [mutablePStr appendFormat:@" or type = %@ ",p];
  9866. }
  9867. [mutablePStr appendString:@";"];
  9868. __block NSMutableArray *price_name_array = [NSMutableArray array];
  9869. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9870. char *name = (char *)sqlite3_column_text(stmt, 0);
  9871. if (!name)
  9872. name = "";
  9873. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  9874. }];
  9875. // 再根据name 拼sql
  9876. NSMutableString *mutable_price_name = [NSMutableString string];
  9877. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  9878. for (int i = 1; i < price_name_array.count; i++) {
  9879. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  9880. }
  9881. [mutable_price_name appendString:@")"];
  9882. price_name = mutable_price_name;
  9883. } else {
  9884. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  9885. if ([price_name isEqualToString:@""]) {
  9886. price_name = @"";
  9887. } else {
  9888. __block NSString *price;
  9889. price_name = [self translateSingleQuote:price_name];
  9890. [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) {
  9891. char *p = (char *)sqlite3_column_text(stmt, 0);
  9892. if (p == NULL) {
  9893. p = "";
  9894. }
  9895. price = [NSString stringWithUTF8String:p];
  9896. }];
  9897. if ([price isEqualToString:@""]) {
  9898. price_name = @"";
  9899. } else {
  9900. price = [self translateSingleQuote:price];
  9901. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  9902. }
  9903. }
  9904. }
  9905. } else {
  9906. price_name = @"";
  9907. }
  9908. int limit = [[params valueForKey:@"limit"] intValue];
  9909. int offset = [[params valueForKey:@"offset"] intValue];
  9910. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9911. 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];
  9912. 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];
  9913. // int result= [iSalesDB AddExFunction:db];
  9914. int count =0;
  9915. NSString *sqlQuery = nil;
  9916. if(keyword.length==0)
  9917. {
  9918. // 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];
  9919. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  9920. sqlQuery = sql;
  9921. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  9922. }
  9923. else
  9924. {
  9925. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  9926. keyword = keyword.lowercaseString;
  9927. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9928. 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];
  9929. 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]];
  9930. }
  9931. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  9932. sqlite3_stmt * statement;
  9933. [ret setValue:@"2" forKey:@"result"];
  9934. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  9935. // sqlQuery = @"select editable,decrypt(company_name),country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  9936. int i = 0;
  9937. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  9938. {
  9939. while (sqlite3_step(statement) == SQLITE_ROW)
  9940. {
  9941. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  9942. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  9943. int editable = sqlite3_column_int(statement, 0);
  9944. char *company_name = (char*)sqlite3_column_text(statement, 1);
  9945. NSString *nscompany_name =nil;
  9946. if(company_name==nil)
  9947. nscompany_name=@"";
  9948. else
  9949. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  9950. char *country = (char*)sqlite3_column_text(statement, 2);
  9951. if(country==nil)
  9952. country="";
  9953. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  9954. // char *addr = (char*)sqlite3_column_text(statement, 3);
  9955. // if(addr==nil)
  9956. // addr="";
  9957. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  9958. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  9959. if(zipcode==nil)
  9960. zipcode="";
  9961. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  9962. char *state = (char*)sqlite3_column_text(statement, 5);
  9963. if(state==nil)
  9964. state="";
  9965. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  9966. char *city = (char*)sqlite3_column_text(statement, 6);
  9967. if(city==nil)
  9968. city="";
  9969. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  9970. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  9971. // NSString *nscontact_name = nil;
  9972. // if(contact_name==nil)
  9973. // nscontact_name=@"";
  9974. // else
  9975. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  9976. char *phone = (char*)sqlite3_column_text(statement, 8);
  9977. NSString *nsphone = nil;
  9978. if(phone==nil)
  9979. nsphone=@"";
  9980. else
  9981. nsphone= [[NSString alloc]initWithUTF8String:phone];
  9982. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  9983. if(contact_id==nil)
  9984. contact_id="";
  9985. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  9986. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  9987. if(addr_1==nil)
  9988. addr_1="";
  9989. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  9990. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  9991. if(addr_2==nil)
  9992. addr_2="";
  9993. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  9994. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  9995. if(addr_3==nil)
  9996. addr_3="";
  9997. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  9998. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  9999. if(addr_4==nil)
  10000. addr_4="";
  10001. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10002. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10003. if(first_name==nil)
  10004. first_name="";
  10005. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10006. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10007. if(last_name==nil)
  10008. last_name="";
  10009. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10010. char *fax = (char*)sqlite3_column_text(statement, 16);
  10011. NSString *nsfax = nil;
  10012. if(fax==nil)
  10013. nsfax=@"";
  10014. else
  10015. {
  10016. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10017. if(nsfax.length>0)
  10018. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10019. }
  10020. char *email = (char*)sqlite3_column_text(statement, 17);
  10021. NSString *nsemail = nil;
  10022. if(email==nil)
  10023. nsemail=@"";
  10024. else
  10025. {
  10026. nsemail= [[NSString alloc]initWithUTF8String:email];
  10027. if(nsemail.length>0)
  10028. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10029. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10030. }
  10031. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10032. [arr_name addObject:nsfirst_name];
  10033. [arr_name addObject:nslast_name];
  10034. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10035. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10036. {
  10037. // decrypt
  10038. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10039. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10040. nsphone=[AESCrypt fastdecrypt:nsphone];
  10041. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10042. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10043. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10044. }
  10045. [arr_addr addObject:nscompany_name];
  10046. [arr_addr addObject:nscontact_name];
  10047. [arr_addr addObject:@"<br>"];
  10048. [arr_addr addObject:nsaddr_1];
  10049. [arr_addr addObject:nsaddr_2];
  10050. [arr_addr addObject:nsaddr_3];
  10051. [arr_addr addObject:nsaddr_4];
  10052. //[arr_addr addObject:nsaddr];
  10053. [arr_addr addObject:nszipcode];
  10054. [arr_addr addObject:nscity];
  10055. [arr_addr addObject:nsstate];
  10056. [arr_addr addObject:nscountry];
  10057. [arr_addr addObject:@"<br>"];
  10058. [arr_addr addObject:nsphone];
  10059. [arr_addr addObject:nsfax];
  10060. [arr_addr addObject:nsemail];
  10061. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10062. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10063. [item setValue:name forKey:@"name"];
  10064. [item setValue:nscontact_id forKey:@"contact_id"];
  10065. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10066. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10067. i++;
  10068. }
  10069. sqlite3_finalize(statement);
  10070. }
  10071. [iSalesDB close_db:db];
  10072. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10073. dispatch_async(dispatch_get_main_queue(), ^{
  10074. UIApplication * app = [UIApplication sharedApplication];
  10075. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10076. [ret setValue:appDelegate.mode forKey:@"mode"];
  10077. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10078. result(ret);
  10079. });
  10080. return ;
  10081. });
  10082. }
  10083. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10084. //{
  10085. //
  10086. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10087. //
  10088. // NSString* contactId = [params valueForKey:@"contactId"];
  10089. //
  10090. //
  10091. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10092. //
  10093. //
  10094. //
  10095. //
  10096. //
  10097. //
  10098. // sqlite3 *db = [iSalesDB get_db];
  10099. //
  10100. //
  10101. //
  10102. //
  10103. //
  10104. // NSString *sqlQuery = nil;
  10105. //
  10106. //
  10107. // {
  10108. // 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];
  10109. //
  10110. // }
  10111. //
  10112. //
  10113. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10114. // sqlite3_stmt * statement;
  10115. //
  10116. //
  10117. // [ret setValue:@"2" forKey:@"result"];
  10118. //
  10119. //
  10120. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10121. // {
  10122. //
  10123. // //int i = 0;
  10124. // if (sqlite3_step(statement) == SQLITE_ROW)
  10125. // {
  10126. //
  10127. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10128. //
  10129. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10130. //
  10131. //
  10132. // // int editable = sqlite3_column_int(statement, 0);
  10133. //
  10134. //
  10135. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10136. // NSString *nscompany_name =nil;
  10137. // if(company_name==nil)
  10138. // nscompany_name=@"";
  10139. // else
  10140. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10141. //
  10142. //
  10143. // char *country = (char*)sqlite3_column_text(statement, 2);
  10144. // if(country==nil)
  10145. // country="";
  10146. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10147. //
  10148. //
  10149. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10150. // // if(addr==nil)
  10151. // // addr="";
  10152. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10153. //
  10154. //
  10155. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10156. // if(zipcode==nil)
  10157. // zipcode="";
  10158. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10159. //
  10160. //
  10161. // char *state = (char*)sqlite3_column_text(statement, 5);
  10162. // if(state==nil)
  10163. // state="";
  10164. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10165. //
  10166. // char *city = (char*)sqlite3_column_text(statement, 6);
  10167. // if(city==nil)
  10168. // city="";
  10169. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10170. //
  10171. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10172. // // NSString *nscontact_name = nil;
  10173. // // if(contact_name==nil)
  10174. // // nscontact_name=@"";
  10175. // // else
  10176. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10177. //
  10178. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10179. // NSString *nsphone = nil;
  10180. // if(phone==nil)
  10181. // nsphone=@"";
  10182. // else
  10183. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10184. //
  10185. //
  10186. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10187. // // if(contact_id==nil)
  10188. // // contact_id="";
  10189. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10190. //
  10191. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10192. // if(addr_1==nil)
  10193. // addr_1="";
  10194. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10195. //
  10196. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10197. // if(addr_2==nil)
  10198. // addr_2="";
  10199. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10200. //
  10201. //
  10202. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10203. // if(addr_3==nil)
  10204. // addr_3="";
  10205. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10206. //
  10207. //
  10208. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10209. // if(addr_4==nil)
  10210. // addr_4="";
  10211. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10212. //
  10213. //
  10214. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10215. // if(first_name==nil)
  10216. // first_name="";
  10217. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10218. //
  10219. //
  10220. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10221. // if(last_name==nil)
  10222. // last_name="";
  10223. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10224. //
  10225. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10226. // NSString *nsfax = nil;
  10227. // if(fax==nil)
  10228. // nsfax=@"";
  10229. // else
  10230. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10231. //
  10232. // char *email = (char*)sqlite3_column_text(statement, 17);
  10233. // NSString *nsemail = nil;
  10234. // if(email==nil)
  10235. // nsemail=@"";
  10236. // else
  10237. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10238. //
  10239. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10240. // NSString *nsimg_0 = nil;
  10241. // if(img_0==nil)
  10242. // nsimg_0=@"";
  10243. // else
  10244. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10245. //
  10246. // [self copy_bcardImg:nsimg_0];
  10247. //
  10248. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10249. // NSString *nsimg_1 = nil;
  10250. // if(img_1==nil)
  10251. // nsimg_1=@"";
  10252. // else
  10253. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10254. // [self copy_bcardImg:nsimg_1];
  10255. //
  10256. //
  10257. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10258. // NSString *nsimg_2 = nil;
  10259. // if(img_2==nil)
  10260. // nsimg_2=@"";
  10261. // else
  10262. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10263. // [self copy_bcardImg:nsimg_2];
  10264. //
  10265. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10266. // NSString *nsprice_type = nil;
  10267. // if(price_type==nil)
  10268. // nsprice_type=@"";
  10269. // else
  10270. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10271. //
  10272. //
  10273. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10274. // NSString *nsnotes = nil;
  10275. // if(notes==nil)
  10276. // nsnotes=@"";
  10277. // else
  10278. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10279. //
  10280. //
  10281. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10282. // NSString *nssalesrep = nil;
  10283. // if(salesrep==nil)
  10284. // nssalesrep=@"";
  10285. // else
  10286. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10287. //
  10288. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10289. //
  10290. //
  10291. // {
  10292. // // decrypt
  10293. //
  10294. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10295. //
  10296. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10297. //
  10298. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10299. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10300. //
  10301. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10302. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10303. //
  10304. // }
  10305. //
  10306. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10307. //
  10308. // [arr_name addObject:nsfirst_name];
  10309. // [arr_name addObject:nslast_name];
  10310. //
  10311. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10312. //
  10313. //
  10314. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10315. //
  10316. // [arr_ext addObject:nsaddr_1];
  10317. // [arr_ext addObject:nsaddr_2];
  10318. // [arr_ext addObject:nsaddr_3];
  10319. // [arr_ext addObject:nsaddr_4];
  10320. // [arr_ext addObject:@"\r\n"];
  10321. //
  10322. // [arr_ext addObject:nscity];
  10323. // [arr_ext addObject:nsstate];
  10324. // [arr_ext addObject:nszipcode];
  10325. // [arr_ext addObject:nscountry];
  10326. //
  10327. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10328. //
  10329. //
  10330. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10331. //
  10332. //
  10333. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10334. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10335. // [item setValue:nscountry forKey:@"customer_country"];
  10336. // [item setValue:nsphone forKey:@"customer_phone"];
  10337. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10338. // [item setValue:nscompany_name forKey:@"customer_name"];
  10339. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10340. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10341. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10342. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10343. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10344. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10345. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10346. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10347. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10348. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10349. // [item setValue:nscity forKey:@"customer_city"];
  10350. // [item setValue:nsstate forKey:@"customer_state"];
  10351. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10352. // [item setValue:contactId forKey:@"customer_cid"];
  10353. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10354. // [item setValue:nsfax forKey:@"customer_fax"];
  10355. // [item setValue:nsemail forKey:@"customer_email"];
  10356. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10357. //
  10358. //
  10359. // [ret setObject:item forKey:@"customerInfo"];
  10360. // // i++;
  10361. //
  10362. //
  10363. //
  10364. // }
  10365. //
  10366. //
  10367. //
  10368. //
  10369. // sqlite3_finalize(statement);
  10370. // }
  10371. //
  10372. //
  10373. //
  10374. // [iSalesDB close_db:db];
  10375. //
  10376. //
  10377. //
  10378. //
  10379. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10380. // dispatch_async(dispatch_get_main_queue(), ^{
  10381. // UIApplication * app = [UIApplication sharedApplication];
  10382. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10383. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10384. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10385. // result(ret);
  10386. // });
  10387. //
  10388. // return ;
  10389. // });
  10390. //}
  10391. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10392. {
  10393. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10394. NSString* order_code= appDelegate.order_code;
  10395. NSString* user = appDelegate.user;
  10396. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10397. NSString* category = [params valueForKey:@"category"];
  10398. if (!category || [category isEqualToString:@""]) {
  10399. category = @"%";
  10400. }
  10401. category = [self translateSingleQuote:category];
  10402. int limit = [[params valueForKey:@"limit"] intValue];
  10403. int offset = [[params valueForKey:@"offset"] intValue];
  10404. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10405. NSString *limit_str = @"";
  10406. if (limited) {
  10407. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10408. }
  10409. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10410. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10411. sqlite3 *db = [iSalesDB get_db];
  10412. // [iSalesDB AddExFunction:db];
  10413. int count;
  10414. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  10415. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color 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 %@ ;",order_code, category,limit_str];
  10416. double price_min = 0;
  10417. double price_max = 0;
  10418. if ([params.allKeys containsObject:@"alert"]) {
  10419. // alert
  10420. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10421. NSString *alert = params[@"alert"];
  10422. if ([alert isEqualToString:@"Display All"]) {
  10423. alert = [NSString stringWithFormat:@""];
  10424. } else {
  10425. alert = [self translateSingleQuote:alert];
  10426. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10427. }
  10428. // available
  10429. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10430. NSString *available = params[@"available"];
  10431. NSString *available_condition;
  10432. if ([available isEqualToString:@"Display All"]) {
  10433. available_condition = @"";
  10434. } else if ([available isEqualToString:@"Available Now"]) {
  10435. available_condition = @"and availability > 0";
  10436. } else {
  10437. available_condition = @"and availability == 0";
  10438. }
  10439. // best seller
  10440. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10441. NSString *best_seller = @"";
  10442. NSString *order_best_seller = @"m.name asc";
  10443. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10444. best_seller = @"and best_seller > 0";
  10445. order_best_seller = @"m.best_seller desc,m.name asc";
  10446. }
  10447. // price
  10448. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10449. NSString *price = params[@"price"];
  10450. price_min = 0;
  10451. price_max = MAXFLOAT;
  10452. if (user && price != nil) {
  10453. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10454. NSMutableString *priceName = [NSMutableString string];
  10455. for (int i = 0; i < priceTypeArray.count; i++) {
  10456. NSString *pricetype = priceTypeArray[i];
  10457. pricetype = [self translateSingleQuote:pricetype];
  10458. if (i == 0) {
  10459. [priceName appendFormat:@"'%@'",pricetype];
  10460. } else {
  10461. [priceName appendFormat:@",'%@'",pricetype];
  10462. }
  10463. }
  10464. if ([price isEqualToString:@"Display All"]) {
  10465. price = [NSString stringWithFormat:@""];
  10466. } else if([price containsString:@"+"]){
  10467. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10468. price_min = [price doubleValue];
  10469. 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];
  10470. } else {
  10471. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10472. price_min = [[priceArray objectAtIndex:0] doubleValue];
  10473. price_max = [[priceArray objectAtIndex:1] doubleValue];
  10474. 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];
  10475. }
  10476. } else {
  10477. price = @"";
  10478. }
  10479. // sold_by_qty : Sold in quantities of %@
  10480. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10481. NSString *qty = params[@"sold_by_qty"];
  10482. if ([qty isEqualToString:@"Display All"]) {
  10483. qty = @"";
  10484. } else {
  10485. qty = [self translateSingleQuote:qty];
  10486. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10487. }
  10488. // cate
  10489. // category = [self translateSingleQuote:category];
  10490. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  10491. // cate mutiple selection
  10492. NSString *category_id = params[@"category"];
  10493. NSMutableArray *cate_id_array = nil;
  10494. NSMutableString *cateWhere = [NSMutableString string];
  10495. if ([category_id isEqualToString:@""] || !category_id) {
  10496. [cateWhere appendString:@"1 = 1"];
  10497. } else {
  10498. if ([category_id containsString:@","]) {
  10499. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10500. } else {
  10501. cate_id_array = [@[category_id] mutableCopy];
  10502. }
  10503. [cateWhere appendString:@"("];
  10504. for (int i = 0; i < cate_id_array.count; i++) {
  10505. if (i == 0) {
  10506. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10507. } else {
  10508. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10509. }
  10510. }
  10511. [cateWhere appendString:@")"];
  10512. }
  10513. // where bestseller > 0 order by bestseller desc
  10514. // sql query: alert availability(int) best_seller(int) price qty
  10515. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color 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,order_code,order_best_seller,limit_str];
  10516. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  10517. }
  10518. DebugLog(@"offline category where: %@",where);
  10519. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  10520. if (!user) {
  10521. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  10522. }
  10523. [ret setValue:filter forKey:@"filter"];
  10524. DebugLog(@"offline_category sql:%@",sqlQuery);
  10525. sqlite3_stmt * statement;
  10526. [ret setValue:@"2" forKey:@"result"];
  10527. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10528. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10529. // int count=0;
  10530. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10531. {
  10532. int i=0;
  10533. while (sqlite3_step(statement) == SQLITE_ROW)
  10534. {
  10535. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10536. char *name = (char*)sqlite3_column_text(statement, 0);
  10537. if(name==nil)
  10538. name="";
  10539. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10540. char *description = (char*)sqlite3_column_text(statement, 1);
  10541. if(description==nil)
  10542. description="";
  10543. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10544. int product_id = sqlite3_column_int(statement, 2);
  10545. int wid = sqlite3_column_int(statement, 3);
  10546. int closeout = sqlite3_column_int(statement, 4);
  10547. int cid = sqlite3_column_int(statement, 5);
  10548. int wisdelete = sqlite3_column_int(statement, 6);
  10549. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  10550. int more_color = sqlite3_column_int(statement, 8);
  10551. // Defaul Category ID
  10552. __block NSString *categoryID = nil;
  10553. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  10554. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10555. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  10556. if(default_category==nil)
  10557. default_category="";
  10558. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  10559. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  10560. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  10561. categoryID = nsdefault_category;
  10562. }];
  10563. if (!categoryID.length) {
  10564. NSString *cateIDs = params[@"category"];
  10565. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  10566. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  10567. for (NSString *cateID in requestCategoryArr) {
  10568. BOOL needBreak = NO;
  10569. for (NSString *itemCateIDBox in itemCategoryArr) {
  10570. if (itemCateIDBox.length > 4) {
  10571. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  10572. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  10573. if ([itemCategoryID isEqualToString:cateID]) {
  10574. needBreak = YES;
  10575. categoryID = itemCategoryID;
  10576. break;
  10577. }
  10578. }
  10579. }
  10580. if (needBreak) {
  10581. break;
  10582. }
  10583. }
  10584. }
  10585. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10586. if(wid !=0 && wisdelete != 1)
  10587. [item setValue:@"true" forKey:@"wish_exists"];
  10588. else
  10589. [item setValue:@"false" forKey:@"wish_exists"];
  10590. if(closeout==0)
  10591. [item setValue:@"false" forKey:@"is_closeout"];
  10592. else
  10593. [item setValue:@"true" forKey:@"is_closeout"];
  10594. if(cid==0)
  10595. [item setValue:@"false" forKey:@"cart_exists"];
  10596. else
  10597. [item setValue:@"true" forKey:@"cart_exists"];
  10598. if (more_color == 0) {
  10599. [item setObject:@(false) forKey:@"more_color"];
  10600. } else if (more_color == 1) {
  10601. [item setObject:@(true) forKey:@"more_color"];
  10602. }
  10603. [item addEntriesFromDictionary:imgjson];
  10604. // [item setValue:nsurl forKey:@"img"];
  10605. [item setValue:nsname forKey:@"name"];
  10606. [item setValue:nsdescription forKey:@"description"];
  10607. if (categoryID) {
  10608. [item setValue:categoryID forKey:@"item_category_id"];
  10609. }
  10610. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  10611. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10612. i++;
  10613. }
  10614. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  10615. [ret setObject:items forKey:@"items"];
  10616. sqlite3_finalize(statement);
  10617. } else {
  10618. DebugLog(@"nothing...");
  10619. }
  10620. DebugLog(@"count:%d",count);
  10621. [iSalesDB close_db:db];
  10622. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10623. dispatch_async(dispatch_get_main_queue(), ^{
  10624. // UIApplication * app = [UIApplication sharedApplication];
  10625. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10626. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10627. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10628. result(ret);
  10629. });
  10630. });
  10631. // NSString* orderCode = [params valueForKey:@"orderCode"];
  10632. }
  10633. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10634. {
  10635. [self categoryList:params limited:YES completionHandler:result];
  10636. }
  10637. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10638. {
  10639. UIApplication * app = [UIApplication sharedApplication];
  10640. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10641. NSString* order_code = appDelegate.order_code;
  10642. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10643. NSString* keyword = [params valueForKey:@"keyword"];
  10644. keyword=keyword.lowercaseString;
  10645. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  10646. int limit = [[params valueForKey:@"limit"] intValue];
  10647. int offset = [[params valueForKey:@"offset"] intValue];
  10648. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10649. NSString *limit_str = @"";
  10650. if (limited) {
  10651. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10652. }
  10653. sqlite3 *db = [iSalesDB get_db];
  10654. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  10655. NSString *sqlQuery = nil;
  10656. if(exactMatch )
  10657. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 %@;",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 ;
  10658. else
  10659. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",order_code, keyword,keyword,keyword,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
  10660. DebugLog(@"offline_search sql:%@",sqlQuery);
  10661. sqlite3_stmt * statement;
  10662. [ret setValue:@"2" forKey:@"result"];
  10663. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10664. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10665. // int count=0;
  10666. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10667. {
  10668. int i=0;
  10669. while (sqlite3_step(statement) == SQLITE_ROW)
  10670. {
  10671. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10672. // char *name = (char*)sqlite3_column_text(statement, 1);
  10673. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  10674. char *name = (char*)sqlite3_column_text(statement, 0);
  10675. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10676. char *description = (char*)sqlite3_column_text(statement, 1);
  10677. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10678. int product_id = sqlite3_column_int(statement, 2);
  10679. // char *url = (char*)sqlite3_column_text(statement, 3);
  10680. // if(url==nil)
  10681. // url="";
  10682. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  10683. int wid = sqlite3_column_int(statement, 3);
  10684. int closeout = sqlite3_column_int(statement, 4);
  10685. int cid = sqlite3_column_int(statement, 5);
  10686. int wisdelete = sqlite3_column_int(statement, 6);
  10687. int more_color = sqlite3_column_int(statement, 7);
  10688. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10689. if(wid !=0 && wisdelete != 1)
  10690. [item setValue:@"true" forKey:@"wish_exists"];
  10691. else
  10692. [item setValue:@"false" forKey:@"wish_exists"];
  10693. if(closeout==0)
  10694. [item setValue:@"false" forKey:@"is_closeout"];
  10695. else
  10696. [item setValue:@"true" forKey:@"is_closeout"];
  10697. if(cid==0)
  10698. [item setValue:@"false" forKey:@"cart_exists"];
  10699. else
  10700. [item setValue:@"true" forKey:@"cart_exists"];
  10701. if (more_color == 0) {
  10702. [item setObject:@(false) forKey:@"more_color"];
  10703. } else if (more_color == 1) {
  10704. [item setObject:@(true) forKey:@"more_color"];
  10705. }
  10706. [item addEntriesFromDictionary:imgjson];
  10707. // [item setValue:nsurl forKey:@"img"];
  10708. [item setValue:nsname forKey:@"fash_name"];
  10709. [item setValue:nsdescription forKey:@"description"];
  10710. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  10711. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10712. i++;
  10713. }
  10714. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  10715. [ret setObject:items forKey:@"items"];
  10716. sqlite3_finalize(statement);
  10717. }
  10718. DebugLog(@"count:%d",count);
  10719. [iSalesDB close_db:db];
  10720. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10721. dispatch_async(dispatch_get_main_queue(), ^{
  10722. result(ret);
  10723. });
  10724. });
  10725. }
  10726. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10727. {
  10728. [self search:params limited:YES completionHandler:result];
  10729. }
  10730. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10731. //{
  10732. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10733. // NSString* order_code = appDelegate.order_code;
  10734. // NSString* user = appDelegate.user;
  10735. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10736. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  10737. //
  10738. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10739. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10740. //
  10741. // // category
  10742. // NSDictionary *category_menu = [self offline_category_menu];
  10743. // [filter setValue:category_menu forKey:@"category"];
  10744. //
  10745. // NSString* where= nil;
  10746. // NSString* orderby= @"m.name";
  10747. // if (!filterSearch) {
  10748. // int covertype = [[params valueForKey:@"covertype"] intValue];
  10749. //
  10750. // switch (covertype) {
  10751. // case 0:
  10752. // {
  10753. // where=@"m.category like'%%#005#%%'";
  10754. // break;
  10755. // }
  10756. // case 1:
  10757. // {
  10758. // where=@"m.alert like '%QS%'";
  10759. // break;
  10760. // }
  10761. // case 2:
  10762. // {
  10763. // where=@"m.availability>0";
  10764. // break;
  10765. // }
  10766. // case 3:
  10767. // {
  10768. // where=@"m.best_seller>0";
  10769. // orderby=@"m.best_seller desc,m.name asc";
  10770. // break;
  10771. // }
  10772. // default:
  10773. // where=@"1=1";
  10774. // break;
  10775. // }
  10776. //
  10777. // }
  10778. //
  10779. //
  10780. // int limit = [[params valueForKey:@"limit"] intValue];
  10781. // int offset = [[params valueForKey:@"offset"] intValue];
  10782. //
  10783. // NSString *limit_str = @"";
  10784. // if (limited) {
  10785. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  10786. // }
  10787. //
  10788. //
  10789. // sqlite3 *db = [iSalesDB get_db];
  10790. // // [iSalesDB AddExFunction:db];
  10791. //
  10792. // int count;
  10793. //
  10794. // NSString *sqlQuery = nil;
  10795. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  10796. // sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 %@ %@;",order_code, where,orderby,limit_str];
  10797. //
  10798. //
  10799. // double price_min = 0;
  10800. // double price_max = 0;
  10801. // if (filterSearch) {
  10802. // // alert
  10803. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10804. // NSString *alert = params[@"alert"];
  10805. // if ([alert isEqualToString:@"Display All"]) {
  10806. // alert = [NSString stringWithFormat:@""];
  10807. // } else {
  10808. // alert = [self translateSingleQuote:alert];
  10809. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10810. // }
  10811. //
  10812. // // available
  10813. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10814. // NSString *available = params[@"available"];
  10815. // NSString *available_condition;
  10816. // if ([available isEqualToString:@"Display All"]) {
  10817. // available_condition = @"";
  10818. // } else if ([available isEqualToString:@"Available Now"]) {
  10819. // available_condition = @"and availability > 0";
  10820. // } else {
  10821. // available_condition = @"and availability == 0";
  10822. // }
  10823. //
  10824. // // best seller
  10825. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10826. // NSString *best_seller = @"";
  10827. // NSString *order_best_seller = @"m.name asc";
  10828. //
  10829. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10830. // best_seller = @"and best_seller > 0";
  10831. // order_best_seller = @"m.best_seller desc,m.name asc";
  10832. // }
  10833. //
  10834. // // price
  10835. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10836. // NSString *price = params[@"price"];
  10837. // price_min = 0;
  10838. // price_max = MAXFLOAT;
  10839. // if (user) {
  10840. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10841. // NSMutableString *priceName = [NSMutableString string];
  10842. // for (int i = 0; i < priceTypeArray.count; i++) {
  10843. // NSString *pricetype = priceTypeArray[i];
  10844. // pricetype = [self translateSingleQuote:pricetype];
  10845. // if (i == 0) {
  10846. // [priceName appendFormat:@"'%@'",pricetype];
  10847. // } else {
  10848. // [priceName appendFormat:@",'%@'",pricetype];
  10849. // }
  10850. // }
  10851. //
  10852. // if ([price isEqualToString:@"Display All"]) {
  10853. // price = [NSString stringWithFormat:@""];
  10854. // } else if([price containsString:@"+"]){
  10855. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10856. // price_min = [price doubleValue];
  10857. // 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];
  10858. // } else {
  10859. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10860. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  10861. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  10862. // 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];
  10863. // }
  10864. // } else {
  10865. // price = @"";
  10866. // }
  10867. //
  10868. // // sold_by_qty : Sold in quantities of %@
  10869. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10870. // NSString *qty = params[@"sold_by_qty"];
  10871. // if ([qty isEqualToString:@"Display All"]) {
  10872. // qty = @"";
  10873. // } else {
  10874. // qty = [self translateSingleQuote:qty];
  10875. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10876. // }
  10877. //
  10878. // // category
  10879. // NSString *category_id = params[@"ctgId"];
  10880. // NSMutableArray *cate_id_array = nil;
  10881. // NSMutableString *cateWhere = [NSMutableString string];
  10882. //
  10883. // if ([category_id isEqualToString:@""] || !category_id) {
  10884. // [cateWhere appendString:@"1 = 1"];
  10885. // } else {
  10886. //
  10887. // if ([category_id containsString:@","]) {
  10888. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10889. // } else {
  10890. // cate_id_array = [@[category_id] mutableCopy];
  10891. // }
  10892. // /*-----------*/
  10893. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  10894. //
  10895. // [cateWhere appendString:@"("];
  10896. // for (int i = 0; i < cate_id_array.count; i++) {
  10897. //
  10898. // for (NSString *key0 in cateDic.allKeys) {
  10899. //
  10900. // if ([key0 containsString:@"category_"]) {
  10901. //
  10902. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  10903. //
  10904. // for (NSString *key1 in category0.allKeys) {
  10905. //
  10906. // if ([key1 containsString:@"category_"]) {
  10907. //
  10908. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  10909. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  10910. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  10911. //
  10912. // cate_id_array[i] = [category1 objectForKey:@"id"];
  10913. // if (i == 0) {
  10914. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10915. // } else {
  10916. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10917. // }
  10918. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  10919. // [category0 setValue:category1 forKey:key1];
  10920. // [cateDic setValue:category0 forKey:key0];
  10921. //
  10922. // }
  10923. //
  10924. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  10925. //
  10926. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  10927. //
  10928. // cate_id_array[i] = [category0 objectForKey:@"id"];
  10929. // if (i == 0) {
  10930. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10931. // } else {
  10932. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10933. // }
  10934. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  10935. // [cateDic setValue:category0 forKey:key0];
  10936. //
  10937. // }
  10938. //
  10939. // }
  10940. //
  10941. // }
  10942. //
  10943. // }
  10944. //
  10945. // }
  10946. //
  10947. // }
  10948. // [cateWhere appendString:@")"];
  10949. //
  10950. // [filter setValue:cateDic forKey:@"category"];
  10951. // }
  10952. //
  10953. // // where bestseller > 0 order by bestseller desc
  10954. // // sql query: alert availability(int) best_seller(int) price qty
  10955. //
  10956. // sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color 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,order_code,order_best_seller,limit_str];
  10957. //
  10958. //
  10959. // // count
  10960. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  10961. //
  10962. // }
  10963. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  10964. //
  10965. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  10966. //
  10967. //
  10968. //
  10969. //
  10970. // if (!user) {
  10971. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  10972. // }
  10973. //
  10974. // [ret setValue:filter forKey:@"filter"];
  10975. //
  10976. //
  10977. //
  10978. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  10979. //
  10980. // sqlite3_stmt * statement;
  10981. // [ret setValue:@"2" forKey:@"result"];
  10982. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10983. //
  10984. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10985. // // int count=0;
  10986. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10987. // {
  10988. //
  10989. // int i=0;
  10990. // while (sqlite3_step(statement) == SQLITE_ROW)
  10991. // {
  10992. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10993. //
  10994. //
  10995. // char *name = (char*)sqlite3_column_text(statement, 0);
  10996. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10997. //
  10998. // char *description = (char*)sqlite3_column_text(statement, 1);
  10999. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11000. //
  11001. //
  11002. //
  11003. // int product_id = sqlite3_column_int(statement, 2);
  11004. //
  11005. //
  11006. // int wid = sqlite3_column_int(statement, 3);
  11007. // int closeout = sqlite3_column_int(statement, 4);
  11008. // int cid = sqlite3_column_int(statement, 5);
  11009. // int wisdelete = sqlite3_column_int(statement, 6);
  11010. // int more_color = sqlite3_column_int(statement, 7);
  11011. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11012. //
  11013. // if(wid !=0 && wisdelete != 1)
  11014. // [item setValue:@"true" forKey:@"wish_exists"];
  11015. // else
  11016. // [item setValue:@"false" forKey:@"wish_exists"];
  11017. //
  11018. // if(closeout==0)
  11019. // [item setValue:@"false" forKey:@"is_closeout"];
  11020. // else
  11021. // [item setValue:@"true" forKey:@"is_closeout"];
  11022. //
  11023. // if(cid==0)
  11024. // [item setValue:@"false" forKey:@"cart_exists"];
  11025. // else
  11026. // [item setValue:@"true" forKey:@"cart_exists"];
  11027. //
  11028. // if (more_color == 0) {
  11029. // [item setObject:@(false) forKey:@"more_color"];
  11030. // } else if (more_color == 1) {
  11031. // [item setObject:@(true) forKey:@"more_color"];
  11032. // }
  11033. //
  11034. // [item addEntriesFromDictionary:imgjson];
  11035. //
  11036. //
  11037. // // [item setValue:nsurl forKey:@"img"];
  11038. // [item setValue:nsname forKey:@"fash_name"];
  11039. // [item setValue:nsdescription forKey:@"description"];
  11040. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11041. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11042. // i++;
  11043. // }
  11044. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11045. // [ret setObject:items forKey:@"items"];
  11046. // sqlite3_finalize(statement);
  11047. // }
  11048. //
  11049. // [iSalesDB close_db:db];
  11050. //
  11051. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11052. //
  11053. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11054. // dispatch_async(dispatch_get_main_queue(), ^{
  11055. //
  11056. // result(ret);
  11057. // });
  11058. //
  11059. // });
  11060. //
  11061. //
  11062. //
  11063. //}
  11064. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11065. //{
  11066. // [self itemsearch:params limited:YES completionHandler:result];
  11067. //}
  11068. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11069. {
  11070. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11071. NSString* offline_command=params[@"offline_Command"];
  11072. NSMutableDictionary* ret=nil;
  11073. if([offline_command isEqualToString:@"model_NIYMAL"])
  11074. {
  11075. NSString* category = params[@"category"];
  11076. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11077. }
  11078. dispatch_async(dispatch_get_main_queue(), ^{
  11079. result(ret);
  11080. });
  11081. });
  11082. }
  11083. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11084. {
  11085. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11086. int sort = [[params objectForKey:@"sort"] intValue];
  11087. NSString *sort_str = @"";
  11088. switch (sort) {
  11089. case 0:{
  11090. sort_str = @"order by w.modify_time desc";
  11091. }
  11092. break;
  11093. case 1:{
  11094. sort_str = @"order by w.modify_time asc";
  11095. }
  11096. break;
  11097. case 2:{
  11098. sort_str = @"order by m.name asc";
  11099. }
  11100. break;
  11101. case 3:{
  11102. sort_str = @"order by m.name desc";
  11103. }
  11104. break;
  11105. case 4:{
  11106. sort_str = @"order by m.description asc";
  11107. }
  11108. break;
  11109. default:
  11110. break;
  11111. }
  11112. // NSString* user = appDelegate.user;
  11113. sqlite3 *db = [iSalesDB get_db];
  11114. // order by w.create_time
  11115. 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];
  11116. // 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];
  11117. sqlite3_stmt * statement;
  11118. NSDate *date1 = [NSDate date];
  11119. // NSDate *date2 = nil;
  11120. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11121. int count=0;
  11122. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11123. {
  11124. while (sqlite3_step(statement) == SQLITE_ROW)
  11125. {
  11126. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11127. int product_id = sqlite3_column_double(statement, 0);
  11128. char *description = (char*)sqlite3_column_text(statement, 1);
  11129. if(description==nil)
  11130. description= "";
  11131. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11132. int item_id = sqlite3_column_double(statement, 2);
  11133. NSDate *date_image = [NSDate date];
  11134. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11135. printf("image : ");
  11136. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11137. // char *url = (char*)sqlite3_column_text(statement, 3);
  11138. // if(url==nil)
  11139. // url="";
  11140. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11141. int qty = sqlite3_column_int(statement, 3);
  11142. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11143. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11144. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11145. item[@"description"]= nsdescription;
  11146. item[@"img"]= nsurl;
  11147. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11148. count++;
  11149. }
  11150. printf("total time:");
  11151. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11152. ret[@"count"]= [NSNumber numberWithInt:count];
  11153. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11154. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11155. ret[@"result"]= [NSNumber numberWithInt:2];
  11156. sqlite3_finalize(statement);
  11157. }
  11158. [iSalesDB close_db:db];
  11159. dispatch_async(dispatch_get_main_queue(), ^{
  11160. UIApplication * app = [UIApplication sharedApplication];
  11161. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11162. appDelegate.wish_count =count;
  11163. [appDelegate update_count_mark];
  11164. result(ret);
  11165. });
  11166. });
  11167. }
  11168. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11169. {
  11170. // UIApplication * app = [UIApplication sharedApplication];
  11171. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11172. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11173. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11174. sqlite3 *db = [iSalesDB get_db];
  11175. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11176. NSString* product_id=params[@"product_id"];
  11177. NSString *item_count_str = params[@"item_count"];
  11178. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11179. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11180. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11181. // NSString *sql = @"";
  11182. for(int i=0;i<arr.count;i++)
  11183. {
  11184. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11185. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11186. __block int cart_count = 0;
  11187. if (!item_count_str) {
  11188. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11189. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11190. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11191. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11192. cart_count = [[model_set_components lastObject] intValue];
  11193. }];
  11194. }
  11195. if(count==0)
  11196. {
  11197. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11198. sqlite3_stmt *stmt;
  11199. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11200. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11201. if (item_count_arr) {
  11202. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11203. } else {
  11204. sqlite3_bind_int(stmt,2,cart_count);
  11205. }
  11206. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11207. [iSalesDB execSql:@"ROLLBACK" db:db];
  11208. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11209. [iSalesDB close_db:db];
  11210. DebugLog(@"add to wishlist error");
  11211. dispatch_async(dispatch_get_main_queue(), ^{
  11212. result(ret);
  11213. });
  11214. }
  11215. } else {
  11216. int qty = 0;
  11217. if (item_count_arr) {
  11218. qty = [item_count_arr[i] intValue];
  11219. } else {
  11220. qty = cart_count;
  11221. }
  11222. 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]];
  11223. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11224. [iSalesDB execSql:@"ROLLBACK" db:db];
  11225. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11226. [iSalesDB close_db:db];
  11227. DebugLog(@"add to wishlist error");
  11228. dispatch_async(dispatch_get_main_queue(), ^{
  11229. result(ret);
  11230. });
  11231. }
  11232. }
  11233. }
  11234. // [iSalesDB execSql:sql db:db];
  11235. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11236. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11237. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11238. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11239. [iSalesDB close_db:db];
  11240. ret[@"result"]= [NSNumber numberWithInt:2];
  11241. dispatch_async(dispatch_get_main_queue(), ^{
  11242. UIApplication * app = [UIApplication sharedApplication];
  11243. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11244. appDelegate.wish_count =count;
  11245. [appDelegate update_count_mark];
  11246. result(ret);
  11247. });
  11248. });
  11249. }
  11250. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11251. {
  11252. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11253. bool bLogin = appDelegate.bLogin;
  11254. __block NSString* contact_id = appDelegate.contact_id;
  11255. __block NSString* user = appDelegate.user;
  11256. __block NSString* order_code = appDelegate.order_code;
  11257. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11258. NSString* model_name = [params valueForKey:@"product_name"];
  11259. NSString* product_id = [params valueForKey:@"product_id"];
  11260. NSString* category = [params valueForKey:@"category"];
  11261. NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  11262. if(category==nil) {
  11263. category = default_category_id;
  11264. } else {
  11265. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11266. // 参数重有多个category id
  11267. if (arr_0.count > 1) {
  11268. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11269. // 取交集
  11270. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11271. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11272. [set_0 intersectSet:set_1];
  11273. if (set_0.count == 1) {
  11274. category = (NSString *)[set_0 anyObject];
  11275. } else {
  11276. if ([set_0 containsObject:default_category_id]) {
  11277. category = default_category_id;
  11278. } else {
  11279. category = (NSString *)[set_0 anyObject];
  11280. }
  11281. }
  11282. }
  11283. }
  11284. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11285. sqlite3 *db = [iSalesDB get_db];
  11286. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11287. NSString *sqlQuery = nil;
  11288. if(product_id==nil)
  11289. 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,seat_dimension,product_content_writing 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='%@';
  11290. else
  11291. 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,seat_dimension,product_content_writing 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=%@;
  11292. sqlite3_stmt * statement;
  11293. [ret setValue:@"2" forKey:@"result"];
  11294. [ret setValue:@"3" forKey:@"detail_section_count"];
  11295. // int count=0;
  11296. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11297. {
  11298. if (sqlite3_step(statement) == SQLITE_ROW)
  11299. {
  11300. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11301. char *name = (char*)sqlite3_column_text(statement, 0);
  11302. if(name==nil)
  11303. name="";
  11304. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11305. char *description = (char*)sqlite3_column_text(statement, 1);
  11306. if(description==nil)
  11307. description="";
  11308. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11309. int product_id = sqlite3_column_int(statement, 2);
  11310. char *color = (char*)sqlite3_column_text(statement, 3);
  11311. if(color==nil)
  11312. color="";
  11313. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11314. //
  11315. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11316. // if(legcolor==nil)
  11317. // legcolor="";
  11318. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11319. //
  11320. //
  11321. int availability = sqlite3_column_int(statement, 5);
  11322. //
  11323. int incoming_stock = sqlite3_column_int(statement, 6);
  11324. char *demension = (char*)sqlite3_column_text(statement, 7);
  11325. if(demension==nil)
  11326. demension="";
  11327. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11328. // ,,,,,,,,,
  11329. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11330. if(seat_height==nil)
  11331. seat_height="";
  11332. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11333. char *material = (char*)sqlite3_column_text(statement, 9);
  11334. if(material==nil)
  11335. material="";
  11336. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11337. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11338. if(box_dim==nil)
  11339. box_dim="";
  11340. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11341. char *volume = (char*)sqlite3_column_text(statement, 11);
  11342. if(volume==nil)
  11343. volume="";
  11344. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11345. double weight = sqlite3_column_double(statement, 12);
  11346. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11347. if(model_set==nil)
  11348. model_set="";
  11349. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11350. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11351. if(load_ability==nil)
  11352. load_ability="";
  11353. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11354. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11355. // if(default_category==nil)
  11356. // default_category="";
  11357. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11358. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11359. if(fabric_content==nil)
  11360. fabric_content="";
  11361. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11362. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11363. if(assembling==nil)
  11364. assembling="";
  11365. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11366. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11367. if(made_in==nil)
  11368. made_in="";
  11369. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11370. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11371. if(special_remarks==nil)
  11372. special_remarks="";
  11373. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11374. int stockUcom = sqlite3_column_double(statement, 20);
  11375. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11376. if(product_group==nil)
  11377. product_group="";
  11378. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11379. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11380. // if(fashion_selector==nil)
  11381. // fashion_selector="";
  11382. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11383. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11384. if(selector_field==nil)
  11385. selector_field="";
  11386. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11387. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11388. if(property_field==nil)
  11389. property_field="";
  11390. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11391. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11392. if(packaging==nil)
  11393. packaging="";
  11394. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11395. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11396. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11397. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11398. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11399. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11400. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11401. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11402. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11403. NSString* Availability=nil;
  11404. if(availability>0)
  11405. Availability=[NSString stringWithFormat:@"%d",availability];
  11406. else
  11407. Availability = @"Out of Stock";
  11408. [img_section setValue:Availability forKey:@"Availability"];
  11409. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11410. char *eta = (char*)sqlite3_column_text(statement, 25);
  11411. if(eta==nil)
  11412. eta="";
  11413. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11414. if ([nseta isEqualToString:@"null"]) {
  11415. nseta = @"";
  11416. }
  11417. if (availability <= 0) {
  11418. [img_section setValue:nseta forKey:@"ETA"];
  11419. }
  11420. int item_id = sqlite3_column_int(statement, 26);
  11421. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11422. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11423. NSString* Price=nil;
  11424. if(bLogin==false)
  11425. Price=@"Must Sign in.";
  11426. else
  11427. {
  11428. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11429. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11430. if(price==nil)
  11431. Price=@"No Price.";
  11432. else
  11433. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11434. }
  11435. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11436. [img_section setValue:Price forKey:@"price"];
  11437. [img_section setValue:nsname forKey:@"model_name"];
  11438. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11439. if (order_code) { // 离线order code即so#
  11440. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",order_code,item_id];
  11441. __block int cartQTY = 0;
  11442. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11443. cartQTY = sqlite3_column_int(stmt, 0);
  11444. }];
  11445. if (cartQTY > 0) {
  11446. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11447. }
  11448. }
  11449. [ret setObject:img_section forKey:@"img_section"];
  11450. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11451. int detail0_item_count=0;
  11452. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11453. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11454. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11455. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11456. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11457. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11458. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11459. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11460. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11461. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11462. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11463. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11464. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11465. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11466. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11467. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11468. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  11469. {
  11470. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  11471. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11472. }
  11473. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  11474. [detail0_section setValue:@"kv" forKey:@"type"];
  11475. [detail0_section setValue:@"Product Information" forKey:@"title"];
  11476. [ret setObject:detail0_section forKey:@"detail_0"];
  11477. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  11478. // [detail1_section setValue:@"detail" forKey:@"target"];
  11479. // [detail1_section setValue:@"popup" forKey:@"action"];
  11480. // [detail1_section setValue:@"content" forKey:@"type"];
  11481. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  11482. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  11483. // [detail1_section setValue:@"true" forKey:@"single_row"];
  11484. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  11485. [ret setObject:detail1_section forKey:@"detail_1"];
  11486. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  11487. [detail2_section setValue:@"detail" forKey:@"target"];
  11488. [detail2_section setValue:@"popup" forKey:@"action"];
  11489. [detail2_section setValue:@"content" forKey:@"type"];
  11490. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  11491. [detail2_section setValue:@"true" forKey:@"single_row"];
  11492. [detail2_section setValue:@"local" forKey:@"data"];
  11493. [ret setObject:detail2_section forKey:@"detail_2"];
  11494. }
  11495. sqlite3_finalize(statement);
  11496. }
  11497. else
  11498. {
  11499. [ret setValue:@"8" forKey:@"result"];
  11500. }
  11501. // DebugLog(@"count:%d",count);
  11502. [iSalesDB close_db:db];
  11503. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11504. dispatch_async(dispatch_get_main_queue(), ^{
  11505. result(ret);
  11506. });
  11507. });
  11508. }
  11509. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11510. //{
  11511. //
  11512. // UIApplication * app = [UIApplication sharedApplication];
  11513. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11514. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11515. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11516. // sqlite3 *db = [iSalesDB get_db];
  11517. // NSString* collectId=params[@"collectId"];
  11518. //
  11519. //
  11520. //
  11521. //
  11522. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  11523. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  11524. // [iSalesDB execSql:sqlQuery db:db];
  11525. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11526. // [iSalesDB close_db:db];
  11527. //
  11528. //
  11529. // ret[@"result"]= [NSNumber numberWithInt:2];
  11530. // dispatch_async(dispatch_get_main_queue(), ^{
  11531. //
  11532. // appDelegate.wish_count =count;
  11533. //
  11534. // [appDelegate update_count_mark];
  11535. // result(ret);
  11536. // });
  11537. //
  11538. // });
  11539. //}
  11540. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11541. {
  11542. UIApplication * app = [UIApplication sharedApplication];
  11543. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11544. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11545. [iSalesDB disable_trigger];
  11546. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11547. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11548. [iSalesDB enable_trigger];
  11549. dispatch_async(dispatch_get_main_queue(), ^{
  11550. //
  11551. // NSString* user = [params valueForKey:@"user"];
  11552. //
  11553. // NSString* password = [params valueForKey:@"password"];
  11554. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11555. ret[@"result"]=[NSNumber numberWithInt:2 ];
  11556. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  11557. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  11558. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  11559. [appDelegate update_count_mark];
  11560. appDelegate.can_show_price =false;
  11561. appDelegate.can_see_price =false;
  11562. appDelegate.can_create_portfolio =false;
  11563. appDelegate.can_create_order =false;
  11564. appDelegate.can_cancel_order =false;
  11565. appDelegate.can_set_cart_price =false;
  11566. appDelegate.can_delete_order =false;
  11567. appDelegate.can_submit_order =false;
  11568. appDelegate.can_set_tearsheet_price =false;
  11569. appDelegate.can_update_contact_info = false;
  11570. appDelegate.save_order_logout = false;
  11571. appDelegate.submit_order_logout = false;
  11572. appDelegate.alert_sold_in_quantities = false;
  11573. appDelegate.ipad_perm =nil ;
  11574. appDelegate.user_type = USER_ROLE_UNKNOWN;
  11575. appDelegate.OrderFilter= nil;
  11576. [appDelegate SetSo:nil];
  11577. [appDelegate set_main_button_panel];
  11578. result(ret);
  11579. });
  11580. });
  11581. }
  11582. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11583. {
  11584. // iSalesDB.
  11585. // UIApplication * app = [UIApplication sharedApplication];
  11586. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11587. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11588. [iSalesDB disable_trigger];
  11589. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11590. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11591. [iSalesDB enable_trigger];
  11592. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11593. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11594. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  11595. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  11596. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  11597. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  11598. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  11599. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  11600. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  11601. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  11602. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  11603. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  11604. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  11605. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  11606. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  11607. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  11608. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11609. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  11610. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  11611. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  11612. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  11613. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  11614. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  11615. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11616. [arr_name addObject:customer_first_name];
  11617. [arr_name addObject:customer_last_name];
  11618. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11619. // default ship from
  11620. 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';";
  11621. __block NSString *cid = @"";
  11622. __block NSString *name = @"";
  11623. __block NSString *ext = @"";
  11624. __block NSString *contact = @"";
  11625. __block NSString *email = @"";
  11626. __block NSString *fax = @"";
  11627. __block NSString *phone = @"";
  11628. sqlite3 *db = [iSalesDB get_db];
  11629. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  11630. cid = [self textAtColumn:0 statement:statment];
  11631. name = [self textAtColumn:1 statement:statment];
  11632. ext = [self textAtColumn:2 statement:statment];
  11633. contact = [self textAtColumn:3 statement:statment];
  11634. email = [self textAtColumn:4 statement:statment];
  11635. fax = [self textAtColumn:5 statement:statment];
  11636. phone = [self textAtColumn:6 statement:statment];
  11637. }];
  11638. NSString* so_id = [self get_offline_soid:db];
  11639. if(so_id==nil)
  11640. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  11641. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  11642. int result_code =[iSalesDB execSql:sql_neworder db:db];
  11643. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  11644. //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'
  11645. //soId
  11646. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  11647. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  11648. sqlite3_stmt * statement;
  11649. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11650. {
  11651. if (sqlite3_step(statement) == SQLITE_ROW)
  11652. {
  11653. // char *name = (char*)sqlite3_column_text(statement, 1);
  11654. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11655. //ret = sqlite3_column_int(statement, 0);
  11656. char *soId = (char*)sqlite3_column_text(statement, 0);
  11657. if(soId==nil)
  11658. soId="";
  11659. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  11660. [ret setValue:nssoId forKey:@"soId"];
  11661. [ret setValue:nssoId forKey:@"orderCode"];
  11662. }
  11663. sqlite3_finalize(statement);
  11664. }
  11665. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  11666. [ret setValue:@"Regular Mode" forKey:@"mode"];
  11667. [iSalesDB close_db:db];
  11668. dispatch_async(dispatch_get_main_queue(), ^{
  11669. result(ret);
  11670. });
  11671. });
  11672. }
  11673. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11674. {
  11675. UIApplication * app = [UIApplication sharedApplication];
  11676. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11677. bool can_create_backorder= appDelegate.can_create_backorder;
  11678. NSString* user = appDelegate.user;
  11679. NSString* contact_id = appDelegate.contact_id;
  11680. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11681. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11682. sqlite3 *db = [iSalesDB get_db];
  11683. NSString* orderCode=params[@"orderCode"];
  11684. int sort = [[params objectForKey:@"sort"] intValue];
  11685. NSString *sort_str = @"";
  11686. switch (sort) {
  11687. case 0:{
  11688. sort_str = @"order by c.modify_time desc";
  11689. }
  11690. break;
  11691. case 1:{
  11692. sort_str = @"order by c.modify_time asc";
  11693. }
  11694. break;
  11695. case 2:{
  11696. sort_str = @"order by m.name asc";
  11697. }
  11698. break;
  11699. case 3:{
  11700. sort_str = @"order by m.name desc";
  11701. }
  11702. break;
  11703. case 4:{
  11704. sort_str = @"order by m.description asc";
  11705. }
  11706. break;
  11707. default:
  11708. break;
  11709. }
  11710. 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,m.availability 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 ];
  11711. // 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 ];
  11712. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  11713. DebugLog(@"offline_login sql:%@",sqlQuery);
  11714. sqlite3_stmt * statement;
  11715. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11716. NSDate *date1 = [NSDate date];
  11717. int count=0;
  11718. int cart_count=0;
  11719. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  11720. if ( dbresult== SQLITE_OK)
  11721. {
  11722. while (sqlite3_step(statement) == SQLITE_ROW)
  11723. {
  11724. // NSDate *row_date = [NSDate date];
  11725. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  11726. int product_id = sqlite3_column_int(statement, 0);
  11727. char *str_price = (char*)sqlite3_column_text(statement, 1);
  11728. int item_id = sqlite3_column_int(statement, 7);
  11729. NSString* Price=nil;
  11730. if(str_price==nil)
  11731. {
  11732. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11733. // NSDate *price_date = [NSDate date];
  11734. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11735. // DebugLog(@"price time interval");
  11736. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  11737. if(price==nil)
  11738. Price=@"No Price.";
  11739. else
  11740. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11741. }
  11742. else
  11743. {
  11744. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  11745. }
  11746. double discount = sqlite3_column_double(statement, 2);
  11747. int item_count = sqlite3_column_int(statement, 3);
  11748. char *line_note = (char*)sqlite3_column_text(statement, 4);
  11749. NSString *nsline_note=nil;
  11750. if(line_note!=nil)
  11751. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  11752. char *name = (char*)sqlite3_column_text(statement, 5);
  11753. NSString *nsname=nil;
  11754. if(name!=nil)
  11755. nsname= [[NSString alloc]initWithUTF8String:name];
  11756. char *description = (char*)sqlite3_column_text(statement, 6);
  11757. NSString *nsdescription=nil;
  11758. if(description!=nil)
  11759. nsdescription= [[NSString alloc]initWithUTF8String:description];
  11760. int stockUom = sqlite3_column_int(statement, 8);
  11761. int _id = sqlite3_column_int(statement, 9);
  11762. int availability = sqlite3_column_int(statement, 10);
  11763. // NSDate *subtotal_date = [NSDate date];
  11764. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  11765. // DebugLog(@"subtotal_date time interval");
  11766. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  11767. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  11768. double weight=[bsubtotaljson[@"weight"] doubleValue];
  11769. int carton=[bsubtotaljson[@"carton"] intValue];
  11770. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  11771. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  11772. // NSDate *img_date = [NSDate date];
  11773. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  11774. // DebugLog(@"img_date time interval");
  11775. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  11776. itemjson[@"model"]=nsname;
  11777. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  11778. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  11779. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  11780. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  11781. itemjson[@"check"]=@"true";
  11782. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  11783. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  11784. itemjson[@"unit_price"]=Price;
  11785. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  11786. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  11787. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  11788. itemjson[@"note"]=nsline_note;
  11789. if (!can_create_backorder) {
  11790. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  11791. }
  11792. // NSDate *date2 = [NSDate date];
  11793. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  11794. // DebugLog(@"model_bundle time interval");
  11795. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  11796. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  11797. count++;
  11798. // DebugLog(@"row time interval");
  11799. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  11800. }
  11801. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  11802. sqlite3_finalize(statement);
  11803. }
  11804. DebugLog(@"request cart total time interval");
  11805. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11806. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  11807. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11808. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  11809. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  11810. ret[@"count"]=[NSNumber numberWithInt:count ];
  11811. ret[@"mode"]=@"Regular Mode";
  11812. [iSalesDB close_db:db];
  11813. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  11814. DebugLog(@"general notes :%@",general_note);
  11815. ret[@"general_note"]= general_note;
  11816. dispatch_async(dispatch_get_main_queue(), ^{
  11817. result(ret);
  11818. });
  11819. });
  11820. }
  11821. @end