OLDataProvider.m 670 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501
  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. if(params[@"user"]==nil)
  1286. {
  1287. AppDelegate *appDelegate = nil;
  1288. //some UI methods ej
  1289. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  1290. if(appDelegate.user!=nil)
  1291. [params setValue:appDelegate.user forKey:@"user"];
  1292. }
  1293. NSString* template_file=nil;
  1294. switch([params[@"pdf_style"] intValue])
  1295. {
  1296. case 0:
  1297. template_file= @"portfolio_2x3.json";
  1298. break;
  1299. case 1:
  1300. template_file= @"portfolio_3x2.json";
  1301. break;
  1302. default:
  1303. template_file= @"portfolio_3x2.json";
  1304. }
  1305. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1306. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1307. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1308. DebugLog(@"%@",file);
  1309. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1310. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1311. if (file) {
  1312. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1313. [dic setObject:file forKey:@"pdf_path"];
  1314. dic[@"isLocalFile"]=@"true";
  1315. return [RAConvertor dict2data:dic];
  1316. }
  1317. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1318. dic[@"isLocalFile"]=@"true";
  1319. return [RAConvertor dict2data:dic];
  1320. }
  1321. +(NSString*) get_offline_soid:(sqlite3*)db
  1322. {
  1323. NSString* soid=nil;
  1324. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1325. [formatter setDateFormat:@"yyMMdd"];
  1326. NSString* date = [formatter stringFromDate:[NSDate date]];
  1327. for(int i=1;i<999;i++)
  1328. {
  1329. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1330. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1331. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1332. if(count==0)
  1333. return soid;
  1334. }
  1335. return nil;
  1336. }
  1337. +(NSArray*) enumOfflineOrder
  1338. {
  1339. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1340. sqlite3 *db = [iSalesDB get_db];
  1341. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1342. sqlite3_stmt * statement;
  1343. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1344. if ( dbresult== SQLITE_OK)
  1345. {
  1346. while (sqlite3_step(statement) == SQLITE_ROW)
  1347. {
  1348. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1349. NSString* nsso_id=nil;
  1350. if(so_id!=nil)
  1351. {
  1352. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1353. }
  1354. [ret addObject:nsso_id];
  1355. }
  1356. sqlite3_finalize(statement);
  1357. }
  1358. [iSalesDB close_db:db];
  1359. return ret;
  1360. }
  1361. //+(void) uploadFile:(NSString*) file
  1362. //{
  1363. // NSData* data = [NSData dataWithContentsOfFile: file];
  1364. // UIApplication * app = [UIApplication sharedApplication];
  1365. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1366. //
  1367. //
  1368. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1369. //
  1370. //
  1371. //
  1372. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1373. //
  1374. // if(appDelegate.user!=nil)
  1375. // [params setValue:appDelegate.user forKey:@"user"];
  1376. // // if(appDelegate.contact_id!=nil)
  1377. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1378. // if(appDelegate.password!=nil)
  1379. // [params setValue:appDelegate.password forKey:@"password"];
  1380. //
  1381. //
  1382. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1383. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1384. // } error:nil];
  1385. //
  1386. //
  1387. //
  1388. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1389. //
  1390. // NSProgress *progress = nil;
  1391. //
  1392. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1393. //
  1394. //
  1395. //
  1396. // if (error) {
  1397. //
  1398. // NSString* err_msg = [error localizedDescription];
  1399. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1400. //
  1401. //
  1402. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1403. //
  1404. // DebugLog(@"data string: %@",str);
  1405. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1406. //
  1407. // } else {
  1408. // DebugLog(@"response ");
  1409. //
  1410. //
  1411. //
  1412. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1413. //
  1414. // // 再将NSData转为字符串
  1415. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1416. //
  1417. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1418. //
  1419. // DebugLog(@"data string: %@",jsonStr);
  1420. //
  1421. // NSDictionary* json = responseObject;
  1422. //
  1423. //
  1424. // if([[json valueForKey:@"result"] intValue]==2)
  1425. // {
  1426. //// NSString* img_url_down = json[@"img_url_aname"];
  1427. //// NSString* img_url_up = json[@"img_url"];
  1428. //
  1429. //
  1430. // }
  1431. // else
  1432. // {
  1433. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1434. // }
  1435. //
  1436. //
  1437. // }
  1438. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  1439. // }];
  1440. //
  1441. //
  1442. //
  1443. //
  1444. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1445. // //
  1446. //
  1447. //
  1448. //
  1449. //
  1450. //
  1451. //
  1452. // [uploadTask resume];
  1453. //
  1454. //}
  1455. +(NSDictionary *) prepareUpload:(NSArray*) arr_order params:(NSMutableDictionary*)add_params
  1456. {
  1457. assert(add_params[@"user"]!=nil);
  1458. // assert(add_params[@"contact_id"]!=nil);
  1459. // assert(add_params[@"password"]!=nil);
  1460. NSString* serial= [[NSUUID UUID] UUIDString];
  1461. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1462. NSString *cachefolder = [paths objectAtIndex:0];
  1463. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1464. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1465. NSFileManager* fileManager = [NSFileManager defaultManager];
  1466. BOOL bdir=YES;
  1467. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1468. {
  1469. NSError *error = nil;
  1470. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1471. if(!bsuccess)
  1472. DebugLog(@"Create UPLOAD folder failed");
  1473. }
  1474. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1475. ret[@"contact"]=[self prepareContact:serial];
  1476. ret[@"wishlist"]=[self prepareWishlist:serial];
  1477. ret[@"order"]=[self prepareOrder:serial soid:arr_order params:add_params];
  1478. ret[@"portfolio"] = [self preparePortfolio:serial params:add_params];
  1479. ret[@"view_portfolio"] = [self preparePDF:serial];
  1480. NSString* str= [RAConvertor dict2string:ret];
  1481. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1482. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1483. NSError *error=nil;
  1484. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1485. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1486. ZipArchive* zip = [[ZipArchive alloc] init];
  1487. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1488. for(int i=0;i<arr_files.count;i++)
  1489. {
  1490. NSString* file=arr_files[i];
  1491. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1492. }
  1493. if( ![zip CloseZipFile2] )
  1494. {
  1495. zippath = @"";
  1496. }
  1497. ret[@"file"]=zippath;
  1498. return ret;
  1499. }
  1500. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1501. {
  1502. if(filename.length==0)
  1503. return false;
  1504. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1505. bool ret=false;
  1506. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1507. NSString *cachefolder = [paths objectAtIndex:0];
  1508. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1509. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1510. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1511. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1512. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1513. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1514. NSFileManager* fileManager = [NSFileManager defaultManager];
  1515. BOOL bdir=NO;
  1516. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1517. {
  1518. NSError *error = nil;
  1519. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1520. {
  1521. ret=false;
  1522. }
  1523. else
  1524. {
  1525. ret=true;
  1526. }
  1527. }
  1528. return ret;
  1529. }
  1530. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1531. {
  1532. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1533. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1534. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1535. NSString* where=@" 1=1";
  1536. if (ver!=nil) {
  1537. where=@"is_dirty=1";
  1538. }
  1539. sqlite3 *db = [iSalesDB get_db];
  1540. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1541. sqlite3_stmt * statement;
  1542. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1543. int count=0;
  1544. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1545. if ( dbresult== SQLITE_OK)
  1546. {
  1547. while (sqlite3_step(statement) == SQLITE_ROW)
  1548. {
  1549. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1550. int _id = sqlite3_column_int(statement, 0);
  1551. int product_id = sqlite3_column_int(statement, 1);
  1552. int item_id = sqlite3_column_int(statement, 2);
  1553. int qty = sqlite3_column_int(statement, 3);
  1554. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1555. NSString* nscreate_time=nil;
  1556. if(create_time!=nil)
  1557. {
  1558. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1559. }
  1560. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1561. NSString* nsmodify_time=nil;
  1562. if(modify_time!=nil)
  1563. {
  1564. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1565. }
  1566. int is_delete = sqlite3_column_int(statement, 6);
  1567. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1568. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1569. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1570. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1571. itemjson[@"create_time"]=nscreate_time;
  1572. itemjson[@"modify_time"]=nsmodify_time;
  1573. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1574. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1575. count++;
  1576. }
  1577. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1578. sqlite3_finalize(statement);
  1579. }
  1580. ret[@"count"]=[NSNumber numberWithInt:count ];
  1581. [iSalesDB close_db:db];
  1582. return ret;
  1583. }
  1584. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode params:(NSMutableDictionary*)add_params db:(sqlite3*)db
  1585. {
  1586. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1587. // UIApplication * app = [UIApplication sharedApplication];
  1588. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1589. assert(add_params[@"user"]!=nil);
  1590. // assert(add_params[@"password"]!=nil);
  1591. 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 ];
  1592. // 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 ];
  1593. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1594. DebugLog(@"offline_login sql:%@",sqlQuery);
  1595. sqlite3_stmt * statement;
  1596. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1597. int count=0;
  1598. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1599. if ( dbresult== SQLITE_OK)
  1600. {
  1601. while (sqlite3_step(statement) == SQLITE_ROW)
  1602. {
  1603. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1604. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1605. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1606. int product_id = sqlite3_column_int(statement, 0);
  1607. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1608. int item_id = sqlite3_column_int(statement, 7);
  1609. NSString* Price=nil;
  1610. if(str_price==nil)
  1611. {
  1612. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1613. NSNumber* price = [self get_model_default_price:customerID user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db];
  1614. if(price==nil)
  1615. Price=@"No Price.";
  1616. else
  1617. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1618. }
  1619. else
  1620. {
  1621. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1622. }
  1623. double discount = sqlite3_column_double(statement, 2);
  1624. int item_count = sqlite3_column_int(statement, 3);
  1625. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1626. NSString *nsline_note=nil;
  1627. if(line_note!=nil)
  1628. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1629. char *name = (char*)sqlite3_column_text(statement, 5);
  1630. NSString *nsname=nil;
  1631. if(name!=nil)
  1632. nsname= [[NSString alloc]initWithUTF8String:name];
  1633. char *description = (char*)sqlite3_column_text(statement, 6);
  1634. NSString *nsdescription=nil;
  1635. if(description!=nil)
  1636. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1637. // int stockUom = sqlite3_column_int(statement, 8);
  1638. // int _id = sqlite3_column_int(statement, 9);
  1639. //
  1640. //
  1641. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1642. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1643. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1644. // int carton=[bsubtotaljson[@"carton"] intValue];
  1645. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1646. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1647. itemjson[@"model"]=nsname;
  1648. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1649. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1650. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1651. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1652. // itemjson[@"check"]=@"true";
  1653. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1654. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1655. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1656. itemjson[@"unit_price"]=Price;
  1657. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1658. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1659. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1660. itemjson[@"note"]=nsline_note;
  1661. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID user:add_params[@"user"]];
  1662. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1663. count++;
  1664. }
  1665. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1666. sqlite3_finalize(statement);
  1667. }
  1668. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1669. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1670. return ret;
  1671. }
  1672. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid params:(NSMutableDictionary*)add_params
  1673. {
  1674. assert(add_params[@"user"]!=nil);
  1675. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1676. sqlite3 *db = [iSalesDB get_db];
  1677. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1678. // for(int i=0;i<arr_soid.count;i++)
  1679. sqlite3_stmt * statement;
  1680. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1681. //int count=0;
  1682. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1683. if ( dbresult== SQLITE_OK)
  1684. {
  1685. int count=0;
  1686. while (sqlite3_step(statement) == SQLITE_ROW)
  1687. {
  1688. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1689. int _id = sqlite3_column_int(statement, 0);
  1690. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1691. NSString* nssync_data=nil;
  1692. if(sync_data!=nil)
  1693. {
  1694. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1695. }
  1696. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1697. NSString* nsimg_1=nil;
  1698. if(img_1!=nil)
  1699. {
  1700. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1701. [self copy_upImg:serial file:nsimg_1];
  1702. }
  1703. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1704. NSString* nsso_no=nil;
  1705. if(so_no!=nil)
  1706. {
  1707. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1708. }
  1709. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1710. itemjson[@"order_type"]=@"submit order";
  1711. else
  1712. itemjson[@"order_type"]=@"archive order";
  1713. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1714. itemjson[@"json_data"]= nssync_data;
  1715. itemjson[@"order_items"]=[self preparecart:nsso_no params:add_params db:db];
  1716. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1717. count++;
  1718. }
  1719. ret[@"count"]=[NSNumber numberWithInt:count ];
  1720. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1721. }
  1722. sqlite3_finalize(statement);
  1723. [iSalesDB close_db:db];
  1724. return ret;
  1725. }
  1726. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1727. {
  1728. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1729. sqlite3 *db = [iSalesDB get_db];
  1730. // UIApplication * app = [UIApplication sharedApplication];
  1731. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1732. 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";
  1733. sqlite3_stmt * statement;
  1734. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1735. int count=0;
  1736. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1737. if ( dbresult== SQLITE_OK)
  1738. {
  1739. while (sqlite3_step(statement) == SQLITE_ROW)
  1740. {
  1741. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1742. int _id = sqlite3_column_int(statement, 0);
  1743. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1744. NSString* nsimg_0=nil;
  1745. if(img_0!=nil)
  1746. {
  1747. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1748. [self copy_upImg:serial file:nsimg_0];
  1749. }
  1750. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1751. NSString* nsimg_1=nil;
  1752. if(img_1!=nil)
  1753. {
  1754. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1755. [self copy_upImg:serial file:nsimg_1];
  1756. }
  1757. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1758. NSString* nsimg_2=nil;
  1759. if(img_2!=nil)
  1760. {
  1761. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1762. [self copy_upImg:serial file:nsimg_2];
  1763. }
  1764. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1765. NSString* nssync_data=nil;
  1766. if(sync_data!=nil)
  1767. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1768. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1769. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1770. itemjson[@"json_data"]= nssync_data;
  1771. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1772. count++;
  1773. }
  1774. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1775. sqlite3_finalize(statement);
  1776. }
  1777. ret[@"count"]=[NSNumber numberWithInt:count ];
  1778. [iSalesDB close_db:db];
  1779. return ret;
  1780. }
  1781. +(bool) check_offlinedata
  1782. {
  1783. // UIApplication * app = [UIApplication sharedApplication];
  1784. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1785. sqlite3 *db = [iSalesDB get_db];
  1786. NSString * where=@"1=1";
  1787. // if(appDelegate.user!=nil)
  1788. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1789. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1790. [iSalesDB close_db:db];
  1791. if(count==0)
  1792. {
  1793. return false;
  1794. }
  1795. return true;
  1796. //
  1797. //[iSalesDB close_db:db];
  1798. }
  1799. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1800. {
  1801. // UIApplication * app = [UIApplication sharedApplication];
  1802. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1803. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1804. sqlite3 *db = [iSalesDB get_db];
  1805. NSString* collectId=params[@"collectId"];
  1806. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1807. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1808. [iSalesDB execSql:sqlQuery db:db];
  1809. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1810. [iSalesDB close_db:db];
  1811. // appDelegate.wish_count =count;
  1812. //
  1813. // [appDelegate update_count_mark];
  1814. ret[@"result"]= [NSNumber numberWithInt:2];
  1815. ret[@"wish_count"]=@(count);
  1816. return ret;
  1817. }
  1818. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1819. //{
  1820. //
  1821. // UIApplication * app = [UIApplication sharedApplication];
  1822. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1823. //
  1824. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1825. // sqlite3 *db = [iSalesDB get_db];
  1826. // NSString* product_id=params[@"product_id"];
  1827. //
  1828. //
  1829. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1830. //
  1831. //
  1832. // for(int i=0;i<arr.count;i++)
  1833. // {
  1834. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1835. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1836. // if(count==0)
  1837. // {
  1838. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1839. // [iSalesDB execSql:sqlQuery db:db];
  1840. // }
  1841. // }
  1842. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1843. // [iSalesDB close_db:db];
  1844. //
  1845. // appDelegate.wish_count =count;
  1846. //
  1847. // [appDelegate update_count_mark];
  1848. // ret[@"result"]= [NSNumber numberWithInt:2];
  1849. // return ret;
  1850. // //
  1851. // //return ret;
  1852. //}
  1853. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1854. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1855. DebugLog(@"time interval: %lf",interval);
  1856. }
  1857. //+(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1858. //{
  1859. // UIApplication * app = [UIApplication sharedApplication];
  1860. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1861. //
  1862. // int sort = [[params objectForKey:@"sort"] intValue];
  1863. // NSString *sort_str = @"";
  1864. // switch (sort) {
  1865. // case 0:{
  1866. // sort_str = @"order by w.modify_time desc";
  1867. // }
  1868. // break;
  1869. // case 1:{
  1870. // sort_str = @"order by w.modify_time asc";
  1871. // }
  1872. // break;
  1873. // case 2:{
  1874. // sort_str = @"order by m.name asc";
  1875. // }
  1876. // break;
  1877. // case 3:{
  1878. // sort_str = @"order by m.name desc";
  1879. // }
  1880. // break;
  1881. // case 4:{
  1882. // sort_str = @"order by m.description asc";
  1883. // }
  1884. // break;
  1885. //
  1886. // default:
  1887. // break;
  1888. //
  1889. // }
  1890. //
  1891. //
  1892. //// NSString* user = appDelegate.user;
  1893. //
  1894. // sqlite3 *db = [iSalesDB get_db];
  1895. //
  1896. // // order by w.create_time
  1897. // 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];
  1898. //
  1899. //// 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];
  1900. //
  1901. //
  1902. // sqlite3_stmt * statement;
  1903. //
  1904. // NSDate *date1 = [NSDate date];
  1905. //// NSDate *date2 = nil;
  1906. //
  1907. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1908. // int count=0;
  1909. //
  1910. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1911. // {
  1912. //
  1913. //
  1914. // while (sqlite3_step(statement) == SQLITE_ROW)
  1915. // {
  1916. //
  1917. // NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1918. //
  1919. // int product_id = sqlite3_column_double(statement, 0);
  1920. //
  1921. //
  1922. //
  1923. //
  1924. //
  1925. // char *description = (char*)sqlite3_column_text(statement, 1);
  1926. // if(description==nil)
  1927. // description= "";
  1928. // NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1929. //
  1930. // int item_id = sqlite3_column_double(statement, 2);
  1931. //
  1932. // NSDate *date_image = [NSDate date];
  1933. //
  1934. // NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1935. //
  1936. // printf("image : ");
  1937. // [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1938. //
  1939. // // char *url = (char*)sqlite3_column_text(statement, 3);
  1940. // // if(url==nil)
  1941. // // url="";
  1942. // // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1943. //
  1944. // int qty = sqlite3_column_int(statement, 3);
  1945. //
  1946. //
  1947. // item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1948. // item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1949. // item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1950. // item[@"description"]= nsdescription;
  1951. // item[@"img"]= nsurl;
  1952. //
  1953. //
  1954. //
  1955. // ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1956. // count++;
  1957. //
  1958. // }
  1959. // printf("total time:");
  1960. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1961. //
  1962. // ret[@"count"]= [NSNumber numberWithInt:count];
  1963. // ret[@"total_count"]= [NSNumber numberWithInt:count];
  1964. // // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1965. // ret[@"result"]= [NSNumber numberWithInt:2];
  1966. //
  1967. //
  1968. // appDelegate.wish_count =count;
  1969. //
  1970. // [appDelegate update_count_mark];
  1971. // sqlite3_finalize(statement);
  1972. //
  1973. //
  1974. //
  1975. //
  1976. // }
  1977. //
  1978. // [iSalesDB close_db:db];
  1979. //
  1980. // return ret;
  1981. //}
  1982. +(NSDictionary*) offline_notimpl
  1983. {
  1984. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1985. ret[@"result"]=@"8";
  1986. ret[@"err_msg"]=@"offline mode does not support this function.";
  1987. return ret;
  1988. }
  1989. +(NSDictionary*) offline_home
  1990. {
  1991. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1992. NSString *cachefolder = [paths objectAtIndex:0];
  1993. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1994. NSData* json =nil;
  1995. json=[NSData dataWithContentsOfFile:img_cache];
  1996. if(json==nil)
  1997. return nil;
  1998. NSError *error=nil;
  1999. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2000. return menu;
  2001. }
  2002. +(NSDictionary*) offline_category_menu
  2003. {
  2004. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2005. NSString *cachefolder = [paths objectAtIndex:0];
  2006. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  2007. NSData* json =nil;
  2008. json=[NSData dataWithContentsOfFile:img_cache];
  2009. if(json==nil)
  2010. return nil;
  2011. NSError *error=nil;
  2012. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  2013. return menu;
  2014. }
  2015. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  2016. {
  2017. NSString* offline_command=params[@"offline_Command"];
  2018. NSDictionary* ret=nil;
  2019. if([offline_command isEqualToString:@"model_NIYMAL"])
  2020. {
  2021. NSString* category = params[@"category"];
  2022. ret = [self refresh_model_NIYMAL:category];
  2023. }
  2024. return ret;
  2025. }
  2026. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  2027. {
  2028. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2029. [ret setValue:@"2" forKey:@"result"];
  2030. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2031. sqlite3* db= [iSalesDB get_db];
  2032. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2033. [iSalesDB close_db:db];
  2034. [ret setObject:detail1_section forKey:@"detail_1"];
  2035. return ret;
  2036. }
  2037. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id user:(NSString*)user islogin:(bool)blogin db:(sqlite3*)db
  2038. {
  2039. //assert(user!=nil);
  2040. // UIApplication * app = [UIApplication sharedApplication];
  2041. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2042. // NSArray* arr1 = [self get_user_all_price_type];
  2043. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2044. // NSSet *set1 = [NSSet setWithArray:arr1];
  2045. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2046. // if(appDelegate.contact_id==nil)
  2047. // set2=[set1 mutableCopy];
  2048. // else
  2049. // [set2 intersectsSet:set1];
  2050. // NSArray *retarr = [set2 allObjects];
  2051. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2052. // sqlite3 *db = [iSalesDB get_db];
  2053. NSString* sqlQuery = nil;
  2054. if(contact_id==nil)
  2055. {
  2056. if(!blogin)
  2057. return nil;
  2058. 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];
  2059. }
  2060. else
  2061. 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];
  2062. sqlite3_stmt * statement;
  2063. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2064. int count=0;
  2065. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2066. {
  2067. while (sqlite3_step(statement) == SQLITE_ROW)
  2068. {
  2069. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2070. char *name = (char*)sqlite3_column_text(statement, 0);
  2071. if(name==nil)
  2072. name="";
  2073. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2074. // double price = sqlite3_column_double(statement, 1);
  2075. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2076. // if(isnull==nil)
  2077. // item[nsname]= @"No Price";
  2078. // else
  2079. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2080. char *price = (char*)sqlite3_column_text(statement, 1);
  2081. if(price!=nil)
  2082. {
  2083. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2084. nsprice=[AESCrypt fastdecrypt:nsprice];
  2085. if(nsprice.length>0)
  2086. {
  2087. double dp= [nsprice doubleValue];
  2088. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2089. }
  2090. }
  2091. else
  2092. {
  2093. item[nsname]= @"No Price";
  2094. }
  2095. // int type= sqlite3_column_int(statement, 2);
  2096. //item[@"type"]=@"price";
  2097. // item[nsname]= nsprice;
  2098. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2099. count++;
  2100. }
  2101. ret[@"count"]= [NSNumber numberWithInt:count];
  2102. sqlite3_finalize(statement);
  2103. }
  2104. // [iSalesDB close_db:db];
  2105. return ret;
  2106. }
  2107. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2108. //{
  2109. // NSArray* arr1 = [self get_user_all_price_type:db];
  2110. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2111. //
  2112. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2113. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2114. // // [set2 intersectsSet:set1];
  2115. // //
  2116. // //
  2117. // // NSArray *retarr = [set2 allObjects];
  2118. //
  2119. // NSString* whereprice=nil;
  2120. // if(contact_id==nil)
  2121. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2122. // else
  2123. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2124. //
  2125. //
  2126. // // sqlite3 *db = [iSalesDB get_db];
  2127. //
  2128. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2129. // sqlite3_stmt * statement;
  2130. //
  2131. //
  2132. // NSNumber* ret = nil;
  2133. // double dprice=DBL_MAX;
  2134. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2135. // {
  2136. //
  2137. //
  2138. // while (sqlite3_step(statement) == SQLITE_ROW)
  2139. // {
  2140. //
  2141. // // double val = sqlite3_column_double(statement, 0);
  2142. // char *price = (char*)sqlite3_column_text(statement, 0);
  2143. // if(price!=nil)
  2144. // {
  2145. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2146. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2147. // if(nsprice.length>0)
  2148. // {
  2149. // double dp= [nsprice doubleValue];
  2150. // if(dp<dprice)
  2151. // dprice=dp;
  2152. // }
  2153. // }
  2154. // }
  2155. //
  2156. //
  2157. //
  2158. //
  2159. // sqlite3_finalize(statement);
  2160. //
  2161. //
  2162. //
  2163. //
  2164. // }
  2165. //
  2166. // // [iSalesDB close_db:db];
  2167. //
  2168. // if(dprice==DBL_MAX)
  2169. // ret= nil;
  2170. // else
  2171. // ret= [NSNumber numberWithDouble:dprice];
  2172. // return ret;
  2173. //}
  2174. +(NSNumber*) get_model_default_price:(NSString*) contact_id user:(NSString*)user product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2175. {
  2176. assert(user!=nil);
  2177. NSArray* arr1 = [self get_user_all_price_type:db user:user];
  2178. NSArray* arr2 = [self get_contact_default_price_type:contact_id user:user db:db];
  2179. NSString* whereprice=nil;
  2180. if(contact_id==nil)
  2181. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2182. else
  2183. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2184. // sqlite3 *db = [iSalesDB get_db];
  2185. NSString *productIdCondition = @"1 = 1";
  2186. if (product_id) {
  2187. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2188. }
  2189. NSString *itemIdCondition = @"";
  2190. if (item_id) {
  2191. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2192. }
  2193. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2194. sqlite3_stmt * statement;
  2195. NSNumber* ret = nil;
  2196. double dprice=DBL_MAX;
  2197. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2198. {
  2199. while (sqlite3_step(statement) == SQLITE_ROW)
  2200. {
  2201. // double val = sqlite3_column_double(statement, 0);
  2202. char *price = (char*)sqlite3_column_text(statement, 0);
  2203. if(price!=nil)
  2204. {
  2205. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2206. nsprice=[AESCrypt fastdecrypt:nsprice];
  2207. if(nsprice.length>0)
  2208. {
  2209. double dp= [nsprice doubleValue];
  2210. if(dp<dprice)
  2211. dprice=dp;
  2212. }
  2213. }
  2214. }
  2215. sqlite3_finalize(statement);
  2216. }
  2217. // [iSalesDB close_db:db];
  2218. if(dprice==DBL_MAX)
  2219. ret= nil;
  2220. else
  2221. ret= [NSNumber numberWithDouble:dprice];
  2222. return ret;
  2223. }
  2224. +(NSArray*) get_user_all_price_type:(sqlite3*)db user:(NSString*) user
  2225. {
  2226. assert(user!=nil);
  2227. NSArray* ret=nil;
  2228. // sqlite3 *db = [iSalesDB get_db];
  2229. // no customer assigned , use login user contact_id
  2230. // UIApplication * app = [UIApplication sharedApplication];
  2231. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2232. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",user];
  2233. sqlite3_stmt * statement;
  2234. // int count=0;
  2235. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2236. {
  2237. if (sqlite3_step(statement) == SQLITE_ROW)
  2238. {
  2239. char *val = (char*)sqlite3_column_text(statement, 0);
  2240. if(val==nil)
  2241. val="";
  2242. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2243. ret=[RAConvertor string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2244. }
  2245. sqlite3_finalize(statement);
  2246. }
  2247. // [iSalesDB close_db:db];
  2248. return ret;
  2249. }
  2250. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id user:(NSString*)user db:(sqlite3*)db
  2251. {
  2252. // sqlite3 *db = [iSalesDB get_db];
  2253. if(contact_id==nil)
  2254. {
  2255. // no customer assigned , use login user contact_id
  2256. // UIApplication * app = [UIApplication sharedApplication];
  2257. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2258. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",user];
  2259. sqlite3_stmt * statement;
  2260. // int count=0;
  2261. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2262. {
  2263. if (sqlite3_step(statement) == SQLITE_ROW)
  2264. {
  2265. char *val = (char*)sqlite3_column_text(statement, 0);
  2266. if(val==nil)
  2267. val="";
  2268. contact_id = [[NSString alloc]initWithUTF8String:val];
  2269. }
  2270. sqlite3_finalize(statement);
  2271. }
  2272. if(contact_id.length<=0)
  2273. {
  2274. // [iSalesDB close_db:db];
  2275. return nil;
  2276. }
  2277. }
  2278. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2279. sqlite3_stmt * statement;
  2280. NSArray* ret=nil;
  2281. // int count=0;
  2282. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2283. {
  2284. if (sqlite3_step(statement) == SQLITE_ROW)
  2285. {
  2286. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2287. if(price_type==nil)
  2288. price_type="";
  2289. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2290. if(nsprice_type.length>0)
  2291. ret=[RAConvertor string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2292. else
  2293. ret=nil;
  2294. }
  2295. sqlite3_finalize(statement);
  2296. }
  2297. // [iSalesDB close_db:db];
  2298. return ret;
  2299. }
  2300. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2301. {
  2302. NSString* ret= nil;
  2303. // sqlite3 *db = [iSalesDB get_db];
  2304. NSString *sqlQuery = nil;
  2305. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2306. // 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;
  2307. // 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
  2308. if(product_id==nil && model_name)
  2309. 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;
  2310. else if (product_id)
  2311. 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
  2312. sqlite3_stmt * statement;
  2313. // int count=0;
  2314. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2315. {
  2316. if (sqlite3_step(statement) == SQLITE_ROW)
  2317. {
  2318. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2319. if(imgurl==nil)
  2320. imgurl="";
  2321. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2322. ret=nsimgurl;
  2323. }
  2324. sqlite3_finalize(statement);
  2325. }
  2326. else
  2327. {
  2328. [ret setValue:@"8" forKey:@"result"];
  2329. }
  2330. // [iSalesDB close_db:db];
  2331. DebugLog(@"data string: %@",ret );
  2332. return ret;
  2333. }
  2334. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name upc_code:(NSString *) upc_code
  2335. {
  2336. NSString* ret= nil;
  2337. sqlite3 *db = [iSalesDB get_db];
  2338. NSString *sqlQuery = nil;
  2339. if(product_id==nil)
  2340. if(upc_code==nil)
  2341. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2342. else
  2343. sqlQuery = [NSString stringWithFormat:@"select default_category from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upc_code,upc_code,upc_code,upc_code];// select default_category from model where name='%@';
  2344. else
  2345. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2346. sqlite3_stmt * statement;
  2347. // int count=0;
  2348. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2349. {
  2350. if (sqlite3_step(statement) == SQLITE_ROW)
  2351. {
  2352. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2353. if(default_category==nil)
  2354. default_category="";
  2355. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2356. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2357. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2358. ret=nsdefault_category;
  2359. }
  2360. sqlite3_finalize(statement);
  2361. }
  2362. else
  2363. {
  2364. [ret setValue:@"8" forKey:@"result"];
  2365. }
  2366. [iSalesDB close_db:db];
  2367. DebugLog(@"data string: %@",ret );
  2368. return ret;
  2369. }
  2370. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2371. NSString *sql = nil;
  2372. if (product_id != nil) {
  2373. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2374. } else {
  2375. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2376. }
  2377. NSString *result_set = [iSalesDB jk_queryText:sql];
  2378. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2379. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2380. return result_set;
  2381. }
  2382. //+(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2383. //{
  2384. //
  2385. //
  2386. // assert(params[@"user"]!=nil);
  2387. //
  2388. //
  2389. //
  2390. // NSString* model_name = [params valueForKey:@"product_name"];
  2391. //
  2392. // NSString* product_id = [params valueForKey:@"product_id"];
  2393. //
  2394. // NSString* category = [params valueForKey:@"category"];
  2395. //
  2396. // NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2397. // if(category==nil) {
  2398. // category = default_category_id;
  2399. // } else {
  2400. //
  2401. // NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2402. // // 参数重有多个category id
  2403. // if (arr_0.count > 1) {
  2404. // NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2405. //
  2406. // // 取交集
  2407. // NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2408. // NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2409. // [set_0 intersectSet:set_1];
  2410. //
  2411. // if (set_0.count == 1) {
  2412. // category = (NSString *)[set_0 anyObject];
  2413. // } else {
  2414. // if ([set_0 containsObject:default_category_id]) {
  2415. // category = default_category_id;
  2416. // } else {
  2417. // category = (NSString *)[set_0 anyObject];
  2418. // }
  2419. // }
  2420. // }
  2421. // }
  2422. //
  2423. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2424. //
  2425. //
  2426. //
  2427. //
  2428. //
  2429. //
  2430. // sqlite3 *db = [iSalesDB get_db];
  2431. //
  2432. //// int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2433. //
  2434. //
  2435. // NSString *sqlQuery = nil;
  2436. //
  2437. // if(product_id==nil)
  2438. // 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='%@';
  2439. // else
  2440. //
  2441. // 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=%@;
  2442. //
  2443. //
  2444. // sqlite3_stmt * statement;
  2445. // [ret setValue:@"2" forKey:@"result"];
  2446. // [ret setValue:@"3" forKey:@"detail_section_count"];
  2447. //
  2448. // // int count=0;
  2449. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2450. // {
  2451. //
  2452. //
  2453. // if (sqlite3_step(statement) == SQLITE_ROW)
  2454. // {
  2455. // // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2456. //
  2457. //
  2458. // char *name = (char*)sqlite3_column_text(statement, 0);
  2459. // if(name==nil)
  2460. // name="";
  2461. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2462. //
  2463. // char *description = (char*)sqlite3_column_text(statement, 1);
  2464. // if(description==nil)
  2465. // description="";
  2466. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2467. //
  2468. //
  2469. // int product_id = sqlite3_column_int(statement, 2);
  2470. //
  2471. //
  2472. // char *color = (char*)sqlite3_column_text(statement, 3);
  2473. // if(color==nil)
  2474. // color="";
  2475. // // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2476. // //
  2477. // // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2478. // // if(legcolor==nil)
  2479. // // legcolor="";
  2480. // // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2481. // //
  2482. // //
  2483. // int availability = sqlite3_column_int(statement, 5);
  2484. // //
  2485. // int incoming_stock = sqlite3_column_int(statement, 6);
  2486. //
  2487. //
  2488. // char *demension = (char*)sqlite3_column_text(statement, 7);
  2489. // if(demension==nil)
  2490. // demension="";
  2491. // NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2492. //
  2493. //
  2494. //
  2495. // // ,,,,,,,,,
  2496. //
  2497. //
  2498. // char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2499. // if(seat_height==nil)
  2500. // seat_height="";
  2501. // NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2502. //
  2503. //
  2504. // char *material = (char*)sqlite3_column_text(statement, 9);
  2505. // if(material==nil)
  2506. // material="";
  2507. // NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2508. //
  2509. // char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2510. // if(box_dim==nil)
  2511. // box_dim="";
  2512. // NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2513. //
  2514. // char *volume = (char*)sqlite3_column_text(statement, 11);
  2515. // if(volume==nil)
  2516. // volume="";
  2517. // NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2518. //
  2519. // double weight = sqlite3_column_double(statement, 12);
  2520. //
  2521. // char *model_set = (char*)sqlite3_column_text(statement, 13);
  2522. // if(model_set==nil)
  2523. // model_set="";
  2524. // NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2525. //
  2526. // char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2527. // if(load_ability==nil)
  2528. // load_ability="";
  2529. // NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2530. //
  2531. //// char *default_category = (char*)sqlite3_column_text(statement, 15);
  2532. //// if(default_category==nil)
  2533. //// default_category="";
  2534. //// NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2535. //
  2536. //
  2537. // char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2538. // if(fabric_content==nil)
  2539. // fabric_content="";
  2540. // NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2541. //
  2542. // char *assembling = (char*)sqlite3_column_text(statement, 17);
  2543. // if(assembling==nil)
  2544. // assembling="";
  2545. // NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2546. //
  2547. // char *made_in = (char*)sqlite3_column_text(statement, 18);
  2548. // if(made_in==nil)
  2549. // made_in="";
  2550. // NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2551. //
  2552. //
  2553. // char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2554. // if(special_remarks==nil)
  2555. // special_remarks="";
  2556. // NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2557. //
  2558. //
  2559. // int stockUcom = sqlite3_column_double(statement, 20);
  2560. //
  2561. // char *product_group = (char*)sqlite3_column_text(statement, 21);
  2562. // if(product_group==nil)
  2563. // product_group="";
  2564. // NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2565. //
  2566. // // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2567. // // if(fashion_selector==nil)
  2568. // // fashion_selector="";
  2569. // // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2570. //
  2571. // char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2572. // if(selector_field==nil)
  2573. // selector_field="";
  2574. // NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2575. //
  2576. // char *property_field = (char*)sqlite3_column_text(statement, 23);
  2577. // if(property_field==nil)
  2578. // property_field="";
  2579. // NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2580. //
  2581. //
  2582. //
  2583. // char *packaging = (char*)sqlite3_column_text(statement, 24);
  2584. // if(packaging==nil)
  2585. // packaging="";
  2586. // NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2587. //
  2588. // [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2589. //
  2590. // NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2591. // [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2592. // NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2593. //
  2594. // [img_section setValue:model_s_img forKey:@"model_s_img"];
  2595. // [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2596. // [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2597. // [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2598. //
  2599. //
  2600. //
  2601. //
  2602. //
  2603. // NSString* Availability=nil;
  2604. // if(availability>0)
  2605. // Availability=[NSString stringWithFormat:@"%d",availability];
  2606. // else
  2607. // Availability = @"Out of Stock";
  2608. //
  2609. // [img_section setValue:Availability forKey:@"Availability"];
  2610. // [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2611. //
  2612. // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2613. //
  2614. //
  2615. // char *eta = (char*)sqlite3_column_text(statement, 25);
  2616. // if(eta==nil)
  2617. // eta="";
  2618. // NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2619. // if ([nseta isEqualToString:@"null"]) {
  2620. // nseta = @"";
  2621. // }
  2622. // if (availability <= 0) {
  2623. // [img_section setValue:nseta forKey:@"ETA"];
  2624. // }
  2625. //
  2626. //
  2627. // int item_id = sqlite3_column_int(statement, 26);
  2628. // NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2629. // NSString *content_writing = [self textAtColumn:28 statement:statement];
  2630. //
  2631. // NSString* Price=nil;
  2632. // if(appDelegate.bLogin==false)
  2633. // Price=@"Must Sign in.";
  2634. // else
  2635. // {
  2636. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2637. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2638. // if(price==nil)
  2639. // Price=@"No Price.";
  2640. // else
  2641. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2642. // }
  2643. //
  2644. // [img_section setObject:content_writing forKey:@"product_content_writing"];
  2645. // [img_section setValue:Price forKey:@"price"];
  2646. // [img_section setValue:nsname forKey:@"model_name"];
  2647. // [img_section setValue:nsdescription forKey:@"model_descrition"];
  2648. //
  2649. // if (appDelegate.order_code) { // 离线order code即so#
  2650. //
  2651. // 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];
  2652. // __block int cartQTY = 0;
  2653. // [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2654. //
  2655. // cartQTY = sqlite3_column_int(stmt, 0);
  2656. //
  2657. // }];
  2658. //
  2659. // if (cartQTY > 0) {
  2660. // [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2661. // }
  2662. // }
  2663. //
  2664. // [ret setObject:img_section forKey:@"img_section"];
  2665. //
  2666. // NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2667. // int detail0_item_count=0;
  2668. //
  2669. // [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2670. // [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2671. // [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2672. // [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2673. // [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2674. // [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2675. // [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2676. // [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2677. // [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2678. // [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2679. // [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2680. // [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2681. // [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2682. // [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2683. //
  2684. //
  2685. //// NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2686. // NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2687. // for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2688. // {
  2689. // NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2690. // [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2691. // }
  2692. //
  2693. //
  2694. //
  2695. //
  2696. // [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2697. // [detail0_section setValue:@"kv" forKey:@"type"];
  2698. // [detail0_section setValue:@"Product Information" forKey:@"title"];
  2699. //
  2700. // [ret setObject:detail0_section forKey:@"detail_0"];
  2701. //
  2702. //
  2703. // NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2704. // // [detail1_section setValue:@"detail" forKey:@"target"];
  2705. // // [detail1_section setValue:@"popup" forKey:@"action"];
  2706. // // [detail1_section setValue:@"content" forKey:@"type"];
  2707. // // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2708. // // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2709. // // [detail1_section setValue:@"true" forKey:@"single_row"];
  2710. // // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2711. // [ret setObject:detail1_section forKey:@"detail_1"];
  2712. //
  2713. //
  2714. //
  2715. //
  2716. // NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2717. // [detail2_section setValue:@"detail" forKey:@"target"];
  2718. // [detail2_section setValue:@"popup" forKey:@"action"];
  2719. // [detail2_section setValue:@"content" forKey:@"type"];
  2720. // [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2721. // [detail2_section setValue:@"true" forKey:@"single_row"];
  2722. // [detail2_section setValue:@"local" forKey:@"data"];
  2723. // [ret setObject:detail2_section forKey:@"detail_2"];
  2724. // }
  2725. //
  2726. //
  2727. //
  2728. //
  2729. // sqlite3_finalize(statement);
  2730. // }
  2731. // else
  2732. // {
  2733. // [ret setValue:@"8" forKey:@"result"];
  2734. // }
  2735. //// DebugLog(@"count:%d",count);
  2736. //
  2737. //
  2738. // [iSalesDB close_db:db];
  2739. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2740. //
  2741. // return ret;
  2742. //}
  2743. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2744. // NSString* orderCode = [params valueForKey:@"orderCode"];
  2745. NSString* keyword = [params valueForKey:@"keyword"];
  2746. keyword=keyword.lowercaseString;
  2747. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2748. int limit = [[params valueForKey:@"limit"] intValue];
  2749. int offset = [[params valueForKey:@"offset"] intValue];
  2750. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2751. NSString *limit_str = @"";
  2752. if (limited) {
  2753. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2754. }
  2755. sqlite3 *db = [iSalesDB get_db];
  2756. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2757. // UIApplication * app = [UIApplication sharedApplication];
  2758. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2759. NSString *sqlQuery = nil;
  2760. if(exactMatch )
  2761. 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 ;
  2762. else
  2763. 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
  2764. DebugLog(@"offline_search sql:%@",sqlQuery);
  2765. sqlite3_stmt * statement;
  2766. [ret setValue:@"2" forKey:@"result"];
  2767. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2768. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2769. // int count=0;
  2770. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2771. {
  2772. int i=0;
  2773. while (sqlite3_step(statement) == SQLITE_ROW)
  2774. {
  2775. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2776. // char *name = (char*)sqlite3_column_text(statement, 1);
  2777. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2778. char *name = (char*)sqlite3_column_text(statement, 0);
  2779. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2780. char *description = (char*)sqlite3_column_text(statement, 1);
  2781. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2782. int product_id = sqlite3_column_int(statement, 2);
  2783. // char *url = (char*)sqlite3_column_text(statement, 3);
  2784. // if(url==nil)
  2785. // url="";
  2786. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2787. int wid = sqlite3_column_int(statement, 3);
  2788. int closeout = sqlite3_column_int(statement, 4);
  2789. int cid = sqlite3_column_int(statement, 5);
  2790. int wisdelete = sqlite3_column_int(statement, 6);
  2791. int more_color = sqlite3_column_int(statement, 7);
  2792. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2793. if(wid !=0 && wisdelete != 1)
  2794. [item setValue:@"true" forKey:@"wish_exists"];
  2795. else
  2796. [item setValue:@"false" forKey:@"wish_exists"];
  2797. if(closeout==0)
  2798. [item setValue:@"false" forKey:@"is_closeout"];
  2799. else
  2800. [item setValue:@"true" forKey:@"is_closeout"];
  2801. if(cid==0)
  2802. [item setValue:@"false" forKey:@"cart_exists"];
  2803. else
  2804. [item setValue:@"true" forKey:@"cart_exists"];
  2805. if (more_color == 0) {
  2806. [item setObject:@(false) forKey:@"more_color"];
  2807. } else if (more_color == 1) {
  2808. [item setObject:@(true) forKey:@"more_color"];
  2809. }
  2810. [item addEntriesFromDictionary:imgjson];
  2811. // [item setValue:nsurl forKey:@"img"];
  2812. [item setValue:nsname forKey:@"fash_name"];
  2813. [item setValue:nsdescription forKey:@"description"];
  2814. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2815. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2816. i++;
  2817. }
  2818. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2819. [ret setObject:items forKey:@"items"];
  2820. sqlite3_finalize(statement);
  2821. }
  2822. DebugLog(@"count:%d",count);
  2823. [iSalesDB close_db:db];
  2824. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2825. return ret;
  2826. }
  2827. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2828. {
  2829. return [self search:params limited:YES];
  2830. }
  2831. //+(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2832. //{
  2833. // UIApplication * app = [UIApplication sharedApplication];
  2834. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2835. //
  2836. //// [iSalesDB disable_trigger]
  2837. // [iSalesDB disable_trigger];
  2838. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2839. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2840. //
  2841. // [iSalesDB enable_trigger];
  2842. //// [iSalesDB enable_trigger]
  2843. // //
  2844. // // NSString* user = [params valueForKey:@"user"];
  2845. // //
  2846. // // NSString* password = [params valueForKey:@"password"];
  2847. //
  2848. //
  2849. //
  2850. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2851. //
  2852. // ret[@"result"]=[NSNumber numberWithInt:2 ];
  2853. //
  2854. //
  2855. //
  2856. // appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2857. // appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2858. // appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2859. //
  2860. // [appDelegate update_count_mark];
  2861. //
  2862. //
  2863. // appDelegate.can_show_price =false;
  2864. // appDelegate.can_see_price =false;
  2865. // appDelegate.can_create_portfolio =false;
  2866. // appDelegate.can_create_order =false;
  2867. //
  2868. //
  2869. // appDelegate.can_cancel_order =false;
  2870. // appDelegate.can_set_cart_price =false;
  2871. // appDelegate.can_delete_order =false;
  2872. // appDelegate.can_submit_order =false;
  2873. // appDelegate.can_set_tearsheet_price =false;
  2874. // appDelegate.can_update_contact_info = false;
  2875. //
  2876. // appDelegate.save_order_logout = false;
  2877. // appDelegate.submit_order_logout = false;
  2878. // appDelegate.alert_sold_in_quantities = false;
  2879. //
  2880. // appDelegate.ipad_perm =nil ;
  2881. // appDelegate.user_type = USER_ROLE_UNKNOWN;
  2882. // appDelegate.OrderFilter= nil;
  2883. // [appDelegate SetSo:nil];
  2884. // [appDelegate set_main_button_panel];
  2885. //
  2886. //
  2887. // // sqlite3 *db = [iSalesDB get_db];
  2888. // //
  2889. // //
  2890. // //
  2891. // //
  2892. // //
  2893. // // 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"]];
  2894. // //
  2895. // //
  2896. // //
  2897. // //
  2898. // // DebugLog(@"offline_login sql:%@",sqlQuery);
  2899. // // sqlite3_stmt * statement;
  2900. // //
  2901. // //
  2902. // // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2903. // //
  2904. // //
  2905. // // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2906. // // {
  2907. // //
  2908. // //
  2909. // // if (sqlite3_step(statement) == SQLITE_ROW)
  2910. // // {
  2911. // //
  2912. // // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2913. // // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2914. // //
  2915. // //
  2916. // //
  2917. // // int can_show_price = sqlite3_column_int(statement, 0);
  2918. // // int can_see_price = sqlite3_column_int(statement, 1);
  2919. // //
  2920. // // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2921. // // if(contact_id==nil)
  2922. // // contact_id="";
  2923. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2924. // //
  2925. // // int user_type = sqlite3_column_int(statement, 3);
  2926. // //
  2927. // // int can_cancel_order = sqlite3_column_int(statement, 4);
  2928. // // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2929. // // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2930. // // int can_delete_order = sqlite3_column_int(statement, 7);
  2931. // // int can_submit_order = sqlite3_column_int(statement, 8);
  2932. // // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2933. // // int can_create_order = sqlite3_column_int(statement, 10);
  2934. // //
  2935. // //
  2936. // // char *mode = (char*)sqlite3_column_text(statement, 11);
  2937. // // if(mode==nil)
  2938. // // mode="";
  2939. // // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2940. // //
  2941. // //
  2942. // // char *username = (char*)sqlite3_column_text(statement, 12);
  2943. // // if(username==nil)
  2944. // // username="";
  2945. // // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2946. // //
  2947. // //
  2948. // // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2949. // // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2950. // // [header setValue:nscontact_id forKey:@"contact_id"];
  2951. // // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2952. // // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2953. // // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2954. // // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2955. // // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2956. // // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2957. // // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2958. // // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2959. // //
  2960. // // [header setValue:nsusername forKey:@"username"];
  2961. // //
  2962. // //
  2963. // // [ret setObject:header forKey:@"header"];
  2964. // // [ret setValue:nsmode forKey:@"mode"];
  2965. // //
  2966. // //
  2967. // // }
  2968. // //
  2969. // //
  2970. // //
  2971. // // sqlite3_finalize(statement);
  2972. // // }
  2973. // //
  2974. // //
  2975. // //
  2976. // // [iSalesDB close_db:db];
  2977. // //
  2978. // //
  2979. // //
  2980. // //
  2981. // // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2982. //
  2983. // return ret;
  2984. //}
  2985. //+(NSData*) offline_createorder :(NSMutableDictionary *) params
  2986. //{
  2987. //
  2988. // UIApplication * app = [UIApplication sharedApplication];
  2989. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2990. //
  2991. // [iSalesDB disable_trigger];
  2992. // [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2993. // [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2994. // [iSalesDB enable_trigger];
  2995. //
  2996. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2997. // NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2998. // NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2999. //// NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  3000. // // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  3001. // // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  3002. // // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  3003. // // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  3004. // NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  3005. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  3006. // // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  3007. // // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  3008. // NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  3009. // NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  3010. // NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  3011. // NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  3012. // // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  3013. // NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  3014. // // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  3015. // NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  3016. // // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  3017. // // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  3018. // NSString* create_by = [self translateSingleQuote:params[@"user"]];
  3019. //
  3020. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3021. //
  3022. // [arr_name addObject:customer_first_name];
  3023. // [arr_name addObject:customer_last_name];
  3024. //
  3025. // NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3026. //
  3027. // // default ship from
  3028. // 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';";
  3029. //
  3030. // __block NSString *cid = @"";
  3031. // __block NSString *name = @"";
  3032. // __block NSString *ext = @"";
  3033. // __block NSString *contact = @"";
  3034. // __block NSString *email = @"";
  3035. // __block NSString *fax = @"";
  3036. // __block NSString *phone = @"";
  3037. //
  3038. // sqlite3 *db = [iSalesDB get_db];
  3039. //
  3040. // [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  3041. //
  3042. // cid = [self textAtColumn:0 statement:statment];
  3043. // name = [self textAtColumn:1 statement:statment];
  3044. // ext = [self textAtColumn:2 statement:statment];
  3045. // contact = [self textAtColumn:3 statement:statment];
  3046. // email = [self textAtColumn:4 statement:statment];
  3047. // fax = [self textAtColumn:5 statement:statment];
  3048. // phone = [self textAtColumn:6 statement:statment];
  3049. //
  3050. // }];
  3051. //
  3052. // NSString* so_id = [self get_offline_soid:db];
  3053. // if(so_id==nil)
  3054. // so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  3055. //
  3056. // 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];
  3057. //
  3058. //
  3059. //
  3060. // int result =[iSalesDB execSql:sql_neworder db:db];
  3061. // [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  3062. //
  3063. //
  3064. //
  3065. // //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'
  3066. // //soId
  3067. // int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  3068. //
  3069. //
  3070. //
  3071. //
  3072. //
  3073. // NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  3074. // sqlite3_stmt * statement;
  3075. //
  3076. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3077. // {
  3078. // if (sqlite3_step(statement) == SQLITE_ROW)
  3079. // {
  3080. // // char *name = (char*)sqlite3_column_text(statement, 1);
  3081. // // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3082. //
  3083. // //ret = sqlite3_column_int(statement, 0);
  3084. //
  3085. // char *soId = (char*)sqlite3_column_text(statement, 0);
  3086. // if(soId==nil)
  3087. // soId="";
  3088. // NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  3089. // [ret setValue:nssoId forKey:@"soId"];
  3090. // [ret setValue:nssoId forKey:@"orderCode"];
  3091. //
  3092. // }
  3093. // sqlite3_finalize(statement);
  3094. // }
  3095. //
  3096. // [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  3097. // [ret setValue:@"Regular Mode" forKey:@"mode"];
  3098. //
  3099. //
  3100. // [iSalesDB close_db:db];
  3101. //
  3102. // return [RAConvertor dict2data:ret];
  3103. //
  3104. //}
  3105. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  3106. {
  3107. //assert(params[@"order_code"]);
  3108. // assert(params[@"order_code"]);
  3109. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3110. NSString* orderCode = [params valueForKey:@"orderCode"];
  3111. NSString* app_order_code= params[@"appDelegate.order_code"];
  3112. // UIApplication * app = [UIApplication sharedApplication];
  3113. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3114. if(![app_order_code isEqualToString:orderCode]&& app_order_code!=0)
  3115. {
  3116. [iSalesDB disable_trigger];
  3117. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  3118. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  3119. [iSalesDB enable_trigger];
  3120. }
  3121. sqlite3 *db = [iSalesDB get_db];
  3122. int cart_count=[self query_ordercartcount:orderCode db:db];
  3123. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3124. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3125. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3126. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3127. [iSalesDB close_db:db];
  3128. return [RAConvertor dict2data:ret];
  3129. }
  3130. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  3131. {
  3132. assert(params[@"can_create_backorder"]!=nil);
  3133. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3134. sqlite3 *db = [iSalesDB get_db];
  3135. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3136. int count =0;
  3137. if(params[@"count"]!=nil)
  3138. {
  3139. count = [params[@"count"] intValue];
  3140. }
  3141. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3142. NSString* product_id=params[@"product_id"];
  3143. NSString* orderCode=params[@"orderCode"];
  3144. NSString *qty = params[@"qty"];
  3145. NSArray* arr_id=[RAConvertor string2arr:product_id separator:@","];
  3146. NSArray *qty_arr = [RAConvertor string2arr:qty separator:@","];
  3147. __block int number_of_outOfStock = 0;
  3148. for(int i=0;i<arr_id.count;i++)
  3149. {
  3150. NSInteger item_qty= count;
  3151. if (qty) {
  3152. item_qty = [qty_arr[i] integerValue];
  3153. }
  3154. if(item_qty==0)
  3155. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  3156. // 检查新加Model数量是否大于库存
  3157. if (![params[@"can_create_backorder"] boolValue]) {
  3158. __block BOOL needContinue = NO;
  3159. [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) {
  3160. int availability = sqlite3_column_int(stmt, 0);
  3161. // 库存小于购买量为缺货
  3162. if (availability < item_qty || availability <= 0) {
  3163. number_of_outOfStock++;
  3164. needContinue = YES;
  3165. }
  3166. }];
  3167. if (needContinue) {
  3168. continue;
  3169. }
  3170. }
  3171. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  3172. NSString* sql=nil;
  3173. sqlite3_stmt *stmt = nil;
  3174. BOOL shouldStep = NO;
  3175. if(_id<0)
  3176. {
  3177. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  3178. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  3179. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3180. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  3181. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  3182. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3183. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  3184. shouldStep = YES;
  3185. }
  3186. else
  3187. {
  3188. if (qty) { // wish list move to cart
  3189. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  3190. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3191. sqlite3_bind_int(stmt, 1, _id);
  3192. shouldStep = YES;
  3193. } else {
  3194. 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];
  3195. __block BOOL update = YES;
  3196. if (![params[@"can_create_backorder"] boolValue]) {
  3197. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3198. int newQTY = sqlite3_column_int(stmt, 0);
  3199. int availability = sqlite3_column_int(stmt, 1);
  3200. if (newQTY > availability) { // 库存不够
  3201. update = NO;
  3202. number_of_outOfStock++;
  3203. }
  3204. }];
  3205. }
  3206. if (update) {
  3207. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  3208. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  3209. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  3210. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  3211. shouldStep = YES;
  3212. }
  3213. }
  3214. }
  3215. if (shouldStep) {
  3216. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3217. [iSalesDB execSql:@"ROLLBACK" db:db];
  3218. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3219. [iSalesDB close_db:db];
  3220. DebugLog(@"add to cart error");
  3221. return [RAConvertor dict2data:ret];
  3222. }
  3223. }
  3224. }
  3225. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3226. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3227. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3228. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3229. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3230. if (![params[@"can_create_backorder"] boolValue]) {
  3231. if (number_of_outOfStock > 0) {
  3232. ret[@"result"]=[NSNumber numberWithInt:8];
  3233. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  3234. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  3235. }
  3236. }
  3237. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3238. [iSalesDB close_db:db];
  3239. return [RAConvertor dict2data:ret];
  3240. }
  3241. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID user:(NSString*)user
  3242. {
  3243. // UIApplication * app = [UIApplication sharedApplication];
  3244. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3245. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3246. 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];
  3247. sqlite3_stmt * statement;
  3248. int count=0;
  3249. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3250. {
  3251. while (sqlite3_step(statement) == SQLITE_ROW)
  3252. {
  3253. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3254. int bitem_id = sqlite3_column_int(statement, 0);
  3255. int bitem_qty = sqlite3_column_int(statement, 1);
  3256. char *name = (char*)sqlite3_column_text(statement, 2);
  3257. if(name==nil)
  3258. name="";
  3259. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3260. char *description = (char*)sqlite3_column_text(statement, 3);
  3261. if(description==nil)
  3262. description="";
  3263. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3264. double unit_price = sqlite3_column_double(statement, 4);
  3265. int use_unitprice = sqlite3_column_int(statement, 5);
  3266. if(use_unitprice!=1)
  3267. {
  3268. unit_price=[self get_model_default_price:contactID user:user product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3269. }
  3270. itemjson[@"model"]=nsname;
  3271. itemjson[@"description"]=nsdescription;
  3272. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3273. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3274. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3275. if(compute)
  3276. {
  3277. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID user:user];
  3278. }
  3279. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3280. count++;
  3281. }
  3282. sqlite3_finalize(statement);
  3283. }
  3284. ret[@"count"]=@(count);
  3285. if(count==0)
  3286. return nil;
  3287. else
  3288. return ret;
  3289. }
  3290. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3291. {
  3292. //compute: add part to subtotal;
  3293. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3294. dict_item[@(item_id)]=@"1";
  3295. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3296. double cuft=0;
  3297. double weight=0;
  3298. int carton=0;
  3299. 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];
  3300. sqlite3_stmt * statement;
  3301. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3302. {
  3303. if (sqlite3_step(statement) == SQLITE_ROW)
  3304. {
  3305. double ulength = sqlite3_column_double(statement, 0);
  3306. double uwidth = sqlite3_column_double(statement, 1);
  3307. double uheight = sqlite3_column_double(statement, 2);
  3308. double uweight = sqlite3_column_double(statement, 3);
  3309. double mlength = sqlite3_column_double(statement, 4);
  3310. double mwidth = sqlite3_column_double(statement, 5);
  3311. double mheight = sqlite3_column_double(statement, 6);
  3312. double mweight = sqlite3_column_double(statement, 7);
  3313. double ilength = sqlite3_column_double(statement, 8);
  3314. double iwidth = sqlite3_column_double(statement, 9);
  3315. double iheight = sqlite3_column_double(statement, 10);
  3316. double iweight = sqlite3_column_double(statement, 11);
  3317. // int pcs = sqlite3_column_int(statement,12);
  3318. int mpack = sqlite3_column_int(statement, 13);
  3319. int ipack = sqlite3_column_int(statement, 14);
  3320. double ucbf = sqlite3_column_double(statement, 15);
  3321. // double icbf = sqlite3_column_double(statement, 16);
  3322. // double mcbf = sqlite3_column_double(statement, 17);
  3323. if(ipack==0)
  3324. {
  3325. carton= count/mpack ;
  3326. weight = mweight*carton;
  3327. cuft= carton*(mlength*mwidth*mheight);
  3328. int remain=count%mpack;
  3329. if(remain==0)
  3330. {
  3331. //do nothing;
  3332. }
  3333. else
  3334. {
  3335. carton++;
  3336. weight += uweight*remain;
  3337. cuft += (ulength*uwidth*uheight)*remain;
  3338. }
  3339. }
  3340. else
  3341. {
  3342. carton = count/(mpack*ipack);
  3343. weight = mweight*carton;
  3344. cuft= carton*(mlength*mwidth*mheight);
  3345. int remain=count%(mpack*ipack);
  3346. if(remain==0)
  3347. {
  3348. // do nothing;
  3349. }
  3350. else
  3351. {
  3352. carton++;
  3353. int icarton =remain/ipack;
  3354. int iremain=remain%ipack;
  3355. weight += iweight*icarton;
  3356. cuft += (ilength*iwidth*iheight)*icarton;
  3357. if(iremain==0)
  3358. {
  3359. //do nothing;
  3360. }
  3361. else
  3362. {
  3363. weight += uweight*iremain;
  3364. cuft += (ulength*uwidth*uheight)*iremain;
  3365. }
  3366. }
  3367. }
  3368. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  3369. cuft=ucbf*count;
  3370. weight= uweight*count;
  3371. #endif
  3372. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3373. cuft=ucbf*count;
  3374. weight= uweight*count;
  3375. #endif
  3376. }
  3377. sqlite3_finalize(statement);
  3378. }
  3379. if(compute)
  3380. {
  3381. NSArray * arr_count=nil;
  3382. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3383. for(int i=0;i<arr_bundle.count;i++)
  3384. {
  3385. dict_item[arr_bundle[i]]=@"1";
  3386. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3387. cuft+=[bundlejson[@"cuft"] doubleValue];
  3388. weight+=[bundlejson[@"weight"] doubleValue];
  3389. carton+=[bundlejson[@"carton"] intValue];
  3390. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3391. }
  3392. }
  3393. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3394. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3395. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3396. ret[@"items"]=dict_item;
  3397. return ret;
  3398. }
  3399. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3400. {
  3401. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3402. // 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 ];
  3403. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3404. DebugLog(@"offline_login sql:%@",sqlQuery);
  3405. sqlite3_stmt * statement;
  3406. int cart_count=0;
  3407. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3408. if ( dbresult== SQLITE_OK)
  3409. {
  3410. while (sqlite3_step(statement) == SQLITE_ROW)
  3411. {
  3412. int item_id = sqlite3_column_int(statement, 0);
  3413. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3414. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3415. }
  3416. sqlite3_finalize(statement);
  3417. }
  3418. return cart_count;
  3419. }
  3420. //+(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3421. //{
  3422. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3423. // sqlite3 *db = [iSalesDB get_db];
  3424. // UIApplication * app = [UIApplication sharedApplication];
  3425. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3426. //
  3427. // NSString* orderCode=params[@"orderCode"];
  3428. //
  3429. // int sort = [[params objectForKey:@"sort"] intValue];
  3430. // NSString *sort_str = @"";
  3431. // switch (sort) {
  3432. // case 0:{
  3433. // sort_str = @"order by c.modify_time desc";
  3434. // }
  3435. // break;
  3436. // case 1:{
  3437. // sort_str = @"order by c.modify_time asc";
  3438. // }
  3439. // break;
  3440. // case 2:{
  3441. // sort_str = @"order by m.name asc";
  3442. // }
  3443. // break;
  3444. // case 3:{
  3445. // sort_str = @"order by m.name desc";
  3446. // }
  3447. // break;
  3448. // case 4:{
  3449. // sort_str = @"order by m.description asc";
  3450. // }
  3451. // break;
  3452. //
  3453. // default:
  3454. // break;
  3455. //
  3456. // }
  3457. //
  3458. //
  3459. //
  3460. // 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 ];
  3461. //
  3462. //
  3463. //// 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 ];
  3464. //
  3465. //
  3466. //// NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3467. //
  3468. // DebugLog(@"offline_login sql:%@",sqlQuery);
  3469. // sqlite3_stmt * statement;
  3470. //
  3471. //
  3472. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3473. //
  3474. // NSDate *date1 = [NSDate date];
  3475. //
  3476. // int count=0;
  3477. // int cart_count=0;
  3478. // int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3479. // if ( dbresult== SQLITE_OK)
  3480. // {
  3481. //
  3482. //
  3483. // while (sqlite3_step(statement) == SQLITE_ROW)
  3484. // {
  3485. //// NSDate *row_date = [NSDate date];
  3486. //
  3487. //
  3488. // NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3489. //
  3490. //
  3491. //
  3492. //
  3493. // int product_id = sqlite3_column_int(statement, 0);
  3494. //
  3495. // char *str_price = (char*)sqlite3_column_text(statement, 1);
  3496. //
  3497. // int item_id = sqlite3_column_int(statement, 7);
  3498. //
  3499. // NSString* Price=nil;
  3500. // if(str_price==nil)
  3501. // {
  3502. //// NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3503. //// NSDate *price_date = [NSDate date];
  3504. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3505. //// DebugLog(@"price time interval");
  3506. //// [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3507. //
  3508. // if(price==nil)
  3509. // Price=@"No Price.";
  3510. // else
  3511. // Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3512. // }
  3513. // else
  3514. // {
  3515. //
  3516. // Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3517. // }
  3518. //
  3519. //
  3520. // double discount = sqlite3_column_double(statement, 2);
  3521. // int item_count = sqlite3_column_int(statement, 3);
  3522. //
  3523. // char *line_note = (char*)sqlite3_column_text(statement, 4);
  3524. // NSString *nsline_note=nil;
  3525. // if(line_note!=nil)
  3526. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3527. //
  3528. //
  3529. // char *name = (char*)sqlite3_column_text(statement, 5);
  3530. // NSString *nsname=nil;
  3531. // if(name!=nil)
  3532. // nsname= [[NSString alloc]initWithUTF8String:name];
  3533. //
  3534. // char *description = (char*)sqlite3_column_text(statement, 6);
  3535. // NSString *nsdescription=nil;
  3536. // if(description!=nil)
  3537. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  3538. //
  3539. //
  3540. //
  3541. // int stockUom = sqlite3_column_int(statement, 8);
  3542. // int _id = sqlite3_column_int(statement, 9);
  3543. // int availability = sqlite3_column_int(statement, 10);
  3544. //
  3545. //// NSDate *subtotal_date = [NSDate date];
  3546. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3547. //// DebugLog(@"subtotal_date time interval");
  3548. //// [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3549. //
  3550. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3551. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  3552. // int carton=[bsubtotaljson[@"carton"] intValue];
  3553. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3554. //
  3555. //// itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3556. //// NSDate *img_date = [NSDate date];
  3557. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3558. //// DebugLog(@"img_date time interval");
  3559. //// [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3560. //
  3561. // itemjson[@"model"]=nsname;
  3562. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3563. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3564. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3565. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3566. // itemjson[@"check"]=@"true";
  3567. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3568. // itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3569. // itemjson[@"unit_price"]=Price;
  3570. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3571. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3572. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3573. // itemjson[@"note"]=nsline_note;
  3574. // if (!appDelegate.can_create_backorder) {
  3575. // itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3576. // }
  3577. //// NSDate *date2 = [NSDate date];
  3578. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3579. //// DebugLog(@"model_bundle time interval");
  3580. //// [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3581. //
  3582. // ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3583. // count++;
  3584. //
  3585. //// DebugLog(@"row time interval");
  3586. //// [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3587. // }
  3588. //
  3589. //
  3590. // ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3591. // sqlite3_finalize(statement);
  3592. // }
  3593. //
  3594. //
  3595. // DebugLog(@"request cart total time interval");
  3596. // [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3597. //
  3598. //
  3599. // //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3600. //
  3601. // int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3602. // ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3603. // ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3604. // ret[@"count"]=[NSNumber numberWithInt:count ];
  3605. //
  3606. // ret[@"mode"]=@"Regular Mode";
  3607. //
  3608. // [iSalesDB close_db:db];
  3609. //
  3610. // NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3611. //
  3612. // DebugLog(@"general notes :%@",general_note);
  3613. //
  3614. // ret[@"general_note"]= general_note;
  3615. //
  3616. // return [RAConvertor dict2data:ret];
  3617. //}
  3618. +(NSData*) offline_login :(NSMutableDictionary *) params
  3619. {
  3620. NSString* user = [params valueForKey:@"user"];
  3621. NSString* password = [params valueForKey:@"password"];
  3622. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3623. sqlite3 *db = [iSalesDB get_db];
  3624. 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"]];
  3625. DebugLog(@"offline_login sql:%@",sqlQuery);
  3626. sqlite3_stmt * statement;
  3627. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3628. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3629. {
  3630. if (sqlite3_step(statement) == SQLITE_ROW)
  3631. {
  3632. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3633. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3634. int can_show_price = sqlite3_column_int(statement, 0);
  3635. int can_see_price = sqlite3_column_int(statement, 1);
  3636. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3637. if(contact_id==nil)
  3638. contact_id="";
  3639. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3640. int user_type = sqlite3_column_int(statement, 3);
  3641. int can_cancel_order = sqlite3_column_int(statement, 4);
  3642. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3643. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3644. int can_delete_order = sqlite3_column_int(statement, 7);
  3645. int can_submit_order = sqlite3_column_int(statement, 8);
  3646. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3647. int can_create_order = sqlite3_column_int(statement, 10);
  3648. char *mode = (char*)sqlite3_column_text(statement, 11);
  3649. if(mode==nil)
  3650. mode="";
  3651. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3652. char *username = (char*)sqlite3_column_text(statement, 12);
  3653. if(username==nil)
  3654. username="";
  3655. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3656. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3657. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3658. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3659. [header setValue:nscontact_id forKey:@"contact_id"];
  3660. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3661. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3662. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3663. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3664. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3665. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3666. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3667. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3668. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3669. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3670. [header setValue:nsusername forKey:@"username"];
  3671. NSError* error=nil;
  3672. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3673. [header setValue:statusFilter forKey:@"statusFilter"];
  3674. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3675. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3676. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3677. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3678. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3679. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3680. [ret setObject:header forKey:@"header"];
  3681. [ret setValue:nsmode forKey:@"mode"];
  3682. }
  3683. sqlite3_finalize(statement);
  3684. }
  3685. [iSalesDB close_db:db];
  3686. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3687. return [RAConvertor dict2data:ret];
  3688. }
  3689. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3690. {
  3691. assert(params[@"mode"]!=nil);
  3692. NSString* contactId = [params valueForKey:@"contactId"];
  3693. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3694. sqlite3 *db = [iSalesDB get_db];
  3695. NSString *sqlQuery = nil;
  3696. {
  3697. 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];
  3698. }
  3699. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3700. sqlite3_stmt * statement;
  3701. [ret setValue:@"2" forKey:@"result"];
  3702. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3703. {
  3704. //int i = 0;
  3705. if (sqlite3_step(statement) == SQLITE_ROW)
  3706. {
  3707. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3708. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3709. // int editable = sqlite3_column_int(statement, 0);
  3710. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3711. NSString *nscompany_name =nil;
  3712. if(company_name==nil)
  3713. nscompany_name=@"";
  3714. else
  3715. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3716. char *country = (char*)sqlite3_column_text(statement, 2);
  3717. if(country==nil)
  3718. country="";
  3719. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3720. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3721. // if(addr==nil)
  3722. // addr="";
  3723. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3724. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3725. if(zipcode==nil)
  3726. zipcode="";
  3727. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3728. char *state = (char*)sqlite3_column_text(statement, 5);
  3729. if(state==nil)
  3730. state="";
  3731. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3732. char *city = (char*)sqlite3_column_text(statement, 6);
  3733. if(city==nil)
  3734. city="";
  3735. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3736. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3737. // NSString *nscontact_name = nil;
  3738. // if(contact_name==nil)
  3739. // nscontact_name=@"";
  3740. // else
  3741. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3742. char *phone = (char*)sqlite3_column_text(statement, 8);
  3743. NSString *nsphone = nil;
  3744. if(phone==nil)
  3745. nsphone=@"";
  3746. else
  3747. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3748. // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3749. // if(contact_id==nil)
  3750. // contact_id="";
  3751. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3752. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3753. if(addr_1==nil)
  3754. addr_1="";
  3755. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3756. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3757. if(addr_2==nil)
  3758. addr_2="";
  3759. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3760. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3761. if(addr_3==nil)
  3762. addr_3="";
  3763. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3764. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3765. if(addr_4==nil)
  3766. addr_4="";
  3767. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3768. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3769. if(first_name==nil)
  3770. first_name="";
  3771. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3772. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3773. if(last_name==nil)
  3774. last_name="";
  3775. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3776. char *fax = (char*)sqlite3_column_text(statement, 16);
  3777. NSString *nsfax = nil;
  3778. if(fax==nil)
  3779. nsfax=@"";
  3780. else
  3781. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3782. char *email = (char*)sqlite3_column_text(statement, 17);
  3783. NSString *nsemail = nil;
  3784. if(email==nil)
  3785. nsemail=@"";
  3786. else
  3787. nsemail= [[NSString alloc]initWithUTF8String:email];
  3788. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3789. NSString *nsimg_0 = nil;
  3790. if(img_0==nil)
  3791. nsimg_0=@"";
  3792. else
  3793. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3794. [self copy_bcardImg:nsimg_0];
  3795. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3796. NSString *nsimg_1 = nil;
  3797. if(img_1==nil)
  3798. nsimg_1=@"";
  3799. else
  3800. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3801. [self copy_bcardImg:nsimg_1];
  3802. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3803. NSString *nsimg_2 = nil;
  3804. if(img_2==nil)
  3805. nsimg_2=@"";
  3806. else
  3807. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3808. [self copy_bcardImg:nsimg_2];
  3809. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3810. NSString *nsprice_type = nil;
  3811. if(price_type==nil)
  3812. nsprice_type=@"";
  3813. else
  3814. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3815. char *notes = (char*)sqlite3_column_text(statement, 22);
  3816. NSString *nsnotes = nil;
  3817. if(notes==nil)
  3818. nsnotes=@"";
  3819. else
  3820. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3821. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3822. NSString *nssalesrep = nil;
  3823. if(salesrep==nil)
  3824. nssalesrep=@"";
  3825. else
  3826. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3827. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3828. {
  3829. // decrypt
  3830. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3831. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3832. nsphone=[AESCrypt fastdecrypt:nsphone];
  3833. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3834. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3835. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3836. }
  3837. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3838. [arr_name addObject:nsfirst_name];
  3839. [arr_name addObject:nslast_name];
  3840. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3841. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3842. [arr_ext addObject:nsaddr_1];
  3843. [arr_ext addObject:nsaddr_2];
  3844. [arr_ext addObject:nsaddr_3];
  3845. [arr_ext addObject:nsaddr_4];
  3846. [arr_ext addObject:@"\r\n"];
  3847. [arr_ext addObject:nscity];
  3848. [arr_ext addObject:nsstate];
  3849. [arr_ext addObject:nszipcode];
  3850. [arr_ext addObject:nscountry];
  3851. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3852. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3853. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3854. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3855. [item setValue:nscountry forKey:@"customer_country"];
  3856. [item setValue:nsphone forKey:@"customer_phone"];
  3857. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3858. [item setValue:nscompany_name forKey:@"customer_name"];
  3859. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3860. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3861. [item setValue:nsext forKey:@"customer_contact_ext"];
  3862. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3863. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3864. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3865. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3866. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3867. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3868. [item setValue:nslast_name forKey:@"customer_last_name"];
  3869. [item setValue:nscity forKey:@"customer_city"];
  3870. [item setValue:nsstate forKey:@"customer_state"];
  3871. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3872. [item setValue:contactId forKey:@"customer_cid"];
  3873. [item setValue:nscontact_name forKey:@"customer_contact"];
  3874. [item setValue:nsfax forKey:@"customer_fax"];
  3875. [item setValue:nsemail forKey:@"customer_email"];
  3876. [item setValue:contact_type forKey:@"customer_contact_type"];
  3877. [ret setObject:item forKey:@"customerInfo"];
  3878. // i++;
  3879. }
  3880. // UIApplication * app = [UIApplication sharedApplication];
  3881. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3882. [ret setValue:params[@"mode"] forKey:@"mode"];
  3883. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3884. sqlite3_finalize(statement);
  3885. }
  3886. [iSalesDB close_db:db];
  3887. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3888. return ret;
  3889. }
  3890. + (bool) copy_bcardImg:(NSString*) filename
  3891. {
  3892. if(filename.length==0)
  3893. return false;
  3894. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3895. bool ret=false;
  3896. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3897. NSString *cachefolder = [paths objectAtIndex:0];
  3898. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3899. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3900. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3901. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3902. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3903. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3904. //
  3905. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3906. NSFileManager* fileManager = [NSFileManager defaultManager];
  3907. BOOL bdir=NO;
  3908. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3909. {
  3910. NSError *error = nil;
  3911. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3912. {
  3913. ret=false;
  3914. }
  3915. else
  3916. {
  3917. ret=true;
  3918. }
  3919. // NSError *error = nil;
  3920. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3921. //
  3922. // if(!bsuccess)
  3923. // {
  3924. // DebugLog(@"Create offline_createimg folder failed");
  3925. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3926. // return [RAConvertor dict2data:ret];
  3927. // }
  3928. // if(bsuccess)
  3929. // {
  3930. // sqlite3 *db = [self get_db];
  3931. //
  3932. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3933. // [iSalesDB close_db:db];
  3934. // }
  3935. }
  3936. return ret;
  3937. //
  3938. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3939. // if(bsuccess)
  3940. // {
  3941. // NSError *error = nil;
  3942. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3943. // {
  3944. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3945. // }
  3946. // else
  3947. // {
  3948. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3949. //
  3950. // ret[@"img_url_aname"]=filename;
  3951. // ret[@"img_url"]=savedImagePath;
  3952. // }
  3953. // }
  3954. }
  3955. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3956. {
  3957. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3958. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3959. NSString *cachefolder = [paths objectAtIndex:0];
  3960. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3961. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3962. NSFileManager* fileManager = [NSFileManager defaultManager];
  3963. BOOL bdir=YES;
  3964. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3965. {
  3966. NSError *error = nil;
  3967. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3968. if(!bsuccess)
  3969. {
  3970. DebugLog(@"Create offline_createimg folder failed");
  3971. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3972. return [RAConvertor dict2data:ret];
  3973. }
  3974. // if(bsuccess)
  3975. // {
  3976. // sqlite3 *db = [self get_db];
  3977. //
  3978. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3979. // [iSalesDB close_db:db];
  3980. // }
  3981. }
  3982. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3983. //JEPG格式
  3984. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3985. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3986. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3987. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3988. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3989. if(bsuccess)
  3990. {
  3991. NSError *error = nil;
  3992. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3993. {
  3994. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3995. }
  3996. else
  3997. {
  3998. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3999. ret[@"img_url_aname"]=filename;
  4000. ret[@"img_url"]=filename;
  4001. }
  4002. }
  4003. else
  4004. {
  4005. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  4006. }
  4007. return [RAConvertor dict2data:ret];
  4008. }
  4009. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  4010. {
  4011. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  4012. category = [category substringToIndex:3];
  4013. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4014. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  4015. params[@"category"]= category;
  4016. ret[@"params"]= params;
  4017. [ret setValue:@"detail" forKey:@"target"];
  4018. [ret setValue:@"popup" forKey:@"action"];
  4019. [ret setValue:@"content" forKey:@"type"];
  4020. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  4021. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  4022. [ret setValue:@"true" forKey:@"single_row"];
  4023. [ret setValue:@"true" forKey:@"partial_refresh"];
  4024. // sqlite3 *db = [iSalesDB get_db];
  4025. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  4026. NSString *sqlQuery =nil;
  4027. #ifdef BUILD_NPD
  4028. 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 ;
  4029. #else
  4030. 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 i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY];
  4031. #endif
  4032. sqlite3_stmt * statement;
  4033. int count = 0;
  4034. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  4035. // int count=0;
  4036. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4037. {
  4038. int i=0;
  4039. while (sqlite3_step(statement) == SQLITE_ROW)
  4040. {
  4041. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4042. // char *name = (char*)sqlite3_column_text(statement, 1);
  4043. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4044. char *name = (char*)sqlite3_column_text(statement, 0);
  4045. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4046. int product_id = sqlite3_column_int(statement, 1);
  4047. char *url = (char*)sqlite3_column_text(statement, 2);
  4048. if(url==nil)
  4049. url="";
  4050. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4051. [item setValue:nsurl forKey:@"picture_path"];
  4052. [item setValue:nsname forKey:@"fash_name"];
  4053. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4054. [item setValue:category forKey:@"category"];
  4055. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4056. i++;
  4057. }
  4058. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4059. sqlite3_finalize(statement);
  4060. }
  4061. DebugLog(@"count:%d",count);
  4062. // [iSalesDB close_db:db];
  4063. return ret;
  4064. }
  4065. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  4066. {
  4067. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4068. [ret setValue:key forKey:@"key"];
  4069. [ret setValue:value forKey:@"val"];
  4070. [ret setValue:@"price" forKey:@"type"];
  4071. return ret;
  4072. }
  4073. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  4074. {
  4075. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4076. [ret setValue:key forKey:@"key"];
  4077. [ret setValue:value forKey:@"val"];
  4078. return ret;
  4079. }
  4080. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  4081. {
  4082. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4083. [ret setValue:@"0" forKey:@"img_count"];
  4084. // sqlite3 *db = [iSalesDB get_db];
  4085. 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 ;
  4086. sqlite3_stmt * statement;
  4087. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4088. {
  4089. int i=0;
  4090. if (sqlite3_step(statement) == SQLITE_ROW)
  4091. {
  4092. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4093. // char *name = (char*)sqlite3_column_text(statement, 1);
  4094. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4095. char *value = (char*)sqlite3_column_text(statement, 0);
  4096. if(value==nil)
  4097. value="";
  4098. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4099. char *key = (char*)sqlite3_column_text(statement, 1);
  4100. if(key==nil)
  4101. key="";
  4102. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  4103. [item setValue:nsvalue forKey:@"val"];
  4104. [item setValue:nskey forKey:@"key"];
  4105. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4106. [ret setValue:@"1" forKey:@"count"];
  4107. i++;
  4108. }
  4109. sqlite3_finalize(statement);
  4110. }
  4111. // [iSalesDB close_db:db];
  4112. return ret;
  4113. }
  4114. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  4115. {
  4116. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4117. [ret setValue:@"0" forKey:@"count"];
  4118. // sqlite3 *db = [iSalesDB get_db];
  4119. 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;
  4120. sqlite3_stmt * statement;
  4121. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4122. {
  4123. int i=0;
  4124. while (sqlite3_step(statement) == SQLITE_ROW)
  4125. {
  4126. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4127. // char *name = (char*)sqlite3_column_text(statement, 1);
  4128. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4129. char *value = (char*)sqlite3_column_text(statement, 0);
  4130. if(value==nil)
  4131. value="";
  4132. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  4133. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  4134. if(selector_display==nil)
  4135. selector_display="";
  4136. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  4137. int product_id = sqlite3_column_int(statement, 2);
  4138. char *category = (char*)sqlite3_column_text(statement, 3);
  4139. if(category==nil)
  4140. category="";
  4141. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  4142. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  4143. [item setValue:nsvalue forKey:@"title"];
  4144. [item setValue:url forKey:@"pic_url"];
  4145. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  4146. [params setValue:@"2" forKey:@"count"];
  4147. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  4148. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  4149. [param0 setValue:@"product_id" forKey:@"name"];
  4150. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  4151. [param1 setValue:nscategory forKey:@"val"];
  4152. [param1 setValue:@"category" forKey:@"name"];
  4153. [params setObject:param0 forKey:@"param_0"];
  4154. [params setObject:param1 forKey:@"param_1"];
  4155. [item setObject:params forKey:@"params"];
  4156. [ret setValue:nsselector_display forKey:@"name"];
  4157. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4158. i++;
  4159. }
  4160. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4161. [ret setValue:@"switch" forKey:@"action"];
  4162. sqlite3_finalize(statement);
  4163. }
  4164. // [iSalesDB close_db:db];
  4165. return ret;
  4166. }
  4167. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  4168. {
  4169. // model 在 category search 显示的图片。
  4170. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  4171. // sqlite3 *db = [iSalesDB get_db];
  4172. 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];
  4173. sqlite3_stmt * statement;
  4174. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4175. {
  4176. while (sqlite3_step(statement) == SQLITE_ROW)
  4177. {
  4178. char *url = (char*)sqlite3_column_text(statement, 0);
  4179. if(url==nil)
  4180. url="";
  4181. int type = sqlite3_column_int(statement, 1);
  4182. if(type==0)
  4183. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  4184. else
  4185. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  4186. }
  4187. sqlite3_finalize(statement);
  4188. }
  4189. // [iSalesDB close_db:db];
  4190. return ret;
  4191. }
  4192. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  4193. {
  4194. int item_id=-1;
  4195. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  4196. sqlite3_stmt * statement;
  4197. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4198. {
  4199. if (sqlite3_step(statement) == SQLITE_ROW)
  4200. {
  4201. item_id = sqlite3_column_int(statement, 0);
  4202. }
  4203. sqlite3_finalize(statement);
  4204. }
  4205. return item_id;
  4206. }
  4207. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  4208. {
  4209. // NSString* ret = @"";
  4210. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  4211. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  4212. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  4213. sqlite3_stmt * statement;
  4214. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4215. {
  4216. while (sqlite3_step(statement) == SQLITE_ROW)
  4217. {
  4218. int bitem_id = sqlite3_column_int(statement, 0);
  4219. int bitem_qty = sqlite3_column_int(statement, 1);
  4220. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  4221. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  4222. }
  4223. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  4224. sqlite3_finalize(statement);
  4225. }
  4226. // if(ret==nil)
  4227. // ret=@"";
  4228. *count=arr_count;
  4229. return arr_bundle;
  4230. }
  4231. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  4232. {
  4233. // get default sold qty, return -1 if model not found;
  4234. int ret = -1;
  4235. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  4236. sqlite3_stmt * statement;
  4237. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4238. {
  4239. if (sqlite3_step(statement) == SQLITE_ROW)
  4240. {
  4241. ret = sqlite3_column_int(statement, 0);
  4242. }
  4243. sqlite3_finalize(statement);
  4244. }
  4245. return ret;
  4246. }
  4247. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  4248. {
  4249. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4250. [ret setValue:@"0" forKey:@"img_count"];
  4251. // sqlite3 *db = [iSalesDB get_db];
  4252. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  4253. sqlite3_stmt * statement;
  4254. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4255. {
  4256. int i=0;
  4257. while (sqlite3_step(statement) == SQLITE_ROW)
  4258. {
  4259. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4260. // char *name = (char*)sqlite3_column_text(statement, 1);
  4261. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  4262. char *url = (char*)sqlite3_column_text(statement, 0);
  4263. if(url==nil)
  4264. url="";
  4265. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  4266. [item setValue:nsurl forKey:@"s"];
  4267. [item setValue:nsurl forKey:@"l"];
  4268. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  4269. i++;
  4270. }
  4271. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  4272. sqlite3_finalize(statement);
  4273. }
  4274. // [iSalesDB close_db:db];
  4275. return ret;
  4276. }
  4277. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  4278. // UIApplication * app = [UIApplication sharedApplication];
  4279. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4280. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  4281. sqlite3 *db = [iSalesDB get_db];
  4282. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4283. NSString* product_id=params[@"product_id"];
  4284. NSString *item_count_str = params[@"item_count"];
  4285. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  4286. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  4287. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4288. // NSString *sql = @"";
  4289. for(int i=0;i<arr.count;i++)
  4290. {
  4291. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  4292. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  4293. __block int cart_count = 0;
  4294. if (!item_count_str) {
  4295. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  4296. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4297. NSString *model_set = [self textAtColumn:0 statement:stmt];
  4298. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  4299. cart_count = [[model_set_components lastObject] intValue];
  4300. }];
  4301. }
  4302. if(count==0)
  4303. {
  4304. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  4305. sqlite3_stmt *stmt;
  4306. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4307. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  4308. if (item_count_arr) {
  4309. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  4310. } else {
  4311. sqlite3_bind_int(stmt,2,cart_count);
  4312. }
  4313. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4314. [iSalesDB execSql:@"ROLLBACK" db:db];
  4315. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4316. [iSalesDB close_db:db];
  4317. DebugLog(@"add to wishlist error");
  4318. return ret;
  4319. }
  4320. } else {
  4321. int qty = 0;
  4322. if (item_count_arr) {
  4323. qty = [item_count_arr[i] intValue];
  4324. } else {
  4325. qty = cart_count;
  4326. }
  4327. 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]];
  4328. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4329. [iSalesDB execSql:@"ROLLBACK" db:db];
  4330. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4331. [iSalesDB close_db:db];
  4332. DebugLog(@"add to wishlist error");
  4333. return ret;
  4334. }
  4335. }
  4336. }
  4337. // [iSalesDB execSql:sql db:db];
  4338. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4339. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4340. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4341. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4342. [iSalesDB close_db:db];
  4343. ret[@"wish_count"]=@(count);
  4344. ret[@"result"]= [NSNumber numberWithInt:2];
  4345. return ret;
  4346. }
  4347. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4348. // 0 category
  4349. // 1 search
  4350. // 2 itemsearch
  4351. NSData *ret = nil;
  4352. NSDictionary *items = nil;
  4353. switch (from) {
  4354. case 0:{
  4355. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4356. }
  4357. break;
  4358. case 1:{
  4359. items = [[self search:params limited:NO] objectForKey:@"items"];
  4360. }
  4361. break;
  4362. case 2:{
  4363. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4364. }
  4365. break;
  4366. default:
  4367. break;
  4368. }
  4369. if (!items) {
  4370. return ret;
  4371. }
  4372. int count = [[items objectForKey:@"count"] intValue];
  4373. NSMutableString *product_id_str = [@"" mutableCopy];
  4374. for (int i = 0; i < count; i++) {
  4375. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4376. NSDictionary *item = [items objectForKey:key];
  4377. NSString *product_id = [item objectForKey:@"product_id"];
  4378. if (i == 0) {
  4379. [product_id_str appendString:product_id];
  4380. } else {
  4381. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4382. }
  4383. }
  4384. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4385. if ([add_to isEqualToString:@"cart"]) {
  4386. NSString *order_code = [params objectForKey:@"orderCode"];
  4387. if (order_code.length) {
  4388. NSDictionary *newParams = @{
  4389. @"product_id" : product_id_str,
  4390. @"orderCode" : order_code,
  4391. @"can_create_backorder":params[@"can_create_backorder"]
  4392. };
  4393. ret = [self offline_add2cart:newParams.mutableCopy];
  4394. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4395. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4396. ret = [RAConvertor dict2data:retDic];
  4397. }
  4398. } else if([add_to isEqualToString:@"wishlist"]) {
  4399. NSDictionary *newParams = @{
  4400. @"product_id" : product_id_str
  4401. };
  4402. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4403. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4404. ret = [RAConvertor dict2data:retDic];
  4405. } else if([add_to isEqualToString:@"portfolio"]) {
  4406. NSDictionary *newParams = @{
  4407. @"product_id" : product_id_str
  4408. };
  4409. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4410. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4411. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4412. ret = [RAConvertor dict2data:retDic];
  4413. }
  4414. return ret;
  4415. }
  4416. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4417. {
  4418. return [self addAll:params from:0];
  4419. }
  4420. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4421. {
  4422. return [self addAll:params from:1];
  4423. }
  4424. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4425. {
  4426. return [self addAll:params from:2];
  4427. }
  4428. #pragma mark - Jack
  4429. #warning 做SQL操作时转义!!
  4430. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4431. // "val_227" : {
  4432. // "check" : 1,
  4433. // "value" : "US United States",
  4434. // "value_id" : "228"
  4435. // },
  4436. if (!countryCode) {
  4437. countryCode = @"US";
  4438. }
  4439. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4440. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4441. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4442. int code_id = sqlite3_column_int(stmt, 3); // id
  4443. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4444. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4445. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4446. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4447. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4448. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4449. }
  4450. long n = *count;
  4451. *count = n + 1;
  4452. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4453. [container setValue:countryDic forKey:key];
  4454. }] mutableCopy];
  4455. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4456. return ret;
  4457. }
  4458. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4459. countryCode = [self translateSingleQuote:countryCode];
  4460. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4461. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4462. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4463. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4464. if (name == NULL) {
  4465. name = "";
  4466. }
  4467. if (code == NULL) {
  4468. code = "";
  4469. }
  4470. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4471. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4472. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4473. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4474. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4475. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4476. }
  4477. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4478. [container setValue:stateDic forKey:key];
  4479. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4480. DebugLog(@"query all state error: %@",err_msg);
  4481. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4482. // [stateDic setValue:@"Other" forKey:@"value"];
  4483. // [stateDic setValue:@"" forKey:@"value_id"];
  4484. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4485. //
  4486. // if (state_code && [@"" isEqualToString:state_code]) {
  4487. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4488. // }
  4489. //
  4490. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4491. // [container setValue:stateDic forKey:key];
  4492. }] mutableCopy];
  4493. [ret removeObjectForKey:@"result"];
  4494. // failure 可以不用了,一样的
  4495. if (ret.allKeys.count == 0) {
  4496. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4497. [stateDic setValue:@"Other" forKey:@"value"];
  4498. [stateDic setValue:@"" forKey:@"value_id"];
  4499. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4500. if (state_code && [@"" isEqualToString:state_code]) {
  4501. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4502. }
  4503. NSString *key = [NSString stringWithFormat:@"val_0"];
  4504. [ret setValue:stateDic forKey:key];
  4505. }
  4506. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4507. return ret;
  4508. }
  4509. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4510. codeId = [self translateSingleQuote:codeId];
  4511. 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];
  4512. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4513. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4514. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4515. if (name == NULL) {
  4516. name = "";
  4517. }
  4518. if (code == NULL) {
  4519. code = "";
  4520. }
  4521. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4522. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4523. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4524. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4525. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4526. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4527. }
  4528. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4529. [container setValue:stateDic forKey:key];
  4530. }] mutableCopy];
  4531. [ret removeObjectForKey:@"result"];
  4532. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4533. return ret;
  4534. }
  4535. + (NSDictionary *)offline_getPrice {
  4536. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4537. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4538. char *name = (char *) sqlite3_column_text(stmt, 1);
  4539. int type = sqlite3_column_int(stmt, 2);
  4540. int orderBy = sqlite3_column_int(stmt, 3);
  4541. if (name == NULL) {
  4542. name = "";
  4543. }
  4544. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4545. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4546. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4547. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4548. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4549. if (orderBy == 0) {
  4550. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4551. }
  4552. [container setValue:priceDic forKey:key];
  4553. }] mutableCopy];
  4554. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4555. return ret;
  4556. }
  4557. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4558. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4559. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4560. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4561. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4562. // int _id = sqlite3_column_int(stmt, 0);
  4563. NSString *name = [self textAtColumn:1 statement:stmt];
  4564. NSDictionary *typeDic = @{
  4565. @"value_id" : name,
  4566. @"value" : name,
  4567. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4568. };
  4569. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4570. [ret setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  4571. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4572. }];
  4573. return ret;
  4574. }
  4575. + (NSDictionary *)offline_getSalesRep:(NSString*) user{
  4576. // 首先从offline_login表中取出sales_code
  4577. // AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4578. // NSString *user = app.user;
  4579. user = [self translateSingleQuote:user];
  4580. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4581. __block NSString *user_code = @"";
  4582. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4583. char *code = (char *)sqlite3_column_text(stmt, 0);
  4584. if (code == NULL) {
  4585. code = "";
  4586. }
  4587. user_code = [NSString stringWithUTF8String:code];
  4588. }];
  4589. // 再取所有salesRep
  4590. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4591. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4592. // 1 name 2 code 3 salesrep_id
  4593. char *name = (char *)sqlite3_column_text(stmt, 1);
  4594. char *code = (char *)sqlite3_column_text(stmt, 2);
  4595. int salesrep_id = sqlite3_column_int(stmt, 3);
  4596. if (name == NULL) {
  4597. name = "";
  4598. }
  4599. if (code == NULL) {
  4600. code = "";
  4601. }
  4602. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4603. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4604. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4605. // 比较code 相等则check
  4606. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4607. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4608. }
  4609. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4610. }] mutableCopy];
  4611. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  4612. return ret;
  4613. }
  4614. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4615. zipcode = [self translateSingleQuote:zipcode];
  4616. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4617. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4618. char *country = (char *)sqlite3_column_text(stmt, 0);
  4619. char *state = (char *)sqlite3_column_text(stmt, 1);
  4620. char *city = (char *)sqlite3_column_text(stmt, 2);
  4621. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4622. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4623. if (country == NULL) {
  4624. country = "";
  4625. }
  4626. if (state == NULL) {
  4627. state = "";
  4628. }
  4629. if (city == NULL) {
  4630. city = "";
  4631. }
  4632. if (country_code == NULL) {
  4633. country_code = "";
  4634. }
  4635. if (state_code == NULL) {
  4636. state_code = "";
  4637. }
  4638. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4639. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4640. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4641. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4642. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4643. }] mutableCopy];
  4644. return ret;
  4645. }
  4646. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4647. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4648. [item setValue:value forKey:valueKey];
  4649. [dic setValue:item forKey:itemKey];
  4650. }
  4651. + (NSString *)countryCodeByid:(NSString *)code_id {
  4652. NSString *ret = nil;
  4653. code_id = [self translateSingleQuote:code_id];
  4654. sqlite3 *db = [iSalesDB get_db];
  4655. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4656. sqlite3_stmt * statement;
  4657. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4658. while (sqlite3_step(statement) == SQLITE_ROW) {
  4659. char *code = (char *)sqlite3_column_text(statement, 0);
  4660. if (code == NULL) {
  4661. code = "";
  4662. }
  4663. ret = [NSString stringWithUTF8String:code];
  4664. }
  4665. sqlite3_finalize(statement);
  4666. }
  4667. [iSalesDB close_db:db];
  4668. return ret;
  4669. }
  4670. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4671. NSString *ret = nil;
  4672. code = [self translateSingleQuote:code];
  4673. sqlite3 *db = [iSalesDB get_db];
  4674. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4675. sqlite3_stmt * statement;
  4676. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4677. while (sqlite3_step(statement) == SQLITE_ROW) {
  4678. char *_id = (char *)sqlite3_column_text(statement, 0);
  4679. if (_id == NULL) {
  4680. _id = "";
  4681. }
  4682. ret = [NSString stringWithFormat:@"%s",_id];
  4683. }
  4684. sqlite3_finalize(statement);
  4685. }
  4686. [iSalesDB close_db:db];
  4687. return ret;
  4688. }
  4689. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4690. NSString *name = nil;
  4691. codeId = [self translateSingleQuote:codeId];
  4692. sqlite3 *db = [iSalesDB get_db];
  4693. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4694. sqlite3_stmt * statement;
  4695. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4696. while (sqlite3_step(statement) == SQLITE_ROW) {
  4697. char *value = (char *)sqlite3_column_text(statement, 0);
  4698. if (value == NULL) {
  4699. value = "";
  4700. }
  4701. name = [NSString stringWithUTF8String:value];
  4702. }
  4703. sqlite3_finalize(statement);
  4704. }
  4705. [iSalesDB close_db:db];
  4706. return name;
  4707. }
  4708. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4709. NSString *ret = nil;
  4710. sqlite3 *db = [iSalesDB get_db];
  4711. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4712. sqlite3_stmt * statement;
  4713. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4714. while (sqlite3_step(statement) == SQLITE_ROW) {
  4715. char *name = (char *)sqlite3_column_text(statement, 0);
  4716. if (name == NULL) {
  4717. name = "";
  4718. }
  4719. ret = [NSString stringWithUTF8String:name];
  4720. }
  4721. sqlite3_finalize(statement);
  4722. }
  4723. [iSalesDB close_db:db];
  4724. return ret;
  4725. }
  4726. + (NSString *)salesRepCodeById:(NSString *)_id {
  4727. NSString *ret = nil;
  4728. _id = [self translateSingleQuote:_id];
  4729. sqlite3 *db = [iSalesDB get_db];
  4730. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4731. sqlite3_stmt * statement;
  4732. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4733. while (sqlite3_step(statement) == SQLITE_ROW) {
  4734. char *rep = (char *)sqlite3_column_text(statement, 0);
  4735. if (rep == NULL) {
  4736. rep = "";
  4737. }
  4738. ret = [NSString stringWithUTF8String:rep];
  4739. }
  4740. sqlite3_finalize(statement);
  4741. }
  4742. [iSalesDB close_db:db];
  4743. return ret;
  4744. }
  4745. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4746. char *tx = (char *)sqlite3_column_text(stmt, col);
  4747. if (tx == NULL) {
  4748. tx = "";
  4749. }
  4750. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4751. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4752. if (!text) {
  4753. text = @"";
  4754. }
  4755. // 将字符全部为' '的字符串干掉
  4756. int spaceCount = 0;
  4757. for (int i = 0; i < text.length; i++) {
  4758. if ([text characterAtIndex:i] == ' ') {
  4759. spaceCount++;
  4760. }
  4761. }
  4762. if (spaceCount == text.length) {
  4763. text = @"";
  4764. }
  4765. return text;
  4766. }
  4767. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4768. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4769. NSData *data = [NSData dataWithContentsOfFile:path];
  4770. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4771. return ret;
  4772. }
  4773. + (NSString *)textFileName:(NSString *)name {
  4774. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4775. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4776. if (!text) {
  4777. text = @"";
  4778. }
  4779. return text;
  4780. }
  4781. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4782. return [[dic objectForKey:key] mutableCopy];
  4783. }
  4784. + (id)translateSingleQuote:(NSString *)string {
  4785. if ([string isKindOfClass:[NSString class]])
  4786. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4787. return string;
  4788. }
  4789. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4790. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4791. }
  4792. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4793. NSString* ret= nil;
  4794. NSString *sqlQuery = nil;
  4795. // 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
  4796. 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];
  4797. sqlite3_stmt * statement;
  4798. // int count=0;
  4799. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4800. {
  4801. if (sqlite3_step(statement) == SQLITE_ROW)
  4802. {
  4803. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4804. if(imgurl==nil)
  4805. imgurl="";
  4806. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4807. ret=nsimgurl;
  4808. }
  4809. sqlite3_finalize(statement);
  4810. }
  4811. else
  4812. {
  4813. [ret setValue:@"8" forKey:@"result"];
  4814. }
  4815. // [iSalesDB close_db:db];
  4816. // DebugLog(@"data string: %@",ret );
  4817. return ret;
  4818. }
  4819. #pragma mark contact Advanced search
  4820. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4821. {
  4822. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4823. return [RAConvertor dict2data:contactAdvanceDic];
  4824. }
  4825. #pragma mark create new contact
  4826. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4827. assert(params[@"user"]!=nil);
  4828. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4829. NSData *data = [NSData dataWithContentsOfFile:path];
  4830. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4831. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4832. NSString *countryCode = nil;
  4833. NSString *countryCode_id = nil;
  4834. NSString *stateCode = nil;
  4835. NSString *city = nil;
  4836. NSString *zipCode = nil;
  4837. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4838. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4839. NSString *code_id = params[@"country"];
  4840. countryCode_id = code_id;
  4841. countryCode = [self countryCodeByid:code_id];
  4842. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4843. NSString *zip_code = params[@"zipcode"];
  4844. // 剔除全部为空格
  4845. int spaceCount = 0;
  4846. for (int i = 0; i < zip_code.length; i++) {
  4847. if ([zip_code characterAtIndex:i] == ' ') {
  4848. spaceCount++;
  4849. }
  4850. }
  4851. if (spaceCount == zip_code.length) {
  4852. zip_code = @"";
  4853. }
  4854. zipCode = zip_code;
  4855. if (zipCode.length > 0) {
  4856. countryCode_id = params[@"country"];
  4857. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4858. countryCode = [dic valueForKey:@"country_code"];
  4859. if (!countryCode) {
  4860. // countryCode = @"US";
  4861. NSString *code_id = params[@"country"];
  4862. countryCode = [self countryCodeByid:code_id];
  4863. }
  4864. stateCode = [dic valueForKey:@"state_code"];
  4865. if (!stateCode.length) {
  4866. stateCode = params[@"state"];
  4867. }
  4868. city = [dic valueForKey:@"city"];
  4869. if (!city.length) {
  4870. city = params[@"city"];
  4871. }
  4872. // zip code
  4873. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4874. [zipDic setValue:zipCode forKey:@"value"];
  4875. [section_0 setValue:zipDic forKey:@"item_11"];
  4876. } else {
  4877. NSString *code_id = params[@"country"];
  4878. countryCode = [self countryCodeByid:code_id];
  4879. stateCode = params[@"state"];
  4880. city = params[@"city"];
  4881. }
  4882. }
  4883. } else {
  4884. // default: US United States
  4885. countryCode = @"US";
  4886. countryCode_id = @"228";
  4887. }
  4888. // country
  4889. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4890. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4891. // state
  4892. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4893. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4894. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4895. // city
  4896. if (city) {
  4897. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4898. [cityDic setValue:city forKey:@"value"];
  4899. [section_0 setValue:cityDic forKey:@"item_13"];
  4900. }
  4901. // price type
  4902. NSDictionary *priceDic = [self offline_getPrice];
  4903. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4904. // contact type
  4905. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4906. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4907. // Sales Rep
  4908. NSDictionary *repDic = [self offline_getSalesRep:params[@"user"]];
  4909. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4910. [ret setValue:section_0 forKey:@"section_0"];
  4911. return [RAConvertor dict2data:ret];
  4912. }
  4913. #pragma mark save
  4914. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4915. NSString *addr = nil;
  4916. NSString *contact_name = nil;
  4917. NSString *companyName = [params objectForKey:@"company"];
  4918. if (companyName) {
  4919. companyName = [AESCrypt fastencrypt:companyName];
  4920. } else {
  4921. companyName = @"";
  4922. }
  4923. DebugLog(@"company");
  4924. companyName = [self translateSingleQuote:companyName];
  4925. NSString *addr1 = [params objectForKey:@"address"];
  4926. NSString *addr2 = [params objectForKey:@"address2"];
  4927. NSString *addr3 = [params objectForKey:@"address_3"];
  4928. NSString *addr4 = [params objectForKey:@"address_4"];
  4929. if (!addr2) {
  4930. addr2 = @"";
  4931. }
  4932. if (!addr3) {
  4933. addr3 = @"";
  4934. }
  4935. if (!addr4) {
  4936. addr4 = @"";
  4937. }
  4938. if (!addr1) {
  4939. addr1 = @"";
  4940. }
  4941. DebugLog(@"addr");
  4942. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4943. addr = [AESCrypt fastencrypt:addr];
  4944. addr = [self translateSingleQuote:addr];
  4945. if (addr1 && ![addr1 isEqualToString:@""]) {
  4946. addr1 = [AESCrypt fastencrypt:addr1];
  4947. }
  4948. addr1 = [self translateSingleQuote:addr1];
  4949. addr2 = [self translateSingleQuote:addr2];
  4950. addr3 = [self translateSingleQuote:addr3];
  4951. addr4 = [self translateSingleQuote:addr4];
  4952. NSString *country = [params objectForKey:@"country"];
  4953. if (country) {
  4954. country = [self countryNameByCountryCodeId:country];
  4955. } else {
  4956. country = @"";
  4957. }
  4958. DebugLog(@"country");
  4959. country = [self translateSingleQuote:country];
  4960. NSString *state = [params objectForKey:@"state"];
  4961. if (!state) {
  4962. state = @"";
  4963. }
  4964. DebugLog(@"state");
  4965. state = [self translateSingleQuote:state];
  4966. NSString *city = [params objectForKey:@"city"];
  4967. if (!city) {
  4968. city = @"";
  4969. }
  4970. city = [self translateSingleQuote:city];
  4971. NSString *zipcode = [params objectForKey:@"zipcode"];
  4972. if (!zipcode) {
  4973. zipcode = @"";
  4974. }
  4975. DebugLog(@"zip");
  4976. zipcode = [self translateSingleQuote:zipcode];
  4977. NSString *fistName = [params objectForKey:@"firstname"];
  4978. if (!fistName) {
  4979. fistName = @"";
  4980. }
  4981. NSString *lastName = [params objectForKey:@"lastname"];
  4982. if (!lastName) {
  4983. lastName = @"";
  4984. }
  4985. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  4986. DebugLog(@"contact_name");
  4987. contact_name = [self translateSingleQuote:contact_name];
  4988. fistName = [self translateSingleQuote:fistName];
  4989. lastName = [self translateSingleQuote:lastName];
  4990. NSString *phone = [params objectForKey:@"phone"];
  4991. if (phone) {
  4992. phone = [AESCrypt fastencrypt:phone];
  4993. } else {
  4994. phone = @"";
  4995. }
  4996. DebugLog(@"PHONE");
  4997. phone = [self translateSingleQuote:phone];
  4998. NSString *fax = [params objectForKey:@"fax"];
  4999. if (!fax) {
  5000. fax = @"";
  5001. }
  5002. DebugLog(@"FAX");
  5003. fax = [self translateSingleQuote:fax];
  5004. NSString *email = [params objectForKey:@"email"];
  5005. if (!email) {
  5006. email = @"";
  5007. }
  5008. DebugLog(@"EMAIL:%@",email);
  5009. email = [self translateSingleQuote:email];
  5010. NSString *notes = [params objectForKey:@"contact_notes"];
  5011. if (!notes) {
  5012. notes = @"";
  5013. }
  5014. DebugLog(@"NOTE:%@",notes);
  5015. notes = [self translateSingleQuote:notes];
  5016. NSString *price = [params objectForKey:@"price_name"];
  5017. if (price) {
  5018. price = [self priceNameByPriceId:price];
  5019. } else {
  5020. price = @"";
  5021. }
  5022. DebugLog(@"PRICE");
  5023. price = [self translateSingleQuote:price];
  5024. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5025. if (salesRep) {
  5026. salesRep = [self salesRepCodeById:salesRep];
  5027. } else {
  5028. salesRep = @"";
  5029. }
  5030. salesRep = [self translateSingleQuote:salesRep];
  5031. NSString *img = [params objectForKey:@"business_card"];
  5032. NSArray *array = [img componentsSeparatedByString:@","];
  5033. NSString *img_0 = array[0];
  5034. if (!img_0) {
  5035. img_0 = @"";
  5036. }
  5037. img_0 = [self translateSingleQuote:img_0];
  5038. NSString *img_1 = array[1];
  5039. if (!img_1) {
  5040. img_1 = @"";
  5041. }
  5042. img_1 = [self translateSingleQuote:img_1];
  5043. NSString *img_2 = array[2];
  5044. if (!img_2) {
  5045. img_2 = @"";
  5046. }
  5047. img_2 = [self translateSingleQuote:img_2];
  5048. NSString *contact_id = [NSUUID UUID].UUIDString;
  5049. NSString *contact_type = [params objectForKey:@"type_name"];
  5050. if (!contact_type) {
  5051. contact_type = @"";
  5052. }
  5053. contact_type = [self translateSingleQuote:contact_type];
  5054. // 判断更新时是否为customer
  5055. if (update) {
  5056. contact_id = [params objectForKey:@"contact_id"];
  5057. if (!contact_id) {
  5058. contact_id = @"";
  5059. }
  5060. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5061. __block int customer = 0;
  5062. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5063. customer = sqlite3_column_int(stmt, 0);
  5064. }];
  5065. isCustomer = customer ? YES : NO;
  5066. }
  5067. NSMutableDictionary *sync_dic = [params mutableCopy];
  5068. if (isCustomer) {
  5069. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5070. } else {
  5071. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5072. }
  5073. NSString *sync_data = nil;
  5074. NSString *sql = nil;
  5075. if (update){
  5076. contact_id = [params objectForKey:@"contact_id"];
  5077. if (!contact_id) {
  5078. contact_id = @"";
  5079. }
  5080. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5081. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5082. sync_data = [RAConvertor dict2string:sync_dic];
  5083. sync_data = [self translateSingleQuote:sync_data];
  5084. 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];
  5085. } else {
  5086. contact_id = [self translateSingleQuote:contact_id];
  5087. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5088. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5089. sync_data = [RAConvertor dict2string:sync_dic];
  5090. sync_data = [self translateSingleQuote:sync_data];
  5091. 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];
  5092. }
  5093. int result = [iSalesDB execSql:sql];
  5094. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5095. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5096. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5097. }
  5098. #pragma mark save new contact
  5099. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5100. {
  5101. return [self offline_saveContact:params update:NO isCustomer:YES];
  5102. }
  5103. #pragma mark edit contact
  5104. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5105. {
  5106. assert(params[@"user"]!=nil);
  5107. // {
  5108. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5109. // password = 123456;
  5110. // user = EvanK;
  5111. // }
  5112. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5113. NSData *data = [NSData dataWithContentsOfFile:path];
  5114. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5115. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5116. NSString *countryCode = nil;
  5117. NSString *countryCode_id = nil;
  5118. NSString *stateCode = nil;
  5119. /*------contact infor------*/
  5120. __block NSString *country = nil;
  5121. __block NSString *company_name = nil;
  5122. __block NSString *contact_id = params[@"contact_id"];
  5123. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5124. __block NSString *zipcode = nil;
  5125. __block NSString *state = nil; // state_code
  5126. __block NSString *city = nil; //
  5127. __block NSString *firt_name,*last_name;
  5128. __block NSString *phone,*fax,*email;
  5129. __block NSString *notes,*price_type,*sales_rep;
  5130. __block NSString *img_0,*img_1,*img_2;
  5131. __block NSString *contact_type;
  5132. contact_id = [self translateSingleQuote:contact_id];
  5133. 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];
  5134. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5135. country = [self textAtColumn:0 statement:stmt]; // country name
  5136. company_name = [self textAtColumn:1 statement:stmt];
  5137. addr_1 = [self textAtColumn:2 statement:stmt];
  5138. addr_2 = [self textAtColumn:3 statement:stmt];
  5139. addr_3 = [self textAtColumn:4 statement:stmt];
  5140. addr_4 = [self textAtColumn:5 statement:stmt];
  5141. zipcode = [self textAtColumn:6 statement:stmt];
  5142. state = [self textAtColumn:7 statement:stmt]; // state code
  5143. city = [self textAtColumn:8 statement:stmt];
  5144. firt_name = [self textAtColumn:9 statement:stmt];
  5145. last_name = [self textAtColumn:10 statement:stmt];
  5146. phone = [self textAtColumn:11 statement:stmt];
  5147. fax = [self textAtColumn:12 statement:stmt];
  5148. email = [self textAtColumn:13 statement:stmt];
  5149. notes = [self textAtColumn:14 statement:stmt];
  5150. price_type = [self textAtColumn:15 statement:stmt]; // name
  5151. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5152. img_0 = [self textAtColumn:17 statement:stmt];
  5153. img_1 = [self textAtColumn:18 statement:stmt];
  5154. img_2 = [self textAtColumn:19 statement:stmt];
  5155. contact_type = [self textAtColumn:20 statement:stmt];
  5156. }];
  5157. // decrypt
  5158. if (company_name) {
  5159. company_name = [AESCrypt fastdecrypt:company_name];
  5160. }
  5161. if (addr_1) {
  5162. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5163. }
  5164. if (phone) {
  5165. phone = [AESCrypt fastdecrypt:phone];
  5166. }
  5167. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5168. countryCode = [iSalesDB jk_queryText:countrySql];
  5169. stateCode = state;
  5170. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5171. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5172. NSString *code_id = params[@"country"];
  5173. countryCode_id = code_id;
  5174. countryCode = [self countryCodeByid:code_id];
  5175. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5176. NSString *zip_code = params[@"zipcode"];
  5177. // 剔除全部为空格
  5178. int spaceCount = 0;
  5179. for (int i = 0; i < zip_code.length; i++) {
  5180. if ([zip_code characterAtIndex:i] == ' ') {
  5181. spaceCount++;
  5182. }
  5183. }
  5184. if (spaceCount == zip_code.length) {
  5185. zip_code = @"";
  5186. }
  5187. if (zipcode.length > 0) {
  5188. zipcode = zip_code;
  5189. countryCode_id = params[@"country"];
  5190. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5191. countryCode = [dic valueForKey:@"country_code"];
  5192. if (!countryCode) {
  5193. NSString *code_id = params[@"country"];
  5194. countryCode = [self countryCodeByid:code_id];
  5195. }
  5196. stateCode = [dic valueForKey:@"state_code"];
  5197. if (!stateCode.length) {
  5198. stateCode = params[@"state"];
  5199. }
  5200. city = [dic valueForKey:@"city"];
  5201. if (!city.length) {
  5202. city = params[@"city"];
  5203. }
  5204. // zip code
  5205. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5206. [zipDic setValue:zipcode forKey:@"value"];
  5207. [section_0 setValue:zipDic forKey:@"item_8"];
  5208. } else {
  5209. NSString *code_id = params[@"country"];
  5210. countryCode = [self countryCodeByid:code_id];
  5211. stateCode = params[@"state"];
  5212. city = params[@"city"];
  5213. }
  5214. }
  5215. }
  5216. // 0 Country
  5217. // 1 Company Name
  5218. // 2 Contact ID
  5219. // 3 Picture
  5220. // 4 Address 1
  5221. // 5 Address 2
  5222. // 6 Address 3
  5223. // 7 Address 4
  5224. // 8 Zip Code
  5225. // 9 State/Province
  5226. // 10 City
  5227. // 11 Contact First Name
  5228. // 12 Contact Last Name
  5229. // 13 Phone
  5230. // 14 Fax
  5231. // 15 Email
  5232. // 16 Contact Notes
  5233. // 17 Price Type
  5234. // 18 Contact Type
  5235. // 19 Sales Rep
  5236. // country
  5237. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5238. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5239. // company
  5240. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5241. // contact_id
  5242. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5243. // picture
  5244. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5245. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5246. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5247. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5248. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5249. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5250. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5251. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5252. // addr 1 2 3 4
  5253. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5254. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5255. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5256. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5257. // zip code
  5258. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5259. // state
  5260. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5261. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5262. // city
  5263. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5264. // first last
  5265. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5266. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5267. // phone fax email
  5268. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5269. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5270. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5271. // notes
  5272. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5273. // price
  5274. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5275. for (NSString *key in priceDic.allKeys) {
  5276. if ([key containsString:@"val_"]) {
  5277. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5278. if ([dic[@"value"] isEqualToString:price_type]) {
  5279. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5280. [priceDic setValue:dic forKey:key];
  5281. }
  5282. }
  5283. }
  5284. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5285. // Contact Rep
  5286. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5287. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5288. // Sales Rep
  5289. NSMutableDictionary *repDic = [[self offline_getSalesRep:params[@"user"]] mutableCopy];
  5290. for (NSString *key in repDic.allKeys) {
  5291. if ([key containsString:@"val_"]) {
  5292. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5293. NSString *value = dic[@"value"];
  5294. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5295. if (code && [code isEqualToString:sales_rep]) {
  5296. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5297. [repDic setValue:dic forKey:key];
  5298. }
  5299. }
  5300. }
  5301. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5302. [ret setValue:section_0 forKey:@"section_0"];
  5303. return [RAConvertor dict2data:ret];
  5304. }
  5305. #pragma mark save contact
  5306. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5307. {
  5308. return [self offline_saveContact:params update:YES isCustomer:YES];
  5309. }
  5310. #pragma mark category
  5311. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5312. if (ck) {
  5313. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5314. for (NSString *key in res.allKeys) {
  5315. if (![key isEqualToString:@"count"]) {
  5316. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5317. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5318. if ([val[@"value"] isEqualToString:ck]) {
  5319. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5320. }
  5321. [res setValue:val forKey:key];
  5322. }
  5323. }
  5324. [dic setValue:res forKey:valueKey];
  5325. }
  5326. }
  5327. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5328. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5329. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5330. NSString* category = [params valueForKey:@"category"];
  5331. if (!category || [category isEqualToString:@""]) {
  5332. category = @"%";
  5333. }
  5334. category = [self translateSingleQuote:category];
  5335. int limit = [[params valueForKey:@"limit"] intValue];
  5336. int offset = [[params valueForKey:@"offset"] intValue];
  5337. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5338. NSString *limit_str = @"";
  5339. if (limited) {
  5340. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5341. }
  5342. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5343. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5344. sqlite3 *db = [iSalesDB get_db];
  5345. // [iSalesDB AddExFunction:db];
  5346. int count;
  5347. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5348. 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];
  5349. double price_min = 0;
  5350. double price_max = 0;
  5351. if ([params.allKeys containsObject:@"alert"]) {
  5352. // alert
  5353. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5354. NSString *alert = params[@"alert"];
  5355. if ([alert isEqualToString:@"Display All"]) {
  5356. alert = [NSString stringWithFormat:@""];
  5357. } else {
  5358. alert = [self translateSingleQuote:alert];
  5359. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5360. }
  5361. // available
  5362. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5363. NSString *available = params[@"available"];
  5364. NSString *available_condition;
  5365. if ([available isEqualToString:@"Display All"]) {
  5366. available_condition = @"";
  5367. } else if ([available isEqualToString:@"Available Now"]) {
  5368. available_condition = @"and availability > 0";
  5369. } else {
  5370. available_condition = @"and availability == 0";
  5371. }
  5372. // best seller
  5373. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5374. NSString *best_seller = @"";
  5375. NSString *order_best_seller = @"m.name asc";
  5376. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5377. best_seller = @"and best_seller > 0";
  5378. order_best_seller = @"m.best_seller desc,m.name asc";
  5379. }
  5380. // price
  5381. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5382. NSString *price = params[@"price"];
  5383. price_min = 0;
  5384. price_max = MAXFLOAT;
  5385. if (params[@"user"] && price != nil) {
  5386. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5387. NSMutableString *priceName = [NSMutableString string];
  5388. for (int i = 0; i < priceTypeArray.count; i++) {
  5389. NSString *pricetype = priceTypeArray[i];
  5390. pricetype = [self translateSingleQuote:pricetype];
  5391. if (i == 0) {
  5392. [priceName appendFormat:@"'%@'",pricetype];
  5393. } else {
  5394. [priceName appendFormat:@",'%@'",pricetype];
  5395. }
  5396. }
  5397. if ([price isEqualToString:@"Display All"]) {
  5398. price = [NSString stringWithFormat:@""];
  5399. } else if([price containsString:@"+"]){
  5400. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5401. price_min = [price doubleValue];
  5402. 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];
  5403. } else {
  5404. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5405. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5406. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5407. 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];
  5408. }
  5409. } else {
  5410. price = @"";
  5411. }
  5412. // sold_by_qty : Sold in quantities of %@
  5413. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5414. NSString *qty = params[@"sold_by_qty"];
  5415. if ([qty isEqualToString:@"Display All"]) {
  5416. qty = @"";
  5417. } else {
  5418. qty = [self translateSingleQuote:qty];
  5419. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5420. }
  5421. // model name;
  5422. NSString* modelname =params[@"modelName"];
  5423. if(modelname.length==0)
  5424. {
  5425. modelname=@"";
  5426. }
  5427. else
  5428. {
  5429. modelname = modelname.lowercaseString;
  5430. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5431. }
  5432. //modelDescription
  5433. NSString* modelDescription =params[@"modelDescription"];
  5434. if(modelDescription.length==0)
  5435. {
  5436. modelDescription=@"";
  5437. }
  5438. else
  5439. {
  5440. modelDescription = modelDescription.lowercaseString;
  5441. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5442. }
  5443. // cate
  5444. // category = [self translateSingleQuote:category];
  5445. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5446. // cate mutiple selection
  5447. NSString *category_id = params[@"category"];
  5448. NSMutableArray *cate_id_array = nil;
  5449. NSMutableString *cateWhere = [NSMutableString string];
  5450. if ([category_id isEqualToString:@""] || !category_id) {
  5451. [cateWhere appendString:@"1 = 1"];
  5452. } else {
  5453. if ([category_id containsString:@","]) {
  5454. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5455. } else {
  5456. cate_id_array = [@[category_id] mutableCopy];
  5457. }
  5458. [cateWhere appendString:@"("];
  5459. for (int i = 0; i < cate_id_array.count; i++) {
  5460. if (i == 0) {
  5461. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5462. } else {
  5463. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5464. }
  5465. }
  5466. [cateWhere appendString:@")"];
  5467. }
  5468. // where bestseller > 0 order by bestseller desc
  5469. // sql query: alert availability(int) best_seller(int) price qty
  5470. 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];
  5471. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5472. }
  5473. DebugLog(@"offline category where: %@",where);
  5474. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5475. if (!params[@"user"]) {
  5476. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5477. }
  5478. [ret setValue:filter forKey:@"filter"];
  5479. DebugLog(@"offline_category sql:%@",sqlQuery);
  5480. sqlite3_stmt * statement;
  5481. [ret setValue:@"2" forKey:@"result"];
  5482. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5483. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5484. // int count=0;
  5485. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5486. {
  5487. int i=0;
  5488. while (sqlite3_step(statement) == SQLITE_ROW)
  5489. {
  5490. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5491. char *name = (char*)sqlite3_column_text(statement, 0);
  5492. if(name==nil)
  5493. name="";
  5494. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5495. char *description = (char*)sqlite3_column_text(statement, 1);
  5496. if(description==nil)
  5497. description="";
  5498. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5499. int product_id = sqlite3_column_int(statement, 2);
  5500. int wid = sqlite3_column_int(statement, 3);
  5501. int closeout = sqlite3_column_int(statement, 4);
  5502. int cid = sqlite3_column_int(statement, 5);
  5503. int wisdelete = sqlite3_column_int(statement, 6);
  5504. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5505. int more_color = sqlite3_column_int(statement, 8);
  5506. // Defaul Category ID
  5507. __block NSString *categoryID = nil;
  5508. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5509. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5510. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5511. if(default_category==nil)
  5512. default_category="";
  5513. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5514. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5515. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5516. categoryID = nsdefault_category;
  5517. }];
  5518. if (!categoryID.length) {
  5519. NSString *cateIDs = params[@"category"];
  5520. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5521. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5522. for (NSString *cateID in requestCategoryArr) {
  5523. BOOL needBreak = NO;
  5524. for (NSString *itemCateIDBox in itemCategoryArr) {
  5525. if (itemCateIDBox.length > 4) {
  5526. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5527. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5528. if ([itemCategoryID isEqualToString:cateID]) {
  5529. needBreak = YES;
  5530. categoryID = itemCategoryID;
  5531. break;
  5532. }
  5533. }
  5534. }
  5535. if (needBreak) {
  5536. break;
  5537. }
  5538. }
  5539. }
  5540. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5541. if(wid !=0 && wisdelete != 1)
  5542. [item setValue:@"true" forKey:@"wish_exists"];
  5543. else
  5544. [item setValue:@"false" forKey:@"wish_exists"];
  5545. if(closeout==0)
  5546. [item setValue:@"false" forKey:@"is_closeout"];
  5547. else
  5548. [item setValue:@"true" forKey:@"is_closeout"];
  5549. if(cid==0)
  5550. [item setValue:@"false" forKey:@"cart_exists"];
  5551. else
  5552. [item setValue:@"true" forKey:@"cart_exists"];
  5553. if (more_color == 0) {
  5554. [item setObject:@(false) forKey:@"more_color"];
  5555. } else if (more_color == 1) {
  5556. [item setObject:@(true) forKey:@"more_color"];
  5557. }
  5558. [item addEntriesFromDictionary:imgjson];
  5559. // [item setValue:nsurl forKey:@"img"];
  5560. [item setValue:nsname forKey:@"name"];
  5561. [item setValue:nsdescription forKey:@"description"];
  5562. if (categoryID) {
  5563. [item setValue:categoryID forKey:@"item_category_id"];
  5564. }
  5565. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5566. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5567. i++;
  5568. }
  5569. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5570. [ret setObject:items forKey:@"items"];
  5571. sqlite3_finalize(statement);
  5572. } else {
  5573. DebugLog(@"nothing...");
  5574. }
  5575. DebugLog(@"count:%d",count);
  5576. [iSalesDB close_db:db];
  5577. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5578. return ret;
  5579. }
  5580. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5581. {
  5582. return [self categoryList:params limited:YES];
  5583. }
  5584. # pragma mark item search
  5585. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5586. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5587. // assert(params[@"order_code"]);
  5588. // params[@"user"]
  5589. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5590. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5591. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5592. // category
  5593. NSDictionary *category_menu = [self offline_category_menu];
  5594. [filter setValue:category_menu forKey:@"category"];
  5595. NSString* where= nil;
  5596. NSString* orderby= @"m.name";
  5597. if (!filterSearch) {
  5598. int covertype = [[params valueForKey:@"covertype"] intValue];
  5599. switch (covertype) {
  5600. case 0:
  5601. {
  5602. where=@"m.category like'%%#005#%%'";
  5603. break;
  5604. }
  5605. case 1:
  5606. {
  5607. where=@"m.alert like '%QS%'";
  5608. break;
  5609. }
  5610. case 2:
  5611. {
  5612. where=@"m.availability>0";
  5613. break;
  5614. }
  5615. case 3:
  5616. {
  5617. where=@"m.best_seller>0";
  5618. orderby=@"m.best_seller desc,m.name asc";
  5619. break;
  5620. }
  5621. default:
  5622. where=@"1=1";
  5623. break;
  5624. }
  5625. }
  5626. int limit = [[params valueForKey:@"limit"] intValue];
  5627. int offset = [[params valueForKey:@"offset"] intValue];
  5628. NSString *limit_str = @"";
  5629. if (limited) {
  5630. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5631. }
  5632. sqlite3 *db = [iSalesDB get_db];
  5633. // [iSalesDB AddExFunction:db];
  5634. int count;
  5635. NSString *sqlQuery = nil;
  5636. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5637. 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];
  5638. double price_min = 0;
  5639. double price_max = 0;
  5640. if (filterSearch) {
  5641. // alert
  5642. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5643. NSString *alert = params[@"alert"];
  5644. if ([alert isEqualToString:@"Display All"]) {
  5645. alert = [NSString stringWithFormat:@""];
  5646. } else {
  5647. alert = [self translateSingleQuote:alert];
  5648. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5649. }
  5650. // available
  5651. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5652. NSString *available = params[@"available"];
  5653. NSString *available_condition;
  5654. if ([available isEqualToString:@"Display All"]) {
  5655. available_condition = @"";
  5656. } else if ([available isEqualToString:@"Available Now"]) {
  5657. available_condition = @"and availability > 0";
  5658. } else {
  5659. available_condition = @"and availability == 0";
  5660. }
  5661. // best seller
  5662. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5663. NSString *best_seller = @"";
  5664. NSString *order_best_seller = @"m.name asc";
  5665. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5666. best_seller = @"and best_seller > 0";
  5667. order_best_seller = @"m.best_seller desc,m.name asc";
  5668. }
  5669. // price
  5670. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5671. NSString *price = params[@"price"];
  5672. price_min = 0;
  5673. price_max = MAXFLOAT;
  5674. if (params[@"user"]) {
  5675. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:params[@"user"] db:db];
  5676. NSMutableString *priceName = [NSMutableString string];
  5677. for (int i = 0; i < priceTypeArray.count; i++) {
  5678. NSString *pricetype = priceTypeArray[i];
  5679. pricetype = [self translateSingleQuote:pricetype];
  5680. if (i == 0) {
  5681. [priceName appendFormat:@"'%@'",pricetype];
  5682. } else {
  5683. [priceName appendFormat:@",'%@'",pricetype];
  5684. }
  5685. }
  5686. if ([price isEqualToString:@"Display All"]) {
  5687. price = [NSString stringWithFormat:@""];
  5688. } else if([price containsString:@"+"]){
  5689. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5690. price_min = [price doubleValue];
  5691. 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];
  5692. } else {
  5693. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5694. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5695. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5696. 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];
  5697. }
  5698. } else {
  5699. price = @"";
  5700. }
  5701. // sold_by_qty : Sold in quantities of %@
  5702. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5703. NSString *qty = params[@"sold_by_qty"];
  5704. if ([qty isEqualToString:@"Display All"]) {
  5705. qty = @"";
  5706. } else {
  5707. qty = [self translateSingleQuote:qty];
  5708. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5709. }
  5710. // model name;
  5711. NSString* modelname =params[@"modelName"];
  5712. if(modelname.length==0)
  5713. {
  5714. modelname=@"";
  5715. }
  5716. else
  5717. {
  5718. modelname = modelname.lowercaseString;
  5719. modelname=[NSString stringWithFormat:@"and lower(name) like'%%%@%%'",modelname];
  5720. }
  5721. //modelDescription
  5722. NSString* modelDescription =params[@"modelDescription"];
  5723. if(modelDescription.length==0)
  5724. {
  5725. modelDescription=@"";
  5726. }
  5727. else
  5728. {
  5729. modelDescription = modelDescription.lowercaseString;
  5730. modelDescription=[NSString stringWithFormat:@"and lower(description) like'%%%@%%'",modelDescription];
  5731. }
  5732. //lower(description) like'%%%@%%'
  5733. // category
  5734. NSString *category_id = params[@"ctgId"];
  5735. NSMutableArray *cate_id_array = nil;
  5736. NSMutableString *cateWhere = [NSMutableString string];
  5737. if ([category_id isEqualToString:@""] || !category_id) {
  5738. [cateWhere appendString:@"1 = 1"];
  5739. } else {
  5740. if ([category_id containsString:@","]) {
  5741. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5742. } else {
  5743. cate_id_array = [@[category_id] mutableCopy];
  5744. }
  5745. /*-----------*/
  5746. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5747. [cateWhere appendString:@"("];
  5748. for (int i = 0; i < cate_id_array.count; i++) {
  5749. for (NSString *key0 in cateDic.allKeys) {
  5750. if ([key0 containsString:@"category_"]) {
  5751. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5752. for (NSString *key1 in category0.allKeys) {
  5753. if ([key1 containsString:@"category_"]) {
  5754. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5755. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5756. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5757. cate_id_array[i] = [category1 objectForKey:@"id"];
  5758. if (i == 0) {
  5759. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5760. } else {
  5761. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5762. }
  5763. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5764. [category0 setValue:category1 forKey:key1];
  5765. [cateDic setValue:category0 forKey:key0];
  5766. }
  5767. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5768. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5769. cate_id_array[i] = [category0 objectForKey:@"id"];
  5770. if (i == 0) {
  5771. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5772. } else {
  5773. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5774. }
  5775. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5776. [cateDic setValue:category0 forKey:key0];
  5777. }
  5778. }
  5779. }
  5780. }
  5781. }
  5782. }
  5783. [cateWhere appendString:@")"];
  5784. [filter setValue:cateDic forKey:@"category"];
  5785. }
  5786. // where bestseller > 0 order by bestseller desc
  5787. // sql query: alert availability(int) best_seller(int) price qty
  5788. 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];
  5789. // count
  5790. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,modelname,modelDescription,price];
  5791. }
  5792. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5793. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5794. if (!params[@"user"]) {
  5795. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5796. }
  5797. [ret setValue:filter forKey:@"filter"];
  5798. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5799. sqlite3_stmt * statement;
  5800. [ret setValue:@"2" forKey:@"result"];
  5801. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5802. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5803. // int count=0;
  5804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5805. {
  5806. int i=0;
  5807. while (sqlite3_step(statement) == SQLITE_ROW)
  5808. {
  5809. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5810. char *name = (char*)sqlite3_column_text(statement, 0);
  5811. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5812. char *description = (char*)sqlite3_column_text(statement, 1);
  5813. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5814. int product_id = sqlite3_column_int(statement, 2);
  5815. int wid = sqlite3_column_int(statement, 3);
  5816. int closeout = sqlite3_column_int(statement, 4);
  5817. int cid = sqlite3_column_int(statement, 5);
  5818. int wisdelete = sqlite3_column_int(statement, 6);
  5819. int more_color = sqlite3_column_int(statement, 7);
  5820. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5821. if(wid !=0 && wisdelete != 1)
  5822. [item setValue:@"true" forKey:@"wish_exists"];
  5823. else
  5824. [item setValue:@"false" forKey:@"wish_exists"];
  5825. if(closeout==0)
  5826. [item setValue:@"false" forKey:@"is_closeout"];
  5827. else
  5828. [item setValue:@"true" forKey:@"is_closeout"];
  5829. if(cid==0)
  5830. [item setValue:@"false" forKey:@"cart_exists"];
  5831. else
  5832. [item setValue:@"true" forKey:@"cart_exists"];
  5833. if (more_color == 0) {
  5834. [item setObject:@(false) forKey:@"more_color"];
  5835. } else if (more_color == 1) {
  5836. [item setObject:@(true) forKey:@"more_color"];
  5837. }
  5838. [item addEntriesFromDictionary:imgjson];
  5839. // [item setValue:nsurl forKey:@"img"];
  5840. [item setValue:nsname forKey:@"fash_name"];
  5841. [item setValue:nsdescription forKey:@"description"];
  5842. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5843. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5844. i++;
  5845. }
  5846. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5847. [ret setObject:items forKey:@"items"];
  5848. sqlite3_finalize(statement);
  5849. }
  5850. [iSalesDB close_db:db];
  5851. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5852. return ret;
  5853. }
  5854. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5855. {
  5856. return [self itemsearch:params limited:YES];
  5857. }
  5858. #pragma mark order detail
  5859. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5860. if (str1.length == 0) {
  5861. str1 = @"&nbsp";
  5862. }
  5863. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5864. return str;
  5865. }
  5866. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5867. {
  5868. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5869. [fromformatter setDateFormat:from];
  5870. NSDate *date = [fromformatter dateFromString:datetime];
  5871. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5872. [toformatter setDateFormat:to];
  5873. NSString * ret = [toformatter stringFromDate:date];
  5874. return ret;
  5875. }
  5876. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5877. // 把毫秒去掉
  5878. if ([dateTime containsString:@"."]) {
  5879. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5880. }
  5881. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5882. formatter.dateFormat = formate;
  5883. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5884. NSDate *date = [formatter dateFromString:dateTime];
  5885. formatter.dateFormat = newFormate;
  5886. NSString *result = [formatter stringFromDate:date];
  5887. return result ? result : @"";
  5888. }
  5889. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5890. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5891. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5892. }
  5893. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5894. {
  5895. assert(params[@"mode"]!=nil);
  5896. assert(params[@"user"]!=nil);
  5897. DebugLog(@"offline oderdetail params: %@",params);
  5898. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5899. int orderId = [params[@"orderId"] intValue];
  5900. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5901. // decrypt card number and card security code
  5902. // 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 ];
  5903. 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];
  5904. sqlite3 *db = [iSalesDB get_db];
  5905. sqlite3_stmt * statement;
  5906. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5907. NSString *nssoid = @"";
  5908. NSString* orderinfo = @"";
  5909. NSString *moreInfo = @"";
  5910. double handlingFee = 0;
  5911. double payments_and_credist = 0;
  5912. double totalPrice = 0;
  5913. double shippingFee = 0;
  5914. double lift_gate = 0;
  5915. NSString *customer_contact = @"";
  5916. NSString *customer_email = @"";
  5917. NSString *customer_fax = @"";
  5918. NSString *customer_phone = @"";
  5919. NSString *customer_city = @"";
  5920. NSString *customer_state = @"";
  5921. NSString *customer_zipcode = @"";
  5922. NSString *customer_country = @"";
  5923. BOOL must_call = NO;
  5924. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5925. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5926. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5927. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5928. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5929. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5930. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5931. {
  5932. if (sqlite3_step(statement) == SQLITE_ROW)
  5933. {
  5934. // int order_id = sqlite3_column_int(statement, 0);
  5935. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5936. // ret[@"sign_url"] = sign_url;
  5937. section_1 = @{
  5938. @"data":sign_url,
  5939. @"title":@"Signature",
  5940. @"type":@"sign_url"
  5941. }.mutableCopy;
  5942. [ret setObject:section_1 forKey:@"section_1"];
  5943. customer_contact = [self textAtColumn:52 statement:statement];
  5944. customer_email = [self textAtColumn:53 statement:statement];
  5945. customer_phone = [self textAtColumn:54 statement:statement];
  5946. customer_fax = [self textAtColumn:55 statement:statement];
  5947. customer_city = [self textAtColumn:60 statement:statement];
  5948. customer_state = [self textAtColumn:61 statement:statement];
  5949. customer_zipcode = [self textAtColumn:62 statement:statement];
  5950. customer_country = [self textAtColumn:63 statement:statement];
  5951. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  5952. int offline_edit=sqlite3_column_int(statement, 56);
  5953. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5954. char *soid = (char*)sqlite3_column_text(statement, 1);
  5955. if(soid==nil)
  5956. soid= "";
  5957. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5958. // so#
  5959. ret[@"so#"] = nssoid;
  5960. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5961. if(poNumber==nil)
  5962. poNumber= "";
  5963. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5964. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5965. if(create_time==nil)
  5966. create_time= "";
  5967. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5968. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5969. int status = sqlite3_column_int(statement, 4);
  5970. int erpStatus = sqlite3_column_int(statement, 49);
  5971. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5972. // status
  5973. if (status > 1 && status != 3) {
  5974. status = erpStatus;
  5975. } else if (status == 3) {
  5976. status = 15;
  5977. }
  5978. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5979. ret[@"order_status"] = nsstatus;
  5980. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5981. if(company_name==nil)
  5982. company_name= "";
  5983. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5984. // company name
  5985. ret[@"company_name"] = nscompany_name;
  5986. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5987. if(customer_contact==nil)
  5988. customer_contact= "";
  5989. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5990. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5991. if(addr_1==nil)
  5992. addr_1="";
  5993. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5994. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5995. if(addr_2==nil)
  5996. addr_2="";
  5997. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5998. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5999. if(addr_3==nil)
  6000. addr_3="";
  6001. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  6002. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  6003. if(addr_4==nil)
  6004. addr_4="";
  6005. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  6006. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  6007. [arr_addr addObject:nsaddr_1];
  6008. [arr_addr addObject:nsaddr_2];
  6009. [arr_addr addObject:nsaddr_3];
  6010. [arr_addr addObject:nsaddr_4];
  6011. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  6012. [arr_addr addObject:customer_state];
  6013. [arr_addr addObject:customer_zipcode];
  6014. [arr_addr addObject:customer_country];
  6015. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  6016. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  6017. char *logist = (char*)sqlite3_column_text(statement, 11);
  6018. if(logist==nil)
  6019. logist= "";
  6020. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  6021. if (/*status == -11 || */status == 10 || status == 11) {
  6022. nslogist = [self textAtColumn:59 statement:statement];
  6023. };
  6024. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  6025. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  6026. shipping = @"Shipping To Be Quoted";
  6027. } else {
  6028. shippingFee = sqlite3_column_double(statement, 12);
  6029. }
  6030. // Shipping
  6031. // ret[@"Shipping"] = shipping;
  6032. NSDictionary *shipping_item = @{
  6033. @"title":@"Shipping",
  6034. @"value":shipping
  6035. };
  6036. [price_data setObject:shipping_item forKey:@"item_1"];
  6037. int have_lift_gate = sqlite3_column_int(statement, 17);
  6038. lift_gate = sqlite3_column_double(statement, 13);
  6039. // Liftgate Fee(No loading dock)
  6040. if (!have_lift_gate) {
  6041. lift_gate = 0;
  6042. }
  6043. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6044. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6045. if (sqlite3_column_int(statement, 57)) {
  6046. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6047. liftgate_value = @"Shipping To Be Quoted";
  6048. }
  6049. NSDictionary *liftgate_item = @{
  6050. @"title":@"Liftgate Fee(No loading dock)",
  6051. @"value":liftgate_value
  6052. };
  6053. [price_data setObject:liftgate_item forKey:@"item_2"];
  6054. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6055. // [ret removeObjectForKey:@"Shipping"];
  6056. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6057. // }
  6058. //
  6059. // if (have_lift_gate) {
  6060. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6061. // }
  6062. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6063. if(general_notes==nil)
  6064. general_notes= "";
  6065. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6066. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6067. if(internal_notes==nil)
  6068. internal_notes= "";
  6069. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6070. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6071. if(payment_type==nil)
  6072. payment_type= "";
  6073. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6074. // order info
  6075. orderinfo = [self textFileName:@"order_info.html"];
  6076. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6077. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6078. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6079. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6080. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6081. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6082. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6083. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6084. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6085. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6086. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6087. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6088. NSString *payment = nil;
  6089. // id -> show
  6090. __block NSString *show_pay_type = nspayment_type;
  6091. [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) {
  6092. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6093. if (show_typ_ch != NULL) {
  6094. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6095. }
  6096. }];
  6097. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6098. {
  6099. payment = [self textFileName:@"creditcardpayment.html"];
  6100. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6101. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6102. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6103. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6104. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6105. NSString *card_type = [self textAtColumn:42 statement:statement];
  6106. if (card_type.length > 0) { // 显示星号
  6107. card_type = @"****";
  6108. }
  6109. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6110. if (card_number.length > 0 && card_number.length > 4) {
  6111. for (int i = 0; i < card_number.length - 4; i++) {
  6112. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6113. }
  6114. } else {
  6115. card_number = @"";
  6116. }
  6117. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6118. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6119. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6120. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6121. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6122. card_expiration = @"****";
  6123. }
  6124. NSString *card_city = [self textAtColumn:46 statement:statement];
  6125. NSString *card_state = [self textAtColumn:47 statement:statement];
  6126. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6127. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6128. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6129. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6130. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6131. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6132. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6133. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6134. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6135. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6136. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6137. }
  6138. else
  6139. {
  6140. payment=[self textFileName:@"normalpayment.html"];
  6141. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6142. }
  6143. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6144. ret[@"result"]= [NSNumber numberWithInt:2];
  6145. // more info
  6146. moreInfo = [self textFileName:@"more_info.html"];
  6147. /*****ship to******/
  6148. // ShipToCompany_or_&nbsp
  6149. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6150. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6151. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6152. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6153. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6154. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6155. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"]; // 手动输入的可能是\r 或 \n
  6156. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6157. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6158. /*****ship from******/
  6159. // ShipFromCompany_or_&nbsp
  6160. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6161. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6162. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6163. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6164. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6165. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6166. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6167. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6168. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6169. /*****freight to******/
  6170. // FreightBillToCompany_or_&nbsp
  6171. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6172. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6173. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6174. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6175. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6176. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6177. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6178. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6179. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6180. /*****merchandise to******/
  6181. // MerchandiseBillToCompany_or_&nbsp
  6182. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6183. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6184. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6185. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6186. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6187. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6188. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6189. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6190. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6191. /*****return to******/
  6192. // ReturnToCompany_or_&nbsp
  6193. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6194. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6195. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6196. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6197. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6198. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6199. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r" withString:@"<br/>"];
  6200. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"];
  6201. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6202. //
  6203. // DebugLog(@"more info : %@",moreInfo);
  6204. // handling fee
  6205. handlingFee = sqlite3_column_double(statement, 33);
  6206. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6207. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6208. if (sqlite3_column_int(statement, 58)) {
  6209. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6210. handling_fee_value = @"Shipping To Be Quoted";
  6211. }
  6212. NSDictionary *handling_fee_item = @{
  6213. @"title":@"Handling Fee",
  6214. @"value":handling_fee_value
  6215. };
  6216. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6217. //
  6218. // customer info
  6219. customerID = [self textAtColumn:36 statement:statement];
  6220. // mode
  6221. ret[@"mode"] = params[@"mode"];
  6222. // model_count
  6223. ret[@"model_count"] = @(0);
  6224. }
  6225. sqlite3_finalize(statement);
  6226. }
  6227. [iSalesDB close_db:db];
  6228. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID,@"mode":params[@"mode"]} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6229. // "customer_email" = "Shui Hu";
  6230. // "customer_fax" = "";
  6231. // "customer_first_name" = F;
  6232. // "customer_last_name" = L;
  6233. // "customer_name" = ",da He Xiang Dong Liu A";
  6234. // "customer_phone" = "Hey Xuan Feng";
  6235. contactInfo[@"customer_phone"] = customer_phone;
  6236. contactInfo[@"customer_fax"] = customer_fax;
  6237. contactInfo[@"customer_email"] = customer_email;
  6238. NSString *first_name = @"";
  6239. NSString *last_name = @"";
  6240. if ([customer_contact isEqualToString:@""]) {
  6241. } else if ([customer_contact containsString:@" "]) {
  6242. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6243. first_name = [customer_contact substringToIndex:first_space_index];
  6244. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6245. }
  6246. contactInfo[@"customer_first_name"] = first_name;
  6247. contactInfo[@"customer_last_name"] = last_name;
  6248. ret[@"customerInfo"] = contactInfo;
  6249. // models
  6250. if (nssoid) {
  6251. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6252. __block double TotalCuft = 0;
  6253. __block double TotalWeight = 0;
  6254. __block int TotalCarton = 0;
  6255. __block double allItemPrice = 0;
  6256. 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];
  6257. sqlite3 *db1 = [iSalesDB get_db];
  6258. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6259. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6260. int product_id = sqlite3_column_int(stmt, 0);
  6261. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6262. int item_id = sqlite3_column_int(stmt, 7);
  6263. NSString* Price=nil;
  6264. if(str_price==nil)
  6265. {
  6266. NSNumber* price = [self get_model_default_price:customerID user:params[@"user"] product_id:nil item_id:@(item_id) db:db1];
  6267. if(price==nil)
  6268. Price=@"No Price.";
  6269. else
  6270. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6271. }
  6272. else
  6273. {
  6274. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6275. }
  6276. double discount = sqlite3_column_double(stmt, 2);
  6277. int item_count = sqlite3_column_int(stmt, 3);
  6278. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6279. NSString *nsline_note=nil;
  6280. if(line_note!=nil)
  6281. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6282. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6283. // NSString *nsname = nil;
  6284. // if(name!=nil)
  6285. // nsname= [[NSString alloc]initWithUTF8String:name];
  6286. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6287. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6288. // NSString *nsdescription=nil;
  6289. // if(description!=nil)
  6290. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6291. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6292. // int stockUom = sqlite3_column_int(stmt, 8);
  6293. // int _id = sqlite3_column_int(stmt, 9);
  6294. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6295. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6296. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6297. int carton=[bsubtotaljson[@"carton"] intValue];
  6298. TotalCuft += cuft;
  6299. TotalWeight += weight;
  6300. TotalCarton += carton;
  6301. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:params[@"user"]];
  6302. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6303. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6304. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6305. itemjson[@"note"]=nsline_note;
  6306. itemjson[@"origin_price"] = Price;
  6307. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6308. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6309. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6310. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6311. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6312. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6313. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6314. if(itemjson[@"combine"] != nil)
  6315. {
  6316. // int citem=0;
  6317. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6318. for(int bc=0;bc<bcount;bc++)
  6319. {
  6320. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6321. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6322. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6323. subTotal += uprice * modulus * item_count;
  6324. }
  6325. }
  6326. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6327. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6328. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6329. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6330. itemjson[@"type"] = @"order_item";
  6331. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6332. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6333. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6334. allItemPrice += subTotal;
  6335. }];
  6336. section_3[@"data"] = model_data;
  6337. section_3[@"type"] = @"sub_order";
  6338. section_3[@"title"] = @"Models";
  6339. section_3[@"switch"] = @(false);
  6340. ret[@"section_3"] = section_3;
  6341. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6342. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6343. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6344. // payments/Credits
  6345. // payments_and_credist = sqlite3_column_double(statement, 34);
  6346. payments_and_credist = allItemPrice;
  6347. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6348. NSDictionary *sub_total_item = @{
  6349. @"title":@"Sub-Total",
  6350. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6351. };
  6352. [price_data setObject:sub_total_item forKey:@"item_0"];
  6353. // // total
  6354. // totalPrice = sqlite3_column_double(statement, 35);
  6355. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6356. } else {
  6357. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6358. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6359. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6360. // payments/Credits
  6361. payments_and_credist = 0;
  6362. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6363. NSDictionary *sub_total_item = @{
  6364. @"title":@"Sub-Total",
  6365. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6366. };
  6367. [price_data setObject:sub_total_item forKey:@"item_0"];
  6368. }
  6369. // total
  6370. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6371. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6372. NSDictionary *total_item = @{
  6373. @"title":@"Total",
  6374. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6375. };
  6376. [price_data setObject:total_item forKey:@"item_4"];
  6377. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6378. section_4 = @{
  6379. @"data":price_data,
  6380. @"title":@"Price Info",
  6381. @"type":@"price_info"
  6382. }.mutableCopy;
  6383. ret[@"section_4"] = section_4;
  6384. // ret[@"order_info"]= orderinfo;
  6385. section_0 = @{
  6386. @"data":orderinfo,
  6387. @"title":@"Order Info",
  6388. @"type":@"order_info"
  6389. }.mutableCopy;
  6390. ret[@"section_0"] = section_0;
  6391. // ret[@"more_order_info"] = moreInfo;
  6392. section_2 = @{
  6393. @"data":moreInfo,
  6394. @"title":@"More Info",
  6395. @"type":@"more_order_info"
  6396. }.mutableCopy;
  6397. ret[@"section_2"] = section_2;
  6398. ret[@"count"] = @(5);
  6399. return [RAConvertor dict2data:ret];
  6400. }
  6401. #pragma mark order list
  6402. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6403. NSString *nsstatus = @"";
  6404. switch (status) {
  6405. case 0:
  6406. {
  6407. nsstatus=@"Temp Order";
  6408. break;
  6409. }
  6410. case 1:
  6411. {
  6412. nsstatus=@"Saved Order";
  6413. break;
  6414. }
  6415. case 2: {
  6416. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6417. break;
  6418. }
  6419. case 3:
  6420. case 15:
  6421. {
  6422. nsstatus=@"Cancelled";
  6423. break;
  6424. }
  6425. case 10:
  6426. {
  6427. nsstatus=@"Quote Submitted";
  6428. break;
  6429. }
  6430. case 11:
  6431. {
  6432. nsstatus=@"Sales Order Submitted";
  6433. break;
  6434. }
  6435. case 12:
  6436. {
  6437. nsstatus=@"Processing";
  6438. break;
  6439. }
  6440. case 13:
  6441. {
  6442. nsstatus=@"Shipped";
  6443. break;
  6444. }
  6445. case 14:
  6446. {
  6447. nsstatus=@"Closed";
  6448. break;
  6449. }
  6450. case -11:
  6451. {
  6452. nsstatus = @"Ready For Submit";
  6453. break;
  6454. }
  6455. default:
  6456. break;
  6457. }
  6458. return nsstatus;
  6459. }
  6460. + (double) orderTotalPrice:(NSString *)so_id user:(NSString*) user db:(sqlite3 *)db1 close:(BOOL)close
  6461. {
  6462. double total = 0;
  6463. __block double payments_and_credist = 0;
  6464. __block double allItemPrice = 0;
  6465. // sqlite3 *db1 = [iSalesDB get_db];
  6466. if (so_id) {
  6467. // 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];
  6468. 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];
  6469. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6470. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6471. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6472. int product_id = sqlite3_column_int(stmt, 0);
  6473. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6474. int discount = sqlite3_column_int(stmt, 2);
  6475. int item_count = sqlite3_column_int(stmt, 3);
  6476. // int item_id = sqlite3_column_int(stmt, 7);
  6477. int item_id = sqlite3_column_int(stmt, 4);
  6478. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6479. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6480. NSString* Price=nil;
  6481. if(str_price==nil)
  6482. {
  6483. NSNumber* price = [self get_model_default_price:customerID user:user product_id:nil item_id:@(item_id) db:db1];
  6484. if(price==nil)
  6485. Price=@"No Price.";
  6486. else
  6487. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6488. }
  6489. else
  6490. {
  6491. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6492. }
  6493. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID user:user];
  6494. itemjson[@"The unit price"]=Price;
  6495. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6496. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6497. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6498. if(itemjson[@"combine"] != nil)
  6499. {
  6500. // int citem=0;
  6501. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6502. for(int bc=0;bc<bcount;bc++)
  6503. {
  6504. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6505. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6506. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6507. subTotal += uprice * modulus * item_count;
  6508. }
  6509. }
  6510. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6511. allItemPrice += subTotal;
  6512. }];
  6513. payments_and_credist = allItemPrice;
  6514. } else {
  6515. // payments/Credits
  6516. payments_and_credist = 0;
  6517. }
  6518. // lift_gate handlingFee shippingFee
  6519. __block double lift_gate = 0;
  6520. __block double handlingFee = 0;
  6521. __block double shippingFee = 0;
  6522. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6523. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6524. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6525. if (have_lift_gate) {
  6526. lift_gate = sqlite3_column_double(stmt, 1);
  6527. }
  6528. handlingFee = sqlite3_column_double(stmt, 2);
  6529. shippingFee = sqlite3_column_double(stmt, 3);
  6530. }];
  6531. // total
  6532. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6533. if (close) {
  6534. [iSalesDB close_db:db1];
  6535. }
  6536. return total;
  6537. }
  6538. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6539. {
  6540. assert(params[@"user"]!=nil);
  6541. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6542. int limit = [[params valueForKey:@"limit"] intValue];
  6543. int offset = [[params valueForKey:@"offset"] intValue];
  6544. NSString* keyword = [params valueForKey:@"keyWord"];
  6545. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6546. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6547. NSString* where=@"1 = 1";
  6548. if(keyword.length>0)
  6549. 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]];
  6550. if (orderStatus.length > 0) {
  6551. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6552. if (order_status_array.count == 1) {
  6553. int status_value = [[order_status_array firstObject] intValue];
  6554. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6555. if (status_value == 15 || status_value == 3) {
  6556. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6557. } else {
  6558. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6559. }
  6560. } else {
  6561. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6562. }
  6563. } else if (order_status_array.count > 1) {
  6564. for (int i = 0; i < order_status_array.count;i++) {
  6565. NSString *status = order_status_array[i];
  6566. NSString *condition = @" or";
  6567. if (i == 0) {
  6568. condition = @" and (";
  6569. }
  6570. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6571. int status_value = [status intValue];
  6572. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6573. if (status_value == 15 || status_value == 3) {
  6574. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6575. } else {
  6576. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6577. }
  6578. } else {
  6579. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6580. }
  6581. }
  6582. where = [where stringByAppendingString:@" )"];
  6583. }
  6584. }
  6585. 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];
  6586. // DebugLog(@"order list sql: %@",sqlQuery);
  6587. sqlite3 *db = [iSalesDB get_db];
  6588. sqlite3_stmt * statement;
  6589. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6590. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6591. {
  6592. int count=0;
  6593. while (sqlite3_step(statement) == SQLITE_ROW)
  6594. {
  6595. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6596. int order_id = sqlite3_column_double(statement, 0);
  6597. char *soid = (char*)sqlite3_column_text(statement, 1);
  6598. if(soid==nil)
  6599. soid= "";
  6600. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6601. int status = sqlite3_column_double(statement, 2);
  6602. int erpStatus = sqlite3_column_double(statement, 9);
  6603. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6604. if(sales_rep==nil)
  6605. sales_rep= "";
  6606. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6607. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6608. if(create_by==nil)
  6609. create_by= "";
  6610. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6611. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6612. if(company_name==nil)
  6613. company_name= "";
  6614. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6615. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6616. if(create_time==nil)
  6617. create_time= "";
  6618. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6619. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6620. // double total_price = sqlite3_column_double(statement, 7);
  6621. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] user:params[@"user"] db:db close:NO];
  6622. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6623. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6624. int offline_edit = sqlite3_column_int(statement, 10);
  6625. // ": "JH",
  6626. // "": "$8307.00",
  6627. // "": "MOB1608050001",
  6628. // "": "ArpithaT",
  6629. // "": "1st Stage Property Transformations",
  6630. // "": "JANICE SUTTON",
  6631. // "": 2255,
  6632. // "": "08/02/2016 09:49:18",
  6633. // "": 1,
  6634. // "": "Saved Order"
  6635. // "": "1470384050483",
  6636. // "model_count": "6 / 28"
  6637. item[@"sales_rep"]= nssales_rep;
  6638. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6639. item[@"so#"]= nssoid;
  6640. item[@"create_by"]= nscreate_by;
  6641. item[@"customer_name"]= nscompany_name;
  6642. item[@"customer_contact"] = customer_contact;
  6643. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6644. item[@"purchase_time"]= nscreate_time;
  6645. int statusCode = status;
  6646. if (statusCode == 2) {
  6647. statusCode = erpStatus;
  6648. } else if (statusCode == 3) {
  6649. statusCode = 15;
  6650. }
  6651. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6652. item[@"order_status"]= nsstatus;
  6653. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6654. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6655. // item[@"model_count"]
  6656. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6657. count++;
  6658. }
  6659. ret[@"count"]= [NSNumber numberWithInt:count];
  6660. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6661. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6662. ret[@"result"]= [NSNumber numberWithInt:2];
  6663. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6664. ret[@"time_zone"] = @"PST";
  6665. sqlite3_finalize(statement);
  6666. }
  6667. 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];
  6668. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6669. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6670. [iSalesDB close_db:db];
  6671. return [RAConvertor dict2data:ret];
  6672. }
  6673. #pragma mark update gnotes
  6674. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6675. {
  6676. DebugLog(@"params: %@",params);
  6677. // comments = Meyoyoyoyoyoyoy;
  6678. // orderCode = MOB1608110001;
  6679. // password = 123456;
  6680. // user = EvanK;
  6681. 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]];
  6682. int ret = [iSalesDB execSql:sql];
  6683. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6684. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6685. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6686. // [dic setValue:@"160409" forKey:@"min_ver"];
  6687. return [RAConvertor dict2data:dic];
  6688. }
  6689. #pragma mark move to wishlist
  6690. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6691. {
  6692. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6693. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6694. _id = [NSString stringWithFormat:@"(%@)",_id];
  6695. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6696. sqlite3 *db = [iSalesDB get_db];
  6697. __block NSString *product_id = @"";
  6698. __block NSString *item_count_str = @"";
  6699. // __block NSString *item_id = nil;
  6700. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6701. // product_id = [self textAtColumn:0 statement:stmt];
  6702. int item_count = sqlite3_column_int(stmt, 1);
  6703. // item_id = [self textAtColumn:2 statement:stmt];
  6704. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6705. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6706. if (p_id.length) {
  6707. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6708. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6709. }
  6710. }];
  6711. [iSalesDB close_db:db];
  6712. // 去除第一个,
  6713. if (product_id.length > 1) {
  6714. product_id = [product_id substringFromIndex:1];
  6715. }
  6716. if (item_count_str.length > 1) {
  6717. item_count_str = [item_count_str substringFromIndex:1];
  6718. }
  6719. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6720. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6721. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6722. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6723. sqlite3 *db1 = [iSalesDB get_db];
  6724. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6725. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6726. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6727. // 删除
  6728. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6729. int ret = [iSalesDB execSql:deleteSql db:db1];
  6730. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6731. [iSalesDB close_db:db1];
  6732. return [RAConvertor dict2data:dic];
  6733. }
  6734. #pragma mark cart delete
  6735. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6736. {
  6737. // cartItemId = 548;
  6738. // orderCode = MOB1608110001;
  6739. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6740. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6741. _id = [NSString stringWithFormat:@"(%@)",_id];
  6742. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6743. int ret = [iSalesDB execSql:sql];
  6744. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6745. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6746. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6747. // [dic setValue:@"160409" forKey:@"min_ver"];
  6748. return [RAConvertor dict2data:dic];
  6749. }
  6750. #pragma mark set price
  6751. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6752. {
  6753. DebugLog(@"cart set price params: %@",params);
  6754. // "cartitem_id" = 1;
  6755. // discount = "0.000000";
  6756. // "item_note" = "";
  6757. // price = "269.000000";
  6758. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6759. NSString *notes = [self valueInParams:params key:@"item_note"];
  6760. NSString *discount = [self valueInParams:params key:@"discount"];
  6761. NSString *price = [self valueInParams:params key:@"price"];
  6762. // bool badd_price_changed=false;
  6763. // sqlite3* db=[iSalesDB get_db];
  6764. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6765. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6766. // NSRange range;
  6767. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6768. //
  6769. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6770. // badd_price_changed=true;
  6771. // [iSalesDB close_db:db];
  6772. //
  6773. // if(badd_price_changed)
  6774. // {
  6775. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6776. // }
  6777. //
  6778. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6779. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6780. int ret = [iSalesDB execSql:sql];
  6781. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6782. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6783. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6784. // [dic setValue:@"160409" forKey:@"min_ver"];
  6785. return [RAConvertor dict2data:dic];
  6786. }
  6787. #pragma mark set line notes
  6788. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6789. {
  6790. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6791. NSString *notes = [self valueInParams:params key:@"notes"];
  6792. notes = [self translateSingleQuote:notes];
  6793. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6794. int ret = [iSalesDB execSql:sql];
  6795. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6796. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6797. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6798. // [dic setValue:@"160409" forKey:@"min_ver"];
  6799. return [RAConvertor dict2data:dic];
  6800. }
  6801. #pragma mark set qty
  6802. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6803. {
  6804. assert(params[@"can_create_backorder"]!=nil);
  6805. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6806. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6807. int item_count = [params[@"inputInt"] intValue];
  6808. // 购买检查数量大于库存
  6809. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6810. if (!params[@"can_create_backorder"]) {
  6811. 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];
  6812. __block BOOL out_of_stock = NO;
  6813. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6814. int availability = sqlite3_column_int(stmt, 0);
  6815. if (availability < item_count) {
  6816. out_of_stock = YES;
  6817. }
  6818. }];
  6819. if (out_of_stock) { // 缺货
  6820. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6821. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6822. return [RAConvertor dict2data:dic];
  6823. }
  6824. }
  6825. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6826. int ret = [iSalesDB execSql:sql];
  6827. __block int item_id = 0;
  6828. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6829. item_id = sqlite3_column_int(stmt, 0);
  6830. }];
  6831. sqlite3 *db = [iSalesDB get_db];
  6832. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6833. [iSalesDB close_db:db];
  6834. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6835. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6836. // [dic setValue:@"160409" forKey:@"min_ver"];
  6837. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6838. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6839. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6840. return [RAConvertor dict2data:dic];
  6841. }
  6842. #pragma mark place order
  6843. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6844. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6845. if (aname) {
  6846. [dic setValue:aname forKey:@"aname"];
  6847. }
  6848. if (control) {
  6849. [dic setValue:control forKey:@"control"];
  6850. }
  6851. if (keyboard) {
  6852. [dic setValue:keyboard forKey:@"keyboard"];
  6853. }
  6854. if (name) {
  6855. [dic setValue:name forKey:@"name"];
  6856. }
  6857. if (value) {
  6858. [dic setValue:value forKey:@"value"];
  6859. }
  6860. return dic;
  6861. }
  6862. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6863. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6864. orderCode = [self translateSingleQuote:orderCode];
  6865. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6866. 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];
  6867. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6868. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6869. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6870. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6871. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6872. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6873. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6874. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6875. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6876. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6877. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6878. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6879. NSString *email = [self textAtColumn:11 statement:stmt];
  6880. NSString *phone = [self textAtColumn:12 statement:stmt];
  6881. NSString *fax = [self textAtColumn:13 statement:stmt];
  6882. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6883. NSString *city = [self textAtColumn:15 statement:stmt];
  6884. NSString *state = [self textAtColumn:16 statement:stmt];
  6885. NSString *country = [self textAtColumn:17 statement:stmt];
  6886. NSString *name = [self textAtColumn:18 statement:stmt];
  6887. // contact id
  6888. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6889. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6890. [contact_id_dic setValue:@"text" forKey:@"control"];
  6891. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6892. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6893. [contact_id_dic setValue:@"true" forKey:@"required"];
  6894. [contact_id_dic setValue:contact_id forKey:@"value"];
  6895. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6896. // business card
  6897. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6898. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6899. [business_card_dic setValue:@"img" forKey:@"control"];
  6900. [business_card_dic setValue:@"1" forKey:@"disable"];
  6901. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6902. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6903. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6904. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6905. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6906. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6907. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6908. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6909. // fax
  6910. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6911. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6912. // zipcode
  6913. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6914. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6915. // city
  6916. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6917. [customer_dic setValue:city_dic forKey:@"item_12"];
  6918. // state
  6919. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6920. [customer_dic setValue:state_dic forKey:@"item_13"];
  6921. // country
  6922. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6923. [customer_dic setValue:country_dic forKey:@"item_14"];
  6924. // company name
  6925. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6926. [customer_dic setValue:company_dic forKey:@"item_2"];
  6927. // addr_1
  6928. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6929. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6930. // addr_2
  6931. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6932. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6933. // addr_3
  6934. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6935. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6936. // addr_4
  6937. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6938. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6939. // Contact
  6940. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6941. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6942. // email
  6943. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6944. [customer_dic setValue:email_dic forKey:@"item_8"];
  6945. // phone
  6946. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6947. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6948. // title
  6949. [customer_dic setValue:@"Customer" forKey:@"title"];
  6950. // count
  6951. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6952. }];
  6953. // setting
  6954. NSDictionary *setting = params[@"setting"];
  6955. NSNumber *hide = setting[@"CustomerHide"];
  6956. [customer_dic setValue:hide forKey:@"hide"];
  6957. return customer_dic;
  6958. }
  6959. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6960. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6961. // setting
  6962. NSDictionary *setting = params[@"setting"];
  6963. NSNumber *hide = setting[@"ShipToHide"];
  6964. [dic setValue:hide forKey:@"hide"];
  6965. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6966. orderCode = [self translateSingleQuote:orderCode];
  6967. 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];
  6968. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6969. NSString *cid = [self textAtColumn:0 statement:stmt];
  6970. NSString *name = [self textAtColumn:1 statement:stmt];
  6971. NSString *ext = [self textAtColumn:2 statement:stmt];
  6972. NSString *contact = [self textAtColumn:3 statement:stmt];
  6973. NSString *email = [self textAtColumn:4 statement:stmt];
  6974. NSString *fax = [self textAtColumn:5 statement:stmt];
  6975. NSString *phone = [self textAtColumn:6 statement:stmt];
  6976. // count
  6977. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6978. // title
  6979. [dic setValue:@"Ship To" forKey:@"title"];
  6980. // choose
  6981. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6982. [choose_dic setValue:@"choose" forKey:@"aname"];
  6983. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6984. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6985. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6986. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6987. @"receive_contact" : @"customer_contact",
  6988. @"receive_email" : @"customer_email",
  6989. @"receive_ext" : @"customer_contact_ext",
  6990. @"receive_fax" : @"customer_fax",
  6991. @"receive_name" : @"customer_name",
  6992. @"receive_phone" : @"customer_phone"},
  6993. @"refresh" : [NSNumber numberWithInteger:1],
  6994. @"type" : @"pull"};
  6995. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6996. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6997. @"name" : @"Add new address",
  6998. @"refresh" : [NSNumber numberWithInteger:1],
  6999. @"value" : @"new_addr"
  7000. };
  7001. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  7002. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  7003. @"key_map" : @{@"receive_cid" : @"customer_cid",
  7004. @"receive_contact" : @"customer_contact",
  7005. @"receive_email" : @"customer_email",
  7006. @"receive_ext" : @"customer_contact_ext",
  7007. @"receive_fax" : @"customer_fax",
  7008. @"receive_name" : @"customer_name",
  7009. @"receive_phone" : @"customer_phone"},
  7010. @"name" : @"select_ship_to",
  7011. @"refresh" : [NSNumber numberWithInteger:1],
  7012. @"value" : @"Sales_Order_Ship_To"};
  7013. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  7014. [dic setValue:choose_dic forKey:@"item_0"];
  7015. // contact id
  7016. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7017. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7018. [contact_id_dic setValue:@"true" forKey:@"required"];
  7019. [dic setValue:contact_id_dic forKey:@"item_1"];
  7020. // company name
  7021. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7022. [dic setValue:company_name_dic forKey:@"item_2"];
  7023. // address
  7024. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7025. NSNumber *required = setting[@"ShippingToAddressRequire"];
  7026. if ([required integerValue]) {
  7027. [ext_dic setValue:@"true" forKey:@"required"];
  7028. }
  7029. [dic setValue:ext_dic forKey:@"item_3"];
  7030. // contact
  7031. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7032. [dic setValue:contact_dic forKey:@"item_4"];
  7033. // phone
  7034. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7035. [dic setValue:phone_dic forKey:@"item_5"];
  7036. // fax
  7037. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7038. [dic setValue:fax_dic forKey:@"item_6"];
  7039. // email
  7040. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7041. [dic setValue:email_dic forKey:@"item_7"];
  7042. }];
  7043. return dic;
  7044. }
  7045. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7046. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7047. // setting
  7048. NSDictionary *setting = params[@"setting"];
  7049. NSNumber *hide = setting[@"ShipFromHide"];
  7050. [dic setValue:hide forKey:@"hide"];
  7051. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7052. orderCode = [self translateSingleQuote:orderCode];
  7053. 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];
  7054. 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';";
  7055. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7056. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7057. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7058. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7059. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7060. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7061. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7062. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7063. if (!cid.length) {
  7064. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7065. cid = [self textAtColumn:0 statement:statment];
  7066. name = [self textAtColumn:1 statement:statment];
  7067. ext = [self textAtColumn:2 statement:statment];
  7068. contact = [self textAtColumn:3 statement:statment];
  7069. email = [self textAtColumn:4 statement:statment];
  7070. fax = [self textAtColumn:5 statement:statment];
  7071. phone = [self textAtColumn:6 statement:statment];
  7072. }];
  7073. }
  7074. // count
  7075. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7076. // title
  7077. [dic setValue:@"Ship From" forKey:@"title"];
  7078. // hide
  7079. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7080. // choose
  7081. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7082. [choose_dic setValue:@"choose" forKey:@"aname"];
  7083. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7084. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7085. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7086. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7087. @"sender_contact" : @"customer_contact",
  7088. @"sender_email" : @"customer_email",
  7089. @"sender_ext" : @"customer_contact_ext",
  7090. @"sender_fax" : @"customer_fax",
  7091. @"sender_name" : @"customer_name",
  7092. @"sender_phone" : @"customer_phone"},
  7093. @"name" : @"select_cid",
  7094. @"refresh" : [NSNumber numberWithInteger:0],
  7095. @"value" : @"Sales_Order_Ship_From"};
  7096. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7097. [dic setValue:choose_dic forKey:@"item_0"];
  7098. // contact id
  7099. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7100. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7101. [contact_id_dic setValue:@"true" forKey:@"required"];
  7102. [dic setValue:contact_id_dic forKey:@"item_1"];
  7103. // company name
  7104. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7105. [dic setValue:company_name_dic forKey:@"item_2"];
  7106. // address
  7107. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7108. [dic setValue:ext_dic forKey:@"item_3"];
  7109. // contact
  7110. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7111. [dic setValue:contact_dic forKey:@"item_4"];
  7112. // phone
  7113. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7114. [dic setValue:phone_dic forKey:@"item_5"];
  7115. // fax
  7116. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7117. [dic setValue:fax_dic forKey:@"item_6"];
  7118. // email
  7119. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7120. [dic setValue:email_dic forKey:@"item_7"];
  7121. }];
  7122. return dic;
  7123. }
  7124. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7125. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7126. // setting
  7127. NSDictionary *setting = params[@"setting"];
  7128. NSNumber *hide = setting[@"FreightBillToHide"];
  7129. [dic setValue:hide forKey:@"hide"];
  7130. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7131. orderCode = [self translateSingleQuote:orderCode];
  7132. 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];
  7133. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7134. NSString *cid = [self textAtColumn:0 statement:stmt];
  7135. NSString *name = [self textAtColumn:1 statement:stmt];
  7136. NSString *ext = [self textAtColumn:2 statement:stmt];
  7137. NSString *contact = [self textAtColumn:3 statement:stmt];
  7138. NSString *email = [self textAtColumn:4 statement:stmt];
  7139. NSString *fax = [self textAtColumn:5 statement:stmt];
  7140. NSString *phone = [self textAtColumn:6 statement:stmt];
  7141. // count
  7142. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7143. // title
  7144. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7145. // hide
  7146. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7147. // choose
  7148. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7149. [choose_dic setValue:@"choose" forKey:@"aname"];
  7150. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7151. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7152. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7153. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7154. @"shipping_billto_contact" : @"receive_contact",
  7155. @"shipping_billto_email" : @"receive_email",
  7156. @"shipping_billto_ext" : @"receive_ext",
  7157. @"shipping_billto_fax" : @"receive_fax",
  7158. @"shipping_billto_name" : @"receive_name",
  7159. @"shipping_billto_phone" : @"receive_phone"},
  7160. @"type" : @"pull"};
  7161. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7162. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7163. @"type" : @"pull",
  7164. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7165. @"shipping_billto_contact" : @"customer_contact",
  7166. @"shipping_billto_email" : @"customer_email",
  7167. @"shipping_billto_ext" : @"customer_contact_ext",
  7168. @"shipping_billto_fax" : @"customer_fax",
  7169. @"shipping_billto_name" : @"customer_name",
  7170. @"shipping_billto_phone" : @"customer_phone"}
  7171. };
  7172. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7173. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7174. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7175. @"shipping_billto_contact" : @"sender_contact",
  7176. @"shipping_billto_email" : @"sender_email",
  7177. @"shipping_billto_ext" : @"sender_ext",
  7178. @"shipping_billto_fax" : @"sender_fax",
  7179. @"shipping_billto_name" : @"sender_name",
  7180. @"shipping_billto_phone" : @"sender_phone"},
  7181. @"type" : @"pull"
  7182. };
  7183. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7184. NSDictionary *select_freight_bill_to_dic = @{
  7185. @"aname" : @"Select freight bill to",
  7186. @"name" : @"select_cid",
  7187. @"refresh" : [NSNumber numberWithInteger:0],
  7188. @"value" : @"Sales_Order_Freight_Bill_To",
  7189. @"key_map" : @{
  7190. @"shipping_billto_cid" : @"customer_cid",
  7191. @"shipping_billto_contact" : @"customer_contact",
  7192. @"shipping_billto_email" : @"customer_email",
  7193. @"shipping_billto_ext" : @"customer_contact_ext",
  7194. @"shipping_billto_fax" : @"customer_fax",
  7195. @"shipping_billto_name" : @"customer_name",
  7196. @"shipping_billto_phone" : @"customer_phone"
  7197. }
  7198. };
  7199. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7200. [dic setValue:choose_dic forKey:@"item_0"];
  7201. // contact id
  7202. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7203. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7204. [contact_id_dic setValue:@"true" forKey:@"required"];
  7205. [dic setValue:contact_id_dic forKey:@"item_1"];
  7206. // company name
  7207. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7208. [dic setValue:company_name_dic forKey:@"item_2"];
  7209. // address
  7210. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7211. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7212. if ([ext_required integerValue]) {
  7213. [ext_dic setValue:@"true" forKey:@"required"];
  7214. }
  7215. [dic setValue:ext_dic forKey:@"item_3"];
  7216. // contact
  7217. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7218. [dic setValue:contact_dic forKey:@"item_4"];
  7219. // phone
  7220. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7221. [dic setValue:phone_dic forKey:@"item_5"];
  7222. // fax
  7223. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7224. [dic setValue:fax_dic forKey:@"item_6"];
  7225. // email
  7226. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7227. [dic setValue:email_dic forKey:@"item_7"];
  7228. }];
  7229. return dic;
  7230. }
  7231. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7232. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7233. // setting
  7234. NSDictionary *setting = params[@"setting"];
  7235. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7236. [dic setValue:hide forKey:@"hide"];
  7237. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7238. orderCode = [self translateSingleQuote:orderCode];
  7239. 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];
  7240. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7241. NSString *cid = [self textAtColumn:0 statement:stmt];
  7242. NSString *name = [self textAtColumn:1 statement:stmt];
  7243. NSString *ext = [self textAtColumn:2 statement:stmt];
  7244. NSString *contact = [self textAtColumn:3 statement:stmt];
  7245. NSString *email = [self textAtColumn:4 statement:stmt];
  7246. NSString *fax = [self textAtColumn:5 statement:stmt];
  7247. NSString *phone = [self textAtColumn:6 statement:stmt];
  7248. // count
  7249. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7250. // title
  7251. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7252. // hide
  7253. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7254. // choose
  7255. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7256. [choose_dic setValue:@"choose" forKey:@"aname"];
  7257. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7258. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7259. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7260. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7261. @"billing_contact" : @"receive_contact",
  7262. @"billing_email" : @"receive_email",
  7263. @"billing_ext" : @"receive_ext",
  7264. @"billing_fax" : @"receive_fax",
  7265. @"billing_name" : @"receive_name",
  7266. @"billing_phone" : @"receive_phone"},
  7267. @"type" : @"pull"};
  7268. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7269. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7270. @"type" : @"pull",
  7271. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7272. @"billing_contact" : @"customer_contact",
  7273. @"billing_email" : @"customer_email",
  7274. @"billing_ext" : @"customer_contact_ext",
  7275. @"billing_fax" : @"customer_fax",
  7276. @"billing_name" : @"customer_name",
  7277. @"billing_phone" : @"customer_phone"}
  7278. };
  7279. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7280. NSDictionary *select_bill_to_dic = @{
  7281. @"aname" : @"Select bill to",
  7282. @"name" : @"select_cid",
  7283. @"refresh" : [NSNumber numberWithInteger:0],
  7284. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7285. @"key_map" : @{
  7286. @"billing_cid" : @"customer_cid",
  7287. @"billing_contact" : @"customer_contact",
  7288. @"billing_email" : @"customer_email",
  7289. @"billing_ext" : @"customer_contact_ext",
  7290. @"billing_fax" : @"customer_fax",
  7291. @"billing_name" : @"customer_name",
  7292. @"billing_phone" : @"customer_phone"
  7293. }
  7294. };
  7295. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7296. [dic setValue:choose_dic forKey:@"item_0"];
  7297. // contact id
  7298. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7299. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7300. [contact_id_dic setValue:@"true" forKey:@"required"];
  7301. [dic setValue:contact_id_dic forKey:@"item_1"];
  7302. // company name
  7303. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7304. [dic setValue:company_name_dic forKey:@"item_2"];
  7305. // address
  7306. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7307. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7308. if ([ext_required integerValue]) {
  7309. [ext_dic setValue:@"true" forKey:@"required"];
  7310. }
  7311. [dic setValue:ext_dic forKey:@"item_3"];
  7312. // contact
  7313. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7314. [dic setValue:contact_dic forKey:@"item_4"];
  7315. // phone
  7316. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7317. [dic setValue:phone_dic forKey:@"item_5"];
  7318. // fax
  7319. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7320. [dic setValue:fax_dic forKey:@"item_6"];
  7321. // email
  7322. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7323. [dic setValue:email_dic forKey:@"item_7"];
  7324. }];
  7325. return dic;
  7326. }
  7327. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7328. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7329. // setting
  7330. NSDictionary *setting = params[@"setting"];
  7331. NSNumber *hide = setting[@"ReturnToHide"];
  7332. [dic setValue:hide forKey:@"hide"];
  7333. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7334. orderCode = [self translateSingleQuote:orderCode];
  7335. 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];
  7336. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7337. NSString *cid = [self textAtColumn:0 statement:stmt];
  7338. NSString *name = [self textAtColumn:1 statement:stmt];
  7339. NSString *ext = [self textAtColumn:2 statement:stmt];
  7340. NSString *contact = [self textAtColumn:3 statement:stmt];
  7341. NSString *email = [self textAtColumn:4 statement:stmt];
  7342. NSString *fax = [self textAtColumn:5 statement:stmt];
  7343. NSString *phone = [self textAtColumn:6 statement:stmt];
  7344. // count
  7345. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7346. // title
  7347. [dic setValue:@"Return To" forKey:@"title"];
  7348. // hide
  7349. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7350. // choose
  7351. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7352. [choose_dic setValue:@"choose" forKey:@"aname"];
  7353. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7354. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7355. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7356. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7357. @"returnto_contact" : @"sender_contact",
  7358. @"returnto_email" : @"sender_email",
  7359. @"returnto_ext" : @"sender_ext",
  7360. @"returnto_fax" : @"sender_fax",
  7361. @"returnto_name" : @"sender_name",
  7362. @"returnto_phone" : @"sender_phone"},
  7363. @"type" : @"pull"};
  7364. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7365. NSDictionary *select_return_to_dic = @{
  7366. @"aname" : @"Select return to",
  7367. @"name" : @"select_cid",
  7368. @"refresh" : [NSNumber numberWithInteger:0],
  7369. @"value" : @"Contact_Return_To",
  7370. @"key_map" : @{
  7371. @"returnto_cid" : @"customer_cid",
  7372. @"returnto_contact" : @"customer_contact",
  7373. @"returnto_email" : @"customer_email",
  7374. @"returnto_ext" : @"customer_contact_ext",
  7375. @"returnto_fax" : @"customer_fax",
  7376. @"returnto_name" : @"customer_name",
  7377. @"returnto_phone" : @"customer_phone"
  7378. }
  7379. };
  7380. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7381. [dic setValue:choose_dic forKey:@"item_0"];
  7382. // contact id
  7383. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7384. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7385. [contact_id_dic setValue:@"true" forKey:@"required"];
  7386. [dic setValue:contact_id_dic forKey:@"item_1"];
  7387. // company name
  7388. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7389. [dic setValue:company_name_dic forKey:@"item_2"];
  7390. // address
  7391. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7392. [dic setValue:ext_dic forKey:@"item_3"];
  7393. // contact
  7394. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7395. [dic setValue:contact_dic forKey:@"item_4"];
  7396. // phone
  7397. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7398. [dic setValue:phone_dic forKey:@"item_5"];
  7399. // fax
  7400. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7401. [dic setValue:fax_dic forKey:@"item_6"];
  7402. // email
  7403. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7404. [dic setValue:email_dic forKey:@"item_7"];
  7405. }];
  7406. return dic;
  7407. }
  7408. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db
  7409. {
  7410. assert(params[@"user"]!=nil);
  7411. assert(params[@"contact_id"]!=nil);
  7412. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7413. orderCode = [self translateSingleQuote:orderCode];
  7414. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7415. __block double TotalCuft = 0;
  7416. __block double TotalWeight = 0;
  7417. __block int TotalCarton = 0;
  7418. __block double payments = 0;
  7419. // setting
  7420. NSDictionary *setting = params[@"setting"];
  7421. NSNumber *hide = setting[@"ModelInformationHide"];
  7422. [dic setValue:hide forKey:@"hide"];
  7423. 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];
  7424. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7425. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7426. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7427. // item id
  7428. int item_id = sqlite3_column_int(stmt, 0);
  7429. // count
  7430. int item_count = sqlite3_column_int(stmt, 1);
  7431. // stockUom
  7432. int stockUom = sqlite3_column_int(stmt, 2);
  7433. // unit price
  7434. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7435. NSString* Price=nil;
  7436. if([str_price isEqualToString:@""])
  7437. {
  7438. NSNumber* price = [self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db];
  7439. if(price==nil)
  7440. Price=@"No Price.";
  7441. else
  7442. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7443. }
  7444. else
  7445. {
  7446. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7447. }
  7448. // discount
  7449. double discount = sqlite3_column_double(stmt, 4);
  7450. // name
  7451. NSString *name = [self textAtColumn:5 statement:stmt];
  7452. // description
  7453. NSString *description = [self textAtColumn:6 statement:stmt];
  7454. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7455. // line note
  7456. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7457. int avaulability = sqlite3_column_int(stmt, 8);
  7458. // img
  7459. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7460. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:params[@"contact_id"] user:params[@"user"]];
  7461. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7462. if(combine != nil)
  7463. {
  7464. // int citem=0;
  7465. int bcount=[[combine valueForKey:@"count"] intValue];
  7466. for(int bc=0;bc<bcount;bc++)
  7467. {
  7468. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7469. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7470. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7471. subTotal += uprice * modulus * item_count;
  7472. }
  7473. }
  7474. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7475. [model_dic setValue:@"model" forKey:@"control"];
  7476. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7477. [model_dic setValue:description forKey:@"description"];
  7478. [model_dic setValue:line_note forKey:@"note"];
  7479. [model_dic setValue:img forKey:@"img_url"];
  7480. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7481. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7482. [model_dic setValue:Price forKey:@"unit_price"];
  7483. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7484. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7485. if (combine) {
  7486. [model_dic setValue:combine forKey:@"combine"];
  7487. }
  7488. // well,what under the row is the info for total
  7489. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7490. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7491. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7492. int carton=[bsubtotaljson[@"carton"] intValue];
  7493. TotalCuft += cuft;
  7494. TotalWeight += weight;
  7495. TotalCarton += carton;
  7496. payments += subTotal;
  7497. //---------------------------
  7498. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7499. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7500. }];
  7501. [dic setValue:@"Model Information" forKey:@"title"];
  7502. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7503. return dic;
  7504. }
  7505. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7506. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7507. [dic setValue:@"Remarks Content" forKey:@"title"];
  7508. // setting
  7509. NSDictionary *setting = params[@"setting"];
  7510. NSNumber *hide = setting[@"RemarksContentHide"];
  7511. [dic setValue:hide forKey:@"hide"];
  7512. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7513. orderCode = [self translateSingleQuote:orderCode];
  7514. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7515. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7516. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7517. int mustCall = sqlite3_column_int(stmt, 1);
  7518. // NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7519. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7520. NSDictionary *po_dic = @{
  7521. @"aname" : @"PO#",
  7522. @"control" : @"edit",
  7523. @"keyboard" : @"text",
  7524. @"name" : @"poNumber",
  7525. @"value" : poNumber
  7526. };
  7527. NSDictionary *must_call_dic = @{
  7528. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7529. @"control" : @"switch",
  7530. @"name" : @"must_call",
  7531. @"value" : mustCall ? @"true" : @"false"
  7532. };
  7533. NSDictionary *general_notes_dic = @{
  7534. @"aname" : @"General notes",
  7535. @"control" : @"text_view",
  7536. @"keyboard" : @"text",
  7537. @"name" : @"comments",
  7538. @"value" : generalNotes
  7539. };
  7540. // NSDictionary *internal_notes_dic = @{
  7541. // @"aname" : @"Internal notes",
  7542. // @"control" : @"text_view",
  7543. // @"keyboard" : @"text",
  7544. // @"name" : @"internal_notes",
  7545. // @"value" : internalNotes
  7546. // };
  7547. [dic setValue:po_dic forKey:@"item_0"];
  7548. [dic setValue:must_call_dic forKey:@"item_1"];
  7549. [dic setValue:general_notes_dic forKey:@"item_2"];
  7550. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7551. }];
  7552. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7553. return dic;
  7554. }
  7555. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7556. // params
  7557. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7558. orderCode = [self translateSingleQuote:orderCode];
  7559. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7560. // setting
  7561. NSDictionary *setting = params[@"setting"];
  7562. NSNumber *hide = setting[@"OrderTotalHide"];
  7563. [dic setValue:hide forKey:@"hide"];
  7564. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7565. __block double lift_gate_value = 0;
  7566. __block double handling_fee = 0;
  7567. __block double shipping = 0;
  7568. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7569. int lift_gate = sqlite3_column_int(stmt, 0);
  7570. lift_gate_value = sqlite3_column_double(stmt, 1);
  7571. shipping = sqlite3_column_double(stmt, 2);
  7572. handling_fee = sqlite3_column_double(stmt, 3);
  7573. if (!lift_gate) {
  7574. lift_gate_value = 0;
  7575. }
  7576. }];
  7577. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7578. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7579. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7580. double payments = [[total valueForKey:@"payments"] doubleValue];
  7581. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7582. double totalPrice = payments;
  7583. [dic setValue:@"Order Total" forKey:@"title"];
  7584. NSDictionary *payments_dic = @{
  7585. @"align" : @"right",
  7586. @"aname" : @"Payments/Credits",
  7587. @"control" : @"text",
  7588. @"name" : @"paymentsAndCredits",
  7589. @"type" : @"price",
  7590. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7591. };
  7592. [dic setValue:payments_dic forKey:@"item_0"];
  7593. // version 1.71 remove
  7594. // NSDictionary *handling_fee_dic = @{
  7595. // @"align" : @"right",
  7596. // @"aname" : @"Handling Fee",
  7597. // @"control" : @"text",
  7598. // @"name" : @"handling_fee_value",
  7599. // @"required" : @"true",
  7600. // @"type" : @"price",
  7601. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7602. // };
  7603. NSDictionary *shipping_dic = @{
  7604. @"align" : @"right",
  7605. @"aname" : @"Shipping*",
  7606. @"control" : @"text",
  7607. @"name" : @"shipping",
  7608. @"required" : @"true",
  7609. @"type" : @"price",
  7610. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7611. };
  7612. NSDictionary *lift_gate_dic = @{
  7613. @"align" : @"right",
  7614. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7615. @"control" : @"text",
  7616. @"name" : @"lift_gate_value",
  7617. @"required" : @"true",
  7618. @"type" : @"price",
  7619. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7620. };
  7621. int item_count = 1;
  7622. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7623. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7624. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7625. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7626. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7627. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7628. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7629. } else {
  7630. }
  7631. }
  7632. // version 1.71 remove
  7633. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7634. NSDictionary *total_price_dic = @{
  7635. @"align" : @"right",
  7636. @"aname" : @"Total",
  7637. @"control" : @"text",
  7638. @"name" : @"totalPrice",
  7639. @"type" : @"price",
  7640. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7641. };
  7642. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7643. NSDictionary *total_cuft_dic = @{
  7644. @"align" : @"right",
  7645. @"aname" : @"Total Cuft",
  7646. @"control" : @"text",
  7647. @"name" : @"",
  7648. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7649. };
  7650. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7651. NSDictionary *total_weight_dic = @{
  7652. @"align" : @"right",
  7653. @"aname" : @"Total Weight",
  7654. @"control" : @"text",
  7655. @"name" : @"",
  7656. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7657. };
  7658. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7659. NSDictionary *total_carton_dic = @{
  7660. @"align" : @"right",
  7661. @"aname" : @"Total Carton",
  7662. @"control" : @"text",
  7663. @"name" : @"",
  7664. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7665. };
  7666. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7667. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7668. return dic;
  7669. }
  7670. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7671. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7672. orderCode = [self translateSingleQuote:orderCode];
  7673. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7674. // setting
  7675. NSDictionary *setting = params[@"setting"];
  7676. NSNumber *hide = setting[@"SignatureHide"];
  7677. [dic setValue:hide forKey:@"hide"];
  7678. [dic setValue:@"Signature" forKey:@"title"];
  7679. __block NSString *pic_path = @"";
  7680. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7681. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7682. pic_path = [self textAtColumn:0 statement:stmt];
  7683. }];
  7684. NSDictionary *pic_dic = @{
  7685. @"aname" : @"Signature",
  7686. @"avalue" :pic_path,
  7687. @"control" : @"signature",
  7688. @"name" : @"sign_picpath",
  7689. @"value" : pic_path
  7690. };
  7691. [dic setValue:pic_dic forKey:@"item_0"];
  7692. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7693. return dic;
  7694. }
  7695. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7696. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7697. // setting
  7698. NSDictionary *setting = params[@"setting"];
  7699. NSNumber *hide = setting[@"ShippingMethodHide"];
  7700. [dic setValue:hide forKey:@"hide"];
  7701. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7702. orderCode = [self translateSingleQuote:orderCode];
  7703. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7704. __block NSString *logist = @"";
  7705. __block NSString *lift_gate = @"";
  7706. __block int lift_gate_integer = 0;
  7707. __block NSString *logistic_note = @"";
  7708. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7709. logist = [self textAtColumn:0 statement:stmt];
  7710. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7711. logistic_note = [self textAtColumn:2 statement:stmt];
  7712. }];
  7713. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7714. [dic setValue:@"Shipping Method" forKey:@"title"];
  7715. // val_0
  7716. int PERSONAL_PICK_UP_check = 0;
  7717. int USE_MY_CARRIER_check = 0;
  7718. NSString *logistic_note_text = @"";
  7719. int will_call_check = 0;
  7720. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7721. will_call_check = 1;
  7722. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7723. PERSONAL_PICK_UP_check = 1;
  7724. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7725. USE_MY_CARRIER_check = 1;
  7726. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7727. logistic_note = [logistic_note_array firstObject];
  7728. if (logistic_note_array.count > 1) {
  7729. logistic_note_text = [logistic_note_array lastObject];
  7730. }
  7731. }
  7732. }
  7733. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7734. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7735. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7736. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7737. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7738. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7739. NSDictionary *val0_subItem_item0 = @{
  7740. @"aname" : @"Option",
  7741. @"cadedate" : @{
  7742. @"count" : [NSNumber numberWithInteger:2],
  7743. @"val_0" : @{
  7744. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7745. @"refresh" : [NSNumber numberWithInteger:0],
  7746. @"value" : @"PERSONAL PICK UP",
  7747. @"value_id" : @"PERSONAL PICK UP"
  7748. },
  7749. @"val_1" : @{
  7750. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7751. @"refresh" : [NSNumber numberWithInteger:0],
  7752. @"sub_item" : @{
  7753. @"count" : [NSNumber numberWithInteger:1],
  7754. @"item_0" : @{
  7755. @"aname" : @"BOL",
  7756. @"control" : @"edit",
  7757. @"keyboard" : @"text",
  7758. @"name" : @"logist_note_text",
  7759. @"refresh" : [NSNumber numberWithInteger:0],
  7760. @"required" : @"false",
  7761. @"value" : logistic_note_text
  7762. }
  7763. },
  7764. @"value" : @"USE MY CARRIER",
  7765. @"value_id" : @"USE MY CARRIER"
  7766. }
  7767. },
  7768. @"control" : @"enum",
  7769. @"name" : @"logistic_note",
  7770. @"required" : @"true",
  7771. @"single_select" : @"true"
  7772. };
  7773. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7774. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7775. // val_1
  7776. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7777. if([lift_gate isEqualToString:@""]) {
  7778. if (lift_gate_integer == 1) {
  7779. lift_gate = @"true";
  7780. } else {
  7781. lift_gate = @"false";
  7782. }
  7783. }
  7784. int common_carrier_check = 0;
  7785. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7786. common_carrier_check = 1;
  7787. [params setValue:lift_gate forKey:@"lift_gate"];
  7788. }
  7789. NSDictionary *val_1 = @{
  7790. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7791. @"sub_item" : @{
  7792. @"count" : [NSNumber numberWithInteger:1],
  7793. @"item_0" : @{
  7794. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7795. @"control" : @"switch",
  7796. @"name" : @"lift_gate",
  7797. @"refresh" : [NSNumber numberWithInteger:1],
  7798. @"required" : @"true",
  7799. @"value" : lift_gate
  7800. }
  7801. },
  7802. @"value" : @"COMMON CARRIER",
  7803. @"value_id" : @"COMMON CARRIER"
  7804. };
  7805. // cadedate
  7806. NSDictionary *cadedate = @{
  7807. @"count" : [NSNumber numberWithInteger:2],
  7808. @"val_0" : val_0,
  7809. @"val_1" : val_1
  7810. };
  7811. // item_0
  7812. NSMutableDictionary *item_0 = @{
  7813. @"aname" : @"Shipping",
  7814. @"cadedate" : cadedate,
  7815. @"control" : @"enum",
  7816. @"name" : @"logist",
  7817. @"refresh" : [NSNumber numberWithInteger:1],
  7818. @"single_select" : @"true",
  7819. }.mutableCopy;
  7820. NSNumber *required = setting[@"ShippingMethodRequire"];
  7821. if ([required integerValue]) {
  7822. [item_0 setValue:@"true" forKey:@"required"];
  7823. }
  7824. [dic setValue:item_0 forKey:@"item_0"];
  7825. if ([logist isEqualToString:@"WILL CALL"]) {
  7826. [dic removeObjectForKey:@"lift_gate"];
  7827. }
  7828. return dic;
  7829. }
  7830. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7831. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7832. orderCode = [self translateSingleQuote:orderCode];
  7833. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7834. __block int submit_as_integer = 0;
  7835. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7836. submit_as_integer = sqlite3_column_int(stmt, 0);
  7837. }];
  7838. int check11 = 0;
  7839. int check10 = 0;
  7840. if (submit_as_integer == 11) {
  7841. check11 = 1;
  7842. }
  7843. if (submit_as_integer == 10) {
  7844. check10 = 1;
  7845. }
  7846. // section_0
  7847. NSMutableDictionary *dic = @{
  7848. @"count" : @(1),
  7849. @"item_0" : @{
  7850. @"aname" : @"Submit Order As",
  7851. @"cadedate" : @{
  7852. @"count" : @(2),
  7853. @"val_0" : @{
  7854. @"check" : [NSNumber numberWithInteger:check11],
  7855. @"value" : @"Sales Order",
  7856. @"value_id" : @(11)
  7857. },
  7858. @"val_1" : @{
  7859. @"check" : [NSNumber numberWithInteger:check10],
  7860. @"value" : @"Quote",
  7861. @"value_id" : @(10)
  7862. }
  7863. },
  7864. @"control" : @"enum",
  7865. @"name" : @"erpOrderStatus",
  7866. @"required" : @"true",
  7867. @"single_select" : @"true"
  7868. },
  7869. @"title" : @"Order Type"
  7870. }.mutableCopy;
  7871. // setting
  7872. NSDictionary *setting = params[@"setting"];
  7873. NSNumber *hide = setting[@"OrderTypeHide"];
  7874. [dic setValue:hide forKey:@"hide"];
  7875. return dic;
  7876. }
  7877. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7878. countryCode = [self translateSingleQuote:countryCode];
  7879. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7880. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7881. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7882. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7883. if (name == NULL) {
  7884. name = "";
  7885. }
  7886. if (code == NULL) {
  7887. code = "";
  7888. }
  7889. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7890. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7891. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7892. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7893. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7894. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7895. }
  7896. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7897. [container setValue:stateDic forKey:key];
  7898. }] mutableCopy];
  7899. [ret removeObjectForKey:@"result"];
  7900. // failure 可以不用了,一样的
  7901. if (ret.allKeys.count == 0) {
  7902. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7903. [stateDic setValue:@"Other" forKey:@"value"];
  7904. [stateDic setValue:@"" forKey:@"value_id"];
  7905. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7906. if (state_code && [@"" isEqualToString:state_code]) {
  7907. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7908. }
  7909. NSString *key = [NSString stringWithFormat:@"val_0"];
  7910. [ret setValue:stateDic forKey:key];
  7911. }
  7912. [ret setValue:[NSNumber numberWithLong:ret.allKeys.count] forKey:@"count"];
  7913. return ret;
  7914. }
  7915. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7916. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7917. orderCode = [self translateSingleQuote:orderCode];
  7918. 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];
  7919. __block NSString *payType = @"";
  7920. __block NSString *firstName = @"";
  7921. __block NSString *lastName = @"";
  7922. __block NSString *addr1 = @"";
  7923. __block NSString *addr2 = @"";
  7924. __block NSString *zipcode = @"";
  7925. __block NSString *cardType = @"";
  7926. __block NSString *cardNumber = @"";
  7927. __block NSString *securityCode = @"";
  7928. __block NSString *month = @"";
  7929. __block NSString *year = @"";
  7930. __block NSString *city = @"";
  7931. __block NSString *state = @"";
  7932. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7933. payType = [self textAtColumn:0 statement:stmt];
  7934. firstName = [self textAtColumn:1 statement:stmt];
  7935. lastName = [self textAtColumn:2 statement:stmt];
  7936. addr1 = [self textAtColumn:3 statement:stmt];
  7937. addr2 = [self textAtColumn:4 statement:stmt];
  7938. zipcode = [self textAtColumn:5 statement:stmt];
  7939. cardType = [self textAtColumn:6 statement:stmt];
  7940. cardNumber = [self textAtColumn:7 statement:stmt];
  7941. securityCode = [self textAtColumn:8 statement:stmt];
  7942. month = [self textAtColumn:9 statement:stmt];
  7943. year = [self textAtColumn:10 statement:stmt];
  7944. city = [self textAtColumn:11 statement:stmt];
  7945. state = [self textAtColumn:12 statement:stmt];
  7946. }];
  7947. NSString *required = @"true";
  7948. // setting
  7949. NSDictionary *setting = params[@"setting"];
  7950. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7951. if ([requiredNumber integerValue]) {
  7952. required = @"true";
  7953. } else {
  7954. required = @"false";
  7955. }
  7956. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7957. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7958. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7959. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7960. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7961. NSString *type = [self textAtColumn:1 statement:stmt];
  7962. NSMutableDictionary *val = @{
  7963. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7964. @"value" : type,
  7965. @"value_id" : type_id
  7966. }.mutableCopy;
  7967. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7968. NSDictionary *sub_item = @{
  7969. @"count" : @(3),
  7970. @"item_0" : @{
  7971. @"aname" : @"choose",
  7972. @"control" : @"multi_action",
  7973. @"count" : @(1),
  7974. @"item_0" : @{
  7975. @"aname" : @"Same as customer",
  7976. @"key_map" : @{
  7977. @"credit_card_address1" : @"customer_address1",
  7978. @"credit_card_address2" : @"customer_address2",
  7979. @"credit_card_city" : @"customer_city",
  7980. @"credit_card_first_name" : @"customer_first_name",
  7981. @"credit_card_last_name" : @"customer_last_name",
  7982. @"credit_card_state" : @"customer_state",
  7983. @"credit_card_zipcode" : @"customer_zipcode"
  7984. },
  7985. @"type" : @"pull"
  7986. }
  7987. },
  7988. @"item_1" : @{
  7989. @"aname" : @"",
  7990. @"color" : @"red",
  7991. @"control" : @"text",
  7992. @"name" : @"",
  7993. @"value" : @"USA Credit cards only"
  7994. },
  7995. @"item_2" : @{
  7996. @"aname" : @"Fill",
  7997. @"cadedate" : @{
  7998. @"count" : @(2),
  7999. @"val_0" : @{
  8000. @"check" : @(1),
  8001. @"sub_item" : @{
  8002. @"count" : @(11),
  8003. @"item_0" : @{
  8004. @"aname" : @"Type",
  8005. @"cadedate" : @{
  8006. @"count" : @(2),
  8007. @"val_0" : @{
  8008. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8009. @"value" : @"VISA",
  8010. @"value_id" : @"VISA"/*@(0)*/
  8011. },
  8012. @"val_1" : @{
  8013. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8014. @"value" : @"MASTER CARD",
  8015. @"value_id" : @"MASTER CARD"/*@(1)*/
  8016. }
  8017. },
  8018. @"control" : @"enum",
  8019. @"name" : @"credit_card_type",
  8020. @"required" : @"true",
  8021. @"single_select" : @"true"
  8022. },
  8023. @"item_1" : @{
  8024. @"aname" : @"Number",
  8025. @"control" : @"edit",
  8026. @"keyboard" : @"int",
  8027. @"length" : @"16",
  8028. @"name" : @"credit_card_number",
  8029. @"required" : @"true",
  8030. @"value" : cardNumber
  8031. },
  8032. @"item_10" : @{
  8033. @"aname" : @"State",
  8034. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8035. @"control" : @"enum",
  8036. @"enum" : @"true",
  8037. @"name" : @"credit_card_state",
  8038. @"required" : @"true",
  8039. @"single_select" : @"true"
  8040. },
  8041. @"item_2" : @{
  8042. @"aname" : @"Expiration Date",
  8043. @"control" : @"monthpicker",
  8044. @"name" : @"credit_card_expiration",
  8045. @"required" : @"true",
  8046. @"type" : @"date",
  8047. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8048. },
  8049. @"item_3" : @{
  8050. @"aname" : @"Security Code",
  8051. @"control" : @"edit",
  8052. @"keyboard" : @"int",
  8053. @"length" : @"3",
  8054. @"name" : @"credit_card_security_code",
  8055. @"required" : @"true",
  8056. @"value" : securityCode
  8057. },
  8058. @"item_4" : @{
  8059. @"aname" : @"First Name",
  8060. @"control" : @"edit",
  8061. @"keyboard" : @"text",
  8062. @"name" : @"credit_card_first_name",
  8063. @"required" : @"true",
  8064. @"value" : firstName
  8065. },
  8066. @"item_5" : @{
  8067. @"aname" : @"Last Name",
  8068. @"control" : @"edit",
  8069. @"keyboard" : @"text",
  8070. @"name" : @"credit_card_last_name",
  8071. @"required" : @"true",
  8072. @"value" : lastName
  8073. },
  8074. @"item_6" : @{
  8075. @"aname" : @"Address 1",
  8076. @"control" : @"edit",
  8077. @"keyboard" : @"text",
  8078. @"name" : @"credit_card_address1",
  8079. @"required" : @"true",
  8080. @"value" : addr1
  8081. },
  8082. @"item_7" : @{
  8083. @"aname" : @"Address 2",
  8084. @"control" : @"edit",
  8085. @"keyboard" : @"text",
  8086. @"name" : @"credit_card_address2",
  8087. @"value" : addr2
  8088. },
  8089. @"item_8" : @{
  8090. @"aname" : @"zip code",
  8091. @"control" : @"edit",
  8092. @"keyboard" : @"text",
  8093. @"name" : @"credit_card_zipcode",
  8094. @"required" : @"true",
  8095. @"value" : zipcode
  8096. },
  8097. @"item_9" : @{
  8098. @"aname" : @"City",
  8099. @"control" : @"edit",
  8100. @"keyboard" : @"text",
  8101. @"name" : @"credit_card_city",
  8102. @"required" : @"true",
  8103. @"value" : city
  8104. }
  8105. },
  8106. @"value" : @"Fill Now",
  8107. @"value_id" : @""
  8108. },
  8109. @"val_1" : @{
  8110. @"check" : @(0),
  8111. @"value" : @"Fill Later",
  8112. @"value_id" : @""
  8113. }
  8114. },
  8115. @"control" : @"enum",
  8116. @"name" : @"",
  8117. @"single_select" : @"true"
  8118. }
  8119. };
  8120. [val setObject:sub_item forKey:@"sub_item"];
  8121. }
  8122. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8123. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8124. }];
  8125. // "section_2"
  8126. NSMutableDictionary *dic = @{
  8127. @"count" : @(1),
  8128. @"item_0" : @{
  8129. @"aname" : @"Payment",
  8130. @"required" : required,
  8131. @"cadedate" : cadedate,
  8132. // @{
  8133. // @"count" : @(6),
  8134. // @"val_3" : @{
  8135. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8136. // @"value" : @"Check",
  8137. // @"value_id" : @"Check"
  8138. // },
  8139. // @"val_2" : @{
  8140. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8141. // @"value" : @"Cash",
  8142. // @"value_id" : @"Cash"
  8143. // },
  8144. // @"val_1" : @{
  8145. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8146. // @"value" : @"NET 60",
  8147. // @"value_id" : @"NET 30"
  8148. // },
  8149. // @"val_4" : @{
  8150. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8151. // @"value" : @"Wire Transfer",
  8152. // @"value_id" : @"Wire Transfer"
  8153. // },
  8154. // @"val_0" : @{
  8155. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8156. // @"sub_item" : @{
  8157. // @"count" : @(3),
  8158. // @"item_0" : @{
  8159. // @"aname" : @"choose",
  8160. // @"control" : @"multi_action",
  8161. // @"count" : @(1),
  8162. // @"item_0" : @{
  8163. // @"aname" : @"Same as customer",
  8164. // @"key_map" : @{
  8165. // @"credit_card_address1" : @"customer_address1",
  8166. // @"credit_card_address2" : @"customer_address2",
  8167. // @"credit_card_city" : @"customer_city",
  8168. // @"credit_card_first_name" : @"customer_first_name",
  8169. // @"credit_card_last_name" : @"customer_last_name",
  8170. // @"credit_card_state" : @"customer_state",
  8171. // @"credit_card_zipcode" : @"customer_zipcode"
  8172. // },
  8173. // @"type" : @"pull"
  8174. // }
  8175. // },
  8176. // @"item_1" : @{
  8177. // @"aname" : @"",
  8178. // @"color" : @"red",
  8179. // @"control" : @"text",
  8180. // @"name" : @"",
  8181. // @"value" : @"USA Credit cards only"
  8182. // },
  8183. // @"item_2" : @{
  8184. // @"aname" : @"Fill",
  8185. // @"cadedate" : @{
  8186. // @"count" : @(2),
  8187. // @"val_0" : @{
  8188. // @"check" : @(1),
  8189. // @"sub_item" : @{
  8190. // @"count" : @(11),
  8191. // @"item_0" : @{
  8192. // @"aname" : @"Type",
  8193. // @"cadedate" : @{
  8194. // @"count" : @(2),
  8195. // @"val_0" : @{
  8196. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8197. // @"value" : @"VISA",
  8198. // @"value_id" : @(0)
  8199. // },
  8200. // @"val_1" : @{
  8201. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8202. // @"value" : @"MASTER CARD",
  8203. // @"value_id" : @(1)
  8204. // }
  8205. // },
  8206. // @"control" : @"enum",
  8207. // @"name" : @"credit_card_type",
  8208. // @"required" : @"true",
  8209. // @"single_select" : @"true"
  8210. // },
  8211. // @"item_1" : @{
  8212. // @"aname" : @"Number",
  8213. // @"control" : @"edit",
  8214. // @"keyboard" : @"int",
  8215. // @"length" : @"16",
  8216. // @"name" : @"credit_card_number",
  8217. // @"required" : @"true",
  8218. // @"value" : cardNumber
  8219. // },
  8220. // @"item_10" : @{
  8221. // @"aname" : @"State",
  8222. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8223. // @"control" : @"enum",
  8224. // @"enum" : @"true",
  8225. // @"name" : @"credit_card_state",
  8226. // @"required" : @"true",
  8227. // @"single_select" : @"true"
  8228. // },
  8229. // @"item_2" : @{
  8230. // @"aname" : @"Expiration Date",
  8231. // @"control" : @"monthpicker",
  8232. // @"name" : @"credit_card_expiration",
  8233. // @"required" : @"true",
  8234. // @"type" : @"date",
  8235. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8236. // },
  8237. // @"item_3" : @{
  8238. // @"aname" : @"Security Code",
  8239. // @"control" : @"edit",
  8240. // @"keyboard" : @"int",
  8241. // @"length" : @"3",
  8242. // @"name" : @"credit_card_security_code",
  8243. // @"required" : @"true",
  8244. // @"value" : securityCode
  8245. // },
  8246. // @"item_4" : @{
  8247. // @"aname" : @"First Name",
  8248. // @"control" : @"edit",
  8249. // @"keyboard" : @"text",
  8250. // @"name" : @"credit_card_first_name",
  8251. // @"required" : @"true",
  8252. // @"value" : firstName
  8253. // },
  8254. // @"item_5" : @{
  8255. // @"aname" : @"Last Name",
  8256. // @"control" : @"edit",
  8257. // @"keyboard" : @"text",
  8258. // @"name" : @"credit_card_last_name",
  8259. // @"required" : @"true",
  8260. // @"value" : lastName
  8261. // },
  8262. // @"item_6" : @{
  8263. // @"aname" : @"Address 1",
  8264. // @"control" : @"edit",
  8265. // @"keyboard" : @"text",
  8266. // @"name" : @"credit_card_address1",
  8267. // @"required" : @"true",
  8268. // @"value" : addr1
  8269. // },
  8270. // @"item_7" : @{
  8271. // @"aname" : @"Address 2",
  8272. // @"control" : @"edit",
  8273. // @"keyboard" : @"text",
  8274. // @"name" : @"credit_card_address2",
  8275. // @"value" : addr2
  8276. // },
  8277. // @"item_8" : @{
  8278. // @"aname" : @"zip code",
  8279. // @"control" : @"edit",
  8280. // @"keyboard" : @"text",
  8281. // @"name" : @"credit_card_zipcode",
  8282. // @"required" : @"true",
  8283. // @"value" : zipcode
  8284. // },
  8285. // @"item_9" : @{
  8286. // @"aname" : @"City",
  8287. // @"control" : @"edit",
  8288. // @"keyboard" : @"text",
  8289. // @"name" : @"credit_card_city",
  8290. // @"required" : @"true",
  8291. // @"value" : city
  8292. // }
  8293. // },
  8294. // @"value" : @"Fill Now",
  8295. // @"value_id" : @""
  8296. // },
  8297. // @"val_1" : @{
  8298. // @"check" : @(0),
  8299. // @"value" : @"Fill Later",
  8300. // @"value_id" : @""
  8301. // }
  8302. // },
  8303. // @"control" : @"enum",
  8304. // @"name" : @"",
  8305. // @"single_select" : @"true"
  8306. // }
  8307. // },
  8308. // @"value" : @"Visa/Master",
  8309. // @"value_id" : @"Credit Card"
  8310. // },
  8311. // @"val_5" : @{
  8312. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8313. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8314. // @"value_id" : @"FOLLOW EXISTING"
  8315. // }
  8316. // },
  8317. @"control" : @"enum",
  8318. @"name" : @"paymentType",
  8319. @"single_select" : @"true"
  8320. },
  8321. @"title" : @"Payment Information"
  8322. }.mutableCopy;
  8323. NSNumber *hide = setting[@"PaymentInformationHide"];
  8324. [dic setValue:hide forKey:@"hide"];
  8325. return dic;
  8326. }
  8327. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8328. // params
  8329. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8330. orderCode = [self translateSingleQuote:orderCode];
  8331. // 缺货检查
  8332. 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];
  8333. __block BOOL outOfStock = NO;
  8334. sqlite3 *database = db;
  8335. if (!db) {
  8336. database = [iSalesDB get_db];
  8337. }
  8338. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8339. int availability = sqlite3_column_int(stmt, 0);
  8340. int item_qty = sqlite3_column_int(stmt, 1);
  8341. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8342. }];
  8343. if (!db) {
  8344. [iSalesDB close_db:database];
  8345. }
  8346. return outOfStock;
  8347. }
  8348. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8349. {
  8350. assert(params[@"user"]!=nil);
  8351. assert(params[@"contact_id"]!=nil);
  8352. assert(params[@"can_create_backorder"]!=nil);
  8353. sqlite3 *db = [iSalesDB get_db];
  8354. // params
  8355. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8356. orderCode = [self translateSingleQuote:orderCode];
  8357. // 缺货检查
  8358. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8359. if (![params[@"can_create_backorder"] boolValue]) {
  8360. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8361. if (out_of_stock) {
  8362. [iSalesDB close_db:db];
  8363. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8364. [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"];
  8365. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8366. return [RAConvertor dict2data:resultDic];
  8367. }
  8368. }
  8369. // UISetting
  8370. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8371. NSString *cachefolder = [paths objectAtIndex:0];
  8372. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8373. NSData* json =nil;
  8374. json=[NSData dataWithContentsOfFile:img_cache];
  8375. NSError *error=nil;
  8376. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8377. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8378. [params setObject:setting forKey:@"setting"];
  8379. int section_count = 0;
  8380. // 0 Order Type 1 Shipping Method 2 Payment Information
  8381. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8382. // 0 Order Type
  8383. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8384. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8385. [ret setValue:order_type_dic forKey:key0];
  8386. // 1 Shipping Method
  8387. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8388. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8389. [ret setValue:shipping_method_dic forKey:key1];
  8390. // 2 Payment Information
  8391. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8392. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8393. [ret setValue:payment_info_dic forKey:key2];
  8394. // 3 Customer
  8395. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8396. NSDictionary *customer_dic = [self customerDic:params db:db];
  8397. [ret setValue:customer_dic forKey:key3];
  8398. // 4 Ship To
  8399. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8400. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8401. [ret setValue:ship_to_dic forKey:key4];
  8402. // 5 Ship From
  8403. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8404. if (![shipFromDisable integerValue]) {
  8405. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8406. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8407. [ret setValue:ship_from_dic forKey:key5];
  8408. }
  8409. // 6 Freight Bill To
  8410. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8411. if (![freightBillToDisable integerValue]) {
  8412. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8413. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8414. [ret setValue:freight_bill_to forKey:key6];
  8415. }
  8416. // 7 Merchandise Bill To
  8417. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8418. if (![merchandiseBillToDisable integerValue]) {
  8419. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8420. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8421. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8422. }
  8423. // 8 Return To
  8424. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8425. if (![returnToDisable integerValue]) {
  8426. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8427. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8428. [ret setValue:return_to_dic forKey:key8];
  8429. }
  8430. // 9 Model Information
  8431. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8432. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8433. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8434. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8435. [ret setValue:model_info_dic forKey:key9];
  8436. // 10 Remarks Content
  8437. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8438. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8439. [ret setValue:remarks_content_dic forKey:key10];
  8440. // 11 Order Total
  8441. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8442. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8443. [ret setValue:order_total_dic forKey:key11];
  8444. // 12 Signature
  8445. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8446. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8447. [ret setValue:sign_dic forKey:key12];
  8448. [ret setValue:@(section_count) forKey:@"section_count"];
  8449. [iSalesDB close_db:db];
  8450. return [RAConvertor dict2data:ret];
  8451. // return nil;
  8452. }
  8453. #pragma mark addr editor
  8454. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8455. NSMutableDictionary *new_item = [item mutableCopy];
  8456. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8457. return new_item;
  8458. }
  8459. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8460. {
  8461. // "is_subaction" = true;
  8462. // orderCode = MOB1608240002;
  8463. // password = 123456;
  8464. // "subaction_tag" = 1;
  8465. // user = EvanK;
  8466. // {
  8467. // "is_subaction" = true;
  8468. // orderCode = MOB1608240002;
  8469. // password = 123456;
  8470. // "refresh_trigger" = zipcode;
  8471. // "subaction_tag" = 1;
  8472. // user = EvanK;
  8473. // }
  8474. NSString *country_code = nil;
  8475. NSString *zipCode = nil;
  8476. NSString *stateCode = nil;
  8477. NSString *city = nil;
  8478. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8479. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8480. NSString *code_id = params[@"country"];
  8481. country_code = [self countryCodeByid:code_id];
  8482. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8483. NSString *zip_code = params[@"zipcode"];
  8484. // 剔除全部为空格
  8485. int spaceCount = 0;
  8486. for (int i = 0; i < zip_code.length; i++) {
  8487. if ([zip_code characterAtIndex:i] == ' ') {
  8488. spaceCount++;
  8489. }
  8490. }
  8491. if (spaceCount == zip_code.length) {
  8492. zip_code = @"";
  8493. }
  8494. zipCode = zip_code;
  8495. if (zipCode.length > 0) {
  8496. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8497. country_code = [dic valueForKey:@"country_code"];
  8498. if (!country_code) {
  8499. // country_code = @"US";
  8500. NSString *code_id = params[@"country"];
  8501. country_code = [self countryCodeByid:code_id];
  8502. }
  8503. stateCode = [dic valueForKey:@"state_code"];
  8504. if(!stateCode.length) {
  8505. stateCode = params[@"state"];
  8506. }
  8507. city = [dic valueForKey:@"city"];
  8508. if (!city.length) {
  8509. city = params[@"city"];
  8510. }
  8511. // zip code
  8512. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8513. // [zipDic setValue:zipCode forKey:@"value"];
  8514. // [section_0 setValue:zipDic forKey:@"item_11"];
  8515. } else {
  8516. NSString *code_id = params[@"country"];
  8517. country_code = [self countryCodeByid:code_id];
  8518. stateCode = params[@"state"];
  8519. city = params[@"city"];
  8520. }
  8521. }
  8522. }
  8523. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8524. // [ret removeObjectForKey:@"up_params"];
  8525. [ret setObject:@"New Address" forKey:@"title"];
  8526. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8527. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8528. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8529. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8530. // [country_dic removeObjectForKey:@"refresh"];
  8531. // [country_dic removeObjectForKey:@"restore"];
  8532. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8533. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8534. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8535. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8536. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8537. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8538. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8539. // [zip_code_dic removeObjectForKey:@"refresh"];
  8540. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8541. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8542. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8543. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8544. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8545. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8546. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8547. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8548. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8549. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8550. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8551. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8552. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8553. // country
  8554. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8555. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8556. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8557. // state
  8558. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8559. // NSDictionary *tmpDic = @{
  8560. // @"value" : @"Other",
  8561. // @"value_id" : @"",
  8562. // @"check" : [NSNumber numberWithInteger:0]
  8563. // };
  8564. //
  8565. // for (int i = 0; i < allState.allKeys.count; i++) {
  8566. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8567. //
  8568. // NSDictionary *tmp = allState[key];
  8569. // [allState setValue:tmpDic forKey:key];
  8570. // tmpDic = tmp;
  8571. // }
  8572. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8573. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8574. [ret setValue:new_section_0 forKey:@"section_0"];
  8575. return [RAConvertor dict2data:ret];
  8576. }
  8577. #pragma mark save addr
  8578. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8579. {
  8580. // NSString *companyName = [self valueInParams:params key:@"company"];
  8581. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8582. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8583. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8584. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8585. // NSString *countryId = [self valueInParams:params key:@"country"];
  8586. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8587. // NSString *city = [self valueInParams:params key:@"city"];
  8588. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8589. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8590. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8591. // NSString *phone = [self valueInParams:params key:@"phone"];
  8592. // NSString *fax = [self valueInParams:params key:@"fax"];
  8593. // NSString *email = [self valueInParams:params key:@"email"];
  8594. return [self offline_saveContact:params update:NO isCustomer:NO];
  8595. }
  8596. #pragma mark cancel order
  8597. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8598. {
  8599. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8600. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8601. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8602. if (order_id.length) {
  8603. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8604. }
  8605. int ret = [iSalesDB execSql:sql];
  8606. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8607. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8608. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8609. // [dic setValue:@"160409" forKey:@"min_ver"];
  8610. return [RAConvertor dict2data:dic];
  8611. }
  8612. #pragma mark sign order
  8613. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8614. {
  8615. //参考 offline_saveBusinesscard
  8616. DebugLog(@"sign order params: %@",params);
  8617. // orderCode = MOB1608240002;
  8618. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8619. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8620. orderCode = [self translateSingleQuote:orderCode];
  8621. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8622. picPath = [self translateSingleQuote:picPath];
  8623. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8624. int ret = [iSalesDB execSql:sql];
  8625. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8626. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8627. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8628. // [dic setValue:@"160409" forKey:@"min_ver"];
  8629. return [RAConvertor dict2data:dic];
  8630. }
  8631. #pragma mark save order
  8632. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8633. NSString *ret = [self valueInParams:params key:key];
  8634. if (translate) {
  8635. ret = [self translateSingleQuote:ret];
  8636. }
  8637. return ret;
  8638. }
  8639. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8640. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8641. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8642. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8643. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8644. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8645. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8646. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8647. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8648. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8649. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8650. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8651. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8652. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8653. if (![total_price isEqualToString:@""]) {
  8654. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8655. } else {
  8656. total_price = @"";
  8657. }
  8658. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8659. if ([paymentsAndCredits isEqualToString:@""]) {
  8660. paymentsAndCredits = @"";
  8661. } else {
  8662. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8663. }
  8664. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8665. if ([handling_fee_value isEqualToString:@""]) {
  8666. handling_fee_value = @"";
  8667. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8668. handling_fee_value = @"";
  8669. } else {
  8670. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8671. }
  8672. NSString *handling_fee_placeholder = handling_fee_value;
  8673. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8674. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8675. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8676. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8677. NSString *lift_gate_placeholder = @"";
  8678. if ([lift_gate_value isEqualToString:@""]) {
  8679. lift_gate_placeholder = @"";
  8680. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8681. lift_gate_placeholder = @"";
  8682. } else {
  8683. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8684. }
  8685. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8686. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8687. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8688. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8689. 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];
  8690. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8691. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8692. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8693. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8694. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8695. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8696. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8697. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8698. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8699. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8700. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8701. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8702. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8703. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8704. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8705. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8706. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8707. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8708. if (!number_nil) {
  8709. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8710. }
  8711. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8712. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8713. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8714. if (!security_code_nil) {
  8715. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8716. }
  8717. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8718. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8719. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8720. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8721. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8722. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8723. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8724. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8725. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8726. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8727. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8728. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8729. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8730. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8731. NSString *contact_id = customer_cid;
  8732. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8733. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8734. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8735. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8736. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8737. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8738. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8739. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8740. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8741. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8742. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8743. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8744. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8745. //
  8746. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8747. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8748. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8749. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8750. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8751. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8752. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8753. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8754. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8755. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8756. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8757. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8758. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8759. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8760. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8761. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8762. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8763. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8764. 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];
  8765. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8766. if (receive_cid.length) {
  8767. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8768. }
  8769. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8770. if (receive_name.length) {
  8771. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8772. }
  8773. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8774. if (receive_ext.length) {
  8775. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8776. }
  8777. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8778. if (receive_contact.length) {
  8779. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8780. }
  8781. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8782. if (receive_phone.length) {
  8783. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8784. }
  8785. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8786. if (receive_email.length) {
  8787. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8788. }
  8789. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8790. if (receive_fax.length) {
  8791. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8792. }
  8793. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8794. if (sender_cid.length) {
  8795. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8796. }
  8797. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8798. if (sender_name.length) {
  8799. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8800. }
  8801. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8802. if (sender_ext.length) {
  8803. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8804. }
  8805. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8806. if(sender_contact.length) {
  8807. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8808. }
  8809. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8810. if (sender_phone.length) {
  8811. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8812. }
  8813. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8814. if (sender_fax.length) {
  8815. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8816. }
  8817. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8818. if (sender_email.length) {
  8819. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8820. }
  8821. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8822. if (shipping_billto_cid.length) {
  8823. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8824. }
  8825. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8826. if (shipping_billto_name.length) {
  8827. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8828. }
  8829. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8830. if (shipping_billto_ext.length) {
  8831. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8832. }
  8833. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8834. if (shipping_billto_contact.length) {
  8835. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8836. }
  8837. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8838. if (shipping_billto_phone.length) {
  8839. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8840. }
  8841. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8842. if (shipping_billto_fax.length) {
  8843. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8844. }
  8845. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8846. if (shipping_billto_email.length) {
  8847. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8848. }
  8849. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8850. if (billing_cid.length) {
  8851. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8852. }
  8853. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8854. if (billing_name.length) {
  8855. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8856. }
  8857. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8858. if (billing_ext.length) {
  8859. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8860. }
  8861. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8862. if (billing_contact.length) {
  8863. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8864. }
  8865. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8866. if (billing_phone.length) {
  8867. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8868. }
  8869. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8870. if (billing_fax.length) {
  8871. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8872. }
  8873. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8874. if (billing_email.length) {
  8875. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8876. }
  8877. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8878. if (returnto_cid.length) {
  8879. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8880. }
  8881. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8882. if (returnto_name.length) {
  8883. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8884. }
  8885. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8886. if (returnto_ext.length) {
  8887. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8888. }
  8889. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8890. if (returnto_contact.length) {
  8891. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8892. }
  8893. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8894. if (returnto_phone.length) {
  8895. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8896. }
  8897. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8898. if (returnto_fax.length) {
  8899. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8900. }
  8901. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8902. if (returnto_email.length) {
  8903. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8904. }
  8905. NSString *order_status = @"status = 1,";
  8906. if (submit) {
  8907. order_status = @"status = -11,";
  8908. }
  8909. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8910. NSString *sync_sql = @"";
  8911. if (submit) {
  8912. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8913. [param setValue:sales_rep forKey:@"sales_rep"];
  8914. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  8915. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8916. }
  8917. 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];
  8918. DebugLog(@"save order contactSql: %@",contactSql);
  8919. DebugLog(@"save order orderSql: %@",orderSql);
  8920. // int contact_ret = [iSalesDB execSql:contactSql];
  8921. int order_ret = [iSalesDB execSql:orderSql];
  8922. int ret = order_ret;
  8923. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8924. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8925. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8926. // [dic setValue:@"160409" forKey:@"min_ver"];
  8927. [dic setObject:so_id forKey:@"so#"];
  8928. return [RAConvertor dict2data:dic];
  8929. }
  8930. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8931. {
  8932. // id foo = nil;
  8933. // NSMutableArray *a = @[].mutableCopy;
  8934. // [a addObject:foo];
  8935. return [self saveorder:param submit:NO];
  8936. }
  8937. #pragma mark add to cart by name
  8938. +(NSData*) offline_add2cartbyupc :(NSMutableDictionary *) params
  8939. {
  8940. NSString *orderCode = [params objectForKey:@"orderCode"];
  8941. NSString *upccode = [params objectForKey:@"upc_code"];
  8942. // product_name = [self translateSingleQuote:product_name];
  8943. // NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8944. // bool search_upc = [params[@"search_upc"] boolValue];
  8945. sqlite3 *db = [iSalesDB get_db];
  8946. __block NSMutableString *product_id_string = [NSMutableString string];
  8947. // NSString *name = [product_name_array objectAtIndex:i];
  8948. NSString *sql =nil;
  8949. sql= [NSString stringWithFormat:@"select product_id from product where upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upccode,upccode,upccode,upccode];
  8950. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8951. int product_id = sqlite3_column_int(stmt, 0);
  8952. [product_id_string appendFormat:@"%d",product_id];
  8953. }];
  8954. if (!orderCode) {
  8955. orderCode = @"";
  8956. }
  8957. NSDictionary *newParams = @{
  8958. @"product_id" : product_id_string,
  8959. @"orderCode" : orderCode,
  8960. @"can_create_backorder":params[@"can_create_backorder"]
  8961. };
  8962. [iSalesDB close_db:db];
  8963. return [self offline_add2cart:[newParams mutableCopy]];
  8964. }
  8965. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8966. {
  8967. NSString *orderCode = [params objectForKey:@"orderCode"];
  8968. NSString *product_name = [params objectForKey:@"product_name"];
  8969. product_name = [self translateSingleQuote:product_name];
  8970. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8971. bool search_upc = [params[@"search_upc"] boolValue];
  8972. sqlite3 *db = [iSalesDB get_db];
  8973. __block NSMutableString *product_id_string = [NSMutableString string];
  8974. for (int i = 0; i < product_name_array.count; i++) {
  8975. NSString *name = [product_name_array objectAtIndex:i];
  8976. NSString *sql =nil;
  8977. if(search_upc)
  8978. 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];
  8979. else
  8980. sql= [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8981. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8982. int product_id = sqlite3_column_int(stmt, 0);
  8983. if (i == product_name_array.count - 1) {
  8984. [product_id_string appendFormat:@"%d",product_id];
  8985. } else {
  8986. [product_id_string appendFormat:@"%d,",product_id];
  8987. }
  8988. }];
  8989. }
  8990. if (!orderCode) {
  8991. orderCode = @"";
  8992. }
  8993. NSDictionary *newParams = @{
  8994. @"product_id" : product_id_string,
  8995. @"orderCode" : orderCode,
  8996. @"can_create_backorder":params[@"can_create_backorder"]
  8997. };
  8998. [iSalesDB close_db:db];
  8999. return [self offline_add2cart:[newParams mutableCopy]];
  9000. }
  9001. #pragma mark reset order
  9002. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  9003. {
  9004. // UIApplication * app = [UIApplication sharedApplication];
  9005. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  9006. [iSalesDB disable_trigger];
  9007. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  9008. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  9009. [iSalesDB enable_trigger];
  9010. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9011. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  9012. return [RAConvertor dict2data:dic];
  9013. }
  9014. #pragma mark submit order
  9015. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  9016. {
  9017. return [self saveorder:params submit:YES];
  9018. }
  9019. #pragma mark copy order
  9020. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  9021. {
  9022. NSMutableDictionary *ret = @{}.mutableCopy;
  9023. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  9024. sqlite3 *db = [iSalesDB get_db];
  9025. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  9026. // 首先查看联系人是否active
  9027. 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];
  9028. __block int customer_is_active = 1;
  9029. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9030. customer_is_active = sqlite3_column_int(stmt, 0);
  9031. }];
  9032. if (!customer_is_active) {
  9033. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9034. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9035. [iSalesDB close_db:db];
  9036. return [RAConvertor dict2data:ret];
  9037. }
  9038. // new order
  9039. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  9040. NSString *new_order_code = [self get_offline_soid:db];
  9041. NSString *user =params[@"user"];// ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  9042. user = [self translateSingleQuote:user];
  9043. 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
  9044. __block int result = 1;
  9045. result = [iSalesDB execSql:insert_order_sql db:db];
  9046. if (!result) {
  9047. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9048. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9049. [iSalesDB close_db:db];
  9050. return [RAConvertor dict2data:ret];
  9051. }
  9052. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  9053. // __block NSString *product_id = @"";
  9054. __weak typeof(self) weakSelf = self;
  9055. 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];
  9056. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9057. int is_active = sqlite3_column_int(stmt, 1);
  9058. if (is_active) {
  9059. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  9060. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  9061. 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];
  9062. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  9063. }
  9064. }];
  9065. // product_id = [product_id substringFromIndex:1];
  9066. //
  9067. // [self offline_add2cart:@{}.mutableCopy];
  9068. [iSalesDB execSql:@"END TRANSACTION" db:db];
  9069. [iSalesDB close_db:db];
  9070. if (result) {
  9071. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9072. } else {
  9073. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9074. }
  9075. [ret setObject:new_order_code forKey:@"so_id"];
  9076. return [RAConvertor dict2data:ret];
  9077. }
  9078. #pragma mark move wish list to cart
  9079. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  9080. assert(params[@"can_create_backorder"]!=nil);
  9081. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  9082. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9083. NSString *collectId = params[@"collectId"];
  9084. 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];
  9085. __block NSString *product_id = @"";
  9086. __block NSString *qty = @"";
  9087. __block int number_of_outOfStock = 0;
  9088. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9089. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9090. int productId = sqlite3_column_int(stmt, 0);
  9091. int item_qty = sqlite3_column_int(stmt, 1);
  9092. int availability = sqlite3_column_int(stmt, 2);
  9093. int _id = sqlite3_column_int(stmt, 3);
  9094. if (![params[@"can_create_backorder"] boolValue]) {
  9095. // 库存小于购买量为缺货
  9096. if (availability >= item_qty) {
  9097. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9098. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9099. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9100. } else {
  9101. number_of_outOfStock++;
  9102. }
  9103. } else {
  9104. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9105. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9106. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9107. }
  9108. }];
  9109. NSMutableDictionary *retDic = nil;
  9110. if (![params[@"can_create_backorder"] boolValue]) {
  9111. if (delete_collectId.count == 0) {
  9112. retDic = [NSMutableDictionary dictionary];
  9113. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9114. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9115. return [RAConvertor dict2data:retDic];
  9116. }
  9117. }
  9118. if (product_id.length > 1) {
  9119. product_id = [product_id substringFromIndex:1];
  9120. }
  9121. if (qty.length > 1) {
  9122. qty = [qty substringFromIndex:1];
  9123. }
  9124. NSString *orderCode = params[@"orderCode"];
  9125. if (!orderCode) {
  9126. orderCode = @"";
  9127. }
  9128. NSDictionary *newParams = @{
  9129. @"product_id" : product_id,
  9130. @"orderCode" : orderCode,
  9131. @"qty" : qty,
  9132. @"can_create_backorder":params[@"can_create_backorder"]
  9133. };
  9134. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9135. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9136. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9137. NSMutableDictionary* wish_return = [[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] mutableCopy];
  9138. NSInteger ret = [wish_return[@"result"] intValue];
  9139. retDic[@"wish_count"]=wish_return[@"wish_count"];
  9140. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9141. }
  9142. if (![params[@"can_create_backorder"] boolValue]) {
  9143. if (number_of_outOfStock > 0) {
  9144. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9145. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9146. }
  9147. }
  9148. return [RAConvertor dict2data:retDic];
  9149. }
  9150. #pragma mark - portfolio
  9151. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9152. // assert(params[@"contact_id"]!=nil);
  9153. assert(params[@"user"]!=nil);
  9154. assert(params[@"can_see_price"]!=nil);
  9155. int sort = [[params valueForKey:@"sort"] intValue];
  9156. int offset = [[params valueForKey:@"offset"] intValue];
  9157. int limit = [[params valueForKey:@"limit"] intValue];
  9158. NSString *orderBy = @"";
  9159. switch (sort) {
  9160. case 0:{
  9161. orderBy = @"p.modify_time desc";
  9162. }
  9163. break;
  9164. case 1:{
  9165. orderBy = @"modify_time asc";
  9166. }
  9167. break;
  9168. case 2:{
  9169. orderBy = @"p.name asc";
  9170. }
  9171. break;
  9172. case 3:{
  9173. orderBy = @"p.name desc";
  9174. }
  9175. break;
  9176. case 4:{
  9177. orderBy = @"p.description asc";
  9178. }
  9179. break;
  9180. case 5: {
  9181. orderBy = @"m.default_category asc";
  9182. }
  9183. default:
  9184. break;
  9185. }
  9186. // 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];
  9187. 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];
  9188. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9189. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9190. sqlite3 *db = [iSalesDB get_db];
  9191. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9192. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9193. int product_id = sqlite3_column_int(stmt, 0);
  9194. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9195. NSString *name = [self textAtColumn:1 statement:stmt];
  9196. NSString *description = [self textAtColumn:2 statement:stmt];
  9197. double price = sqlite3_column_double(stmt, 3);
  9198. double discount = sqlite3_column_double(stmt,4);
  9199. int qty = sqlite3_column_int(stmt, 5);
  9200. int percentage = sqlite3_column_int(stmt, 6);
  9201. int item_id = sqlite3_column_int(stmt, 7);
  9202. // int fashion_id = sqlite3_column_int(stmt, 8);
  9203. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9204. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9205. double percent = sqlite3_column_double(stmt, 11);
  9206. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9207. if ([price_null isEqualToString:@"null"]) {
  9208. price = [[self get_model_default_price:params[@"contact_id"] user:params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9209. }
  9210. NSMutableDictionary *item = @{
  9211. @"linenotes": line_note,
  9212. @"check": @(1),
  9213. @"product_id": product_id_string,
  9214. @"available_qty": @(qty),
  9215. @"available_percent" : @(percent),
  9216. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9217. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9218. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9219. @"img": img_path,
  9220. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9221. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9222. }.mutableCopy;
  9223. if (percentage) {
  9224. [item removeObjectForKey:@"available_qty"];
  9225. } else {
  9226. [item removeObjectForKey:@"available_percent"];
  9227. }
  9228. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9229. if ([qty_null isEqualToString:@"null"]) {
  9230. [item removeObjectForKey:@"available_qty"];
  9231. }
  9232. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9233. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9234. }];
  9235. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9236. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9237. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9238. }
  9239. [iSalesDB close_db:db];
  9240. [dic setValue:[NSNumber numberWithBool:[params[@"can_see_price"] boolValue]] forKey:@"can_see_price"];
  9241. [dic setValue:@"" forKey:@"email_content"];
  9242. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9243. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9244. return [RAConvertor dict2data:dic];
  9245. }
  9246. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9247. __block int qty = 0;
  9248. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9249. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9250. qty = sqlite3_column_int(stmt, 0);
  9251. }];
  9252. return qty;
  9253. }
  9254. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct contact_id:(NSString* ) contact_id
  9255. {
  9256. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9257. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9258. // NSMutableDictionary * values = params[@"replaceValue"];
  9259. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9260. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9261. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9262. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9263. NSString *pdf_path = @"";
  9264. if (direct) {
  9265. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9266. } else {
  9267. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9268. }
  9269. NSString *create_user = [self valueInParams:params key:@"user"];
  9270. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9271. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9272. // model info
  9273. // 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];
  9274. // V1.90 more color
  9275. 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];
  9276. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9277. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9278. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9279. sqlite3 *db = [iSalesDB get_db];
  9280. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9281. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9282. int product_id = sqlite3_column_int(stmt, 0);
  9283. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9284. double price = sqlite3_column_double(stmt, 1);
  9285. double discount = sqlite3_column_double(stmt,2);
  9286. int qty = sqlite3_column_int(stmt, 3);
  9287. int percentage = sqlite3_column_int(stmt, 4);
  9288. int item_id = sqlite3_column_int(stmt, 5);
  9289. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9290. double percent = sqlite3_column_double(stmt, 7);
  9291. int more_color = sqlite3_column_int(stmt, 8);
  9292. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9293. /* if ([price_null isEqualToString:@"null"]) {
  9294. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9295. }
  9296. */
  9297. [product_ids_string appendFormat:@"%@,",product_id_string];
  9298. // Regular Price
  9299. int regular_price = [[params objectForKey:@"regular_price"] intValue];
  9300. NSString *regular_price_str = [self get_portfolio_price:contact_id item_id:item_id price:regular_price db:db];
  9301. // QTY
  9302. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9303. if ([qty_null isEqualToString:@"null"]) {
  9304. // 查available
  9305. qty = [self model_QTY:product_id_string db:db];
  9306. }
  9307. if (percentage) {
  9308. qty = qty * percent / 100;
  9309. }
  9310. // Special Price
  9311. if ([price_null isEqualToString:@"null"]) {
  9312. // price = regular price
  9313. price = [regular_price_str doubleValue];
  9314. }
  9315. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9316. if (![discount_null isEqualToString:@"null"]) {
  9317. price = price * (1 - discount / 100.0);
  9318. }
  9319. NSMutableDictionary *item = @{
  9320. @"line_note": line_note,
  9321. @"product_id": product_id_string,
  9322. @"available_qty": @(qty),
  9323. @"more_color":@(more_color),
  9324. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9325. @"regular_price" : regular_price_str,
  9326. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9327. }.mutableCopy;
  9328. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9329. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9330. }];
  9331. [iSalesDB close_db:db];
  9332. if (product_ids_string.length > 0) {
  9333. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9334. }
  9335. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9336. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9337. return [RAConvertor dict2data:resultDictionary];
  9338. }
  9339. NSString *model_info = [RAConvertor dict2string:dic];
  9340. // 创建PDF
  9341. // 在preview情况下保存,则不需要新建了
  9342. if (direct) {
  9343. NSMutableDictionary *tear_sheet_params = params;
  9344. // if (tear_sheet_id) {
  9345. // tear_sheet_params = values;
  9346. // }
  9347. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9348. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9349. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9350. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9351. resultDictionary = pdfInfo.mutableCopy;
  9352. } else { // 创建PDF失败
  9353. return pdfData;
  9354. }
  9355. } else {
  9356. // pdf_path 就是本地路径
  9357. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9358. }
  9359. // 将文件移动到PDF Cache文件夹
  9360. NSString *newPath = [pdf_path lastPathComponent];
  9361. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9362. NSString *cachefolder = [paths objectAtIndex:0];
  9363. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9364. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9365. NSFileManager *fileManager = [NSFileManager defaultManager];
  9366. NSError *error = nil;
  9367. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9368. if (error) { // 移动文件失败
  9369. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9370. return [RAConvertor dict2data:resultDictionary];
  9371. }
  9372. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9373. pdf_path = [newPath lastPathComponent];
  9374. // 保存信息
  9375. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9376. NSString *off_params = [RAConvertor dict2string:params];
  9377. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9378. pdf_path = [self translateSingleQuote:pdf_path];
  9379. create_user = [self translateSingleQuote:create_user];
  9380. tear_note = [self translateSingleQuote:tear_note];
  9381. tear_name = [self translateSingleQuote:tear_name];
  9382. model_info = [self translateSingleQuote:model_info];
  9383. configureParams = [self translateSingleQuote:configureParams];
  9384. off_params = [self translateSingleQuote:off_params];
  9385. 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];
  9386. if (tear_sheet_id) {
  9387. int _id = [tear_sheet_id intValue];
  9388. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9389. }
  9390. int result = [iSalesDB execSql:save_pdf_sql];
  9391. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9392. //
  9393. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9394. if (remove_Item) {
  9395. // portfolioId
  9396. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9397. }
  9398. return [RAConvertor dict2data:resultDictionary];
  9399. }
  9400. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9401. return [self offline_savePDF:params direct:YES contact_id:params[@"contact_id"]];
  9402. }
  9403. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9404. return [self offline_savePDF:params direct:NO contact_id:params[@"contact_id"]];
  9405. }
  9406. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9407. int offset = [[params valueForKey:@"offset"] intValue];
  9408. int limit = [[params valueForKey:@"limit"] intValue];
  9409. NSString *keyword = [params valueForKey:@"keyWord"];
  9410. NSString *where = @"where is_delete is null or is_delete = 0";
  9411. if (keyword.length) {
  9412. keyword = [self translateSingleQuote:keyword];
  9413. 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];
  9414. }
  9415. 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
  9416. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9417. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9418. NSString *cachefolder = [paths objectAtIndex:0];
  9419. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9420. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9421. NSString *name = [self textAtColumn:0 statement:stmt];
  9422. NSString *note = [self textAtColumn:1 statement:stmt];
  9423. NSString *time = [self textAtColumn:2 statement:stmt];
  9424. NSString *user = [self textAtColumn:3 statement:stmt];
  9425. NSString *path = [self textAtColumn:4 statement:stmt];
  9426. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9427. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9428. path = [pdfFolder stringByAppendingPathComponent:path];
  9429. BOOL bdir=NO;
  9430. NSFileManager* fileManager = [NSFileManager defaultManager];
  9431. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9432. {
  9433. //pdf文件不存在
  9434. path=nil;
  9435. }
  9436. time = [self changeDateTimeFormate:time];
  9437. time = [time stringByAppendingString:@" PST"];
  9438. int sheet_id = sqlite3_column_int(stmt, 5);
  9439. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9440. item[@"tearsheetsId"]=@(sheet_id);
  9441. item[@"pdf_path"]=path;
  9442. item[@"create_time"]=time;
  9443. item[@"create_user"]=user;
  9444. item[@"tear_note"]=note;
  9445. item[@"tear_name"]=name;
  9446. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9447. item[@"model_info"]=model_info;
  9448. item[@"off_params"]=off_params;
  9449. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9450. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9451. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9452. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9453. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9454. }];
  9455. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9456. return [RAConvertor dict2data:dic];
  9457. }
  9458. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9459. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9460. // NSString *user = [params objectForKey:@"user"];
  9461. // 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];
  9462. 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];
  9463. int result = [iSalesDB execSql:sql];
  9464. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9465. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9466. if (result == RESULT_TRUE) {
  9467. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9468. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9469. sqlite3 *db = [iSalesDB get_db];
  9470. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9471. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9472. [iSalesDB close_db:db];
  9473. }
  9474. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9475. return [RAConvertor dict2data:dic];
  9476. }
  9477. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9478. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9479. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9480. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9481. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9482. NSDictionary *company_item = @{
  9483. @"control": @"text",
  9484. @"keyboard": @"text",
  9485. @"name": @"company_name",
  9486. @"value": COMPANY_FULL_NAME,
  9487. @"aname": @"Company Name"
  9488. };
  9489. [section_0 setObject:company_item forKey:@"item_0"];
  9490. [dic setObject:section_0 forKey:@"section_0"];
  9491. // Regurlar Price
  9492. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9493. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9494. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9495. __block long val_count = 0;
  9496. NSString *sql = @"select name,type,order_by from price order by order_by";
  9497. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9498. NSString *name = [self textAtColumn:0 statement:stmt];
  9499. int type = sqlite3_column_int(stmt, 1);
  9500. int order_by = sqlite3_column_int(stmt, 2);
  9501. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9502. NSDictionary *price_dic = @{
  9503. @"value" : name,
  9504. @"value_id" : [NSNumber numberWithInteger:type],
  9505. @"check" : order_by == 0 ? @(1) : @(0)
  9506. };
  9507. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9508. val_count = ++(*count);
  9509. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9510. val_count = 0;
  9511. }];
  9512. [cadedate setObject:@{@"value" : @"None",
  9513. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9514. val_count++;
  9515. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9516. [price setObject:cadedate forKey:@"cadedate"];
  9517. [section1 setObject:price forKey:@"item_2"];
  9518. [dic setObject:section1 forKey:@"section_1"];
  9519. return [RAConvertor dict2data:dic];
  9520. }
  9521. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9522. NSString *product_id = [params objectForKey:@"fashionId"];
  9523. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9524. __block int result = RESULT_TRUE;
  9525. __block int qty = 0;
  9526. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9527. qty = sqlite3_column_int(stmt, 0);
  9528. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9529. result = RESULT_FALSE;
  9530. }];
  9531. NSMutableDictionary *dic = @{
  9532. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9533. @"mode" : @"Regular Mode",
  9534. @"quantity_available" : @(qty),
  9535. @"result" : @(result),
  9536. }.mutableCopy;
  9537. return [RAConvertor dict2data:dic];
  9538. }
  9539. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9540. NSString *item_ids = [params objectForKey:@"item_id"];
  9541. NSString *line_notes = [params objectForKey:@"notes"];
  9542. NSString *price_str = [params objectForKey:@"price"];
  9543. NSString *discount_str = [params objectForKey:@"discount"];
  9544. NSString *percent = [params objectForKey:@"available_percent"];
  9545. NSString *qty = [params objectForKey:@"available_qty"];
  9546. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9547. int dot = 0;
  9548. if (line_notes) {
  9549. line_notes = [self translateSingleQuote:line_notes];
  9550. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9551. sql = [sql stringByAppendingString:line_notes];
  9552. dot = 1;
  9553. } else {
  9554. line_notes = @"";
  9555. }
  9556. if (price_str) {
  9557. if (dot) {
  9558. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9559. } else {
  9560. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9561. dot = 1;
  9562. }
  9563. sql = [sql stringByAppendingString:price_str];
  9564. } else {
  9565. price_str = @"";
  9566. }
  9567. if (discount_str) {
  9568. if (dot) {
  9569. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9570. } else {
  9571. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9572. dot = 1;
  9573. }
  9574. sql = [sql stringByAppendingString:discount_str];
  9575. } else {
  9576. discount_str = @"";
  9577. }
  9578. if (percent) {
  9579. if (dot) {
  9580. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9581. } else {
  9582. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9583. dot = 1;
  9584. }
  9585. sql = [sql stringByAppendingString:percent];
  9586. } else {
  9587. percent = @"";
  9588. }
  9589. if (qty) {
  9590. if (dot) {
  9591. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9592. } else {
  9593. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9594. dot = 1;
  9595. }
  9596. sql = [sql stringByAppendingString:qty];
  9597. } else {
  9598. qty = @"";
  9599. }
  9600. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9601. sql = [sql stringByAppendingString:where];
  9602. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9603. int result = [iSalesDB execSql:sql];
  9604. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9605. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9606. return [RAConvertor dict2data:dic];
  9607. }
  9608. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9609. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9610. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9611. sqlite3 *db = [iSalesDB get_db];
  9612. 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];
  9613. int result = [iSalesDB execSql:sql db:db];
  9614. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9615. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9616. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9617. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9618. [iSalesDB close_db:db];
  9619. return [RAConvertor dict2data:dic];
  9620. }
  9621. + (void)offline_removePDFWithName:(NSString *)name {
  9622. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9623. NSString *cachefolder = [paths objectAtIndex:0];
  9624. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9625. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9626. NSFileManager *fileManager = [NSFileManager defaultManager];
  9627. [fileManager removeItemAtPath:path error:nil];
  9628. }
  9629. + (void)offline_clear_PDFCache {
  9630. NSFileManager *fileManager = [NSFileManager defaultManager];
  9631. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9632. NSString *cachefolder = [paths objectAtIndex:0];
  9633. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9634. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9635. for (NSString *path in pdf_files) {
  9636. [self offline_removePDFWithName:[path lastPathComponent]];
  9637. }
  9638. }
  9639. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9640. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9641. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9642. NSString *user = [params objectForKey:@"user"];
  9643. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %ld;",(long)tearsheetsId]]; // tearsheets_id ---> _id
  9644. if (![create_user isEqualToString:user]) {
  9645. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9646. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9647. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9648. return [RAConvertor dict2data:dic];
  9649. }
  9650. 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]];
  9651. __block int is_local = 0;
  9652. __block NSString *path = @"";
  9653. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9654. is_local = sqlite3_column_int(stmt, 0);
  9655. path = [self textAtColumn:1 statement:stmt];
  9656. }];
  9657. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9658. if (is_local == 1) {
  9659. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %ld and create_user = '%@';",(long)tearsheetsId,[self translateSingleQuote:user]];
  9660. // 删除文件
  9661. [self offline_removePDFWithName:path];
  9662. }
  9663. int result = [iSalesDB execSql:sql];
  9664. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9665. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9666. return [RAConvertor dict2data:dic];
  9667. }
  9668. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial params:(NSMutableDictionary*) add_params
  9669. {
  9670. // assert(add_params[@"contact_id"]!=nil);
  9671. assert(add_params[@"user"]!=nil);
  9672. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9673. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9674. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9675. NSString* where=@"1=1";
  9676. if (ver!=nil) {
  9677. where=@"is_dirty=1";
  9678. }
  9679. 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];
  9680. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9681. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9682. sqlite3 *db = [iSalesDB get_db];
  9683. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9684. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9685. NSInteger _id = sqlite3_column_int(stmt, 0);
  9686. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9687. NSString *name = [self textAtColumn:2 statement:stmt];
  9688. NSString *desc = [self textAtColumn:3 statement:stmt];
  9689. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9690. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9691. NSInteger qty = sqlite3_column_int(stmt, 6);
  9692. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9693. double percent = sqlite3_column_double(stmt, 8);
  9694. double price = sqlite3_column_double(stmt, 9);
  9695. double discount = sqlite3_column_double(stmt, 10);
  9696. NSString *img = [self textAtColumn:11 statement:stmt];
  9697. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9698. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9699. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9700. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9701. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9702. if ([price_null isEqualToString:@"null"]) {
  9703. price = [[self get_model_default_price:add_params[@"contact_id"] user:add_params[@"user"] product_id:nil item_id:@(item_id) db:db] doubleValue];
  9704. }
  9705. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9706. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9707. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9708. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9709. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9710. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9711. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9712. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9713. if ([qty_null isEqualToString:@"null"]) {
  9714. // [item setValue:@"null" forKey:@"available_qty"];
  9715. } else {
  9716. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9717. }
  9718. if ([is_percent_null isEqualToString:@"null"]) {
  9719. // [item setValue:@"null" forKey:@"percentage"];
  9720. } else {
  9721. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9722. }
  9723. if ([percent_null isEqualToString:@"null"]) {
  9724. // [item setValue:@"null" forKey:@"percent"];
  9725. } else {
  9726. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9727. }
  9728. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9729. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9730. [item setValue:name forKey:@"name"];
  9731. [item setValue:desc forKey:@"description"];
  9732. [item setValue:img forKey:@"img"];
  9733. [item setValue:line_note forKey:@"line_note"];
  9734. [item setValue:create_time forKey:@"createtime"];
  9735. [item setValue:modify_time forKey:@"modifytime"];
  9736. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9737. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9738. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9739. }];
  9740. [iSalesDB close_db:db];
  9741. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9742. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9743. }
  9744. return ret;
  9745. }
  9746. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9747. {
  9748. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9749. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9750. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9751. NSString* where=@"1=1";
  9752. if (ver!=nil) {
  9753. where=@"is_dirty=1";
  9754. }
  9755. 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];
  9756. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9757. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9758. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9759. NSInteger _id = sqlite3_column_int(stmt, 0);
  9760. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9761. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9762. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9763. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9764. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9765. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9766. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9767. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9768. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9769. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9770. NSString *uuid = [NSUUID UUID].UUIDString;
  9771. int is_delete = sqlite3_column_int(stmt, 11);
  9772. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9773. int is_local = sqlite3_column_int(stmt, 12);
  9774. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9775. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9776. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9777. }
  9778. [item setObject:pdf_path forKey:@"pdf_path"];
  9779. [item setObject:create_user forKey:@"create_user"];
  9780. [item setObject:tear_note forKey:@"tear_note"];
  9781. [item setObject:tear_name forKey:@"tear_name"];
  9782. [item setObject:model_info forKey:@"model_info"];
  9783. [item setObject:createtime forKey:@"createtime"];
  9784. [item setObject:modifytime forKey:@"modifytime"];
  9785. [item setObject:urlParams forKey:@"urlParams"];
  9786. [item setObject:off_params forKey:@"off_params"];
  9787. [item setObject:uuid forKey:@"pdf_token"];
  9788. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9789. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  9790. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9791. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9792. } else {
  9793. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9794. }
  9795. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9796. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9797. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9798. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9799. }];
  9800. return ret;
  9801. }
  9802. #pragma mark 2020
  9803. +(void) offline_contactlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  9804. {
  9805. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  9806. sqlite3 *db = [iSalesDB get_db];
  9807. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  9808. if (contactType) {
  9809. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  9810. } else {
  9811. contactType = @"1 = 1";
  9812. }
  9813. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  9814. DebugLog(@"offline contact list keyword: %@",keyword);
  9815. // advanced search
  9816. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  9817. if (contact_name) {
  9818. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9819. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  9820. } else {
  9821. contact_name = @"";
  9822. }
  9823. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  9824. if (customer_phone) {
  9825. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9826. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  9827. } else {
  9828. customer_phone = @"";
  9829. }
  9830. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  9831. if (customer_fax) {
  9832. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9833. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  9834. } else {
  9835. customer_fax = @"";
  9836. }
  9837. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  9838. if (customer_zipcode) {
  9839. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9840. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  9841. } else {
  9842. customer_zipcode = @"";
  9843. }
  9844. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  9845. if (customer_sales_rep) {
  9846. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9847. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  9848. } else {
  9849. customer_sales_rep = @"";
  9850. }
  9851. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  9852. if (customer_state) {
  9853. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9854. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  9855. } else {
  9856. customer_state = @"";
  9857. }
  9858. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  9859. if (customer_name) {
  9860. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9861. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  9862. } else {
  9863. customer_name = @"";
  9864. }
  9865. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  9866. if (customer_country) {
  9867. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9868. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  9869. } else {
  9870. customer_country = @"";
  9871. }
  9872. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  9873. if (customer_cid) {
  9874. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9875. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  9876. } else {
  9877. customer_cid = @"";
  9878. }
  9879. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  9880. if (customer_city) {
  9881. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9882. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  9883. } else {
  9884. customer_city = @"";
  9885. }
  9886. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  9887. if (customer_address) {
  9888. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9889. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  9890. } else {
  9891. customer_address = @"";
  9892. }
  9893. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  9894. if (customer_email) {
  9895. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9896. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  9897. } else {
  9898. customer_email = @"";
  9899. }
  9900. NSString *price_name = [params valueForKey:@"price_name"];
  9901. if (price_name) {
  9902. if ([price_name containsString:@","]) {
  9903. // 首先从 price表中查处name
  9904. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  9905. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  9906. for (int i = 1;i < pArray.count;i++) {
  9907. NSString *p = pArray[i];
  9908. [mutablePStr appendFormat:@" or type = %@ ",p];
  9909. }
  9910. [mutablePStr appendString:@";"];
  9911. __block NSMutableArray *price_name_array = [NSMutableArray array];
  9912. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9913. char *name = (char *)sqlite3_column_text(stmt, 0);
  9914. if (!name)
  9915. name = "";
  9916. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  9917. }];
  9918. // 再根据name 拼sql
  9919. NSMutableString *mutable_price_name = [NSMutableString string];
  9920. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  9921. for (int i = 1; i < price_name_array.count; i++) {
  9922. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  9923. }
  9924. [mutable_price_name appendString:@")"];
  9925. price_name = mutable_price_name;
  9926. } else {
  9927. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  9928. if ([price_name isEqualToString:@""]) {
  9929. price_name = @"";
  9930. } else {
  9931. __block NSString *price;
  9932. price_name = [self translateSingleQuote:price_name];
  9933. [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) {
  9934. char *p = (char *)sqlite3_column_text(stmt, 0);
  9935. if (p == NULL) {
  9936. p = "";
  9937. }
  9938. price = [NSString stringWithUTF8String:p];
  9939. }];
  9940. if ([price isEqualToString:@""]) {
  9941. price_name = @"";
  9942. } else {
  9943. price = [self translateSingleQuote:price];
  9944. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  9945. }
  9946. }
  9947. }
  9948. } else {
  9949. price_name = @"";
  9950. }
  9951. int limit = [[params valueForKey:@"limit"] intValue];
  9952. int offset = [[params valueForKey:@"offset"] intValue];
  9953. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9954. 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];
  9955. 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];
  9956. // int result= [iSalesDB AddExFunction:db];
  9957. int count =0;
  9958. NSString *sqlQuery = nil;
  9959. if(keyword.length==0)
  9960. {
  9961. // 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];
  9962. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  9963. sqlQuery = sql;
  9964. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  9965. }
  9966. else
  9967. {
  9968. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  9969. keyword = keyword.lowercaseString;
  9970. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  9971. 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];
  9972. 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]];
  9973. }
  9974. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  9975. sqlite3_stmt * statement;
  9976. [ret setValue:@"2" forKey:@"result"];
  9977. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  9978. // 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";
  9979. int i = 0;
  9980. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  9981. {
  9982. while (sqlite3_step(statement) == SQLITE_ROW)
  9983. {
  9984. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  9985. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  9986. int editable = sqlite3_column_int(statement, 0);
  9987. char *company_name = (char*)sqlite3_column_text(statement, 1);
  9988. NSString *nscompany_name =nil;
  9989. if(company_name==nil)
  9990. nscompany_name=@"";
  9991. else
  9992. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  9993. char *country = (char*)sqlite3_column_text(statement, 2);
  9994. if(country==nil)
  9995. country="";
  9996. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  9997. // char *addr = (char*)sqlite3_column_text(statement, 3);
  9998. // if(addr==nil)
  9999. // addr="";
  10000. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10001. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10002. if(zipcode==nil)
  10003. zipcode="";
  10004. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10005. char *state = (char*)sqlite3_column_text(statement, 5);
  10006. if(state==nil)
  10007. state="";
  10008. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10009. char *city = (char*)sqlite3_column_text(statement, 6);
  10010. if(city==nil)
  10011. city="";
  10012. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10013. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10014. // NSString *nscontact_name = nil;
  10015. // if(contact_name==nil)
  10016. // nscontact_name=@"";
  10017. // else
  10018. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10019. char *phone = (char*)sqlite3_column_text(statement, 8);
  10020. NSString *nsphone = nil;
  10021. if(phone==nil)
  10022. nsphone=@"";
  10023. else
  10024. nsphone= [[NSString alloc]initWithUTF8String:phone];
  10025. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10026. if(contact_id==nil)
  10027. contact_id="";
  10028. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10029. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10030. if(addr_1==nil)
  10031. addr_1="";
  10032. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10033. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10034. if(addr_2==nil)
  10035. addr_2="";
  10036. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10037. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10038. if(addr_3==nil)
  10039. addr_3="";
  10040. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10041. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10042. if(addr_4==nil)
  10043. addr_4="";
  10044. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10045. char *first_name = (char*)sqlite3_column_text(statement, 14);
  10046. if(first_name==nil)
  10047. first_name="";
  10048. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10049. char *last_name = (char*)sqlite3_column_text(statement, 15);
  10050. if(last_name==nil)
  10051. last_name="";
  10052. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10053. char *fax = (char*)sqlite3_column_text(statement, 16);
  10054. NSString *nsfax = nil;
  10055. if(fax==nil)
  10056. nsfax=@"";
  10057. else
  10058. {
  10059. nsfax= [[NSString alloc]initWithUTF8String:fax];
  10060. if(nsfax.length>0)
  10061. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  10062. }
  10063. char *email = (char*)sqlite3_column_text(statement, 17);
  10064. NSString *nsemail = nil;
  10065. if(email==nil)
  10066. nsemail=@"";
  10067. else
  10068. {
  10069. nsemail= [[NSString alloc]initWithUTF8String:email];
  10070. if(nsemail.length>0)
  10071. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  10072. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  10073. }
  10074. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10075. [arr_name addObject:nsfirst_name];
  10076. [arr_name addObject:nslast_name];
  10077. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10078. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  10079. {
  10080. // decrypt
  10081. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10082. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10083. nsphone=[AESCrypt fastdecrypt:nsphone];
  10084. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10085. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10086. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10087. }
  10088. [arr_addr addObject:nscompany_name];
  10089. [arr_addr addObject:nscontact_name];
  10090. [arr_addr addObject:@"<br>"];
  10091. [arr_addr addObject:nsaddr_1];
  10092. [arr_addr addObject:nsaddr_2];
  10093. [arr_addr addObject:nsaddr_3];
  10094. [arr_addr addObject:nsaddr_4];
  10095. //[arr_addr addObject:nsaddr];
  10096. [arr_addr addObject:nszipcode];
  10097. [arr_addr addObject:nscity];
  10098. [arr_addr addObject:nsstate];
  10099. [arr_addr addObject:nscountry];
  10100. [arr_addr addObject:@"<br>"];
  10101. [arr_addr addObject:nsphone];
  10102. [arr_addr addObject:nsfax];
  10103. [arr_addr addObject:nsemail];
  10104. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  10105. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  10106. [item setValue:name forKey:@"name"];
  10107. [item setValue:nscontact_id forKey:@"contact_id"];
  10108. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  10109. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10110. i++;
  10111. }
  10112. sqlite3_finalize(statement);
  10113. }
  10114. [iSalesDB close_db:db];
  10115. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10116. dispatch_async(dispatch_get_main_queue(), ^{
  10117. UIApplication * app = [UIApplication sharedApplication];
  10118. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10119. [ret setValue:appDelegate.mode forKey:@"mode"];
  10120. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  10121. result(ret);
  10122. });
  10123. return ;
  10124. });
  10125. }
  10126. //+(void) offline_contactinfo :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10127. //{
  10128. //
  10129. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10130. //
  10131. // NSString* contactId = [params valueForKey:@"contactId"];
  10132. //
  10133. //
  10134. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10135. //
  10136. //
  10137. //
  10138. //
  10139. //
  10140. //
  10141. // sqlite3 *db = [iSalesDB get_db];
  10142. //
  10143. //
  10144. //
  10145. //
  10146. //
  10147. // NSString *sqlQuery = nil;
  10148. //
  10149. //
  10150. // {
  10151. // 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];
  10152. //
  10153. // }
  10154. //
  10155. //
  10156. // DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  10157. // sqlite3_stmt * statement;
  10158. //
  10159. //
  10160. // [ret setValue:@"2" forKey:@"result"];
  10161. //
  10162. //
  10163. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10164. // {
  10165. //
  10166. // //int i = 0;
  10167. // if (sqlite3_step(statement) == SQLITE_ROW)
  10168. // {
  10169. //
  10170. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  10171. //
  10172. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10173. //
  10174. //
  10175. // // int editable = sqlite3_column_int(statement, 0);
  10176. //
  10177. //
  10178. // char *company_name = (char*)sqlite3_column_text(statement, 1);
  10179. // NSString *nscompany_name =nil;
  10180. // if(company_name==nil)
  10181. // nscompany_name=@"";
  10182. // else
  10183. // nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  10184. //
  10185. //
  10186. // char *country = (char*)sqlite3_column_text(statement, 2);
  10187. // if(country==nil)
  10188. // country="";
  10189. // NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  10190. //
  10191. //
  10192. // // char *addr = (char*)sqlite3_column_text(statement, 3);
  10193. // // if(addr==nil)
  10194. // // addr="";
  10195. // // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  10196. //
  10197. //
  10198. // char *zipcode = (char*)sqlite3_column_text(statement, 4);
  10199. // if(zipcode==nil)
  10200. // zipcode="";
  10201. // NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  10202. //
  10203. //
  10204. // char *state = (char*)sqlite3_column_text(statement, 5);
  10205. // if(state==nil)
  10206. // state="";
  10207. // NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  10208. //
  10209. // char *city = (char*)sqlite3_column_text(statement, 6);
  10210. // if(city==nil)
  10211. // city="";
  10212. // NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  10213. //
  10214. // // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  10215. // // NSString *nscontact_name = nil;
  10216. // // if(contact_name==nil)
  10217. // // nscontact_name=@"";
  10218. // // else
  10219. // // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  10220. //
  10221. // char *phone = (char*)sqlite3_column_text(statement, 8);
  10222. // NSString *nsphone = nil;
  10223. // if(phone==nil)
  10224. // nsphone=@"";
  10225. // else
  10226. // nsphone= [[NSString alloc]initWithUTF8String:phone];
  10227. //
  10228. //
  10229. // // char *contact_id = (char*)sqlite3_column_text(statement, 9);
  10230. // // if(contact_id==nil)
  10231. // // contact_id="";
  10232. // // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  10233. //
  10234. // char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  10235. // if(addr_1==nil)
  10236. // addr_1="";
  10237. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  10238. //
  10239. // char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  10240. // if(addr_2==nil)
  10241. // addr_2="";
  10242. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  10243. //
  10244. //
  10245. // char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  10246. // if(addr_3==nil)
  10247. // addr_3="";
  10248. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  10249. //
  10250. //
  10251. // char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  10252. // if(addr_4==nil)
  10253. // addr_4="";
  10254. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  10255. //
  10256. //
  10257. // char *first_name = (char*)sqlite3_column_text(statement, 14);
  10258. // if(first_name==nil)
  10259. // first_name="";
  10260. // NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  10261. //
  10262. //
  10263. // char *last_name = (char*)sqlite3_column_text(statement, 15);
  10264. // if(last_name==nil)
  10265. // last_name="";
  10266. // NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  10267. //
  10268. // char *fax = (char*)sqlite3_column_text(statement, 16);
  10269. // NSString *nsfax = nil;
  10270. // if(fax==nil)
  10271. // nsfax=@"";
  10272. // else
  10273. // nsfax= [[NSString alloc]initWithUTF8String:fax];
  10274. //
  10275. // char *email = (char*)sqlite3_column_text(statement, 17);
  10276. // NSString *nsemail = nil;
  10277. // if(email==nil)
  10278. // nsemail=@"";
  10279. // else
  10280. // nsemail= [[NSString alloc]initWithUTF8String:email];
  10281. //
  10282. // char *img_0 = (char*)sqlite3_column_text(statement, 18);
  10283. // NSString *nsimg_0 = nil;
  10284. // if(img_0==nil)
  10285. // nsimg_0=@"";
  10286. // else
  10287. // nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  10288. //
  10289. // [self copy_bcardImg:nsimg_0];
  10290. //
  10291. // char *img_1 = (char*)sqlite3_column_text(statement, 19);
  10292. // NSString *nsimg_1 = nil;
  10293. // if(img_1==nil)
  10294. // nsimg_1=@"";
  10295. // else
  10296. // nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  10297. // [self copy_bcardImg:nsimg_1];
  10298. //
  10299. //
  10300. // char *img_2 = (char*)sqlite3_column_text(statement, 20);
  10301. // NSString *nsimg_2 = nil;
  10302. // if(img_2==nil)
  10303. // nsimg_2=@"";
  10304. // else
  10305. // nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  10306. // [self copy_bcardImg:nsimg_2];
  10307. //
  10308. // char *price_type = (char*)sqlite3_column_text(statement, 21);
  10309. // NSString *nsprice_type = nil;
  10310. // if(price_type==nil)
  10311. // nsprice_type=@"";
  10312. // else
  10313. // nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  10314. //
  10315. //
  10316. // char *notes = (char*)sqlite3_column_text(statement, 22);
  10317. // NSString *nsnotes = nil;
  10318. // if(notes==nil)
  10319. // nsnotes=@"";
  10320. // else
  10321. // nsnotes= [[NSString alloc]initWithUTF8String:notes];
  10322. //
  10323. //
  10324. // char *salesrep = (char*)sqlite3_column_text(statement, 23);
  10325. // NSString *nssalesrep = nil;
  10326. // if(salesrep==nil)
  10327. // nssalesrep=@"";
  10328. // else
  10329. // nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  10330. //
  10331. // NSString *contact_type = [self textAtColumn:24 statement:statement];
  10332. //
  10333. //
  10334. // {
  10335. // // decrypt
  10336. //
  10337. // nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  10338. //
  10339. // // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  10340. //
  10341. // nsphone=[AESCrypt fastdecrypt:nsphone];
  10342. // // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10343. //
  10344. // nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  10345. // // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  10346. //
  10347. // }
  10348. //
  10349. // NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  10350. //
  10351. // [arr_name addObject:nsfirst_name];
  10352. // [arr_name addObject:nslast_name];
  10353. //
  10354. // NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  10355. //
  10356. //
  10357. // NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  10358. //
  10359. // [arr_ext addObject:nsaddr_1];
  10360. // [arr_ext addObject:nsaddr_2];
  10361. // [arr_ext addObject:nsaddr_3];
  10362. // [arr_ext addObject:nsaddr_4];
  10363. // [arr_ext addObject:@"\r\n"];
  10364. //
  10365. // [arr_ext addObject:nscity];
  10366. // [arr_ext addObject:nsstate];
  10367. // [arr_ext addObject:nszipcode];
  10368. // [arr_ext addObject:nscountry];
  10369. //
  10370. // NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  10371. //
  10372. //
  10373. // nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  10374. //
  10375. //
  10376. // [item setValue:nsimg_2 forKey:@"business_card_2"];
  10377. // [item setValue:nsimg_0 forKey:@"business_card_0"];
  10378. // [item setValue:nscountry forKey:@"customer_country"];
  10379. // [item setValue:nsphone forKey:@"customer_phone"];
  10380. // [item setValue:nsimg_1 forKey:@"business_card_1"];
  10381. // [item setValue:nscompany_name forKey:@"customer_name"];
  10382. // [item setValue:nsprice_type forKey:@"customer_price_type"];
  10383. // [item setValue:nsfirst_name forKey:@"customer_first_name"];
  10384. // [item setValue:nsext forKey:@"customer_contact_ext"];
  10385. // [item setValue:nszipcode forKey:@"customer_zipcode"];
  10386. // [item setValue:nsaddr_1 forKey:@"customer_address1"];
  10387. // [item setValue:nsaddr_2 forKey:@"customer_address2"];
  10388. // [item setValue:nsaddr_3 forKey:@"customer_address3"];
  10389. // [item setValue:nsaddr_4 forKey:@"customer_address4"];
  10390. // [item setValue:nsnotes forKey:@"customer_contact_notes"];
  10391. // [item setValue:nslast_name forKey:@"customer_last_name"];
  10392. // [item setValue:nscity forKey:@"customer_city"];
  10393. // [item setValue:nsstate forKey:@"customer_state"];
  10394. // [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  10395. // [item setValue:contactId forKey:@"customer_cid"];
  10396. // [item setValue:nscontact_name forKey:@"customer_contact"];
  10397. // [item setValue:nsfax forKey:@"customer_fax"];
  10398. // [item setValue:nsemail forKey:@"customer_email"];
  10399. // [item setValue:contact_type forKey:@"customer_contact_type"];
  10400. //
  10401. //
  10402. // [ret setObject:item forKey:@"customerInfo"];
  10403. // // i++;
  10404. //
  10405. //
  10406. //
  10407. // }
  10408. //
  10409. //
  10410. //
  10411. //
  10412. // sqlite3_finalize(statement);
  10413. // }
  10414. //
  10415. //
  10416. //
  10417. // [iSalesDB close_db:db];
  10418. //
  10419. //
  10420. //
  10421. //
  10422. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10423. // dispatch_async(dispatch_get_main_queue(), ^{
  10424. // UIApplication * app = [UIApplication sharedApplication];
  10425. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10426. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10427. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10428. // result(ret);
  10429. // });
  10430. //
  10431. // return ;
  10432. // });
  10433. //}
  10434. + (void)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10435. {
  10436. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10437. NSString* order_code= appDelegate.order_code;
  10438. NSString* user = appDelegate.user;
  10439. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10440. NSString* category = [params valueForKey:@"category"];
  10441. if (!category || [category isEqualToString:@""]) {
  10442. category = @"%";
  10443. }
  10444. category = [self translateSingleQuote:category];
  10445. int limit = [[params valueForKey:@"limit"] intValue];
  10446. int offset = [[params valueForKey:@"offset"] intValue];
  10447. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10448. NSString *limit_str = @"";
  10449. if (limited) {
  10450. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10451. }
  10452. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10453. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10454. sqlite3 *db = [iSalesDB get_db];
  10455. // [iSalesDB AddExFunction:db];
  10456. int count;
  10457. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  10458. 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];
  10459. double price_min = 0;
  10460. double price_max = 0;
  10461. if ([params.allKeys containsObject:@"alert"]) {
  10462. // alert
  10463. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10464. NSString *alert = params[@"alert"];
  10465. if ([alert isEqualToString:@"Display All"]) {
  10466. alert = [NSString stringWithFormat:@""];
  10467. } else {
  10468. alert = [self translateSingleQuote:alert];
  10469. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10470. }
  10471. // available
  10472. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10473. NSString *available = params[@"available"];
  10474. NSString *available_condition;
  10475. if ([available isEqualToString:@"Display All"]) {
  10476. available_condition = @"";
  10477. } else if ([available isEqualToString:@"Available Now"]) {
  10478. available_condition = @"and availability > 0";
  10479. } else {
  10480. available_condition = @"and availability == 0";
  10481. }
  10482. // best seller
  10483. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10484. NSString *best_seller = @"";
  10485. NSString *order_best_seller = @"m.name asc";
  10486. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10487. best_seller = @"and best_seller > 0";
  10488. order_best_seller = @"m.best_seller desc,m.name asc";
  10489. }
  10490. // price
  10491. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10492. NSString *price = params[@"price"];
  10493. price_min = 0;
  10494. price_max = MAXFLOAT;
  10495. if (user && price != nil) {
  10496. NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10497. NSMutableString *priceName = [NSMutableString string];
  10498. for (int i = 0; i < priceTypeArray.count; i++) {
  10499. NSString *pricetype = priceTypeArray[i];
  10500. pricetype = [self translateSingleQuote:pricetype];
  10501. if (i == 0) {
  10502. [priceName appendFormat:@"'%@'",pricetype];
  10503. } else {
  10504. [priceName appendFormat:@",'%@'",pricetype];
  10505. }
  10506. }
  10507. if ([price isEqualToString:@"Display All"]) {
  10508. price = [NSString stringWithFormat:@""];
  10509. } else if([price containsString:@"+"]){
  10510. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10511. price_min = [price doubleValue];
  10512. 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];
  10513. } else {
  10514. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10515. price_min = [[priceArray objectAtIndex:0] doubleValue];
  10516. price_max = [[priceArray objectAtIndex:1] doubleValue];
  10517. 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];
  10518. }
  10519. } else {
  10520. price = @"";
  10521. }
  10522. // sold_by_qty : Sold in quantities of %@
  10523. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10524. NSString *qty = params[@"sold_by_qty"];
  10525. if ([qty isEqualToString:@"Display All"]) {
  10526. qty = @"";
  10527. } else {
  10528. qty = [self translateSingleQuote:qty];
  10529. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10530. }
  10531. // cate
  10532. // category = [self translateSingleQuote:category];
  10533. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  10534. // cate mutiple selection
  10535. NSString *category_id = params[@"category"];
  10536. NSMutableArray *cate_id_array = nil;
  10537. NSMutableString *cateWhere = [NSMutableString string];
  10538. if ([category_id isEqualToString:@""] || !category_id) {
  10539. [cateWhere appendString:@"1 = 1"];
  10540. } else {
  10541. if ([category_id containsString:@","]) {
  10542. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10543. } else {
  10544. cate_id_array = [@[category_id] mutableCopy];
  10545. }
  10546. [cateWhere appendString:@"("];
  10547. for (int i = 0; i < cate_id_array.count; i++) {
  10548. if (i == 0) {
  10549. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10550. } else {
  10551. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10552. }
  10553. }
  10554. [cateWhere appendString:@")"];
  10555. }
  10556. // where bestseller > 0 order by bestseller desc
  10557. // sql query: alert availability(int) best_seller(int) price qty
  10558. 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];
  10559. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  10560. }
  10561. DebugLog(@"offline category where: %@",where);
  10562. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  10563. if (!user) {
  10564. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  10565. }
  10566. [ret setValue:filter forKey:@"filter"];
  10567. DebugLog(@"offline_category sql:%@",sqlQuery);
  10568. sqlite3_stmt * statement;
  10569. [ret setValue:@"2" forKey:@"result"];
  10570. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10571. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10572. // int count=0;
  10573. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10574. {
  10575. int i=0;
  10576. while (sqlite3_step(statement) == SQLITE_ROW)
  10577. {
  10578. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10579. char *name = (char*)sqlite3_column_text(statement, 0);
  10580. if(name==nil)
  10581. name="";
  10582. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10583. char *description = (char*)sqlite3_column_text(statement, 1);
  10584. if(description==nil)
  10585. description="";
  10586. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10587. int product_id = sqlite3_column_int(statement, 2);
  10588. int wid = sqlite3_column_int(statement, 3);
  10589. int closeout = sqlite3_column_int(statement, 4);
  10590. int cid = sqlite3_column_int(statement, 5);
  10591. int wisdelete = sqlite3_column_int(statement, 6);
  10592. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  10593. int more_color = sqlite3_column_int(statement, 8);
  10594. // Defaul Category ID
  10595. __block NSString *categoryID = nil;
  10596. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  10597. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  10598. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  10599. if(default_category==nil)
  10600. default_category="";
  10601. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  10602. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  10603. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  10604. categoryID = nsdefault_category;
  10605. }];
  10606. if (!categoryID.length) {
  10607. NSString *cateIDs = params[@"category"];
  10608. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  10609. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  10610. for (NSString *cateID in requestCategoryArr) {
  10611. BOOL needBreak = NO;
  10612. for (NSString *itemCateIDBox in itemCategoryArr) {
  10613. if (itemCateIDBox.length > 4) {
  10614. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  10615. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  10616. if ([itemCategoryID isEqualToString:cateID]) {
  10617. needBreak = YES;
  10618. categoryID = itemCategoryID;
  10619. break;
  10620. }
  10621. }
  10622. }
  10623. if (needBreak) {
  10624. break;
  10625. }
  10626. }
  10627. }
  10628. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10629. if(wid !=0 && wisdelete != 1)
  10630. [item setValue:@"true" forKey:@"wish_exists"];
  10631. else
  10632. [item setValue:@"false" forKey:@"wish_exists"];
  10633. if(closeout==0)
  10634. [item setValue:@"false" forKey:@"is_closeout"];
  10635. else
  10636. [item setValue:@"true" forKey:@"is_closeout"];
  10637. if(cid==0)
  10638. [item setValue:@"false" forKey:@"cart_exists"];
  10639. else
  10640. [item setValue:@"true" forKey:@"cart_exists"];
  10641. if (more_color == 0) {
  10642. [item setObject:@(false) forKey:@"more_color"];
  10643. } else if (more_color == 1) {
  10644. [item setObject:@(true) forKey:@"more_color"];
  10645. }
  10646. [item addEntriesFromDictionary:imgjson];
  10647. // [item setValue:nsurl forKey:@"img"];
  10648. [item setValue:nsname forKey:@"name"];
  10649. [item setValue:nsdescription forKey:@"description"];
  10650. if (categoryID) {
  10651. [item setValue:categoryID forKey:@"item_category_id"];
  10652. }
  10653. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  10654. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10655. i++;
  10656. }
  10657. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  10658. [ret setObject:items forKey:@"items"];
  10659. sqlite3_finalize(statement);
  10660. } else {
  10661. DebugLog(@"nothing...");
  10662. }
  10663. DebugLog(@"count:%d",count);
  10664. [iSalesDB close_db:db];
  10665. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10666. dispatch_async(dispatch_get_main_queue(), ^{
  10667. // UIApplication * app = [UIApplication sharedApplication];
  10668. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10669. // [ret setValue:appDelegate.mode forKey:@"mode"];
  10670. // [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  10671. result(ret);
  10672. });
  10673. });
  10674. // NSString* orderCode = [params valueForKey:@"orderCode"];
  10675. }
  10676. +(void) offline_category :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10677. {
  10678. [self categoryList:params limited:YES completionHandler:result];
  10679. }
  10680. + (void) search:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10681. {
  10682. UIApplication * app = [UIApplication sharedApplication];
  10683. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  10684. NSString* order_code = appDelegate.order_code;
  10685. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10686. NSString* keyword = [params valueForKey:@"keyword"];
  10687. keyword=keyword.lowercaseString;
  10688. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  10689. int limit = [[params valueForKey:@"limit"] intValue];
  10690. int offset = [[params valueForKey:@"offset"] intValue];
  10691. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  10692. NSString *limit_str = @"";
  10693. if (limited) {
  10694. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  10695. }
  10696. sqlite3 *db = [iSalesDB get_db];
  10697. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  10698. NSString *sqlQuery = nil;
  10699. if(exactMatch )
  10700. 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 ;
  10701. else
  10702. 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
  10703. DebugLog(@"offline_search sql:%@",sqlQuery);
  10704. sqlite3_stmt * statement;
  10705. [ret setValue:@"2" forKey:@"result"];
  10706. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  10707. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  10708. // int count=0;
  10709. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  10710. {
  10711. int i=0;
  10712. while (sqlite3_step(statement) == SQLITE_ROW)
  10713. {
  10714. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  10715. // char *name = (char*)sqlite3_column_text(statement, 1);
  10716. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  10717. char *name = (char*)sqlite3_column_text(statement, 0);
  10718. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  10719. char *description = (char*)sqlite3_column_text(statement, 1);
  10720. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  10721. int product_id = sqlite3_column_int(statement, 2);
  10722. // char *url = (char*)sqlite3_column_text(statement, 3);
  10723. // if(url==nil)
  10724. // url="";
  10725. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  10726. int wid = sqlite3_column_int(statement, 3);
  10727. int closeout = sqlite3_column_int(statement, 4);
  10728. int cid = sqlite3_column_int(statement, 5);
  10729. int wisdelete = sqlite3_column_int(statement, 6);
  10730. int more_color = sqlite3_column_int(statement, 7);
  10731. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  10732. if(wid !=0 && wisdelete != 1)
  10733. [item setValue:@"true" forKey:@"wish_exists"];
  10734. else
  10735. [item setValue:@"false" forKey:@"wish_exists"];
  10736. if(closeout==0)
  10737. [item setValue:@"false" forKey:@"is_closeout"];
  10738. else
  10739. [item setValue:@"true" forKey:@"is_closeout"];
  10740. if(cid==0)
  10741. [item setValue:@"false" forKey:@"cart_exists"];
  10742. else
  10743. [item setValue:@"true" forKey:@"cart_exists"];
  10744. if (more_color == 0) {
  10745. [item setObject:@(false) forKey:@"more_color"];
  10746. } else if (more_color == 1) {
  10747. [item setObject:@(true) forKey:@"more_color"];
  10748. }
  10749. [item addEntriesFromDictionary:imgjson];
  10750. // [item setValue:nsurl forKey:@"img"];
  10751. [item setValue:nsname forKey:@"fash_name"];
  10752. [item setValue:nsdescription forKey:@"description"];
  10753. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  10754. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  10755. i++;
  10756. }
  10757. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  10758. [ret setObject:items forKey:@"items"];
  10759. sqlite3_finalize(statement);
  10760. }
  10761. DebugLog(@"count:%d",count);
  10762. [iSalesDB close_db:db];
  10763. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  10764. dispatch_async(dispatch_get_main_queue(), ^{
  10765. result(ret);
  10766. });
  10767. });
  10768. }
  10769. +(void) offline_search:(NSMutableDictionary *) params completionHandler:(resultHandler)result
  10770. {
  10771. [self search:params limited:YES completionHandler:result];
  10772. }
  10773. //+ (void) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited completionHandler:(resultHandler)result
  10774. //{
  10775. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  10776. // NSString* order_code = appDelegate.order_code;
  10777. // NSString* user = appDelegate.user;
  10778. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  10779. // BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  10780. //
  10781. // NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  10782. // NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  10783. //
  10784. // // category
  10785. // NSDictionary *category_menu = [self offline_category_menu];
  10786. // [filter setValue:category_menu forKey:@"category"];
  10787. //
  10788. // NSString* where= nil;
  10789. // NSString* orderby= @"m.name";
  10790. // if (!filterSearch) {
  10791. // int covertype = [[params valueForKey:@"covertype"] intValue];
  10792. //
  10793. // switch (covertype) {
  10794. // case 0:
  10795. // {
  10796. // where=@"m.category like'%%#005#%%'";
  10797. // break;
  10798. // }
  10799. // case 1:
  10800. // {
  10801. // where=@"m.alert like '%QS%'";
  10802. // break;
  10803. // }
  10804. // case 2:
  10805. // {
  10806. // where=@"m.availability>0";
  10807. // break;
  10808. // }
  10809. // case 3:
  10810. // {
  10811. // where=@"m.best_seller>0";
  10812. // orderby=@"m.best_seller desc,m.name asc";
  10813. // break;
  10814. // }
  10815. // default:
  10816. // where=@"1=1";
  10817. // break;
  10818. // }
  10819. //
  10820. // }
  10821. //
  10822. //
  10823. // int limit = [[params valueForKey:@"limit"] intValue];
  10824. // int offset = [[params valueForKey:@"offset"] intValue];
  10825. //
  10826. // NSString *limit_str = @"";
  10827. // if (limited) {
  10828. // limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  10829. // }
  10830. //
  10831. //
  10832. // sqlite3 *db = [iSalesDB get_db];
  10833. // // [iSalesDB AddExFunction:db];
  10834. //
  10835. // int count;
  10836. //
  10837. // NSString *sqlQuery = nil;
  10838. // where = [where stringByAppendingString:@" and m.is_active = 1"];
  10839. // 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];
  10840. //
  10841. //
  10842. // double price_min = 0;
  10843. // double price_max = 0;
  10844. // if (filterSearch) {
  10845. // // alert
  10846. // [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  10847. // NSString *alert = params[@"alert"];
  10848. // if ([alert isEqualToString:@"Display All"]) {
  10849. // alert = [NSString stringWithFormat:@""];
  10850. // } else {
  10851. // alert = [self translateSingleQuote:alert];
  10852. // alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  10853. // }
  10854. //
  10855. // // available
  10856. // [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  10857. // NSString *available = params[@"available"];
  10858. // NSString *available_condition;
  10859. // if ([available isEqualToString:@"Display All"]) {
  10860. // available_condition = @"";
  10861. // } else if ([available isEqualToString:@"Available Now"]) {
  10862. // available_condition = @"and availability > 0";
  10863. // } else {
  10864. // available_condition = @"and availability == 0";
  10865. // }
  10866. //
  10867. // // best seller
  10868. // [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  10869. // NSString *best_seller = @"";
  10870. // NSString *order_best_seller = @"m.name asc";
  10871. //
  10872. // if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  10873. // best_seller = @"and best_seller > 0";
  10874. // order_best_seller = @"m.best_seller desc,m.name asc";
  10875. // }
  10876. //
  10877. // // price
  10878. // [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  10879. // NSString *price = params[@"price"];
  10880. // price_min = 0;
  10881. // price_max = MAXFLOAT;
  10882. // if (user) {
  10883. // NSArray *priceTypeArray = [self get_contact_default_price_type:nil user:user db:db];
  10884. // NSMutableString *priceName = [NSMutableString string];
  10885. // for (int i = 0; i < priceTypeArray.count; i++) {
  10886. // NSString *pricetype = priceTypeArray[i];
  10887. // pricetype = [self translateSingleQuote:pricetype];
  10888. // if (i == 0) {
  10889. // [priceName appendFormat:@"'%@'",pricetype];
  10890. // } else {
  10891. // [priceName appendFormat:@",'%@'",pricetype];
  10892. // }
  10893. // }
  10894. //
  10895. // if ([price isEqualToString:@"Display All"]) {
  10896. // price = [NSString stringWithFormat:@""];
  10897. // } else if([price containsString:@"+"]){
  10898. // price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  10899. // price_min = [price doubleValue];
  10900. // 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];
  10901. // } else {
  10902. // NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  10903. // price_min = [[priceArray objectAtIndex:0] doubleValue];
  10904. // price_max = [[priceArray objectAtIndex:1] doubleValue];
  10905. // 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];
  10906. // }
  10907. // } else {
  10908. // price = @"";
  10909. // }
  10910. //
  10911. // // sold_by_qty : Sold in quantities of %@
  10912. // [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  10913. // NSString *qty = params[@"sold_by_qty"];
  10914. // if ([qty isEqualToString:@"Display All"]) {
  10915. // qty = @"";
  10916. // } else {
  10917. // qty = [self translateSingleQuote:qty];
  10918. // qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  10919. // }
  10920. //
  10921. // // category
  10922. // NSString *category_id = params[@"ctgId"];
  10923. // NSMutableArray *cate_id_array = nil;
  10924. // NSMutableString *cateWhere = [NSMutableString string];
  10925. //
  10926. // if ([category_id isEqualToString:@""] || !category_id) {
  10927. // [cateWhere appendString:@"1 = 1"];
  10928. // } else {
  10929. //
  10930. // if ([category_id containsString:@","]) {
  10931. // cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  10932. // } else {
  10933. // cate_id_array = [@[category_id] mutableCopy];
  10934. // }
  10935. // /*-----------*/
  10936. // NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  10937. //
  10938. // [cateWhere appendString:@"("];
  10939. // for (int i = 0; i < cate_id_array.count; i++) {
  10940. //
  10941. // for (NSString *key0 in cateDic.allKeys) {
  10942. //
  10943. // if ([key0 containsString:@"category_"]) {
  10944. //
  10945. // NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  10946. //
  10947. // for (NSString *key1 in category0.allKeys) {
  10948. //
  10949. // if ([key1 containsString:@"category_"]) {
  10950. //
  10951. // NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  10952. // [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  10953. // if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  10954. //
  10955. // cate_id_array[i] = [category1 objectForKey:@"id"];
  10956. // if (i == 0) {
  10957. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10958. // } else {
  10959. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10960. // }
  10961. // [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  10962. // [category0 setValue:category1 forKey:key1];
  10963. // [cateDic setValue:category0 forKey:key0];
  10964. //
  10965. // }
  10966. //
  10967. // } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  10968. //
  10969. // if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  10970. //
  10971. // cate_id_array[i] = [category0 objectForKey:@"id"];
  10972. // if (i == 0) {
  10973. // [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  10974. // } else {
  10975. // [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  10976. // }
  10977. // [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  10978. // [cateDic setValue:category0 forKey:key0];
  10979. //
  10980. // }
  10981. //
  10982. // }
  10983. //
  10984. // }
  10985. //
  10986. // }
  10987. //
  10988. // }
  10989. //
  10990. // }
  10991. // [cateWhere appendString:@")"];
  10992. //
  10993. // [filter setValue:cateDic forKey:@"category"];
  10994. // }
  10995. //
  10996. // // where bestseller > 0 order by bestseller desc
  10997. // // sql query: alert availability(int) best_seller(int) price qty
  10998. //
  10999. // 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];
  11000. //
  11001. //
  11002. // // count
  11003. // where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  11004. //
  11005. // }
  11006. // where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  11007. //
  11008. // count = [iSalesDB get_recordcount:db table:@"product" where:where];
  11009. //
  11010. //
  11011. //
  11012. //
  11013. // if (!user) {
  11014. // [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  11015. // }
  11016. //
  11017. // [ret setValue:filter forKey:@"filter"];
  11018. //
  11019. //
  11020. //
  11021. // DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  11022. //
  11023. // sqlite3_stmt * statement;
  11024. // [ret setValue:@"2" forKey:@"result"];
  11025. // [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  11026. //
  11027. // NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  11028. // // int count=0;
  11029. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11030. // {
  11031. //
  11032. // int i=0;
  11033. // while (sqlite3_step(statement) == SQLITE_ROW)
  11034. // {
  11035. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11036. //
  11037. //
  11038. // char *name = (char*)sqlite3_column_text(statement, 0);
  11039. // NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11040. //
  11041. // char *description = (char*)sqlite3_column_text(statement, 1);
  11042. // NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11043. //
  11044. //
  11045. //
  11046. // int product_id = sqlite3_column_int(statement, 2);
  11047. //
  11048. //
  11049. // int wid = sqlite3_column_int(statement, 3);
  11050. // int closeout = sqlite3_column_int(statement, 4);
  11051. // int cid = sqlite3_column_int(statement, 5);
  11052. // int wisdelete = sqlite3_column_int(statement, 6);
  11053. // int more_color = sqlite3_column_int(statement, 7);
  11054. // NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  11055. //
  11056. // if(wid !=0 && wisdelete != 1)
  11057. // [item setValue:@"true" forKey:@"wish_exists"];
  11058. // else
  11059. // [item setValue:@"false" forKey:@"wish_exists"];
  11060. //
  11061. // if(closeout==0)
  11062. // [item setValue:@"false" forKey:@"is_closeout"];
  11063. // else
  11064. // [item setValue:@"true" forKey:@"is_closeout"];
  11065. //
  11066. // if(cid==0)
  11067. // [item setValue:@"false" forKey:@"cart_exists"];
  11068. // else
  11069. // [item setValue:@"true" forKey:@"cart_exists"];
  11070. //
  11071. // if (more_color == 0) {
  11072. // [item setObject:@(false) forKey:@"more_color"];
  11073. // } else if (more_color == 1) {
  11074. // [item setObject:@(true) forKey:@"more_color"];
  11075. // }
  11076. //
  11077. // [item addEntriesFromDictionary:imgjson];
  11078. //
  11079. //
  11080. // // [item setValue:nsurl forKey:@"img"];
  11081. // [item setValue:nsname forKey:@"fash_name"];
  11082. // [item setValue:nsdescription forKey:@"description"];
  11083. // [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11084. // [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  11085. // i++;
  11086. // }
  11087. // [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  11088. // [ret setObject:items forKey:@"items"];
  11089. // sqlite3_finalize(statement);
  11090. // }
  11091. //
  11092. // [iSalesDB close_db:db];
  11093. //
  11094. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11095. //
  11096. // DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11097. // dispatch_async(dispatch_get_main_queue(), ^{
  11098. //
  11099. // result(ret);
  11100. // });
  11101. //
  11102. // });
  11103. //
  11104. //
  11105. //
  11106. //}
  11107. //+(void) offline_itemsearch :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11108. //{
  11109. // [self itemsearch:params limited:YES completionHandler:result];
  11110. //}
  11111. +(void) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11112. {
  11113. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11114. NSString* offline_command=params[@"offline_Command"];
  11115. NSMutableDictionary* ret=nil;
  11116. if([offline_command isEqualToString:@"model_NIYMAL"])
  11117. {
  11118. NSString* category = params[@"category"];
  11119. ret = [[self refresh_model_NIYMAL:category] mutableCopy];
  11120. }
  11121. dispatch_async(dispatch_get_main_queue(), ^{
  11122. result(ret);
  11123. });
  11124. });
  11125. }
  11126. +(void) offline_wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11127. {
  11128. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11129. int sort = [[params objectForKey:@"sort"] intValue];
  11130. NSString *sort_str = @"";
  11131. switch (sort) {
  11132. case 0:{
  11133. sort_str = @"order by w.modify_time desc";
  11134. }
  11135. break;
  11136. case 1:{
  11137. sort_str = @"order by w.modify_time asc";
  11138. }
  11139. break;
  11140. case 2:{
  11141. sort_str = @"order by m.name asc";
  11142. }
  11143. break;
  11144. case 3:{
  11145. sort_str = @"order by m.name desc";
  11146. }
  11147. break;
  11148. case 4:{
  11149. sort_str = @"order by m.description asc";
  11150. }
  11151. break;
  11152. default:
  11153. break;
  11154. }
  11155. // NSString* user = appDelegate.user;
  11156. sqlite3 *db = [iSalesDB get_db];
  11157. // order by w.create_time
  11158. 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];
  11159. // 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];
  11160. sqlite3_stmt * statement;
  11161. NSDate *date1 = [NSDate date];
  11162. // NSDate *date2 = nil;
  11163. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11164. int count=0;
  11165. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11166. {
  11167. while (sqlite3_step(statement) == SQLITE_ROW)
  11168. {
  11169. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  11170. int product_id = sqlite3_column_double(statement, 0);
  11171. char *description = (char*)sqlite3_column_text(statement, 1);
  11172. if(description==nil)
  11173. description= "";
  11174. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  11175. int item_id = sqlite3_column_double(statement, 2);
  11176. NSDate *date_image = [NSDate date];
  11177. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  11178. printf("image : ");
  11179. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  11180. // char *url = (char*)sqlite3_column_text(statement, 3);
  11181. // if(url==nil)
  11182. // url="";
  11183. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  11184. int qty = sqlite3_column_int(statement, 3);
  11185. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  11186. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  11187. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  11188. item[@"description"]= nsdescription;
  11189. item[@"img"]= nsurl;
  11190. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  11191. count++;
  11192. }
  11193. printf("total time:");
  11194. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11195. ret[@"count"]= [NSNumber numberWithInt:count];
  11196. ret[@"total_count"]= [NSNumber numberWithInt:count];
  11197. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  11198. ret[@"result"]= [NSNumber numberWithInt:2];
  11199. sqlite3_finalize(statement);
  11200. }
  11201. [iSalesDB close_db:db];
  11202. dispatch_async(dispatch_get_main_queue(), ^{
  11203. UIApplication * app = [UIApplication sharedApplication];
  11204. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11205. appDelegate.wish_count =count;
  11206. [appDelegate update_count_mark];
  11207. result(ret);
  11208. });
  11209. });
  11210. }
  11211. +(void) offline_add2wishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11212. {
  11213. // UIApplication * app = [UIApplication sharedApplication];
  11214. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11215. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11216. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11217. sqlite3 *db = [iSalesDB get_db];
  11218. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  11219. NSString* product_id=params[@"product_id"];
  11220. NSString *item_count_str = params[@"item_count"];
  11221. NSArray* arr=[RAConvertor string2arr:product_id separator:@","];
  11222. NSArray *item_count_arr = item_count_str ? [RAConvertor string2arr:item_count_str separator:@","] : nil;
  11223. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11224. // NSString *sql = @"";
  11225. for(int i=0;i<arr.count;i++)
  11226. {
  11227. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  11228. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  11229. __block int cart_count = 0;
  11230. if (!item_count_str) {
  11231. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  11232. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11233. NSString *model_set = [self textAtColumn:0 statement:stmt];
  11234. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  11235. cart_count = [[model_set_components lastObject] intValue];
  11236. }];
  11237. }
  11238. if(count==0)
  11239. {
  11240. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  11241. sqlite3_stmt *stmt;
  11242. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  11243. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],(int)[arr[i] length],NULL);
  11244. if (item_count_arr) {
  11245. sqlite3_bind_int(stmt,2,[item_count_arr[i] intValue]);
  11246. } else {
  11247. sqlite3_bind_int(stmt,2,cart_count);
  11248. }
  11249. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  11250. [iSalesDB execSql:@"ROLLBACK" db:db];
  11251. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  11252. [iSalesDB close_db:db];
  11253. DebugLog(@"add to wishlist error");
  11254. dispatch_async(dispatch_get_main_queue(), ^{
  11255. result(ret);
  11256. });
  11257. }
  11258. } else {
  11259. int qty = 0;
  11260. if (item_count_arr) {
  11261. qty = [item_count_arr[i] intValue];
  11262. } else {
  11263. qty = cart_count;
  11264. }
  11265. 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]];
  11266. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  11267. [iSalesDB execSql:@"ROLLBACK" db:db];
  11268. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  11269. [iSalesDB close_db:db];
  11270. DebugLog(@"add to wishlist error");
  11271. dispatch_async(dispatch_get_main_queue(), ^{
  11272. result(ret);
  11273. });
  11274. }
  11275. }
  11276. }
  11277. // [iSalesDB execSql:sql db:db];
  11278. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11279. [iSalesDB execSql:@"END TRANSACTION" db:db];
  11280. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  11281. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  11282. [iSalesDB close_db:db];
  11283. ret[@"result"]= [NSNumber numberWithInt:2];
  11284. dispatch_async(dispatch_get_main_queue(), ^{
  11285. UIApplication * app = [UIApplication sharedApplication];
  11286. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11287. appDelegate.wish_count =count;
  11288. [appDelegate update_count_mark];
  11289. result(ret);
  11290. });
  11291. });
  11292. }
  11293. +(void) offline_model :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11294. {
  11295. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  11296. bool bLogin = appDelegate.bLogin;
  11297. __block NSString* contact_id = appDelegate.contact_id;
  11298. __block NSString* user = appDelegate.user;
  11299. __block NSString* order_code = appDelegate.order_code;
  11300. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11301. NSString* model_name = [params valueForKey:@"product_name"];
  11302. NSString* product_id = [params valueForKey:@"product_id"];
  11303. NSString* upc_code = [params valueForKey:@"upc_code"];
  11304. NSString* category = [params valueForKey:@"category"];
  11305. NSString *default_category_id = [self model_default_category:product_id model_name:model_name upc_code:upc_code];
  11306. if(category==nil) {
  11307. category = default_category_id;
  11308. } else {
  11309. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  11310. // 参数重有多个category id
  11311. if (arr_0.count > 1) {
  11312. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  11313. // 取交集
  11314. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  11315. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  11316. [set_0 intersectSet:set_1];
  11317. if (set_0.count == 1) {
  11318. category = (NSString *)[set_0 anyObject];
  11319. } else {
  11320. if ([set_0 containsObject:default_category_id]) {
  11321. category = default_category_id;
  11322. } else {
  11323. category = (NSString *)[set_0 anyObject];
  11324. }
  11325. }
  11326. }
  11327. }
  11328. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11329. sqlite3 *db = [iSalesDB get_db];
  11330. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  11331. NSString *sqlQuery = nil;
  11332. if(product_id==nil)
  11333. if(model_name==nil)
  11334. {
  11335. 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 upc_code = '%@' or upc_code like '%@;%%' or upc_code like '%%;%@;%%' or upc_code like '%%;%@';",upc_code,upc_code,upc_code,upc_code]; // 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='%@';
  11336. }
  11337. else
  11338. {
  11339. 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='%@';
  11340. }
  11341. else
  11342. 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=%@;
  11343. sqlite3_stmt * statement;
  11344. [ret setValue:@"2" forKey:@"result"];
  11345. [ret setValue:@"3" forKey:@"detail_section_count"];
  11346. // int count=0;
  11347. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11348. {
  11349. if (sqlite3_step(statement) == SQLITE_ROW)
  11350. {
  11351. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  11352. char *name = (char*)sqlite3_column_text(statement, 0);
  11353. if(name==nil)
  11354. name="";
  11355. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  11356. char *description = (char*)sqlite3_column_text(statement, 1);
  11357. if(description==nil)
  11358. description="";
  11359. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  11360. int product_id = sqlite3_column_int(statement, 2);
  11361. char *color = (char*)sqlite3_column_text(statement, 3);
  11362. if(color==nil)
  11363. color="";
  11364. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  11365. //
  11366. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  11367. // if(legcolor==nil)
  11368. // legcolor="";
  11369. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  11370. //
  11371. //
  11372. int availability = sqlite3_column_int(statement, 5);
  11373. //
  11374. int incoming_stock = sqlite3_column_int(statement, 6);
  11375. char *demension = (char*)sqlite3_column_text(statement, 7);
  11376. if(demension==nil)
  11377. demension="";
  11378. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  11379. // ,,,,,,,,,
  11380. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  11381. if(seat_height==nil)
  11382. seat_height="";
  11383. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  11384. char *material = (char*)sqlite3_column_text(statement, 9);
  11385. if(material==nil)
  11386. material="";
  11387. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  11388. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  11389. if(box_dim==nil)
  11390. box_dim="";
  11391. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  11392. char *volume = (char*)sqlite3_column_text(statement, 11);
  11393. if(volume==nil)
  11394. volume="";
  11395. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  11396. double weight = sqlite3_column_double(statement, 12);
  11397. char *model_set = (char*)sqlite3_column_text(statement, 13);
  11398. if(model_set==nil)
  11399. model_set="";
  11400. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  11401. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  11402. if(load_ability==nil)
  11403. load_ability="";
  11404. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  11405. // char *default_category = (char*)sqlite3_column_text(statement, 15);
  11406. // if(default_category==nil)
  11407. // default_category="";
  11408. // NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  11409. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  11410. if(fabric_content==nil)
  11411. fabric_content="";
  11412. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  11413. char *assembling = (char*)sqlite3_column_text(statement, 17);
  11414. if(assembling==nil)
  11415. assembling="";
  11416. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  11417. char *made_in = (char*)sqlite3_column_text(statement, 18);
  11418. if(made_in==nil)
  11419. made_in="";
  11420. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  11421. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  11422. if(special_remarks==nil)
  11423. special_remarks="";
  11424. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  11425. int stockUcom = sqlite3_column_double(statement, 20);
  11426. char *product_group = (char*)sqlite3_column_text(statement, 21);
  11427. if(product_group==nil)
  11428. product_group="";
  11429. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  11430. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  11431. // if(fashion_selector==nil)
  11432. // fashion_selector="";
  11433. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  11434. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  11435. if(selector_field==nil)
  11436. selector_field="";
  11437. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  11438. char *property_field = (char*)sqlite3_column_text(statement, 23);
  11439. if(property_field==nil)
  11440. property_field="";
  11441. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  11442. char *packaging = (char*)sqlite3_column_text(statement, 24);
  11443. if(packaging==nil)
  11444. packaging="";
  11445. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  11446. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  11447. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  11448. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  11449. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  11450. [img_section setValue:model_s_img forKey:@"model_s_img"];
  11451. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  11452. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  11453. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  11454. NSString* Availability=nil;
  11455. if(availability>0)
  11456. Availability=[NSString stringWithFormat:@"%d",availability];
  11457. else
  11458. Availability = @"Out of Stock";
  11459. [img_section setValue:Availability forKey:@"Availability"];
  11460. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  11461. char *eta = (char*)sqlite3_column_text(statement, 25);
  11462. if(eta==nil)
  11463. eta="";
  11464. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  11465. if ([nseta isEqualToString:@"null"]) {
  11466. nseta = @"";
  11467. }
  11468. if (availability <= 0) {
  11469. [img_section setValue:nseta forKey:@"ETA"];
  11470. }
  11471. int item_id = sqlite3_column_int(statement, 26);
  11472. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  11473. NSString *content_writing = [self textAtColumn:28 statement:statement];
  11474. NSString* Price=nil;
  11475. if(bLogin==false)
  11476. Price=@"Must Sign in.";
  11477. else
  11478. {
  11479. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11480. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11481. if(price==nil)
  11482. Price=@"No Price.";
  11483. else
  11484. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11485. }
  11486. [img_section setObject:content_writing forKey:@"product_content_writing"];
  11487. [img_section setValue:Price forKey:@"price"];
  11488. [img_section setValue:nsname forKey:@"model_name"];
  11489. [img_section setValue:nsdescription forKey:@"model_descrition"];
  11490. if (order_code) { // 离线order code即so#
  11491. 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];
  11492. __block int cartQTY = 0;
  11493. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  11494. cartQTY = sqlite3_column_int(stmt, 0);
  11495. }];
  11496. if (cartQTY > 0) {
  11497. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  11498. }
  11499. }
  11500. [ret setObject:img_section forKey:@"img_section"];
  11501. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  11502. int detail0_item_count=0;
  11503. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11504. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11505. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11506. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11507. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11508. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11509. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11510. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11511. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11512. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11513. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11514. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11515. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11516. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11517. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  11518. NSDictionary* pricejson = [self get_model_all_price:contact_id item_id:item_id user:user islogin:bLogin db:db];
  11519. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  11520. {
  11521. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  11522. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  11523. }
  11524. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  11525. [detail0_section setValue:@"kv" forKey:@"type"];
  11526. [detail0_section setValue:@"Product Information" forKey:@"title"];
  11527. [ret setObject:detail0_section forKey:@"detail_0"];
  11528. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  11529. // [detail1_section setValue:@"detail" forKey:@"target"];
  11530. // [detail1_section setValue:@"popup" forKey:@"action"];
  11531. // [detail1_section setValue:@"content" forKey:@"type"];
  11532. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  11533. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  11534. // [detail1_section setValue:@"true" forKey:@"single_row"];
  11535. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  11536. [ret setObject:detail1_section forKey:@"detail_1"];
  11537. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  11538. [detail2_section setValue:@"detail" forKey:@"target"];
  11539. [detail2_section setValue:@"popup" forKey:@"action"];
  11540. [detail2_section setValue:@"content" forKey:@"type"];
  11541. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  11542. [detail2_section setValue:@"true" forKey:@"single_row"];
  11543. [detail2_section setValue:@"local" forKey:@"data"];
  11544. [ret setObject:detail2_section forKey:@"detail_2"];
  11545. }
  11546. sqlite3_finalize(statement);
  11547. }
  11548. else
  11549. {
  11550. [ret setValue:@"8" forKey:@"result"];
  11551. }
  11552. // DebugLog(@"count:%d",count);
  11553. [iSalesDB close_db:db];
  11554. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  11555. dispatch_async(dispatch_get_main_queue(), ^{
  11556. result(ret);
  11557. });
  11558. });
  11559. }
  11560. //+(void) offline_deletewishlist :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11561. //{
  11562. //
  11563. // UIApplication * app = [UIApplication sharedApplication];
  11564. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11565. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11566. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  11567. // sqlite3 *db = [iSalesDB get_db];
  11568. // NSString* collectId=params[@"collectId"];
  11569. //
  11570. //
  11571. //
  11572. //
  11573. // // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  11574. // NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  11575. // [iSalesDB execSql:sqlQuery db:db];
  11576. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11577. // [iSalesDB close_db:db];
  11578. //
  11579. //
  11580. // ret[@"result"]= [NSNumber numberWithInt:2];
  11581. // dispatch_async(dispatch_get_main_queue(), ^{
  11582. //
  11583. // appDelegate.wish_count =count;
  11584. //
  11585. // [appDelegate update_count_mark];
  11586. // result(ret);
  11587. // });
  11588. //
  11589. // });
  11590. //}
  11591. +(void) offline_logout :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11592. {
  11593. UIApplication * app = [UIApplication sharedApplication];
  11594. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11595. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11596. [iSalesDB disable_trigger];
  11597. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11598. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11599. [iSalesDB enable_trigger];
  11600. dispatch_async(dispatch_get_main_queue(), ^{
  11601. //
  11602. // NSString* user = [params valueForKey:@"user"];
  11603. //
  11604. // NSString* password = [params valueForKey:@"password"];
  11605. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11606. ret[@"result"]=[NSNumber numberWithInt:2 ];
  11607. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  11608. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  11609. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  11610. [appDelegate update_count_mark];
  11611. appDelegate.can_show_price =false;
  11612. appDelegate.can_see_price =false;
  11613. appDelegate.can_create_portfolio =false;
  11614. appDelegate.can_create_order =false;
  11615. appDelegate.can_cancel_order =false;
  11616. appDelegate.can_set_cart_price =false;
  11617. appDelegate.can_delete_order =false;
  11618. appDelegate.can_submit_order =false;
  11619. appDelegate.can_set_tearsheet_price =false;
  11620. appDelegate.can_update_contact_info = false;
  11621. appDelegate.save_order_logout = false;
  11622. appDelegate.submit_order_logout = false;
  11623. appDelegate.alert_sold_in_quantities = false;
  11624. appDelegate.ipad_perm =nil ;
  11625. appDelegate.user_type = USER_ROLE_UNKNOWN;
  11626. appDelegate.OrderFilter= nil;
  11627. [appDelegate SetSo:nil];
  11628. [appDelegate set_main_button_panel];
  11629. result(ret);
  11630. });
  11631. });
  11632. }
  11633. +(void) offline_createorder :(NSMutableDictionary *) params completionHandler:(resultHandler)result
  11634. {
  11635. // iSalesDB.
  11636. // UIApplication * app = [UIApplication sharedApplication];
  11637. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11638. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11639. [iSalesDB disable_trigger];
  11640. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  11641. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  11642. [iSalesDB enable_trigger];
  11643. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11644. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11645. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  11646. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  11647. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  11648. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  11649. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  11650. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  11651. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  11652. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  11653. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  11654. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  11655. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  11656. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  11657. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  11658. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  11659. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  11660. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  11661. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  11662. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  11663. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  11664. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  11665. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  11666. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  11667. [arr_name addObject:customer_first_name];
  11668. [arr_name addObject:customer_last_name];
  11669. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  11670. // default ship from
  11671. 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';";
  11672. __block NSString *cid = @"";
  11673. __block NSString *name = @"";
  11674. __block NSString *ext = @"";
  11675. __block NSString *contact = @"";
  11676. __block NSString *email = @"";
  11677. __block NSString *fax = @"";
  11678. __block NSString *phone = @"";
  11679. sqlite3 *db = [iSalesDB get_db];
  11680. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  11681. cid = [self textAtColumn:0 statement:statment];
  11682. name = [self textAtColumn:1 statement:statment];
  11683. ext = [self textAtColumn:2 statement:statment];
  11684. contact = [self textAtColumn:3 statement:statment];
  11685. email = [self textAtColumn:4 statement:statment];
  11686. fax = [self textAtColumn:5 statement:statment];
  11687. phone = [self textAtColumn:6 statement:statment];
  11688. }];
  11689. NSString* so_id = [self get_offline_soid:db];
  11690. if(so_id==nil)
  11691. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  11692. 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];
  11693. int result_code =[iSalesDB execSql:sql_neworder db:db];
  11694. [ret setValue:[NSNumber numberWithInt:result_code] forKey:@"result"];
  11695. //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'
  11696. //soId
  11697. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  11698. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  11699. sqlite3_stmt * statement;
  11700. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  11701. {
  11702. if (sqlite3_step(statement) == SQLITE_ROW)
  11703. {
  11704. // char *name = (char*)sqlite3_column_text(statement, 1);
  11705. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  11706. //ret = sqlite3_column_int(statement, 0);
  11707. char *soId = (char*)sqlite3_column_text(statement, 0);
  11708. if(soId==nil)
  11709. soId="";
  11710. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  11711. [ret setValue:nssoId forKey:@"soId"];
  11712. [ret setValue:nssoId forKey:@"orderCode"];
  11713. }
  11714. sqlite3_finalize(statement);
  11715. }
  11716. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  11717. [ret setValue:@"Regular Mode" forKey:@"mode"];
  11718. [iSalesDB close_db:db];
  11719. dispatch_async(dispatch_get_main_queue(), ^{
  11720. result(ret);
  11721. });
  11722. });
  11723. }
  11724. +(void) offline_requestcart :(NSMutableDictionary *) params completionHandler:(resultHandler)result;
  11725. {
  11726. UIApplication * app = [UIApplication sharedApplication];
  11727. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  11728. bool can_create_backorder= appDelegate.can_create_backorder;
  11729. NSString* user = appDelegate.user;
  11730. NSString* contact_id = appDelegate.contact_id;
  11731. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  11732. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  11733. sqlite3 *db = [iSalesDB get_db];
  11734. NSString* orderCode=params[@"orderCode"];
  11735. int sort = [[params objectForKey:@"sort"] intValue];
  11736. NSString *sort_str = @"";
  11737. switch (sort) {
  11738. case 0:{
  11739. sort_str = @"order by c.modify_time desc";
  11740. }
  11741. break;
  11742. case 1:{
  11743. sort_str = @"order by c.modify_time asc";
  11744. }
  11745. break;
  11746. case 2:{
  11747. sort_str = @"order by m.name asc";
  11748. }
  11749. break;
  11750. case 3:{
  11751. sort_str = @"order by m.name desc";
  11752. }
  11753. break;
  11754. case 4:{
  11755. sort_str = @"order by m.description asc";
  11756. }
  11757. break;
  11758. default:
  11759. break;
  11760. }
  11761. 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 ];
  11762. // 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 ];
  11763. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  11764. DebugLog(@"offline_login sql:%@",sqlQuery);
  11765. sqlite3_stmt * statement;
  11766. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  11767. NSDate *date1 = [NSDate date];
  11768. int count=0;
  11769. int cart_count=0;
  11770. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  11771. if ( dbresult== SQLITE_OK)
  11772. {
  11773. while (sqlite3_step(statement) == SQLITE_ROW)
  11774. {
  11775. // NSDate *row_date = [NSDate date];
  11776. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  11777. int product_id = sqlite3_column_int(statement, 0);
  11778. char *str_price = (char*)sqlite3_column_text(statement, 1);
  11779. int item_id = sqlite3_column_int(statement, 7);
  11780. NSString* Price=nil;
  11781. if(str_price==nil)
  11782. {
  11783. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  11784. // NSDate *price_date = [NSDate date];
  11785. NSNumber* price = [self get_model_default_price:contact_id user:user product_id:nil item_id:@(item_id) db:db];
  11786. // DebugLog(@"price time interval");
  11787. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  11788. if(price==nil)
  11789. Price=@"No Price.";
  11790. else
  11791. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  11792. }
  11793. else
  11794. {
  11795. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  11796. }
  11797. double discount = sqlite3_column_double(statement, 2);
  11798. int item_count = sqlite3_column_int(statement, 3);
  11799. char *line_note = (char*)sqlite3_column_text(statement, 4);
  11800. NSString *nsline_note=nil;
  11801. if(line_note!=nil)
  11802. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  11803. char *name = (char*)sqlite3_column_text(statement, 5);
  11804. NSString *nsname=nil;
  11805. if(name!=nil)
  11806. nsname= [[NSString alloc]initWithUTF8String:name];
  11807. char *description = (char*)sqlite3_column_text(statement, 6);
  11808. NSString *nsdescription=nil;
  11809. if(description!=nil)
  11810. nsdescription= [[NSString alloc]initWithUTF8String:description];
  11811. int stockUom = sqlite3_column_int(statement, 8);
  11812. int _id = sqlite3_column_int(statement, 9);
  11813. int availability = sqlite3_column_int(statement, 10);
  11814. // NSDate *subtotal_date = [NSDate date];
  11815. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  11816. // DebugLog(@"subtotal_date time interval");
  11817. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  11818. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  11819. double weight=[bsubtotaljson[@"weight"] doubleValue];
  11820. int carton=[bsubtotaljson[@"carton"] intValue];
  11821. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  11822. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  11823. // NSDate *img_date = [NSDate date];
  11824. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  11825. // DebugLog(@"img_date time interval");
  11826. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  11827. itemjson[@"model"]=nsname;
  11828. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  11829. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  11830. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  11831. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  11832. itemjson[@"check"]=@"true";
  11833. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  11834. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  11835. itemjson[@"unit_price"]=Price;
  11836. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  11837. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  11838. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  11839. itemjson[@"note"]=nsline_note;
  11840. if (!can_create_backorder) {
  11841. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  11842. }
  11843. // NSDate *date2 = [NSDate date];
  11844. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:contact_id user:user];
  11845. // DebugLog(@"model_bundle time interval");
  11846. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  11847. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  11848. count++;
  11849. // DebugLog(@"row time interval");
  11850. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  11851. }
  11852. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  11853. sqlite3_finalize(statement);
  11854. }
  11855. DebugLog(@"request cart total time interval");
  11856. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  11857. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  11858. int wish_count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  11859. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  11860. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  11861. ret[@"count"]=[NSNumber numberWithInt:count ];
  11862. ret[@"mode"]=@"Regular Mode";
  11863. [iSalesDB close_db:db];
  11864. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  11865. DebugLog(@"general notes :%@",general_note);
  11866. ret[@"general_note"]= general_note;
  11867. dispatch_async(dispatch_get_main_queue(), ^{
  11868. result(ret);
  11869. });
  11870. });
  11871. }
  11872. @end